15

我有以下错误。

无服务器:操作失败!

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…

我尝试删除数据库以查看它是否可以重新创建它,但它仍然给出相同的错误并且不重新创建数据库?我在这里做什么?

我所做的是最近在我的 serverless.yml 文件中更改了资源的以下内容。

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}}

我在复制和粘贴时不小心用 userId 创建了它,所以我将其更改为 phoneNumber 作为哈希键,但现在更改不会反映!

编辑::

我找到了解决方案,但这很糟糕。如果我执行 sls remove --stage dev 它将删除我的阶段的所有内容,但实际上所有内容......然后我必须执行 sls deploy --stage dev 重新开始部署,同时我的数据库被清除所有数据...必须有更好的方法。

4

2 回答 2

11

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

于 2017-11-30T19:23:03.553 回答
3

我发现我需要插入一些变量才能使其工作。

环境变量: USERS_TABLE: "users-${opt:stage, self:provider.stage}-${self:provider.environment.BUILD_NUMBER}"

表名: TableName: ${self:provider.environment.USERS_TABLE}

在我的代码中: const existingUser = await dynamoDb.get({ TableName: process.env.USERS_TABLE, Key: { email, }, }).promise();

于 2018-08-10T00:08:37.387 回答