Skip to content

Commit 20a723a

Browse files
committed
-add sns
-new pictures -add watermark logic to lambda handler -use sse for refreshing FE
1 parent 3516ba7 commit 20a723a

File tree

31 files changed

+1032
-198
lines changed

31 files changed

+1032
-198
lines changed

docker-compose.yml

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
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
414
localstack:
5-
image: localstack/localstack # name and tag of LocalStack Docker image to use
6-
container_name: localstack # the main docker container name
15+
container_name: localstack
16+
image: localstack/localstack:latest
717
ports:
818
- "127.0.0.1:4566:4566" # LocalStack Gateway
19+
- "127.0.0.1:4510-4559:4510-4559" # external services port range
920
environment:
10-
- PORT_WEB_UI=9000
11-
- LAMBDA_EXECUTOR=local # the lambda code is executed directly in the context of LocalStack itself
21+
- DEBUG=1 # enable more verbose logs
22+
- DOCKER_HOST=unix:///var/run/docker.sock #unix socket to communicate with the docker daemon
23+
# - LAMBDA_KEEPALIVE_MS=0 # disable lambda keepalive
24+
- LOCALSTACK_HOST=localstack # where services are available from other containers
25+
# - ENFORCE_IAM=1 # enforce IAM policies
26+
volumes:
27+
- "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
28+
- "/var/run/docker.sock:/var/run/docker.sock"
29+
30+
networks:
31+
ls:
32+
name: ls

pom.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@
4242
<groupId>software.amazon.awssdk</groupId>
4343
<artifactId>s3</artifactId>
4444
</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>
4554
<dependency>
4655
<groupId>software.amazon.awssdk</groupId>
4756
<artifactId>dynamodb-enhanced</artifactId>
@@ -58,12 +67,34 @@
5867
<groupId>org.springframework.boot</groupId>
5968
<artifactId>spring-boot-starter-logging</artifactId>
6069
</dependency>
70+
<dependency>
71+
<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>
83+
</dependency>
84+
85+
6186
<!-- Test -->
6287
<dependency>
6388
<groupId>org.springframework.boot</groupId>
6489
<artifactId>spring-boot-starter-test</artifactId>
6590
<scope>test</scope>
6691
</dependency>
92+
<dependency>
93+
<groupId>org.json</groupId>
94+
<artifactId>json</artifactId>
95+
<version>20220924</version>
96+
</dependency>
97+
6798
</dependencies>
6899

69100
<dependencyManagement>
@@ -75,6 +106,13 @@
75106
<type>pom</type>
76107
<scope>import</scope>
77108
</dependency>
109+
<dependency>
110+
<groupId>io.awspring.cloud</groupId>
111+
<artifactId>spring-cloud-aws-dependencies</artifactId>
112+
<version>2.3.1</version>
113+
<type>pom</type>
114+
<scope>import</scope>
115+
</dependency>
78116
</dependencies>
79117
</dependencyManagement>
80118

