Skip to content

Commit 6ec82a6

Browse files
author
Anca
committed
rename db service class + fix config files + readme fix
1 parent b808ca9 commit 6ec82a6

File tree

7 files changed

+43
-29
lines changed

7 files changed

+43
-29
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ needs to be created with the following policies:
6767
- AWSLambda_FullAccess
6868
- AmazonDynamoDBFullAccess
6969

70-
We will be using the user's credentials and export them as temporary environment variable with the
70+
We will be using the user's credentials and export them as temporary environment variables with the
7171
`export` (`set` on Windows) command:
7272

7373
```
@@ -172,7 +172,7 @@ Usage: terraform [global options] <subcommand> [args]
172172
```
173173

174174
From here on, it's smooth sailing, the same as before. Switch to `setup/tflocal` folder, the files are
175-
identical as the ones in `setup/terraform`, but for the newly generated state files, it is a good idea
175+
identical to the ones in `setup/terraform`, but for the newly generated state files, it is a good idea
176176
to separate these "workspaces":
177177

178178
```
@@ -192,7 +192,7 @@ root folder):
192192
$ mvn spring-boot:run -Dspring-boot.run.profiles=dev
193193
```
194194

195-
Go back to `localhost:3000` and a new list will be available, and notice that the functionalities of
195+
Go back to `localhost:3000` and a new list will be available; notice that the functionalities of
196196
the application have not changed.
197197

198198
There you have it, smooth transition from AWS to Localstack, with no code change. 👍🏻

docker-compose.yml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ version: "3.9"
22

33
services:
44
localstack:
5-
image: localstack/localstack
6-
container_name: localstack
5+
image: localstack/localstack # name and tag of LocalStack Docker image to use
6+
container_name: localstack # the main docker container name
77
ports:
88
- "127.0.0.1:4566:4566" # LocalStack Gateway
9-
- "127.0.0.1:4510-4559:4510-4559" # external services port range
109
environment:
11-
- DEFAULT_REGION=eu-central-1 # where the localstack mocks will be running
12-
- DATA_DIR=/tmp/localstack/data
1310
- PORT_WEB_UI=9000
14-
- LAMBDA_EXECUTOR=local
15-
- DOCKER_HOST=unix:///var/run/docker.sock
16-
- START_WEB=1
11+
- LAMBDA_EXECUTOR=local # the lambda code is executed directly in the context of LocalStack itself

