In this tutorial, we will see how using AWS Lambda we can save data in Dynamo DB.
Here are the steps which are required :
1. Create a table in the Dynamo DB with the name Employee
2. Create a AWS Lambda function which can save firstName and surName of an employee using a Employee POJO in the Dynamo DB
3. Create a Policy which will give Read/Write access to only Employee table of DynamoDB
4. Attach Policy to a Role
5. Upload the code of AWS lambda function in the form of jar in the AWS lambda console
6. Attach role created in step 4 to the AWS Lambda
7. Run Test event to invoke AWS Lambda to save Employee data in Dynamo DB
1.2 To create an Employee table, click on the "Create table" button and you will see following screen. Enter table name as "Employee" and partition key as "emp_id" and click on "Create" button.
It will create Employee table for you as can be seen in below screenshot.
Just click on Items tab and you can see table with empId colum has been created.
Here are the steps which are required :
1. Create a table in the Dynamo DB with the name Employee
2. Create a AWS Lambda function which can save firstName and surName of an employee using a Employee POJO in the Dynamo DB
3. Create a Policy which will give Read/Write access to only Employee table of DynamoDB
4. Attach Policy to a Role
5. Upload the code of AWS lambda function in the form of jar in the AWS lambda console
6. Attach role created in step 4 to the AWS Lambda
7. Run Test event to invoke AWS Lambda to save Employee data in Dynamo DB
1. Create a table in the Dynamo DB with the name Employee
1.1 To create a table in Dynamo DB, login to AWS console and in the services search for Dynamo DB or you can find DynamoDB service under Database. Click on Dynamo DB and you will see following screen.
1.2 To create an Employee table, click on the "Create table" button and you will see following screen. Enter table name as "Employee" and partition key as "emp_id" and click on "Create" button.
It will create Employee table for you as can be seen in below screenshot.
Just click on Items tab and you can see table with empId colum has been created.
2. Create a AWS Lambda function which can save firstName and surName of an Employee using an Employee POJO in the Dynamo DB
Dependencies in Pom.xml :
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.11.0</version>
</dependency>
Java Code :
package com.blogspot.javasolutionsguide.handler;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.PutItemOutcome;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.document.spec.PutItemSpec;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.blogspot.javasolutionsguide.request.Employee;
import com.blogspot.javasolutionsguide.response.Response;
public class SaveEmployeeHandler implements RequestHandler<Employee, Response>{
private DynamoDB dynamoDb;
private String DYNAMO_DB_TABLE_NAME = "Employee";
private Regions REGION = Regions.US_EAST_1;
@Override
public Response handleRequest(Employee personRequest, Context context) {
this.initDynamoDbClient();
persistData(personRequest);
Response personResponse = new Response();
personResponse.setMessage("Message Saved Successfully");
return personResponse;
}
private void initDynamoDbClient() {
AmazonDynamoDBClient client = new AmazonDynamoDBClient();
client.setRegion(Region.getRegion(REGION));
this.dynamoDb = new DynamoDB(client);
}
private PutItemOutcome persistData(Employee employee) {
Table table = dynamoDb.getTable(DYNAMO_DB_TABLE_NAME);
PutItemOutcome outcome = table.putItem(new PutItemSpec().withItem(
new Item().withNumber("empId", employee.getEmpId())
.withString("firstName", employee.getFirstName())
.withString("lastName", employee.getLastName())));
return outcome;
}
}
3. Create a Policy which will give Read/Write access to only Employee table of DynamoDB and attach with a Role
We need to provide access to AWS Lambda function to Read/Write to Dynamo DB table. For that, we will create first a Policy and then attach that policy to a Role.
To create a new policy, go to Services and then IAM.
Click on the Policies under Access Management and you will see following screen.
Click on Create Policy and you will see following screen :
Click on "Choose a Service" and type DynamoDB in search box.
Select Dynamo DB and then in Access Level section select GetItem and PutItem.
Select Resources.
Click on Add ARN. Fill Region where your Dynamo Db table is and enter name of your table and click Add.
Click on Review Policy and enter name which you want to give to your policy and enter some description about this policy and then Click on Create Policy.
4. Create a Role and attach policy to the Role
Go to IAM service -> Role, Click on Create Role button and you will see following screen.
Select AWS Service as Trusted Entity. This is the entity to which you want to assign the role, as we want to assign this role to Lambda function, which is AWS service only, we have chosen AWS service. Also in "Choose a use case", select Lambda and then click on Next Permissions. You will see following screen.
Now in the Filter policies, type the policy name that you just created in step 2(DynamoDBEmployeeTblAccess)
Click on Next:Tags button and then Next:Review button on the next screen.On the next screen, give name "DynamoDBEmployeeTblAccess" in Role Name. Add Role description.
Click on "Create Role" button and you will Success message as below:
5. Upload the code of AWS lambda function in the form of jar in the AWS lambda console
All code for this tutorial has been put in my Git repository. Link is given at the end of this tutorial.
6. Attach role created in step 4 to the AWS Lambda
In the Permissions section of your Lambda, select "Attach an existing Role" and select role created in step 4 from drop down and then click on save.
7. Run Test event to invoke AWS Lambda to save Person data in Dynamo DB
Once you have successfullly uploaded the jar,go to Select a Test event" drop down and then click on "Configure test events".
You will see screen with following data.Enter Event name as "HelloWorldEvents" and replace following data :
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
with below Json : {
"empId": 1,
"firstName": "Gaurav",
"lastName": " Bhardwaj"
}
Click on "save" button and then on "Test" button. You should be able to see following screen.
Now go to your Dynamo Db service and open Employee table and click on Start search button and you should be able to see following record :
Summary and Few important points :
So ,in this tutorial, we saw that
- How a Lambda function can be used to save data in Dynamo DB.
- We created only empId attribute and Dynamo Db automatically created rest of the attributes for us when we saved Employee object, as it has firstName and surName attributes and name of the POJO matches with the name of the table.Alternatively, we are free to create table with all three attributes as well from DynamoDB console.
- If we will try to save same object(with same Id) again, it is just going to override the existing object, so there will not be any exception.