Skip to content

Commit 58e98df

Browse files
author
Anca
committed
merge terraform branch
2 parents 4b458ac + cfa33e8 commit 58e98df

File tree

22 files changed

+764
-329
lines changed

22 files changed

+764
-329
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,13 @@ src/main/shipment-list-frontend/.env.production.local
5555
src/main/shipment-list-frontend/npm-debug.log*
5656
src/main/shipment-list-frontend/yarn-debug.log*
5757
src/main/shipment-list-frontend/yarn-error.log*
58+
59+
# terraform
60+
61+
setup/terraform/terraform.tfstate
62+
setup/terraform/.terraform.lock.hcl
63+
setup/terraform/terraform.tfstate.backup
64+
65+
# lambda module
66+
/shipment-picture-lambda-validator/.idea
67+
/shipment-picture-lambda-validator/target/

README.md

Lines changed: 108 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
### Prerequisites
66

7+
** This demo was conceived and ran on macOS Catalina version 10.15.7. Other operating systems might
8+
need slight variations in using command line tools.
9+
710
- Maven 3.8.5 & Java 17
811
- AWS free tier account
912
- Docker - for running LocalStack
10-
- AWS Command Line Interface - for managing your services
13+
- Terraform for creating AWS & LocalStack resources
1114
- npm - for running the frontend app
1215

1316
## Purpose
@@ -37,18 +40,20 @@ The AWS services involved are:
3740
## How we will be using it
3841

3942
We’ll be walking through a few scenarios using the application, and we expect it to maintain the
40-
behavior in both production and development environments.
43+
behavior in both production and development environments. This behaviour can be "scientifically"
44+
backed up
45+
by adding integration tests.
46+
4147
We’ll take advantage of one of the core features of the Spring framework that allows us to bind our
4248
beans to different profiles, such as dev, test, and prod. Of course, these beans need to know how to
4349
behave in each environment, so they’ll get that information from their designated configuration
44-
files,
45-
`application-prod.yml`, and `application-dev.yml`.
50+
files, `application-prod.yml`, and `application-dev.yml`.
4651

4752
## Running it
4853

4954
## Production simulation
5055

51-
Now we don’t have a real production environment because that’s not the point here, but most likely,
56+
Now, we don’t have a real production environment because that’s not the point here, but most likely,
5257
an application like this runs on a container orchestration platform, and all the necessary configs
5358
are still provided. Since we’re only simulating a production instance, all the configurations are
5459
kept in the `application-prod.yml` file.
@@ -62,81 +67,139 @@ needs to be created with the following policies:
6267
- AWSLambda_FullAccess
6368
- AmazonDynamoDBFullAccess
6469

65-
### Creating the picture bucket
70+
We will be using the user's credentials and export them as temporary environment variable with the
71+
`export` (`set` on Windows) command:
72+
73+
```
74+
$ export AWS_ACCESS_KEY_ID=[your_aws_access_key_id]
75+
$ export AWS_SECRET_ACCESS_KEY=[your_aws_secret_access_key_id]
76+
```
77+
78+
### Building the validator module
6679

67-
The `scripts/new-bucket.sh` script will create the necessary S3 resource.
80+
Step into the `shipment-picture-lambda-validator` module and run `mvn clean package shade:shade`.
81+
This will create an uber-jar by packaging all its dependencies. We'll need this one in the next
82+
step.
6883

69-
### Creating the DynamoDB table
84+
### Creating resources - running Terraform
7085