setup/terraform/main.tf

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
# declares the provider it will be using (AWS) and the minimum
3+
# version of the provider required to run the script
14
terraform {
25
required_providers {
36
aws = {
@@ -10,8 +13,10 @@ provider "aws" {
1013
region = "eu-central-1"
1114
}
1215

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
1318
resource "aws_s3_bucket" "shipment_picture_bucket" {
14-
bucket = "shipment-picture-bucket"
19+
bucket = "shipment-picture-bucket"
1520
force_destroy = true
1621
lifecycle {
1722
prevent_destroy = false
@@ -24,6 +29,8 @@ resource "aws_s3_bucket_acl" "shipment_picture_bucket_acl" {
2429
acl = "private"
2530
}
2631

32+
# dynamoDB table is created, with a primary key "shipmentId" and
33+
# enables server-side encryption
2734
resource "aws_dynamodb_table" "shipment" {
2835
name = "shipment"
2936
read_capacity = 10
@@ -42,16 +49,17 @@ resource "aws_dynamodb_table" "shipment" {
4249
stream_view_type = "NEW_AND_OLD_IMAGES"
4350
}
4451

52+
# populates table with sample data from file
4553
resource "aws_dynamodb_table_item" "shipment" {
4654
for_each = local.tf_data
4755
table_name = aws_dynamodb_table.shipment.name
4856
hash_key = "shipmentId"
4957
item = jsonencode(each.value)
5058
}
5159

52-
60+
# the bucket used for storing the lambda jar
5361
resource "aws_s3_bucket" "lambda_code_bucket" {
54-
bucket = "shipment-picture-lambda-validator-bucket"
62+
bucket = "shipment-picture-lambda-validator-bucket"
5563
force_destroy = true
5664
lifecycle {
5765
prevent_destroy = false
@@ -60,15 +68,19 @@ resource "aws_s3_bucket" "lambda_code_bucket" {
6068

6169
resource "aws_s3_bucket_acl" "lambda_code_bucket_acl" {
6270
bucket = aws_s3_bucket.lambda_code_bucket.id
63-
acl = "private"
71+
acl = "private"
6472
}
6573

74+
# bucket object with lambda code
6675
resource "aws_s3_bucket_object" "lambda_code" {
6776
source = "../../shipment-picture-lambda-validator/target/shipment-picture-lambda-validator-1.0-SNAPSHOT.jar"
6877
bucket = aws_s3_bucket.lambda_code_bucket.id
6978
key = "shipment-picture-lambda-validator-1.0-SNAPSHOT.jar"
7079
}
7180

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
7284
resource "aws_lambda_function" "shipment_picture_lambda_validator" {
7385
function_name = "shipment-picture-lambda-validator"
7486
handler = "dev.ancaghenade.shipmentpicturelambdavalidator.ServiceHandler::handleRequest"
@@ -85,6 +97,9 @@ resource "aws_lambda_function" "shipment_picture_lambda_validator" {
8597
}
8698
}
8799

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.
88103
resource "aws_s3_bucket_notification" "demo_bucket_notification" {
89104
bucket = aws_s3_bucket.shipment_picture_bucket.id
90105
lambda_function {
@@ -101,6 +116,8 @@ resource "aws_lambda_permission" "s3_lambda_exec_permission" {
101116
source_arn = aws_s3_bucket.shipment_picture_bucket.arn
102117
}
103118

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
104121
resource "aws_iam_role" "lambda_exec" {
105122
name = "lambda_exec_role"
106123

@@ -120,11 +137,14 @@ resource "aws_iam_role" "lambda_exec" {
120137
EOF
121138
}
122139

140+
# used to attach the AmazonS3FullAccess policy to the IAM role lambda_exec
123141
resource "aws_iam_role_policy_attachment" "lambda_exec_policy" {
124142
policy_arn = "arn:aws:iam::aws:policy/AmazonS3FullAccess"
125143
role = aws_iam_role.lambda_exec.name
126144
}
127145

146+
# used to create a custom IAM policy
147+
# & give permission to the lambda to interract with the S3 and cloudwatch logs
128148
resource "aws_iam_role_policy" "lambda_exec_policy" {
129149
name = "lambda_exec_policy"
130150
role = aws_iam_role.lambda_exec.id

src/main/java/dev/ancaghenade/shipmentlistdemo/repository/ShipmentRepository.java renamed to src/main/java/dev/ancaghenade/shipmentlistdemo/repository/DynamoDBService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
import org.springframework.stereotype.Repository;
1414

1515
@Repository
16-
public class ShipmentRepository {
16+
public class DynamoDBService {
1717

1818
private final DynamoDBMapper dynamoDBMapper;
1919

2020
@Autowired
21-
public ShipmentRepository(DynamoDBMapper dynamoDBMapper) {
21+
public DynamoDBService(DynamoDBMapper dynamoDBMapper) {
2222
this.dynamoDBMapper = dynamoDBMapper;
2323
}
2424

src/main/java/dev/ancaghenade/shipmentlistdemo/service/ShipmentService.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import dev.ancaghenade.shipmentlistdemo.buckets.BucketName;
77
import dev.ancaghenade.shipmentlistdemo.entity.Shipment;
88
import dev.ancaghenade.shipmentlistdemo.repository.S3StorageService;
9-
import dev.ancaghenade.shipmentlistdemo.repository.ShipmentRepository;
9+
import dev.ancaghenade.shipmentlistdemo.repository.DynamoDBService;
1010
import java.io.File;
1111
import java.io.IOException;
1212
import java.nio.file.Files;
@@ -23,26 +23,26 @@
2323
public class ShipmentService {
2424

2525

26-
private final ShipmentRepository shipmentRepository;
26+
private final DynamoDBService dynamoDBService;
2727
private final S3StorageService s3StorageService;
2828

2929

3030
@Autowired
31-
public ShipmentService(ShipmentRepository shipmentRepository, S3StorageService s3StorageService) {
32-
this.shipmentRepository = shipmentRepository;
31+
public ShipmentService(DynamoDBService dynamoDBService, S3StorageService s3StorageService) {
32+
this.dynamoDBService = dynamoDBService;
3333
this.s3StorageService = s3StorageService;
3434
}
3535

3636
public List<Shipment> getAllShipments() {
37-
return shipmentRepository.getAllShipments();
37+
return dynamoDBService.getAllShipments();
3838
}
3939

4040
public String deleteShipment(String shipmentId) {
41-
return shipmentRepository.delete(shipmentId);
41+
return dynamoDBService.delete(shipmentId);
4242
}
4343

4444
public Shipment saveShipment(Shipment shipment) {
45-
return shipmentRepository.upsert(shipment);
45+
return dynamoDBService.upsert(shipment);
4646
}
4747

4848
public void uploadShipmentImage(String shipmentId, MultipartFile file) {
@@ -63,12 +63,12 @@ public void uploadShipmentImage(String shipmentId, MultipartFile file) {
6363
throw new IllegalStateException(e);
6464
}
6565
shipment.setImageLink(fileName);
66-
shipmentRepository.upsert(shipment);
66+
dynamoDBService.upsert(shipment);
6767
}
6868

6969

7070
public byte[] downloadShipmentImage(String shipmentId) {
71-
Shipment shipment = shipmentRepository.getShipment(shipmentId).stream()
71+
Shipment shipment = dynamoDBService.getShipment(shipmentId).stream()
7272
.findFirst()
7373
.orElseThrow(
7474
() -> new IllegalStateException(format("Shipment %s was not found.", shipmentId)));
@@ -93,7 +93,7 @@ private Map<String, String> getMetadata(MultipartFile file) {
9393
}
9494

9595
private Shipment getShipment(String shipmentId) {
96-
return shipmentRepository.getShipment(shipmentId).stream()
96+
return dynamoDBService.getShipment(shipmentId).stream()
9797
.findFirst()
9898
.orElseThrow(
9999
() -> new IllegalStateException(format("Shipment %s was not found.", shipmentId)));

src/main/resources/application-dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ aws:
66
s3:
77
endpoint: https://s3.localhost.localstack.cloud:4566/
88
dynamodb:
9-
endpoint: https://dynamodb.localhost.localstack.cloud:4566/
9+
endpoint: https://localhost.localstack.cloud:4566/
1010
region: eu-central-1

src/main/resources/application-prod.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
aws:
32
credentials:
43
access-key: ${AWS_ACCESS_KEY_ID}

0 commit comments

Comments
 (0)