Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serverless Error, CloudFormation cannot update a stack when a custom-named resource requires replacing

I have the following error.

Serverless: Operation failed!

Serverless Error ---------------------------------------
An error occurred: phoneNumberTable - CloudFormation cannot update a stack when a custom-named resource requires replacing. Rename mysite-api-phonenumber-dev and update the stack again…

I tried deleting the database to see if it could re-create it then but it still gives the same error and doesn’t remake the database? What do I do here?

What I did was recently change in my serverless.yml file the following for the resource.

phoneNumberTable: #This table is used to track phone numbers used in the system
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: ${self:custom.phoneNumberTable}
        AttributeDefinitions: #UserID in this case will be created once and constantly updated as it changes with status regarding the user.
          - AttributeName: phoneNumber
            AttributeType: S
        KeySchema:
          - AttributeName: phoneNumber
            KeyType: HASH
        ProvisionedThroughput:
            ReadCapacityUnits: ${self:custom.dynamoDbCapacityUnits.${self:custom.pstage}}
            WriteCapacityUnits: ${self:custom.dynamoDbCapacityUnits.${self:custom.pstage}}

I accidentally created it with userId when I was copying and pasting so I changed it to phoneNumber for the hash key but the change won't reflect now!

Edit::

I found a solution but it's terrible. If I do sls remove --stage dev it will remove everything for my stage, but literally everything... then I have to do sls deploy --stage dev to start the deploy over again, in the meantime my database is cleared of all data... there has to be a better way somehow.

like image 963
Joseph Astrahan Avatar asked Nov 30 '17 06:11

Joseph Astrahan


People also ask

What is custom named resource in CloudFormation?

Resource names must be unique across all your active stacks. If you reuse templates to create multiple stacks, you must change or remove custom names from your template. If you don't specify a name, CloudFormation generates a unique physical ID to name the resource.

How do I update CloudFormation stack?

To update a AWS CloudFormation stack (console)In the AWS CloudFormation console , from the list of stacks, select the running stack that you want to update. In the stack details pane, choose Update. If you haven't modified the stack template, select Use current template, and then choose Next.

What will happen by default if a CloudFormation stack fails when building a resource stack?

Results: Resources that failed to update transition the stack status to UPDATE_FAILED and roll back to the last known stable state. Resources without a last known stable state will be deleted by CloudFormation upon the next stack operation.


2 Answers

The AWS recommended solution is to rename: https://aws.amazon.com/premiumsupport/knowledge-center/cloudformation-custom-name/

like image 79
Jon Smirl Avatar answered Sep 20 '22 06:09

Jon Smirl


I found I needed to insert some variables to make it work.

Environment variable: USERS_TABLE: "users-${opt:stage, self:provider.stage}-${self:provider.environment.BUILD_NUMBER}"

Table name: TableName: ${self:provider.environment.USERS_TABLE}

In my code: const existingUser = await dynamoDb.get({ TableName: process.env.USERS_TABLE, Key: { email, }, }).promise();

like image 22
Alex K Avatar answered Sep 22 '22 06:09

Alex K