Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PutItem in DynamoDB table by CloudFormation

Tags:

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.

like image 629
Souad Avatar asked Mar 02 '17 09:03

Souad


People also ask

What is PutItem in DynamoDB?

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.

How do I get items from DynamoDB?

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.

Does PutItem overwrite?

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.


1 Answers

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 
like image 194
jens walter Avatar answered Sep 24 '22 01:09

jens walter