Is there any way to put items in a DynamoDB table using CloudFormation ? Something similar to the code in this doc
In the parameter of the template I give the user the possibility to put the values, then I need to insert these values into the table.
PDF. Creates a new item, or replaces an old item with a new item. If an item that has the same primary key as the new item already exists in the specified table, the new item completely replaces the existing item.
To read an item from a DynamoDB table, use the GetItem operation. You must provide the name of the table, along with the primary key of the item you want. The following AWS CLI example shows how to read an item from the ProductCatalog table. With GetItem , you must specify the entire primary key, not just part of it.
Previously, we used the PutItem operation to insert Items into our DynamoDB table. We saw that this operation completely overwrites any existing Item in the table.
The way to achieve this would be to utilize Custom Resources for this.
Here a Cloudformation template which utilizes an inline Lambda for this task.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With