setup/terraform/main.tf

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
2-
# declares the provider it will be using (AWS) and the minimum
3-
# version of the provider required to run the script
41
terraform {
52
required_providers {
63
aws = {
@@ -13,8 +10,6 @@ provider "aws" {
1310
region = "eu-central-1"
1411
}
1512

16-
# S3 bucket, named "shipment-picture-bucket", which is set to be destroyed even if it
17-
# has non-empty contents, and sets the ACL to be private
1813
resource "aws_s3_bucket" "shipment_picture_bucket" {
1914
bucket = "shipment-picture-bucket"
2015
force_destroy = true
@@ -23,14 +18,6 @@ resource "aws_s3_bucket" "shipment_picture_bucket" {
2318
}
2419
}
2520

26-
27-
resource "aws_s3_bucket_acl" "shipment_picture_bucket_acl" {
28-
bucket = aws_s3_bucket.shipment_picture_bucket.id
29-
acl = "private"
30-
}
31-
32-
# dynamoDB table is created, with a primary key "shipmentId" and
33-
# enables server-side encryption
3421
resource "aws_dynamodb_table" "shipment" {
3522
name = "shipment"
3623
read_capacity = 10
@@ -49,15 +36,14 @@ resource "aws_dynamodb_table" "shipment" {
4936
stream_view_type = "NEW_AND_OLD_IMAGES"
5037
}
5138

52-
# populates table with sample data from file
5339
resource "aws_dynamodb_table_item" "shipment" {
5440
for_each = local.tf_data
5541
table_name = aws_dynamodb_table.shipment.name
5642
hash_key = "shipmentId"
5743
item = jsonencode(each.value)
5844
}
5945

60-
# the bucket used for storing the lambda jar
46+
6147
resource "aws_s3_bucket" "lambda_code_bucket" {
6248
bucket = "shipment-picture-lambda-validator-bucket"
6349
force_destroy = true
@@ -66,21 +52,12 @@ resource "aws_s3_bucket" "lambda_code_bucket" {
6652
}
6753
}
6854

69-
resource "aws_s3_bucket_acl" "lambda_code_bucket_acl" {
70-
bucket = aws_s3_bucket.lambda_code_bucket.id
71-
acl = "private"
72-
}
73-
74-
# bucket object with lambda code
7555
resource "aws_s3_bucket_object" "lambda_code" {
7656
source = "../../shipment-picture-lambda-validator/target/shipment-picture-lambda-validator.jar"
7757
bucket = aws_s3_bucket.lambda_code_bucket.id
7858
key = "shipment-picture-lambda-validator.jar"
7959
}
8060

81-
# creates lambda using the JAR file uploaded to the S3 bucket.
82-
# the function is set up with a java 11 runtime, with a specified IAM role,
83-
# memory of 512mb, timeout of 15s, and environment variable
8461
resource "aws_lambda_function" "shipment_picture_lambda_validator" {
8562
function_name = "shipment-picture-lambda-validator"
8663
handler = "dev.ancaghenade.shipmentpicturelambdavalidator.ServiceHandler::handleRequest"
@@ -89,17 +66,14 @@ resource "aws_lambda_function" "shipment_picture_lambda_validator" {
8966
s3_bucket = aws_s3_bucket.lambda_code_bucket.id
9067
s3_key = aws_s3_bucket_object.lambda_code.key
9168
memory_size = 512
92-
timeout = 15
69+
timeout = 60
9370
environment {
9471
variables = {
9572
ENVIRONMENT = var.env
9673
}
9774
}
9875
}
9976

100-
101-
# notification for "shipment-picture-bucket" S3 bucket,
102-
# so that the lambda function will be triggered when a new object is created in the bucket.
10377
resource "aws_s3_bucket_notification" "demo_bucket_notification" {
10478
bucket = aws_s3_bucket.shipment_picture_bucket.id
10579
lambda_function {
@@ -116,8 +90,17 @@ resource "aws_lambda_permission" "s3_lambda_exec_permission" {
11690
source_arn = aws_s3_bucket.shipment_picture_bucket.arn
11791
}
11892

119-
# IAM role with a policy that allows it to assume the role of a lambda function
120-
# the role is attached to the Lambda function
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+
}
103+
121104
resource "aws_iam_role" "lambda_exec" {
122105
name = "lambda_exec_role"
123106

@@ -137,14 +120,13 @@ resource "aws_iam_role" "lambda_exec" {
137120
EOF
138121
}
139122

140-
# used to attach the AmazonS3FullAccess policy to the IAM role lambda_exec
141123
resource "aws_iam_role_policy_attachment" "lambda_exec_policy" {
142124
policy_arn = "arn:aws:iam::aws:policy/AmazonS3FullAccess"
143125
role = aws_iam_role.lambda_exec.name
144126
}
145127

146-
# used to create a custom IAM policy
147-
# & give permission to the lambda to interract with the S3 and cloudwatch logs
128+
129+
148130
resource "aws_iam_role_policy" "lambda_exec_policy" {
149131
name = "lambda_exec_policy"
150132
role = aws_iam_role.lambda_exec.id
@@ -166,11 +148,13 @@ resource "aws_iam_role_policy" "lambda_exec_policy" {
166148
"Effect": "Allow",
167149
"Action": [
168150
"s3:GetObject",
169-
"s3:PutObject"
151+
"s3:PutObject",
152+
"sns:Publish"
170153
],
171154
"Resource": [
172155
"arn:aws:s3:::shipment-picture-bucket",
173-
"arn:aws:s3:::shipment-picture-bucket/*"
156+
"arn:aws:s3:::shipment-picture-bucket/*",
157+
"${aws_sns_topic.update_shipment_picture_topic.arn}"
174158
]
175159
}
176160
]

setup/terraform/vars.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@ variable "env" {
22
type = string
33
description = "dev env"
44
default = ""
5+
}
6+
7+
variable "sns_sub_endpoint" {
8+
type = string
9+
description = "SNS subscriber endpoint"
10+
default = "https://localhost:8081/sns/notifications"
511
}

setup/tflocal/main.tf

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ terraform {
22
required_providers {
33
aws = {
44
source = "hashicorp/aws"
5-
version = ">= 4.52.0"
5+
version = ">= 4.64.0"
66
}
77
}
88
}
99
provider "aws" {
10-
region = "eu-central-1"
10+
region = "eu-east-1"
1111
}
1212

1313
resource "aws_s3_bucket" "shipment_picture_bucket" {
14-
bucket = "shipment-picture-bucket"
14+
bucket = "shipment-picture-bucket"
1515
force_destroy = true
1616
lifecycle {
1717
prevent_destroy = false
@@ -51,7 +51,7 @@ resource "aws_dynamodb_table_item" "shipment" {
5151

5252

5353
resource "aws_s3_bucket" "lambda_code_bucket" {
54-
bucket = "shipment-picture-lambda-validator-bucket"
54+
bucket = "shipment-picture-lambda-validator-bucket"
5555
force_destroy = true
5656
lifecycle {
5757
prevent_destroy = false
@@ -60,7 +60,7 @@ resource "aws_s3_bucket" "lambda_code_bucket" {
6060

6161
resource "aws_s3_bucket_acl" "lambda_code_bucket_acl" {
6262
bucket = aws_s3_bucket.lambda_code_bucket.id
63-
acl = "private"
63+
acl = "private"
6464
}
6565

6666
resource "aws_s3_bucket_object" "lambda_code" {
@@ -81,6 +81,7 @@ resource "aws_lambda_function" "shipment_picture_lambda_validator" {
8181
environment {
8282
variables = {
8383
ENVIRONMENT = var.env
84+
SNS_TOPIC_ARN = aws_sns_topic.update_shipment_picture_topic.arn
8485
}
8586
}
8687
}
@@ -101,6 +102,20 @@ resource "aws_lambda_permission" "s3_lambda_exec_permission" {
101102
source_arn = aws_s3_bucket.shipment_picture_bucket.arn
102103
}
103104

105+
resource "aws_sns_topic" "update_shipment_picture_topic" {
106+
name = "update_shipment_picture_topic"
107+
}
108+
109+
resource "aws_sqs_queue" "update_shipment_picture_topic_queue" {
110+
name = "update_shipment_picture_topic_queue"
111+
}
112+
113+
resource "aws_sns_topic_subscription" "example_subscription" {
114+
topic_arn = aws_sns_topic.update_shipment_picture_topic.arn
115+
protocol = "sqs"
116+
endpoint = aws_sqs_queue.update_shipment_picture_topic_queue.arn
117+
}
118+
104119
resource "aws_iam_role" "lambda_exec" {
105120
name = "lambda_exec_role"
106121

setup/tflocal/setup.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
tflocal init
3+
tflocal plan -var 'env=dev'
4+
tflocal apply -var 'env=dev' --auto-approve

0 commit comments

Comments
 (0)