Skip to content

Commit b615e04

Browse files
k-a-ilAnastasia D.
and
Anastasia D.
authored
Added possibility to deploy sample example with terraform (#37)
Co-authored-by: Anastasia D. <[email protected]>
1 parent 119f0df commit b615e04

21 files changed

+596
-47
lines changed

.circleci/config.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,16 @@ jobs:
2626
command:
2727
pip3 install -r requirements-dev.txt --upgrade
2828
- localstack/wait
29+
- run:
30+
name: Build lambdas
31+
command:
32+
bin/build_lambdas.sh
33+
2934
- run:
3035
name: Deploy infrastructure
3136
command:
32-
bin/deploy.sh
37+
deployment/awslocal/deploy.sh
38+
3339
- run:
3440
name: Export state
3541
command: localstack state export ls-state.zip

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LOCALSTACK_AUTH_TOKEN=YOUR_TOKEN

.github/workflows/cloudpod_release.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@ jobs:
4545
env:
4646
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
4747

48+
- name: Build lambdas
49+
run: |
50+
bin/build_lambdas.sh
51+
4852
- name: Deploy infrastructure
4953
run: |
50-
bin/deploy.sh
54+
deployment/awslocal/deploy.sh
5155
5256
- name: Run Tests
5357
env:

.github/workflows/integration-test.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,13 @@ jobs:
5858
install-awslocal: 'true'
5959
env:
6060
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
61+
- name: Build lambdas
62+
run: |
63+
bin/build_lambdas.sh
6164
6265
- name: Deploy infrastructure
6366
run: |
64-
bin/deploy.sh
67+
deployment/awslocal/deploy.sh
6568
6669
- name: Run Tests
6770
env:

.github/workflows/preview_create.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ jobs:
4646
preview-cmd: |
4747
# Add your custom deployment commands here.
4848
# Below is an example for the Image resizer application.
49-
bin/deploy.sh
49+
bin/build_lambdas.sh && deployment/awslocal/deploy.sh

.gitignore

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,29 @@ volume/
5959

6060
# lambda packages
6161
lambdas/*/package/
62-
lambdas/*/lambda.zip
62+
lambdas/*/lambda.zip
63+
terraform/*/*.zip
64+
65+
# Local .terraform directories
66+
**/.terraform/*
67+
68+
# .tfstate files
69+
*.tfstate
70+
*.tfstate.*
71+
72+
# Crash log files
73+
crash.log
74+
crash.*.log
75+
76+
*.tfvars
77+
*.tfvars.json
78+
79+
override.tf
80+
override.tf.json
81+
*_override.tf
82+
*_override.tf.json
83+
84+
.terraform.tfstate.lock.info
85+
86+
.terraformrc
87+
terraform.rc

.gitlab-ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ deploy:
5656
- .github/*
5757
- when: always
5858
script:
59-
- ./bin/deploy.sh
59+
- ./bin/build_lambdas.sh
60+
- ./deployment/awslocal/deploy.sh
6061
- localstack state export ./ls-state-pod.zip
6162

6263
test:

Makefile

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
export AWS_ACCESS_KEY_ID ?= test
2+
export AWS_SECRET_ACCESS_KEY ?= test
3+
SHELL := /bin/bash
4+
5+
include .env
6+
7+
usage: ## Show this help
8+
@grep -F -h "##" $(MAKEFILE_LIST) | grep -F -v grep -F | sed -e 's/\\$$//' -e 's/##//'
9+
10+
install: ## Install dependencies
11+
@pip install -r requirements-dev.txt
12+
13+
build: ## Build lambdas in the lambdas folder
14+
bin/build_lambdas.sh;
15+
16+
awslocal-setup: ## Deploy the application locally using `awslocal`, a wrapper for the AWS CLI
17+
$(MAKE) build
18+
deployment/awslocal/deploy.sh
19+
20+
terraform-setup: ## Deploy the application locally using `tflocal`, a wrapper for Terraform CLI
21+
$(MAKE) build
22+
cd deployment/terraform; \
23+
tflocal init; \
24+
echo "Deploying Terraform configuration 🚀"; \
25+
tflocal apply --auto-approve; \
26+
echo "Paste the function URLs above to the WebApp 🎉";
27+
28+
terraform-destroy: ## Destroy all resources created locally using terraform scripts
29+
cd deployment/terraform; \
30+
tflocal destroy --auto-approve;
31+
32+
start: ## Start the LocalStack Pro container in the detached mode
33+
@LOCALSTACK_AUTH_TOKEN=$(LOCALSTACK_AUTH_TOKEN) localstack start -d
34+
35+
stop: ## Stop the LocalStack Pro container
36+
localstack stop
37+
38+
.PHONY: usage install build awslocal-setup terraform-setup terraform-destroy start stop

README.md

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,41 @@ source .venv/bin/activate
6969
pip install -r requirements-dev.txt
7070
```
7171

72+
## Instructions
73+
74+
You can set up and deploy the sample application on LocalStack by executing the commands in our Makefile. First, create a `.env` file using the provided `.env.example` file as a template, and include your LocalStack token in it. Then, run `make start` to initiate LocalStack on your machine.
75+
76+
Next, execute `make install` to install needed dependencies.
77+
78+
After that, launch `make terraform-setup` to provision the infrastructure on LocalStack using Terraform CLI and its scripts. Alternatively, run `make awslocal-setup` to set up the infrastructure using `awslocal`, a wrapper for the AWS CLI.
79+
80+
If you prefer, you can also follow these step-by-step instructions for a manual deployment.
81+
7282
### LocalStack
7383

74-
Start LocalStack Pro with the appropriate CORS configuration for the S3 Website:
84+
Start LocalStack Pro with Auth Token:
7585

7686
```bash
77-
LOCALSTACK_AUTH_TOKEN=... localstack start
87+
LOCALSTACK_AUTH_TOKEN=... localstack start (-d)
7888
```
7989

80-
## Instructions
90+
### Terraform
91+
92+
To create the infrastructure using Terraform, run the following commands:
93+
94+
```shell
95+
cd deployment/terraform
96+
tflocal init
97+
tflocal apply --auto-approve
98+
```
99+
100+
We are using the `tflocal` wrapper to configure the local service endpoints, and send the API requests to LocalStack, instead of AWS.
81101

82-
You can create the AWS infrastructure on LocalStack by running `bin/deploy.sh`.
83-
Make sure you have Python 3.11 activated before running the script.
102+
### AWS CLI
84103

85-
Here are instructions to deploy it manually step-by-step.
104+
You can execute the following commands to set up the infrastructure using `awslocal`. All the commands are also available in the `deployment/awslocal/deploy.sh` script.
86105

87-
### Create the buckets
106+
#### Create the buckets
88107

89108
The names are completely configurable via SSM:
90109

@@ -93,14 +112,14 @@ awslocal s3 mb s3://localstack-thumbnails-app-images
93112
awslocal s3 mb s3://localstack-thumbnails-app-resized
94113
```
95114

96-
### Put the bucket names into the parameter store
115+
#### Put the bucket names into the parameter store
97116

98117
```bash
99118
awslocal ssm put-parameter --name /localstack-thumbnail-app/buckets/images --type "String" --value "localstack-thumbnails-app-images"
100119
awslocal ssm put-parameter --name /localstack-thumbnail-app/buckets/resized --type "String" --value "localstack-thumbnails-app-resized"
101120
```
102121

103-
### Create the DLQ Topic for failed lambda invokes
122+
#### Create the DLQ Topic for failed lambda invokes
104123

105124
```bash
106125
awslocal sns create-topic --name failed-resize-topic
@@ -115,9 +134,9 @@ awslocal sns subscribe \
115134
--notification-endpoint [email protected]
116135
```
117136

118-
### Create the lambdas
137+
#### Create the lambdas
119138

120-
#### S3 pre-signed POST URL generator
139+
##### S3 pre-signed POST URL generator
121140

122141
This Lambda is responsible for generating pre-signed POST URLs to upload files to an S3 bucket.
123142

@@ -143,7 +162,7 @@ awslocal lambda create-function-url-config \
143162

144163
Copy the `FunctionUrl` from the response, you will need it later to make the app work.
145164

146-
### Image lister lambda
165+
#### Image lister lambda
147166

148167
The `list` Lambda is very similar:
149168

@@ -166,7 +185,7 @@ awslocal lambda create-function-url-config \
166185
--auth-type NONE
167186
```
168187

169-
### Resizer Lambda
188+
#### Resizer Lambda
170189

171190
```bash
172191
(
@@ -189,23 +208,23 @@ awslocal lambda create-function \
189208
--environment Variables="{STAGE=local}"
190209
```
191210

192-
### Connect the S3 bucket to the resizer lambda
211+
#### Connect the S3 bucket to the resizer lambda
193212

194213
```bash
195214
awslocal s3api put-bucket-notification-configuration \
196215
--bucket localstack-thumbnails-app-images \
197216
--notification-configuration "{\"LambdaFunctionConfigurations\": [{\"LambdaFunctionArn\": \"$(awslocal lambda get-function --function-name resize | jq -r .Configuration.FunctionArn)\", \"Events\": [\"s3:ObjectCreated:*\"]}]}"
198217
```
199218

200-
### Create the static s3 webapp
219+
#### Create the static s3 webapp
201220

202221
```bash
203222
awslocal s3 mb s3://webapp
204223
awslocal s3 sync --delete ./website s3://webapp
205224
awslocal s3 website s3://webapp --index-document index.html
206225
```
207226

208-
### Using the application
227+
#### Using the application
209228

210229
Once deployed, visit http://webapp.s3-website.localhost.localstack.cloud:4566
211230

bin/build_lambdas.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
3+
(cd lambdas/presign; rm -f lambda.zip; zip lambda.zip handler.py)
4+
5+
(cd lambdas/list; rm -f lambda.zip; zip lambda.zip handler.py)
6+
7+
os=$(uname -s)
8+
if [ "$os" == "Darwin" ]; then
9+
(
10+
cd lambdas/resize
11+
rm -rf libs lambda.zip
12+
docker run --platform linux/x86_64 --rm -v "$PWD":/var/task "public.ecr.aws/sam/build-python3.11" /bin/sh -c "pip3 install -r requirements.txt -t libs; exit"
13+
14+
cd libs && zip -r ../lambda.zip . && cd ..
15+
zip lambda.zip handler.py
16+
rm -rf libs
17+
)
18+
else
19+
(
20+
cd lambdas/resize
21+
rm -rf package lambda.zip
22+
mkdir package
23+
pip3 install -r requirements.txt --platform manylinux2014_x86_64 --only-binary=:all: -t package
24+
zip lambda.zip handler.py
25+
cd package
26+
zip -r ../lambda.zip *;
27+
)
28+
fi

buildspec.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ phases:
2525
- localstack start -d
2626
pre_build:
2727
commands:
28-
- bin/deploy.sh
28+
- bin/build_lambdas.sh
29+
- deployment/awslocal/deploy.sh
2930
build:
3031
commands:
3132
- pytest tests

bin/deploy.sh renamed to deployment/awslocal/deploy.sh

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ awslocal sns subscribe \
1414
--protocol email \
1515
--notification-endpoint [email protected]
1616

17-
(cd lambdas/presign; rm -f lambda.zip; zip lambda.zip handler.py)
1817
awslocal lambda create-function \
1918
--function-name presign \
2019
--runtime python3.11 \
@@ -30,7 +29,6 @@ awslocal lambda create-function-url-config \
3029
--function-name presign \
3130
--auth-type NONE
3231

33-
(cd lambdas/list; rm -f lambda.zip; zip lambda.zip handler.py)
3432
awslocal lambda create-function \
3533
--function-name list \
3634
--runtime python3.11 \
@@ -46,28 +44,6 @@ awslocal lambda create-function-url-config \
4644
--function-name list \
4745
--auth-type NONE
4846

49-
os=$(uname -s)
50-
if [ "$os" == "Darwin" ]; then
51-
(
52-
cd lambdas/resize
53-
rm -rf libs lambda.zip
54-
docker run --platform linux/x86_64 --rm -v "$PWD":/var/task "public.ecr.aws/sam/build-python3.11" /bin/sh -c "pip3 install -r requirements.txt -t libs; exit"
55-
56-
cd libs && zip -r ../lambda.zip . && cd ..
57-
zip lambda.zip handler.py
58-
rm -rf libs
59-
)
60-
else
61-
(
62-
cd lambdas/resize
63-
rm -rf package lambda.zip
64-
mkdir package
65-
pip3 install -r requirements.txt --platform manylinux2014_x86_64 --only-binary=:all: -t package
66-
zip lambda.zip handler.py
67-
cd package
68-
zip -r ../lambda.zip *;
69-
)
70-
fi
7147

7248
awslocal lambda create-function \
7349
--function-name resize \

0 commit comments

Comments
 (0)