Two serverless projects use same dynamodb item

I create two serverless project and first one create dynamoDB item call: userInfo,
and when I create the second one and try to access the userInfo item, deploy failed,
error message : already exists in stack "the first one's stack"
is it possible to share item between two cloudFormation?

first serverless project:

resources:
  Resources:
    EmailsDynamoDbTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          -
            AttributeName: id
            AttributeType: S
        KeySchema:
          -
            AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: 'userinfo'

second project: deploy failed

resources:
  Resources:
    TodosDynamoDbTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          -
            AttributeName: id
            AttributeType: S
        KeySchema:
          -
            AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: ${self:provider.environment.DYNAMODB_TABLE}
    testDynamoDbTable:
      Type: 'AWS::DynamoDB::Table'
      # DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          -
            AttributeName: id
            AttributeType: S
        KeySchema:
          -
            AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: 'userinfo'

i think i know why,
there’s no need to add resources: xxxDynamoDbTable if the item already exists

1 Like

Just for any one else who comes across this, the short answer is you can’t manage the same resource in multiple CloudFormation templates (or Serverless services).

As you’ve already discovered, what’s happening here is that your second service is trying to create a DDB table with the same name, and table names must be unique.

Your options are:

  • Include the service name in the table name, which will make it unique. This doesn’t really help you if you only really want one table.
  • Create your DDB table separately. You could do this with “pure” CloudFormation, through the web console by hand (yuck!), or though a “resources only” Serverless service - just create a serverless.yml with a resources section and no functions, and manage it just like a normal service (e.g. with sls deploy, etc).

Is there a way to have serverless skip creating the DDB if it already exists?

Not from another template/service. Deploying the same template does exactly that.

One of the major benefits of using CloudFormation is idempotent deployments - having a template that does one thing one time and another thing another time (based on some outside state, not expressed in the parameters) would break that.