Deploy to different stages with DynamoDb

I have a resource which is a database table. This seems to deploy fine when I run, for example:

sls -s dev deploy

However if I now try to deploy to production:

sls -s production deploy

I get an error:

An error occurred while provisioning your stack: ****
- EvChargePointTable already exists in stack ****

I really need to be able to have (at least) two separate deployments with separate databases (obviously do not want to use real customer data in dev!). Can this be achieved? If so how?

@samredway It looks like you’re trying to use the same table name in different stages. You need to use different tables names. My serverless.yml looks like:

provider:
  environment:
    SHOPS_TABLE: ${shopsTable}

custom:
  stage: "${opt:stage, self:provider.stage}"
  shopsTable: "${self:service}-shops-${self:custom.stage}"

I then use process.env.SHOPS_TABLE inside my code so that it always references the correct table for that stage.

If you don’t want to follow a naming scheme like that for production then look at per-stage environment variables. This would allow you to use EvChargePointTable on production but ${self:service}-EvChargePointTable-${self:custom.stage} in other stages.

The other option is to use different AWS accounts for each stage (which is also a good idea). Just remember that it will only work until you need to deploy S3 buckets that need to be globally unique.

1 Like

This makes sense, I was realising that I would probably have to do something like this, so thanks for the guidance on how to implement.

I have am considering a strategy of running a test database locally for development (each dev on the project will then have there own dev DB) and only using staging for production and potentially a second database for use by Jenkins for automated end to end tests (which would have to be set up something like you suggest).

NOTE TO SERVERLESS DEVS - may be an idea to enable different databases on different stages by adding a pre(or post) fix to the database name e.g. MyDatabase-production and MyDatabase-testing