简体   繁体   中英

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.

AWS 推荐的解决方案是重命名: https ://aws.amazon.com/premiumsupport/knowledge-center/cloudformation-custom-name/

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();

将您的资源重命名为其他名称,部署它,将其重命名(如果需要)并再次部署。

According to https://aws.amazon.com/premiumsupport/knowledge-center/cloudformation-custom-name/

This error typically occurs when a stack update tries to replace resources that have properties with custom names. AWS CloudFormation doesn't replace a resource that has a custom name unless that custom name is changed to a different name. To prevent a stack failure and avoid the error message, change any resources with custom names to use different names before you update a stack.

To resolve this you need change TableName to some other string. What that will do: Serverless will delete your table (because that's no longer a part of a stack) and will create a new table with a new name and a keys.

This problem usually occurs when you modify the partition keys of your DynamoDB table. DynamoDB cannot make the change for you. You must delete the database, making sure to back it up and recreate it. Once the new DB has been created, you can perform your migration.

Ce problème intervient habituellement lorsque vous modifier es clés de partitions de votre table DynamoDB. DynamoDB ne peut pas effectuer le changement pour vous. Vous devez supprimer la base de données en vous assurant de faire un backup et la recréer. Une fois la nouvelle BD créée vous pouvez faire votre migration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM