Simple Java lambda function that records an item into a Dynamodb and runs locally

Simple Java lambda function that records an item into a Dynamodb and runs locally

As with any software that requires me to write code, I have been playing around with Java Lambdas lately. I find them to be the perfect tool for small, simple functions that are meant to be run in a single place, yet are reused. One thing that I have recently experienced is the need to pass data from one place to another, and I came up with the following code that I thought might be useful.

The following code records the value of an item into a DynamoDB table and then runs the code locally. You could record the same item into another DynamoDB table and run the code on another machine. Try it yourself!

In this tutorial, you will learn how to create and test an AWS Lambda function using DynamoDB on a local machine.
Here we use SAM CLI to call the local API and Docker to create the local DynamoDB image.

Serverless Application Model (SAM) is an open source framework for building serverless applications in AWS. Lambda is a compute service in AWS for running workloads using a provisioning server. DynamoDB is a NOSQL database in AWS.

To install dynamoDB locally, Docker must be installed on the system. After a successful local installation of Docker, the DynamoDb image can be pulled from the Docker Hub.
To retrieve DynamoDB Local, run the following on the command line:
docker run -p 8000:8000 amazon/dynamodb-local

This will run the local DynamoDB in the Docker container on port 8000.

You can display local DynamoDB tables using the following command
aws dynamodb list-tables –endpoint-url http://localhost:8000.

Create a new table in Dynamo called personTable. The table can be created from the json file using the command below.
aws dynamodb create-table –cli-input-json file://events/person.json –endpoint-url http://localhost:8000

Here is the structure of the json file

table name : PersonTable,
KeySchema : [
{ AttributeName: Id, KeyType : HASH }
AttributeDefinitions: [
{ AttributeName: Id, AttributeType : S }
Envisaged debit : {
ReadCapacityUnits : 1,
WriteCapacityUnits : 1

We now need to create a local lambda function and call the API from the SAM CLI.
First, create a simple DTO class called Person. It contains the fields id, name and salary.

Next, create a Lambda handler that handles the API request. It can take a persona request payload from the APIGatewayProxyRequestEvent and convert it into a persona object.
Then create an object with the id, name and salary and place it in the dynamoDB table.
The Lambda API must return status code 200 with the response.

Create a standard AWS DynamoDB client with DynamoDBClientBuilder and map the table to DynamoDB.
To do this, add the DynamoDB maven dependency to the pom.xml file.


DynamoDB client object with DynamoDBClientBuilder

AmazonDynamoDB db = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
// here we can use any region
new AwsClientBuilder.EndpointConfiguration(, us-east-1))
DynamoDB db1 = new DynamoDB(db);
Table table = db1.getTable(PersonTable) ;

We need to create a project to call the Lambda function. To do this, run the command from the project directory

Sam’s construction

This operation collects all artifacts and creates an aws package of the template.yml file for later implementation.

Then run the api gateway to call the lambda function with the following command.
sam local start-api

This starts the application at http://localhost:3000/.

To test the application, open postman and do an HTTP post request with the request person json payload.

I hope you found this guide useful.
For more information on Lambda, DynamoDB and SAM, please visit the official AWS documentation page.

Frequently Asked Questions

How do you get data from DynamoDB using lambda function?

To use DynamoDB with a lambda function, you must first create a DynamoDB table and then configure the table to use the lambda function. To create a DynamoDB table: Create a DynamoDB table in the AWS Management Console. Select the table name. Configure the table to use the lambda function. To configure the table to use the lambda function: Select the table name. Select the lambda function. Select the region. Select the table type. Select the table key. Select the table index. Select the table key range. Select the table index range. Select the table schema. Select the table data type. Select the table cache. Select the table cache size. Select the table cache type. Select the table cache policy. Select the table index cache. Select the table index cache size. Select the table index cache type.

How do I give Lambda access to DynamoDB?

Lambda can access DynamoDB through the AWS SDK for Java. How do I give Lambda access to Amazon S3? Lambda can access Amazon S3 through the AWS SDK for Java.

How do I connect to AWS DynamoDB locally?

You can connect to DynamoDB locally by using the AWS SDK for Java.

Related Tags:

dynamodb streamsdynamodb streams lambda pythondynamodb ttl trigger lambdadynamodb stream to sqsaws:dynamodb streams lambdadynamodb trigger lambda python,People also search for,Feedback,Privacy settings,How Search works,dynamodb streams,dynamodb streams lambda python,dynamodb ttl trigger lambda,lambda function to insert data into dynamodb,dynamodb stream to sqs,aws:dynamodb streams lambda,dynamodb trigger lambda python,aws lambda dynamodb python