gpt4 book ai didi

amazon-web-services - 通过 CloudFormation 将项目放入 DynamoDB 表中

转载 作者:行者123 更新时间:2023-12-03 22:19:00 26 4
gpt4 key购买 nike

有没有办法使用 CloudFormation 将项目放入 DynamoDB 表中?类似于此 doc 中的代码

在模板的参数中,我让用户可以输入值,然后我需要将这些值插入表中。

最佳答案

实现这一目标的方法是利用自定义资源。

这里是一个 Cloudformation 模板,它使用内联 Lambda 来完成此任务。

AWSTemplateFormatVersion: '2010-09-09'
Resources:
LambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: dynamodbAccessRole
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- dynamodb:*
Resource: "*"
- Effect: Allow
Action:
- logs:*
Resource: "*"
InitFunction:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: >
const AWS = require("aws-sdk");
const response = require("cfn-response");
const docClient = new AWS.DynamoDB.DocumentClient();
exports.handler = function(event, context) {
console.log(JSON.stringify(event,null,2));
var params = {
TableName: event.ResourceProperties.DynamoTableName,
Item:{
"id": "abc123"
}
};
docClient.put(params, function(err, data) { if (err) {
response.send(event, context, "FAILED", {});
} else {
response.send(event, context, "SUCCESS", {});
}
});
};
Handler: index.handler
Role:
Fn::GetAtt: [ LambdaRole , "Arn" ]
Runtime: nodejs4.3
Timeout: 60
DynamoDB:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
InitializeDynamoDB:
Type: Custom::InitFunction
DependsOn: DynamoDB
Properties:
ServiceToken:
Fn::GetAtt: [ InitFunction , "Arn" ]
DynamoTableName:
Ref: DynamoDB

关于amazon-web-services - 通过 CloudFormation 将项目放入 DynamoDB 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42551408/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com