PutItem in DynamoDB table 作者:CloudFormation
PutItem in DynamoDB table by CloudFormation
有什么方法可以使用 CloudFormation 将项目放入 DynamoDB table?
类似于 doc
中的代码
在模板的参数中,我让用户可以输入值,然后我需要将这些值插入 table。
实现此目的的方法是为此使用自定义资源。
这里是 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
有什么方法可以使用 CloudFormation 将项目放入 DynamoDB table? 类似于 doc
中的代码在模板的参数中,我让用户可以输入值,然后我需要将这些值插入 table。
实现此目的的方法是为此使用自定义资源。
这里是 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