AWS DynamoDB data insertion using lambda function

Sign in to the AWS Management Console:

Go to the AWS Management Console and sign in using your credentials.

Open the DynamoDB Console:

Navigate to the DynamoDB service.

Create a New Table:

Click on "Create table."

Provide a table name. This must be unique within your AWS account and adhere to DynamoDB naming conventions.

Define the primary key:

Partition Key: This is the primary key attribute. It uniquely identifies each item in the table.

Optionally, you can define a Sort Key (also called a range key) to further organize the items with the same partition key.

Specify the table's settings:

Provisioned or On-Demand Capacity: Decide whether to use provisioned capacity (you define the read and write capacity units) or on-demand capacity (automatically scaled by AWS).

Accessing the Table:

After creating the table, you'll be redirected to the table details page. Here, you can see the table's properties, metrics, and options to manage the table.




Open the AWS Lambda Console:

Navigate to the AWS Lambda service in the AWS Management Console.

Create a New Lambda Function
Click on "Create function."
    • Choose the authoring method (you can write code directly in the console or upload a deployment package).
    • Configure the function's settings like name, runtime (e.g., Node.js, Python, Java, etc.), and execution role.

To insert data into a DynamoDB table using an AWS Lambda function, you can use the following Python example utilizing the boto3 library, which is the AWS SDK for Python:

Ensure that your Lambda function has the necessary permissions (via an IAM role) to interact with DynamoDB (specifically dynamodb:PutItem permission).

Here's a sample Python code for a Lambda function that inserts data into a DynamoDB table:

import json

import boto3

def lambda_handler(event, context):

    print(event["message"])

    #establish connection to Dynamo DB resource

    client = boto3.resource("dynamodb")

    table = client.Table("learners")

    

    items_to_insert = {

        'learner_id': event["learner_id"],

        'learner_name': event["learner_name"],

        'learner_location': event["learner_location"]

    }

    try:

        response = table.put_item(Item=items_to_insert)

        #item = response.get('Item', {})

        return {

        'statusCode': 200,

        'body': json.dumps('items inserted successfully')

        }

    except Exception as e:

        return {

        'statusCode': 500,

        'body': json.dumps(str(e))

    }





   Deletion from DynamoDB table using lambda function

  Code of lambda function for deletion in dynamodb table:

import json

import boto3

def lambda_handler(event, context):

  client = boto3.resource("dynamodb")

    table = client.Table('learners') 

    key_to_delete = {

        'learner_id': event["learner_id"],

         }

    try:

        response = table.delete_item(Key=key_to_delete)

        #item = response.get('Item', {})

        return {

        'statusCode': 200,

        'body': json.dumps('items deleted successfully')

        }

    except Exception as e:

        return {

        'statusCode': 500,

        'body': json.dumps(str(e))

    }




pyt


Comments

Popular posts from this blog