DynamoDB Resource already exists

I declared a DynamoDB table in my Resources section, but for some reason when I try to deploy it, it always tries to recreate my DynamoDB table, even though the status is set to Retain:

resources:
  Resources:
    Table:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          -
            AttributeName: FIELD1
            AttributeType: S
          -
            AttributeName: FIELD2
            AttributeType: N
        KeySchema:
          -
            AttributeName: FIELD1
            KeyType: HASH
          -
            AttributeName: FIELD2
            KeyType: RANGE

        ProvisionedThroughput:
          ReadCapacityUnits: 5
          WriteCapacityUnits: 5
        TableName: ${self:provider.environment.DYNAMODB_TABLE}

I checked the output of CloudFormation and this is what I get:

{
            "StackId": "STACKID",
            "EventId": "Table-CREATE_FAILED-2018-01-04T10:11:50.751Z",
            "ResourceStatus": "CREATE_FAILED",
            "ResourceType": "AWS::DynamoDB::Table",
            "Timestamp": "2018-01-04T10:11:50.751Z",
            "ResourceStatusReason": "TABLE already exists",
            "StackName": "STACKNAME",
            "ResourceProperties": "{\"TableName\":\"TABLE\",\"AttributeDefinitions\":[{\"AttributeType\":\"S\",\"AttributeName\":\"FIELD1\"},{\"AttributeType\":\"N\",\"AttributeName\":\"TIMESTAMP\"}],\"ProvisionedThroughput\":{\"WriteCapacityUnits\":\"5\",\"ReadCapacityUnits\":\"5\"},\"KeySchema\":[{\"KeyType\":\"HASH\",\"AttributeName\":\"FIELD1\"},{\"KeyType\":\"FIELD2\",\"AttributeName\":\"TIMESTAMP\"}]}\n",
            "PhysicalResourceId": "",
            "LogicalResourceId": "TABLE"
        }

Any idea what the problem is? If I remove that table manually and run sls deploy, it works fine. If I run sls deploy again, I get that error message.

yes, DeletionPolicy: Retain means serverless will not delete it when remove whole serverless stack to protect the data.

If you change to DeletionPolicy: Delete, serverless framework will remove that database as well.

Normally I set a variable on it, for non-prod environment, set to Delete, for production environment, set to Retain.

2 Likes

When you set DeletionPolicy: Retain in your production environment. How do you get your script to skip the creation of the resource?