首页 > 解决方案 > 如何通过 cloudformation 将值插入 dynamodb?

问题描述

我正在 cloudformation 中创建一个表:

"MyStuffTable": {
    "Type": "AWS::DynamoDB::Table",
    "Properties": {
        "TableName": "MyStuff"
        "AttributeDefinitions": [{
            "AttributeName": "identifier",
            "AttributeType": "S"
        ]},
        "KeySchema": [{
            "AttributeName": "identifier",
            "KeyType": "HASH",
        }],
        "ProvisionedThroughput": {
            "ReadCapacityUnits": "5",
            "WriteCapacityUnits": "1"
         }
    }
}

然后稍后在 cloudformation 中,我想将记录插入到该表中,如下所示:

identifier: Stuff1
data: {My list of stuff here}

并将其插入到下面代码中的值中。我在某处看到了一个使用 的示例Custom::Install,但我现在找不到它,也找不到任何关于它的文档。所以这就是我所拥有的:

MyStuff: {
    "Type": "Custom::Install",
    "DependsOn": [
        "MyStuffTable"
      ],
    "Properties": {
        "ServiceToken": {
            "Fn::GetAtt": ["MyStuffTable","Arn"]
        },
        "Action": "fields",
        "Values": [{<insert records into this array}]
    }
};

当我运行它时,我得到了这个Invalid service token。所以我没有做正确的事情试图引用表来插入记录。我似乎找不到关于 Custom::Install 的任何文档,所以我不确定这是通过 cloudformation 插入记录的正确方法。我似乎也找不到有关通过 cloudformation 插入记录的文档。我知道这是可以做到的。我可能错过了一些非常简单的东西。有任何想法吗?

标签: amazon-web-servicesamazon-dynamodbamazon-cloudformation

解决方案


Custom::Install是 CloudFormation 中的自定义资源

这是一种特殊类型的资源,您必须自己开发。这主要通过 Lambda 函数(也可以是 SNS)来完成。

所以回答你的问题。要将数据添加到表中,您必须在 lambda 中编写自己的自定义资源。lambda 会将记录放入表中。

Action 并且fields是 CloudFormation 传递给示例中的 lambda 的自定义参数Custom::Install。参数可以是您想要的任何参数,因为您正在设计根据您的要求量身定制的自定义资源。


推荐阅读