尝试创建 DynamoDB 表时出现 Cloudformation 错误

问题描述 投票:0回答:1

我正在努力通过 Cloudformation 创建 DynamoDB 表。我使用 Serverless Framework 作为其之上的一层,但资源是使用 Cloudformation Yaml 表示法编写的,因此它应该按照其文档中指定的方式工作。这是我的 serverless.yml 声明 DynamoDB 资源的部分:

resources:
    Resources:
        usersTable:
            Type: AWS::DynamoDB::Table
            Properties:
                TableName: users
                AttributeDefinitions:
                    - 
                      AttributeName: "id"
                      AttributeType: "S"
                    - 
                      AttributeName: "event_id"
                      AttributeType: "N"
                    - 
                      AttributeName: "date_created"
                      AttributeType: "N"
                    - 
                      AttributeName: "date_last_check"
                      AttributeType: "N"
                    - 
                      AttributeName: "can_pass"
                      AttributeType: "N"
                    - 
                      AttributeName: "counter_when_queued"
                      AttributeType: "N"
                KeySchema:
                    - 
                      AttributeName: "id"
                      KeyType: "HASH"
                    - 
                      AttributeName: "event_id"
                      KeyType: "RANGE"
                BillingMode: PAY_PER_REQUEST

但是当我尝试部署时遇到此错误:

UPDATE_FAILED: usersTable (AWS::DynamoDB::Table)
Resource handler returned message: "Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions" (RequestToken: *********, HandlerErrorCode: InvalidRequest)

这很令人困惑,因为我无法定义 KeySchema 中的所有属性(只允许 1 或 2 个)。此外,我遵循与示例中相同的语法:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-dynamodb.html

关于如何解决这个问题有什么建议吗?

amazon-web-services amazon-dynamodb aws-cloudformation serverless serverless-framework
1个回答
0
投票

在 DynamoDB 中,您只需定义用作键的属性。删除您存储在那里的非关键属性,它就会起作用。

      AttributeDefinitions:
                    - 
                      AttributeName: "id"
                      AttributeType: "S"
                    - 
                      AttributeName: "event_id"
                      AttributeType: "N"
                KeySchema:
                    - 
                      AttributeName: "id"
                      KeyType: "HASH"
                    - 
                      AttributeName: "event_id"
                      KeyType: "RANGE
© www.soinside.com 2019 - 2024. All rights reserved.