I was thinking about using multiple stages like production and development using a single account. And how can i do that? “sls deploy --stage prod”. Is this proper way of implementing stages??
That will work. Some resource, like DynamoDB, require a different name for each stage. To do that I like to set the region
and stage
in my provider
:
provider:
region: ${opt:region, "us-east-1"}
stage: ${opt:stage, "dev"}
I can then add -${self:provider.stage}
to resource names so AWS creates different resources for each stage.
Hi, Thanks for your reply. What I was thinking is, How can I do it dynamically? Without providing the data inside the serverless.yml file ? Is there any issue with that approach ?
That is how you do it dynamically. Here’s a more complete example:
service:
name: my-service
provider:
name: aws
runtime: nodejs8.10
stage: ${opt:stage, "dev"}
region: ${opt:region, "us-east-1"}
custom:
myTableName: my-table-${self:provider.stage}
functions:
helloWorld:
handler: handler.helloWorld
environment:
TABLE_NAME: ${self:custom.myTableName}
resources:
Resources:
MyTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: id
KeyType: HASH
TableName: ${self:custom.myTableName}
You can now sls deploy --stage ANY_ENVIRONMENT
and it will set up the complete environment including tables.