86+
Make sure you have Terraform [installed](https://developer.hashicorp.com/terraform/downloads).If
87+
you're
88+
not familiar or uncomfortable with Terraform, there's also a branch that uses only AWS cli to create
89+
resources.
7190

72-
At startup @dynamobee helps set up the table we need and populate it with some sample data.
73-
@dynamobee is library for tracking, managing, and applying database changes
74-
The changelog acts as a database version control. It tracks all the changes made to the database,
75-
and helps you manage database migration.
91+
Under setup/terraform run:
7692

77-
### Running the central backend service
93+
```
94+
$ terraform init
95+
$ terraform plan
96+
```
7897

79-
To run the backend simply use
98+
Once these 2 commands run successfully and no errors occur, it's time to run:
8099

81100
```
82-
mvn spring-boot:run -Dspring-boot.run.profiles=prod
101+
$ terraform apply
83102
```
84103

104+
This should create the needed S3 bucket, the DynamoDB `shipment` table and populate it with some
105+
sample data, and the Lambda function that will help with picture validation.
106+
107+
### Running the GUI
108+
85109
### Running the GUI
86110

87111
Now `cd` into `src/main/shipment-list-frontend` and run `npm install` and `npm start`.
88112
This will spin up the React app that can be accessed on `localhost:3000`.
89113

114+
For running it on Windows, there are some
115+
[extra requirements](https://learn.microsoft.com/en-us/windows/dev-environment/javascript/react-on-windows)
116+
,
117+
but no worries, it should be straightforward.
118+
119+
Go back to the root folder and run the backend simply by using
120+
121+
```
122+
$ mvn spring-boot:run -Dspring-boot.run.profiles=prod
123+
```
124+
125+
Notice the `prod` profile is being set via command line arguments.
126+
90127
### Using the application
91128

92-
You should now be able to see a list of shipments with standard icons, that means that only the
93-
database
94-
is populated, the pictures still need to be added from the `sample-pictures` folder.
95-
The weight of a shipment we can perceive, but not the size, that's why we need to display it,
129+
At `localhost:3000` you should now be able to see a list of shipments with standard icons,
130+
that means that only the database is populated, the pictures still need to be added from the
131+
`sample-pictures` folder.
132+
133+
The weight of a shipment we can perceive, but not the size, that's why we need pictures to
134+
understand,
96135
using the "banana for scale" measuring unit. How else would we know??
97136

98137
Current available actions using the GUI:
138+
99139
- upload a new image
100140
- delete shipment from the list
101141

102-
### Running the Lambda function
142+
Files that are not pictures will be deleted
143+
and the shipment picture will be replaced with a generic icon, because we don't want any trouble.
103144

104-
The Lambda function is still not there. This falls under the `shipment-list-lambda-validator` project.
145+
## Developer environment
146+
147+
<<<<<<< HEAD
148+
To switch to using LocalStack instead of AWS services just run `docker compose up` to spin up a
149+
Localstack
150+
container.
151+
After that, the Spring Boot application needs to start using the dev profile (ideally stop the
152+
previous instance):
153+
=======
154+
155+
To switch to using LocalStack instead of AWS services just run `docker compose up` in the root
156+
folder
157+
to spin up a Localstack container.
158+
159+
To generate the exact same resources on LocalStack, we need `tflocal`, a thin wrapper script around
160+
the terraform command line client. `tflocal` takes care of automatically configuring the local
161+
service
162+
endpoints, which allows you to easily deploy your unmodified Terraform scripts against LocalStack.
163+
164+
You can [install](https://docs.localstack.cloud/user-guide/integrations/terraform/) the `tflocal`
165+
command via pip (requires a local Python installation):
105166

106167
```
107-
git clone https://github.com/tinyg210/shipment-list-lambda-validator.git
168+
$ pip install terraform-local
108169
```
109170

110-
The `create-lambda.sh` script will do everything that needs for the creation and configuration of
111-
the
112-
Lambda. (I know what you're thinking, Terraform will follow.)
113-
Run `add-notif-config-for-lambda.sh`, but before that remember to edit `notification-config.json`
114-
with
115-
your own AWS account ID. This will enable the Lambda to receive notifications every time a picture
116-
is being
117-
added to S3.
171+
Once installed, the `tflocal` command should be available, with the same interface as the terraform
172+
command line. Try it out:
118173

119-
You should now be able to add a new picture for each shipment. Files that are not pictures will be
120-
deleted
121-
and the shipment picture will be replaced with a generic icon.
174+
```
175+
$ tflocal --help
176+
Usage: terraform [global options] <subcommand> [args]
177+
...
178+
```
122179

123-
## Developer environment
180+
From here on, it's smooth sailing, the same as before. You can eve use the same folder to run
181+
your commands:
124182

125-
To switch to using LocalStack instead of AWS services just run `docker compose up` to spin up a
126-
Localstack
127-
container.
128-
After that, the Spring Boot application needs to start using the dev profile (ideally stop the previous instance):
183+
```
184+
$ tflocal init
185+
$ tflocal plan -var 'env=dev'
186+
$ tflocal apply -var 'env=dev'
187+
```
188+
189+
Well, almost, we're doing a little cheating and passing and environmental variable to let the Lambda
190+
know this is the `dev` environment.
191+
192+
After that, the Spring Boot application needs to start using the dev profile (make sure you're in
193+
the
194+
root folder):
129195

130196
```
131-
mvn spring-boot:run -Dspring-boot.run.profiles=dev
197+
$ mvn spring-boot:run -Dspring-boot.run.profiles=dev
132198
```
133199

134-
This should again populate the DynamoDB, this time on LocalStack.
135-
From here on, the rest of the steps are the same, but all the scripts that need to run end
136-
in the `-local` suffix,
137-
as they use the `awslocal` CLI.
200+
Go back to `localhost:3000` and a new list will be available, and notice that the functionalities of
201+
the application have not changed.
138202

139-
The same actions should be easily achieved again, but locally.
203+
There you have it, smooth transition from AWS to Localstack, with no code change. 👍🏻
140204

141-
*Coming soon: creating all resources using Terraform, for a better user experience.
142205

docker-compose.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@ services:
55
image: localstack/localstack
66
container_name: localstack
77
ports:
8-
- "4566:4566" # port of to where localstack can be addressed to
9-
- "9000:9000"
8+
- "127.0.0.1:4566:4566" # LocalStack Gateway
9+
- "127.0.0.1:4510-4559:4510-4559" # external services port range
1010
environment:
11-
- SERVICES=s3,dynamodb,lambda # a list of desired services you want to use.
12-
- DEFAULT_REGION=eu-central-1 # where the localstack mocks to be running
13-
- AWS_ACCESS_KEY_ID=test_access_key
14-
- AWS_SECRET_ACCESS_KEY=test_secret_access_key
11+
- DEFAULT_REGION=eu-central-1 # where the localstack mocks will be running
1512
- DATA_DIR=/tmp/localstack/data
1613
- PORT_WEB_UI=9000
1714
- LAMBDA_EXECUTOR=local

pom.xml

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
</parent>
1212

1313
<groupId>dev.ancaghenade</groupId>
14-
<artifactId>user-profile-demo</artifactId>
14+
<artifactId>shipment-list-demo</artifactId>
1515
<version>0.0.1-SNAPSHOT</version>
1616

17-
<name>user-profile-demo</name>
18-
<description>User Profile Service</description>
17+
<name>shipment-list-demo</name>
18+
<description>Shipment List Service</description>
1919

2020
<properties>
2121
<java.version>17</java.version>
@@ -50,11 +50,6 @@
5050
<artifactId>aws-java-sdk-dynamodb</artifactId>
5151
<version>1.12.383</version>
5252
</dependency>
53-
<dependency>
54-
<groupId>com.github.dynamobee</groupId>
55-
<artifactId>dynamobee</artifactId>
56-
<version>0.6</version>
57-
</dependency>
5853
<dependency>
5954
<groupId>org.springframework.boot</groupId>
6055
<artifactId>spring-boot-autoconfigure</artifactId>
@@ -85,37 +80,6 @@
8580
</excludes>
8681
</configuration>
8782
</plugin>
88-
89-
<!-- TBD? -->
90-
<!-- <plugin>-->
91-
<!-- <groupId>org.apache.maven.plugins</groupId>-->
92-
<!-- <artifactId>maven-checkstyle-plugin</artifactId>-->
93-
<!-- <version>${maven-checkstyle-plugin.version}</version>-->
94-
<!-- <dependencies>-->
95-
<!-- <dependency>-->
96-
<!-- <groupId>com.puppycrawl.tools</groupId>-->
97-
<!-- <artifactId>checkstyle</artifactId>-->
98-
<!-- <version>${checkstyle.version}</version>-->
99-
<!-- </dependency>-->
100-
<!-- </dependencies>-->
101-
<!-- <configuration>-->
102-
<!-- <configLocation>google_checks.xml</configLocation>-->
103-
<!-- <encoding>UTF-8</encoding>-->
104-
<!-- <consoleOutput>true</consoleOutput>-->
105-
<!-- <failsOnError>true</failsOnError>-->
106-
<!-- <linkXRef>false</linkXRef>-->
107-
<!-- <violationSeverity>warning</violationSeverity>-->
108-
<!-- </configuration>-->
109-
<!-- <executions>-->
110-
<!-- <execution>-->
111-
<!-- <id>validate</id>-->
112-
<!-- <phase>validate</phase>-->
113-
<!-- <goals>-->
114-
<!-- <goal>check</goal>-->
115-
<!-- </goals>-->
116-
<!-- </execution>-->
117-
<!-- </executions>-->
118-
<!-- </plugin>-->
11983
</plugins>
12084
</build>
12185
</project>

scripts/new-bucket-local.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

scripts/new-bucket.sh

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)