In my previous articles, I talked about building and deploying serverless applications on AWS using Chalice and SAM.
These were quick fun projects that leveraged the power of serverless computing and allowed us to deploy a serverless application on AWS within a few minutes.
But many people are not able to completely leverage such tutorials if they don’t have an AWS account. Setting up an AWS account and configuring a development environment can be time-consuming. And it can lead to unwanted expenses as well (if you don’t configure it properly).
In this article, I will walk you through the steps required to build and deploy a serverless application without having to create and setup an actual AWS account.
This time, we will create a sample Pet Store application using Amazon API Gateway, AWS Lambda, and Amazon DynamoDB. This application will have APIs for adding a new pet and fetching the list of available pets.
We will be using AWS SAM for this tutorial. You can install and configure SAM by following the guidelines in the previous article here.
Run the sam-init
command to create a new project. This will create a pet-store
folder in your current directory.
sam init -r java11 -d maven --app-template pet-store -n pet-store
For more details about the parameters passed, please refer to the previous article.
Let’s change the pom.xml
to update the name of the module to PetStore
and use Java 11
instead of Java 8
.
<groupId>petstore</groupId>
<artifactId>PetStore</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
Let’s create a Pet
class now to contain the attributes for the pets. We will start with simple attributes like name
, age
and category
.
package com.petstore.model;
public final class Pet {
private String id;
private String name;
private int age;
private String category;
// Getters and Setters
}
Since we will be using Amazon DynamoDB as our data store, let’s add the corresponding SDK dependencies in the pom.xml
.
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
<version>2.15.12</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>apache-client</artifactId>
<version>2.5.12</version>
</dependency>
We will now create two functions in the PetStoreClient
class to read and write items from DynamoDB.
Adding a single item in DynamoDB is a PUT
request. We will create a PutItemRequest
and specify the table name and the item attributes to be added.
We will then use the DynamoDbClient
to put this item in DynamoDB.
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.PutItemRequest;
....
public String write(final Pet pet) {
final String id = UUID.randomUUID().toString();
final PutItemRequest putItemRequest = PutItemRequest.builder()
.tableName("pet-store")
.item(
Map.of(
"id", AttributeValue.builder().s(id).build(),
"name", AttributeValue.builder().s(pet.getName()).build(),
"age", AttributeValue.builder().n(Integer.toString(pet.getAge())).build(),
"category", AttributeValue.builder().s(pet.getCategory()).build()
)
)
.build();
dynamoDbClient.putItem(putItemRequest);
return id;
}
#aws #serverless #cloud #developer