How to do it...

Let's discuss the steps to create a table from Lambda without repeating the common steps we already discussed:

  1. Create a Maven project for the Lambda with the common parent and the DynamoDB Java SDK dependency
  2. Define the Request and Response domain objects

The Request object for the lambda-dynamodb-create-table Lambda is defined as follows:

@Data
public class Request {
private String tableName;
private String partitionKey;
private String sortKey;
private long readCapacityUnits;
private long writeCapacityUnits;
private boolean waitForActive;
}

The Response object is common to all Lambdas, as discussed in the Using the DynamoDB SDK from Lambda recipe. 

  1. Create the service implementation

A table can be created using the DynamoDB wrapper client as follows:

Table table = dynamoDB.createTable(request.getTableName(),
Arrays.asList(
new KeySchemaElement(request.getPartitionKey(), KeyType.HASH),
new KeySchemaElement(request.getSortKey(), KeyType.RANGE)),
Arrays.asList(
new AttributeDefinition(request.getPartitionKey(), ScalarAttributeType.S),
new AttributeDefinition(request.getSortKey(), ScalarAttributeType.N)),
new ProvisionedThroughput(request.getReadCapacityUnits(), request.getWriteCapacityUnits()));

We can optionally wait until a table is created based on the value of waitForActive in the request object: 

if (request.isWaitForActive()) {
try {
table.waitForActive();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

We can check the existence of a table and return an exception if the table already exists:

if (this.dynamoDB.getTable(tableName).getDescription() != null) {
return true;
}

The complete code is available with the code files. 

  1. Define Lambda handler to call the service method and return a response
  2. Package and deploy Lambda
  3. Define the CloudFormation template

In the CloudFormation template, add permissions for Lambda to execute the CreateTable and DescribeTable actions:

- Effect: Allow
Action:
- dynamodb:CreateTable
- dynamodb:DescribeTable
Resource:
- Fn::Sub: arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/*

CreateTable permission is required to create the table and DescribeTable permission is required to wait for table creation.

  1. Deploy the CloudFormation template
  2. Invoke Lambda and test it

Invoke our Lambda from the CLI as follows:

aws lambda invoke 
--invocation-type RequestResponse
--function-name lambda-dynamodb-create-table
--log-type Tail
--payload '{
"tableName":"my_table",
"partitionKey": "id",
"sortKey": "dateandtime",
"readCapacityUnits": 1,
"writeCapacityUnits": 1,
"waitForActive": false
}'
--region us-east-1
--profile admin
outputfile.txt

The waitForActive input parameter is set as false here, and hence the request will return immediately. If you are waiting for table creation by setting waitForActive as true, you might want to raise the timeout for the lambda. Otherwise, your request might time-out with an error message as follows:

{"errorMessage":"2018-09-26T02:55:46.038Z 9fd63df2-c137-11e8-9c78-e9dde91c7800 Task timed out after 15.01 seconds"}

We can now use the aws dynamodb describe-table CLI command to verify the new table's properties. 

To execute the version 2 service implementation, update the API_VERSION Lambda environment variable to V2:

aws lambda update-function-configuration 
--function-name lambda-dynamodb-create-table
--environment Variables={API_VERSION=V2}
--region us-east-1
--profile admin

Now, execute the previous commands and verify the resultant output file.

  1. Cleanup

To delete the table, you can use aws dynamodb delete-table

..................Content has been hidden....................

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