问题描述
我正在尝试使用 CloudFormation 堆栈创建 DynamoDB 表,但是我一直在 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",{
"AttributeName":"Negative score",{
"AttributeName":"Mixed score","AttributeType":"S"
}
],"KeySchema":[
{
"AttributeName":"Filename","KeyType":"HASH"
}
],"ProvisionedThroughput":{
"ReadCapacityUnits":"5","WriteCapacityUnits":"5"
},"TableName":"testtable"
}
}
}
}
我的控制台打印了创建的堆栈,但控制台中没有明确指示为什么它一直失败。
解决方法
查看堆栈的“事件”选项卡。它将向您显示详细的操作并解释哪个步骤首先失败。具体会告诉你:
一个或多个参数值无效:KeySchema 中的属性数与 AttributeDefinitions 中定义的属性数不完全匹配(服务:AmazonDynamoDBv2;状态代码:400;错误代码:ValidationException;请求 ID:12345;代理:null)
问题是您已经为所有表属性提供了定义。您应该只提供关键属性。
根据 AttributeDefinitions 文档:
[AttributeDefinitions is] 描述表和索引的关键架构的属性列表。