I just released the Master Class of " Learn Spring Security ":
>> CHECK OUT THE COURSE
1. IntroductionAWS Lambda is serverless computing service provided by Amazon Web Services and WS DynamoDB is a NoSQL database service also provided by Amazon.
Interestingly, DynamoDB supports both document store and key-value store and is fully managed by AWS.
Before we start, note that this tutorial requires a valid AWS account (you can create one here ). Also, it’s a good idea to first read theAWS Lambda with Javaarticle.
2. Maven DependenciesTo enable lambda we need the following dependency which can be found on Maven Central :
<dependency><groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
</dependency>
To use different AWS resources we need the following dependency which also can also be found on Maven Central :
<dependency><groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>1.3.0</version>
</dependency>
And to build the application, we’re going to use the Maven Shade Plugin :
<plugin><groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin> 3. Lambda Code
There are different ways of creating handlers in a lambda application:
MethodHandler RequestHandler RequestStreamHandlerWe will use RequestHandler interface in our application. We’ll accept the PersonRequest in JSON format, and the response will be PersonResponse also in JSON format :
public class PersonRequest {private String firstName;
private String lastName;
// standard getters and setters
} public class PersonResponse {
private String message;
// standard getters and setters
}
Next is our entry pointclass which will implement RequestHandler interface as:
public class SavePersonHandlerimplements RequestHandler<PersonRequest, PersonResponse> {
private DynamoDB dynamoDb;
private String DYNAMODB_TABLE_NAME = "Person";
private Regions REGION = Regions.US_WEST_2;
public PersonResponse handleRequest(
PersonRequest personRequest, Context context) {
this.initDynamoDbClient();
persistData(personRequest);
PersonResponse personResponse = new PersonResponse();
personResponse.setMessage("Saved Successfully!!!");
return personResponse;
}
private PutItemOutcome persistData(PersonRequest personRequest)
throws ConditionalCheckFailedException {
return this.dynamoDb.getTable(DYNAMODB_TABLE_NAME)
.putItem(
new PutItemSpec().withItem(new Item()
.withString("firstName", personRequest.getFirstName())
.withString("lastName", personRequest.getLastName());
}
private void initDynamoDbClient() {
AmazonDynamoDBClient client = new AmazonDynamoDBClient();
client.setRegion(Region.getRegion(REGION));
this.dynamoDb = new DynamoDB(client);
}
}
Here when we implement the RequestHandler interface, we need to implement handleRequest() for the actual processing of the request. As for therest of the code, we have:
PersonRequest object which will contain the request values passed in JSON format Context object used to get information fromlambda execution environment PersonResponse which isthe response object for the lambda requestWhen creating a DynamoDB object, we’ll first create the AmazonDynamoDBClient object and use that to create a DynamoDB object. Note that the region is mandatory.
To add items in DynamoDB table, we’ll make use of a PutItemSpec object by specifying the number of columns and their values.
We don’t need any predefined schema in DynamoDB table, we just need to define the Primary Key column name, which is “id” in our case.
4. Building the Deployment FileTo build the lambda application, we need to execute the following Maven command:
mvn clean package shade:shadeLambda application will be compiled and packaged into a jar file under the target folder.
5. Creating the DynamoDB TableFollow these steps to create the DynamoDB table:
Login to AWS Account Click “DynamoDB” that can be located under “All Services” This page will show already created DynamoDB tables (if any) Click “Create Table” button Provide “Table name” and “Primary Key” with its datatype as “Number” Click on “Create” button Table will be created 6. Creating the Lambda FunctionFollow these steps to create the Lambda function:
Login to AWS Account Click “Lambda” that can be located under “All Services” This page will show already created LambdaFunction(if any) or no lambda functions are created click on “Get Started Now” “Select blueprint” ->Select “ Blank Function” “Configure triggers” -> Click “Next” button “Configure function” “Name” : SavePerson