Skip to content

Commit

Permalink
Merge pull request #6 from localstack-samples/app_updates
Browse files Browse the repository at this point in the history
App updates
  • Loading branch information
tinyg210 authored Jan 11, 2024
2 parents c14a14d + 33d39f9 commit b146266
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 77 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ services:
environment:
- DEBUG=1 # enable more verbose logs
- DOCKER_HOST=unix:///var/run/docker.sock #unix socket to communicate with the docker daemon
# - LAMBDA_KEEPALIVE_MS=0 # disable lambda keepalive
- LOCALSTACK_HOST=localstack # where services are available from other containers
- ENFORCE_IAM=1 # enforce IAM policies
# - LOCALSTACK_AUTH_TOKEN=${LOCALSTACK_AUTH_TOKEN}
volumes:
- "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.20.26</version>
<version>2.22.12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Binary file added shipment-list-demo-cloud-pod
Binary file not shown.
7 changes: 3 additions & 4 deletions shipment-picture-lambda-validator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
Expand All @@ -21,7 +21,6 @@
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>protocol-core</artifactId>
<version>2.20.47</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
Expand Down Expand Up @@ -68,7 +67,7 @@
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.20.47</version>
<version>2.22.12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@

@Getter
public enum Location {
REGION(Region.US_EAST_1);
private final Region region;


REGION(Region.US_EAST_1);

private final Region region;
Location(Region region) {
this.region = region;
}
Location(Region region) {
this.region = region;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
package dev.ancaghenade.shipmentpicturelambdavalidator;

import software.amazon.awssdk.services.sns.SnsClient;

import java.net.URI;
import java.util.Objects;
import software.amazon.awssdk.services.sns.SnsClient;

public class SNSClientHelper {

private static final String AWS_ENDPOINT_URL = System.getenv("AWS_ENDPOINT_URL");
private static String snsTopicArn;
private static final String AWS_ENDPOINT_URL = System.getenv("AWS_ENDPOINT_URL");
private static String snsTopicArn;

public static SnsClient getSnsClient() {
public static SnsClient getSnsClient() {

var clientBuilder = SnsClient.builder();
var clientBuilder = SnsClient.builder();

if (Objects.nonNull(AWS_ENDPOINT_URL)) {
snsTopicArn = String.format("arn:aws:sns:%s:000000000000:update_shipment_picture_topic",
Location.REGION.getRegion());
if (Objects.nonNull(AWS_ENDPOINT_URL)) {
snsTopicArn = System.getenv("SNS_TOPIC_ARN_DEV");

return clientBuilder
.region(Location.REGION.getRegion())
.endpointOverride(URI.create(AWS_ENDPOINT_URL))
.build();
} else {
snsTopicArn = String.format("arn:aws:sns:%s:%s:update_shipment_picture_topic",
Location.REGION.getRegion(), "932043840972");
return clientBuilder.build();
return clientBuilder
.region(Location.REGION.getRegion())
.endpointOverride(URI.create(AWS_ENDPOINT_URL))
.build();
} else {
snsTopicArn = System.getenv("SNS_TOPIC_ARN_PROD");
return clientBuilder.build();
}
}
public static String topicARN() {
return snsTopicArn;
}
}

public static String topicARN() {
return snsTopicArn;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream,

// Check if the image was already processed
if (s3ObjectResponse.response().metadata().entrySet().stream().anyMatch(
entry -> entry.getKey().equals("exclude-lambda") && entry.getValue().equals("true"))) {
entry -> entry.getKey().equals("skip-processing") && entry.getValue().equals("true"))) {
context.getLogger().log("Object already present.");
return;
}
Expand Down Expand Up @@ -98,7 +98,7 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream,
var putObjectRequest = PutObjectRequest.builder()
.bucket(BUCKET_NAME)
.key(objectKey)
.metadata(Collections.singletonMap("exclude-lambda", "true"))
.metadata(Collections.singletonMap("skip-processing", "true"))
.build();

s3Client.putObject(putObjectRequest, RequestBody.fromBytes(imageBytes));
Expand All @@ -114,7 +114,7 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream,
var putObjectRequest = PutObjectRequest.builder()
.bucket(BUCKET_NAME)
.key(objectKey)
.metadata(Collections.singletonMap("exclude-lambda", "true"))
.metadata(Collections.singletonMap("skip-processing", "true"))
.build();

s3Client.putObject(putObjectRequest, RequestBody.fromBytes(
Expand Down Expand Up @@ -143,7 +143,7 @@ private String getObjectKey(InputStream inputStream, Context context) {
return keys.iterator().next();
}
} catch (IOException ioe) {
context.getLogger().log("caught IOException reading input stream");
context.getLogger().log("caught IOException reading input stream: " + ioe.getMessage());
}
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ aws:
dynamodb:
endpoint: http://localhost.localstack.cloud:4566/
sqs:
endpoint: http://localhost:4566/000000000000
endpoint: http://localhost.localstack.cloud:4566/000000000000
region: us-east-1
4 changes: 2 additions & 2 deletions src/main/resources/buckets.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
shipment-picture-bucket=shipment-picture-bucket-concise-malamute
shipment-picture-bucket-validator=shipment-picture-lambda-validator-bucket-concise-malamute
shipment-picture-bucket=shipment-picture-bucket-certain-yak
shipment-picture-bucket-validator=shipment-picture-lambda-validator-bucket-certain-yak
2 changes: 2 additions & 0 deletions terraform/locals.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
locals {
json_data = file("./data.json")
tf_data = jsondecode(local.json_data)
sns_topic_arn_prod = "arn:aws:sns:${var.aws_region}:${var.account_id}:${var.sns_topic_name}"
sns_topic_arn_dev = "arn:aws:sns:${var.aws_region}:000000000000:${var.sns_topic_name}"
}
23 changes: 20 additions & 3 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "= 4.66.1"
version = "= 5.31.0"
}
}
}
Expand All @@ -19,6 +19,21 @@ resource "random_pet" "random_name" {
separator = "-"
}

variable "aws_region" {
description = "AWS region"
default = "us-east-1"
}

variable "account_id" {
description = "AWS Account ID"
default = "YOUR_AWS_ACCOUNT_NUMBER"
}

variable "sns_topic_name" {
description = "SNS Topic Name"
default = "update_shipment_picture_topic"
}

# S3 bucket
resource "aws_s3_bucket" "shipment_picture_bucket" {
bucket = "shipment-picture-bucket-${random_pet.random_name.id}"
Expand Down Expand Up @@ -75,7 +90,7 @@ resource "aws_s3_bucket_object" "lambda_code" {
resource "aws_lambda_function" "shipment_picture_lambda_validator" {
function_name = "shipment-picture-lambda-validator"
handler = "dev.ancaghenade.shipmentpicturelambdavalidator.ServiceHandler::handleRequest"
runtime = "java11"
runtime = "java17"
role = aws_iam_role.lambda_exec.arn
s3_bucket = aws_s3_bucket.lambda_code_bucket.id
s3_key = aws_s3_bucket_object.lambda_code.key
Expand All @@ -84,6 +99,8 @@ resource "aws_lambda_function" "shipment_picture_lambda_validator" {
environment {
variables = {
BUCKET = aws_s3_bucket.shipment_picture_bucket.bucket
SNS_TOPIC_ARN_DEV = local.sns_topic_arn_dev
SNS_TOPIC_ARN_PROD = local.sns_topic_arn_prod
}
}
}
Expand Down Expand Up @@ -171,7 +188,7 @@ resource "aws_iam_role_policy" "lambda_exec_policy" {

# Define the topic
resource "aws_sns_topic" "update_shipment_picture_topic" {
name = "update_shipment_picture_topic"
name = "${var.sns_topic_name}"
}

# Define the queue
Expand Down

0 comments on commit b146266

Please sign in to comment.