- Notifications
You must be signed in to change notification settings - Fork1
Example lightweight DynamoDB integration that limits AWS Lambda cold starts for Java
License
rjozefowicz/rapid-dynamodb
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Example lightweight DynamoDB integration that limits AWS Lambda cold starts for Java. It is pure-Java8 implementation without third-part libraries that increases fat JAR size and cold starts time.
It uses DynamoDB Low-Level API described onhttps://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Operations_Amazon_DynamoDB.html
This example assumes that there is DynamoDB TableTest created with Primary Keyuuid with String type. It uses JSR-374 JSONP as JSON parser (https://javaee.github.io/jsonp/) for better readibility during preparing JSON requests.
JSON request in the following format:
{"uuid":"NEW_UUID"}
Maven dependencies:
<dependencies> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-lambda-java-core</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>pl.r6lab.aws</groupId> <artifactId>rapid-dynamodb</artifactId> <version>0.1.0</version> </dependency> <dependency> <groupId>javax.json</groupId> <artifactId>javax.json-api</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.json</artifactId> <version>1.1.2</version> </dependency> </dependencies>
Example AWS Lambda handler that accepts request with uuid and after successful PutItem operation performs Scan and return all items:
publicclassRapidDynamoDBLambdaimplementsRequestHandler<Map<String,String>,String> {publicStringhandleRequest(Map<String,String>event,Contextcontext) {RapidDynamoDBClientrapidDynamoDBClient =RapidDynamoDBClient.envAware();DynamoDBRequestputItemRequest =DynamoDBRequest.of(Action.PUT_ITEM,getItemJson(event.get("uuid")));DynamoDBResponseresponse =rapidDynamoDBClient.execute(putItemRequest);if (response.isSuccess()) {DynamoDBRequestscanRequest =DynamoDBRequest.of(Action.SCAN,scanJson());returnrapidDynamoDBClient.execute(scanRequest).getPayload(); }else {context.getLogger().log(response.getPayload());thrownewIllegalStateException("Unable to put item into DynamoDB"); } }privateStringgetItemJson(Stringuuid) {JsonObjectgetItemJson =Json.createObjectBuilder() .add("TableName","Test") .add("Item",Json.createObjectBuilder() .add("uuid",Json.createObjectBuilder() .add("S",uuid))) .build();returngetItemJson.toString(); }privateStringscanJson() {JsonObjectgetItemJson =Json.createObjectBuilder() .add("TableName","Test") .add("ReturnConsumedCapacity","TOTAL") .build();returngetItemJson.toString(); }}
About
Example lightweight DynamoDB integration that limits AWS Lambda cold starts for Java
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.