AWS CloudFormation 堆栈创建一直失败

AWS CloudFormation Stack Creation Keeps Failing

我正在尝试使用 CloudFormation 堆栈创建 DynamoDB table,但是我一直在 AWS 控制台中收到 'CREATE_FAILED' 错误,我不确定哪里出错了.

我的方法create_stack:

cf = boto3.client('cloudformation')
stack_name = 'teststack'

with open('dynamoDBTemplate.json') as json_file:
    template = json.load(json_file)
    template = str(template)

try:
    response = cf.create_stack(
        StackName = stack_name,
        TemplateBody = template,
        TimeoutInMinutes = 123,
        ResourceTypes = [
            'AWS::DynamoDB::Table',
        ],
        OnFailure = 'DO_NOTHING',
        EnableTerminationProtection = True
    )
    print(response)
except ClientError as e:
    print(e)

这是我的 JSON 文件:

{
   "AWSTemplateFormatVersion":"2010-09-09",
   "Resources":{
      "myDynamoDBTable":{
         "Type":"AWS::DynamoDB::Table",
         "Properties":{
            "AttributeDefinitions":[
               {
                  "AttributeName":"Filename",
                  "AttributeType":"S"
               },
               {
                  "AttributeName":"Positive Score",
                  "AttributeType":"S"
               },
               {
                  "AttributeName":"Negative Score",
                  "AttributeType":"S"
               },
               {
                  "AttributeName":"Mixed Score",
                  "AttributeType":"S"
               }
            ],
            "KeySchema":[
               {
                  "AttributeName":"Filename",
                  "KeyType":"HASH"
               }
            ],
            "ProvisionedThroughput":{
               "ReadCapacityUnits":"5",
               "WriteCapacityUnits":"5"
            },
            "TableName":"testtable"
         }
      }
   }
}

我的控制台打印了创建的堆栈,但控制台中没有明确指示为什么一直失败。

查看堆栈的“事件”选项卡。它将向您展示详细的操作并解释哪一步首先失败。具体会告诉你:

One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 12345; Proxy: null)

问题是您已经为所有 table 属性提供了定义。您应该只提供关键属性。

根据 AttributeDefinitions 文档:

[AttributeDefinitions is] A list of attributes that describe the key schema for the table and indexes.