Skip to content

Instantly share code, notes, and snippets.

View theawesomenayak's full-sized avatar

Siben Nayak theawesomenayak

View GitHub Profile
terraform {
required_providers {
wavefront = {
source = "terraform-providers/wavefront"
version = "2.4.0"
}
}
required_version = ">= 0.13"
}
PetStoreFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: PetStoreFunction
Handler: com.petstore.App::handleRequest
Runtime: java11
MemorySize: 512
Environment:
Variables:
PARAM1: VALUE
return DynamoDbClient.builder()
.endpointOverride(URI.create("http://localhost:4566"))
.region(Region.US_WEST_1)
.httpClient(ApacheHttpClient.builder().build())
.build();
version: '3'
services:
localstack:
container_name: ${LOCALSTACK_DOCKER_NAME-localstack_main}
image: localstack/localstack
network_mode: bridge
ports:
- "4566:4566"
environment:
- SERVICES=dynamodb
try {
final String httpMethod = input.getHttpMethod();
if ("GET".equalsIgnoreCase(httpMethod)) {
final List<Pet> pets = DependencyModule.PET_STORE_CLIENT.read();
final String result = DependencyModule.OBJECT_MAPPER.writeValueAsString(pets);
return response.withStatusCode(200).withBody(result);
} else {
final Pet pet = DependencyModule.OBJECT_MAPPER.readValue(input.getBody(), Pet.class);
final String id = DependencyModule.PET_STORE_CLIENT.write(pet);
return response.withStatusCode(200).withBody(id);
public final class DependencyModule {
private static final DynamoDbClient DYNAMO_DB_CLIENT = dynamoDbClient();
public static final PetStoreClient PET_STORE_CLIENT = new PetStoreClient(DYNAMO_DB_CLIENT);
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static DynamoDbClient dynamoDbClient() {
return DynamoDbClient.builder()
.region(Region.US_WEST_1)
import software.amazon.awssdk.services.dynamodb.model.ScanRequest;
import software.amazon.awssdk.services.dynamodb.model.ScanResponse;
....
public List<Pet> read() {
final ScanRequest scanRequest = ScanRequest.builder()
.tableName("pet-store")
.build();
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")
private final DynamoDbClient dynamoDbClient;
public PetStoreClient(final DynamoDbClient dynamoDbClient) {
this.dynamoDbClient = dynamoDbClient;
}
<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>