Sou*_*uad 12 amazon-web-services aws-cloudformation
有没有办法使用CloudFormation将项目放入DynamoDB表中?类似于本文档中的代码
在模板的参数中,我给用户提供了放置值的可能性,然后我需要将这些值插入表中.
jen*_*ter 14
实现这一目标的方法是为此使用自定义资源.
这是一个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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5255 次 |
| 最近记录: |