I am having issue for writing Amazon Kinesis Data to DynamoDB table using AWS Lambda

0

I am having issue for writing Amazon Kinesis Data to DynamoDB table using AWS Lambda. I am able to see data under Data Viewer in Kinesis Data Stream but not reflecting in database. Please help me to understand what is missing.

I am using Lambda code as below :


import json import boto3 import base64

dynamodb = boto3.resource('dynamodb') table = dynamodb.Table('evvents')

def lambda_handler(event,context): for record in event["Records"]: pk = record["kinesis"]["partitionkey"] msg = base64.b64decode(record["kinesis"]["data"])

    table.put_item(Item={
        "pk":pk,
        "data":msg.decode("utf-8")
        })

    return {
       'statusCode':200
    }

Following steps as below :

  1. Created Amazon Kinesis Data Stream
  2. Created Amazon DynamoDB Table and partition key
  3. Created Lambda function using new IAM role created for Lambda service.
  4. Permission AmazonKinesisFullAccess and AWSLambda_FullAccess map with IAM role.
  5. Defined Lambda function Code as below and deployed.
  6. Created trigger for Kinesis stream
  7. Sent data using aws kinesis put-record --stream-name eve n ts --partition-key "1" --data 'test1' --cli-binary-format raw-in-base64-out
1 Answer
0

Hello.

"AmazonKinesisFullAccess" and "AWSLambda_FullAccess" do not have enough policies to execute put_item in DynamoDB.
Therefore, accessing DynamoDB from Lambda may be failing.
So, try setting the following IAM policy for Lambda's IAM role.
If you are performing other operations on DynamoDB, the policy examples in the following document will be helpful.
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/iam-policy-example-data-crud.html

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "test",
      "Effect": "Allow",
      "Action": [
        "dynamodb:PutItem"
      ],
      "Resource": [
        "*"
      ]
    }
  ]
}
profile picture
EXPERT
answered a month ago
profile picture
EXPERT
reviewed a month ago
profile pictureAWS
EXPERT
reviewed a month ago
  • In your code, it is "partitionkey", but if it is not "partitionKey", an error will occur. The "K" in "partitionKey" should be capitalized.

    import json 
    import boto3 
    import base64
    
    dynamodb = boto3.resource('dynamodb') 
    table = dynamodb.Table('evvents')
    
    def lambda_handler(event,context): 
        for record in event["Records"]:
            print(record)
            pk = record["kinesis"]["partitionKey"] # <-- edit
            msg = base64.b64decode(record["kinesis"]["data"])
    
            table.put_item(Item={
                "pk":pk,
                "data":msg.decode("utf-8")
            })
    
        return {
        'statusCode':200
        }
    

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions