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
3942We’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+
4147We’ll take advantage of one of the core features of the Spring framework that allows us to bind our
4248beans to different profiles, such as dev, test, and prod. Of course, these beans need to know how to
4349behave 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,
5257an application like this runs on a container orchestration platform, and all the necessary configs
5358are still provided. Since we’re only simulating a production instance, all the configurations are
5459kept 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
87111Now ` cd ` into ` src/main/shipment-list-frontend ` and run ` npm install ` and ` npm start ` .
88112This 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,
96135using the "banana for scale" measuring unit. How else would we know??
97136
98137Current 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
0 commit comments