How to do it...

I will list and explain the general steps for developing DynamoDB operations from Lambda using the SDK in this section:

  1. Create a Maven project for the Lambda with the common parent and the DynamoDB Java SDK dependency

We follow the same project structure for all our Lambda projects:

In the POM file, define the parent project as serverless-cookbook-parent-aws-java. This parent project defines the common properties and inherits from another parent with basic Java project dependencies and configurations such as lombok, checkstyle configurations, and so on:

<parent>
<groupId>tech.heartin.books.serverlesscookbook</groupId>
<artifactId>serverless-cookbook-parent-aws-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

We also need to add the dependency for the AWS Java SDK for DynamoDB in our POM file:

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>${aws.sdk.version}</version>
</dependency>

The aws.sdk.version property is defined in the parent project.

  1. Define the Request and Response domain objects

The Request POJO and Response POJO are used for input and output mapping for our Lambda handler (MyLambdaHandler). While the Request object will be specific to each operation-specific Lambda, the Response object will be reused for all cases and is defined as follows:

@Data
@AllArgsConstructor
public class Response {
private String message;
private String errorMessage;
}
  1. Create the service implementation

Define a service class for implementing the logic behind DynamoDB operations such as create table, add data, and so on. You may split the service class into an interface and an implementation class, which is a general practice with Java projects. You may also directly put your application logic into the Lambda handler without a service class.

I have defined an interface (DynamoDBService) and two implementations; one uses the DynamoDB wrapper client (DynamoDBServiceImpl1) and the other uses the AmazonDynamoDB client (DynamoDBServiceImpl2). I have used Lambda environment variable in the handler to decide the implementation to use.

The AmazonDynamoDB client can be created as follows:

AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder.defaultClient();

The DynamoDB wrapper client can be created using the AmazonDynamoDB client as follows:

DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);

The code for the operation in the service class is specific to each operation and is shown in the respective recipes.

  1. Define the Lambda handler

The Lambda handler implementation will also be similar for all operations, except for the service method name and log message:

public final class MyLambdaHandler implements RequestHandler<Request, Response> {

private DynamoDBService service;

public Response handleRequest(final Request request, final Context context) {
context.getLogger().log("Creating table " + request.getTableName());

final String version = System.getenv("API_VERSION");
if(version != null && version.equals("V2")) {
service = new DynamoDBServiceImpl2();
} else{
service = new DynamoDBServiceImpl1();
}
return service.createTable(request);
}
}

We will use the Lambda environment variable to determine the version of DynamoDBServiceImpl to use. The default is V1, which is the wrapper client-based code. The JavaDoc comments required to pass the checkstyle checks are not shown here. Refer to the code files repository for the complete code. 

  1. Package and deploy Lambda

The steps to package and deploy Lambda are common for all projects. You can package a Maven project as follows:

mvn clean package

Once the .jar file is created, you can upload the JAR file to S3 as follows:

aws s3 cp 
target/lambda-dynamodb-create-table-0.0.1-SNAPSHOT.jar
s3://serverless-cookbook/lambda-dynamodb-create-table-0.0.1-SNAPSHOT.jar
--profile admin
  1. Define the CloudFormation template

The CloudFormation template to provision our Lambda is similar for all Lambdas in this chapter, except the names and IAM policy permissions required for the DynamoDB operations we do. The IAM policy permissions required by each Lambda are also discussed separately for each case. 

We start the template with the template version and description, define a log group for our Lambda, define a role that can be assumed by Lambda, and create a policy to write logs. The Lambda function's configuration is also similar to other recipes, except the names. You may also add all Lambdas into a single template file if you are planning to deploy them together.

  1. Deploy the CloudFormation template

The deploy stack command for the CloudFormation template is the same for all Lambdas, except the name changes:

aws cloudformation create-stack 
--stack-name myteststack
--template-body file://resources/lambda-dynamodb-create-table-cf-template.yml
--capabilities CAPABILITY_NAMED_IAM
--region us-east-1
--profile admin

How to use describe-stack and delete-stack is shown in the code files. 

  1. Invoke the Lambda

The payload that is passed to the aws lambda invoke command is specific to each Lambda's Request object. 

Our Lambda selects whether to execute version 1 (one that uses a DynamoDB wrapper client) or version 2 (one that uses an AmazonDynamoDB client) based on the value of Lambda environment variable, API_VERSION. The version 2 service implementation can be executed after updating the API_VERSION Lambda environment variable to V2:

aws lambda update-function-configuration 
--function-name <lambda name>
--environment Variables={API_VERSION=V2}
--region us-east-1
--profile admin
  1. Cleanup
    The cleanup steps (if required) are provided with each operation-specific Lambda's recipe.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.29.22