Skip to content

Commit e87c1c9

Browse files
committed
-add better behaviour for non-compliant files
-add update on file placeholder -tweak clients -add new diagram -adapt README file
1 parent 20a723a commit e87c1c9

File tree

23 files changed

+406
-326
lines changed

23 files changed

+406
-326
lines changed

README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ The AWS services involved are:
4040

4141
- [S3](https://docs.localstack.cloud/user-guide/aws/s3/) for storing pictures
4242
- [DynamoDB](https://docs.localstack.cloud/user-guide/aws/dynamodb/) for the entities
43-
- [Lambda](https://docs.localstack.cloud/user-guide/aws/lambda/) function that will validate the pictures.
43+
- [Lambda](https://docs.localstack.cloud/user-guide/aws/lambda/) function that will validate the pictures, apply a watermark and replace non-compliant files.
44+
- [SNS](https://docs.localstack.cloud/user-guide/aws/sns/) that receives update notifications
45+
- [SQS](https://docs.localstack.cloud/user-guide/aws/lambda/) that subscribes to a topic and delivers the messages to the Spring Boot app
4446

4547

46-
#### How we will be using it
48+
#### How to use it
4749

4850
We’ll be walking through a few scenarios using the application, and we expect it to maintain the
4951
behavior in both production and development environments. This behaviour can be "scientifically"
@@ -57,7 +59,7 @@ files, `application-prod.yml`, and `application-dev.yml`.
5759

5860
## Instructions
5961

60-
## Production simulation (Running on AWS)
62+
## Running on AWS
6163

6264
Now, we don’t have a real production environment because that’s not the point here, but most likely,
6365
an application like this runs on a container orchestration platform, and all the necessary configs
@@ -72,6 +74,10 @@ needs to be created with the following policies:
7274
- AmazonS3FullAccess
7375
- AWSLambda_FullAccess
7476
- AmazonDynamoDBFullAccess
77+
- AmazonSNSFullAccess
78+
- AmazonSQSFullAccess
79+
- AWSLambdaExecute
80+
- AmazonS3ObjectLambdaExecutionRolePolicy
7581

7682
We will be using the user's credentials and export them as temporary environment variables with the
7783
`export` (`set` on Windows) command:
@@ -89,10 +95,7 @@ step.
8995

9096
### Creating resources - running Terraform
9197

92-
Make sure you have Terraform [installed](https://developer.hashicorp.com/terraform/downloads).If
93-
you're
94-
not familiar or uncomfortable with Terraform, there's also a branch that uses only AWS cli to create
95-
resources.
98+
Make sure you have Terraform [installed](https://developer.hashicorp.com/terraform/downloads)
9699

97100
Under setup/terraform run:
98101

@@ -108,7 +111,7 @@ $ terraform apply
108111
```
109112

110113
This should create the needed S3 bucket, the DynamoDB `shipment` table and populate it with some
111-
sample data, and the Lambda function that will help with picture validation.
114+
sample data, the Lambda function that will help with picture validation, the SQS and SNS.
112115

113116
### Running the GUI
114117

@@ -134,8 +137,8 @@ At `localhost:3000` you should now be able to see a list of shipments with stand
134137
that means that only the database is populated, the pictures still need to be added from the
135138
`sample-pictures` folder.
136139

137-
The weight of a shipment we can perceive, but not the size, that's why we need pictures to
138-
understand,
140+
The weight of a shipment is already given, but not the size, that's why we need pictures to
141+
understand it better,
139142
using the "banana for scale" measuring unit. How else would we know??
140143

141144
Current available actions using the GUI:
@@ -154,6 +157,12 @@ To switch to using LocalStack instead of AWS services just run `docker compose u
154157
folder
155158
to spin up a Localstack container.
156159

160+
Before we proceed, make sure you clean up your AWS resources by running
161+
162+
```
163+
$ terraform destroy
164+
```
165+
157166
To generate the exact same resources on LocalStack, we need `tflocal`, a thin wrapper script around
158167
the terraform command line client. `tflocal` takes care of automatically configuring the local
159168
service
@@ -175,14 +184,13 @@ Usage: terraform [global options] <subcommand> [args]
175184
...
176185
```
177186

178-
From here on, it's smooth sailing, the same as before. Switch to `setup/tflocal` folder, the files are
179-
identical to the ones in `setup/terraform`, but for the newly generated state files, it is a good idea
180-
to separate these "workspaces":
187+
From here on, it's smooth sailing, the same as before. In the `setup/terraform` folder, run the `cleanup` script
188+
to get rid of any files that keep track of the resources' state. Then:
181189

182190
```
183191
$ tflocal init
184192
$ tflocal plan -var 'env=dev
185-
$ tflocal apply -var 'env=dev'
193+
$ tflocal apply
186194
```
187195

188196
What we're doing here is just passing an environmental variable to let the Lambda

app_diagram.png

114 KB
Loading

docker-compose.yml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
11
version: "3.9"
22

33
services:
4-
ping:
5-
image: alpine:3.14
6-
container_name: ping
7-
tty: true
8-
ports:
9-
- "127.0.0.1:8081:8081"
10-
volumes:
11-
- "/var/run/docker.sock:/var/run/docker.sock"
12-
environment:
13-
- DOCKER_HOST=unix:///var/run/docker.sock
144
localstack:
155
container_name: localstack
166
image: localstack/localstack:latest
177
ports:
188
- "127.0.0.1:4566:4566" # LocalStack Gateway
199
- "127.0.0.1:4510-4559:4510-4559" # external services port range
2010
environment:
21-
- DEBUG=1 # enable more verbose logs
11+
# - DEBUG=1 # enable more verbose logs
2212
- DOCKER_HOST=unix:///var/run/docker.sock #unix socket to communicate with the docker daemon
2313
# - LAMBDA_KEEPALIVE_MS=0 # disable lambda keepalive
2414
- LOCALSTACK_HOST=localstack # where services are available from other containers
25-
# - ENFORCE_IAM=1 # enforce IAM policies
15+
- ENFORCE_IAM=1 # enforce IAM policies
2616
volumes:
2717
- "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
2818
- "/var/run/docker.sock:/var/run/docker.sock"

pom.xml

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2727
</properties>
2828

29-
3029
<!-- Dependencies-->
3130
<dependencies>
3231
<dependency>
@@ -42,15 +41,6 @@
4241
<groupId>software.amazon.awssdk</groupId>
4342
<artifactId>s3</artifactId>
4443
</dependency>
45-
<dependency>
46-
<groupId>software.amazon.awssdk</groupId>
47-
<artifactId>sqs</artifactId>
48-
</dependency>
49-
50-
<dependency>
51-
<groupId>software.amazon.awssdk</groupId>
52-
<artifactId>sns</artifactId>
53-
</dependency>
5444
<dependency>
5545
<groupId>software.amazon.awssdk</groupId>
5646
<artifactId>dynamodb-enhanced</artifactId>
@@ -69,17 +59,7 @@
6959
</dependency>
7060
<dependency>
7161
<groupId>io.awspring.cloud</groupId>
72-
<artifactId>spring-cloud-starter-aws-messaging</artifactId>
73-
</dependency>
74-
<dependency>
75-
<groupId>org.springframework.boot</groupId>
76-
<artifactId>spring-boot-starter-websocket</artifactId>
77-
<version>3.0.4</version>
78-
</dependency>
79-
<dependency>
80-
<groupId>io.projectreactor</groupId>
81-
<artifactId>reactor-core</artifactId>
82-
<version>3.5.4</version>
62+
<artifactId>spring-cloud-aws-starter-sqs</artifactId>
8363
</dependency>
8464

8565

@@ -109,7 +89,7 @@
10989
<dependency>
11090
<groupId>io.awspring.cloud</groupId>
11191
<artifactId>spring-cloud-aws-dependencies</artifactId>
112-
<version>2.3.1</version>
92+
<version>3.0.0-RC1</version>
11393
<type>pom</type>
11494
<scope>import</scope>
11595
</dependency>

setup/terraform/cleanup.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
rm .terraform.lock.hcl
2+
rm -rf .terraform
3+
rm terraform.tfstate
4+
rm terraform.tfstate.backup

setup/terraform/main.tf

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
aws = {
44
source = "hashicorp/aws"
5-
version = ">= 4.52.0"
5+
version = "= 4.66.1"
66
}
77
}
88
}
@@ -90,16 +90,6 @@ resource "aws_lambda_permission" "s3_lambda_exec_permission" {
9090
source_arn = aws_s3_bucket.shipment_picture_bucket.arn
9191
}
9292

93-
resource "aws_sns_topic" "update_shipment_picture_topic" {
94-
name = "update_shipment_picture_topic"
95-
}
96-
97-
98-
resource "aws_sns_topic_subscription" "example_subscription" {
99-
topic_arn = aws_sns_topic.update_shipment_picture_topic.arn
100-
protocol = "https"
101-
endpoint = var.sns_sub_endpoint
102-
}
10393

10494
resource "aws_iam_role" "lambda_exec" {
10595
name = "lambda_exec_role"
@@ -126,7 +116,6 @@ resource "aws_iam_role_policy_attachment" "lambda_exec_policy" {
126116
}
127117

128118

129-
130119
resource "aws_iam_role_policy" "lambda_exec_policy" {
131120
name = "lambda_exec_policy"
132121
role = aws_iam_role.lambda_exec.id
@@ -162,4 +151,56 @@ resource "aws_iam_role_policy" "lambda_exec_policy" {
162151
EOF
163152
}
164153

154+
resource "aws_sns_topic" "update_shipment_picture_topic" {
155+
name = "update_shipment_picture_topic"
156+
}
157+
158+
resource "aws_sqs_queue" "update_shipment_picture_queue" {
159+
name = "update_shipment_picture_queue"
160+
}
161+
162+
resource "aws_sns_topic_subscription" "my_subscription" {
163+
topic_arn = aws_sns_topic.update_shipment_picture_topic.arn
164+
protocol = "sqs"
165+
endpoint = aws_sqs_queue.update_shipment_picture_queue.arn
166+
}
167+
168+
resource "aws_sqs_queue_policy" "my_queue_policy" {
169+
queue_url = aws_sqs_queue.update_shipment_picture_queue.id
170+
171+
policy = <<EOF
172+
{
173+
"Version": "2012-10-17",
174+
"Statement": [
175+
{
176+
"Sid": "AllowSNSSendMessage",
177+
"Effect": "Allow",
178+
"Principal": "*",
179+
"Action": "sqs:SendMessage",
180+
"Resource": "${aws_sqs_queue.update_shipment_picture_queue.arn}",
181+
"Condition": {
182+
"ArnEquals": {
183+
"aws:SourceArn": "${aws_sns_topic.update_shipment_picture_topic.arn}"
184+
}
185+
}
186+
}
187+
]
188+
}
189+
EOF
190+
}
191+
192+
resource "aws_sns_topic_subscription" "my_topic_subscription" {
193+
topic_arn = aws_sns_topic.update_shipment_picture_topic.arn
194+
protocol = "sqs"
195+
endpoint = aws_sqs_queue.update_shipment_picture_queue.arn
196+
197+
# Additional subscription attributes
198+
raw_message_delivery = true
199+
filter_policy = ""
200+
delivery_policy = ""
201+
202+
# Ensure the subscription is confirmed automatically
203+
confirmation_timeout_in_minutes = 1
204+
}
205+
165206

0 commit comments

Comments
 (0)