Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added possibility to deploy sample example with terraform #37

Merged
merged 8 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ jobs:
command:
pip3 install -r requirements-dev.txt --upgrade
- localstack/wait
- run:
name: Build lambdas
command:
bin/build_lambdas.sh

- run:
name: Deploy infrastructure
command:
bin/deploy.sh
deployment/awslocal/deploy.sh

- run:
name: Export state
command: localstack state export ls-state.zip
Expand Down
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LOCALSTACK_AUTH_TOKEN=YOUR_TOKEN
6 changes: 5 additions & 1 deletion .github/workflows/cloudpod_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ jobs:
env:
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}

- name: Build lambdas
run: |
bin/build_lambdas.sh

- name: Deploy infrastructure
run: |
bin/deploy.sh
deployment/awslocal/deploy.sh

- name: Run Tests
env:
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ jobs:
install-awslocal: 'true'
env:
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
- name: Build lambdas
run: |
bin/build_lambdas.sh

- name: Deploy infrastructure
run: |
bin/deploy.sh
deployment/awslocal/deploy.sh

- name: Run Tests
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/preview_create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ jobs:
preview-cmd: |
# Add your custom deployment commands here.
# Below is an example for the Image resizer application.
bin/deploy.sh
bin/build_lambdas.sh && deployment/awslocal/deploy.sh
27 changes: 26 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,29 @@ volume/

# lambda packages
lambdas/*/package/
lambdas/*/lambda.zip
lambdas/*/lambda.zip
terraform/*/*.zip

# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log
crash.*.log

*.tfvars
*.tfvars.json

override.tf
override.tf.json
*_override.tf
*_override.tf.json

.terraform.tfstate.lock.info

.terraformrc
terraform.rc
3 changes: 2 additions & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ deploy:
- .github/*
- when: always
script:
- ./bin/deploy.sh
- ./bin/build_lambdas.sh
- ./deployment/awslocal/deploy.sh
- localstack state export ./ls-state-pod.zip

test:
Expand Down
38 changes: 38 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export AWS_ACCESS_KEY_ID ?= test
export AWS_SECRET_ACCESS_KEY ?= test
SHELL := /bin/bash

include .env

usage: ## Show this help
@grep -F -h "##" $(MAKEFILE_LIST) | grep -F -v grep -F | sed -e 's/\\$$//' -e 's/##//'

install: ## Install dependencies
@pip install -r requirements-dev.txt

build: ## Build lambdas in the lambdas folder
bin/build_lambdas.sh;

awslocal-setup: ## Deploy the application locally using `awslocal`, a wrapper for the AWS CLI
$(MAKE) build
deployment/awslocal/deploy.sh

terraform-setup: ## Deploy the application locally using `tflocal`, a wrapper for Terraform CLI
$(MAKE) build
cd deployment/terraform; \
tflocal init; \
echo "Deploying Terraform configuration 🚀"; \
tflocal apply --auto-approve; \
echo "Paste the function URLs above to the WebApp 🎉";

terraform-destroy: ## Destroy all resources created locally using terraform scripts
cd deployment/terraform; \
tflocal destroy --auto-approve;

start: ## Start the LocalStack Pro container in the detached mode
@LOCALSTACK_AUTH_TOKEN=$(LOCALSTACK_AUTH_TOKEN) localstack start -d

stop: ## Stop the LocalStack Pro container
localstack stop

.PHONY: usage install build awslocal-setup terraform-setup terraform-destroy start stop
51 changes: 35 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,41 @@ source .venv/bin/activate
pip install -r requirements-dev.txt
```

## Instructions

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.

Next, execute `make install` to install needed dependencies.

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.

If you prefer, you can also follow these step-by-step instructions for a manual deployment.

### LocalStack

Start LocalStack Pro with the appropriate CORS configuration for the S3 Website:
Start LocalStack Pro with Auth Token:

```bash
LOCALSTACK_AUTH_TOKEN=... localstack start
LOCALSTACK_AUTH_TOKEN=... localstack start (-d)
```

## Instructions
### Terraform

To create the infrastructure using Terraform, run the following commands:

```shell
cd deployment/terraform
tflocal init
tflocal apply --auto-approve
```

We are using the `tflocal` wrapper to configure the local service endpoints, and send the API requests to LocalStack, instead of AWS.

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

Here are instructions to deploy it manually step-by-step.
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.

### Create the buckets
#### Create the buckets

The names are completely configurable via SSM:

Expand All @@ -93,14 +112,14 @@ awslocal s3 mb s3://localstack-thumbnails-app-images
awslocal s3 mb s3://localstack-thumbnails-app-resized
```

### Put the bucket names into the parameter store
#### Put the bucket names into the parameter store

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

### Create the DLQ Topic for failed lambda invokes
#### Create the DLQ Topic for failed lambda invokes

```bash
awslocal sns create-topic --name failed-resize-topic
Expand All @@ -115,9 +134,9 @@ awslocal sns subscribe \
--notification-endpoint [email protected]
```

### Create the lambdas
#### Create the lambdas

#### S3 pre-signed POST URL generator
##### S3 pre-signed POST URL generator

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

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

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

### Image lister lambda
#### Image lister lambda

The `list` Lambda is very similar:

Expand All @@ -166,7 +185,7 @@ awslocal lambda create-function-url-config \
--auth-type NONE
```

### Resizer Lambda
#### Resizer Lambda

```bash
(
Expand All @@ -189,23 +208,23 @@ awslocal lambda create-function \
--environment Variables="{STAGE=local}"
```

### Connect the S3 bucket to the resizer lambda
#### Connect the S3 bucket to the resizer lambda

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

### Create the static s3 webapp
#### Create the static s3 webapp

```bash
awslocal s3 mb s3://webapp
awslocal s3 sync --delete ./website s3://webapp
awslocal s3 website s3://webapp --index-document index.html
```

### Using the application
#### Using the application

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

Expand Down
28 changes: 28 additions & 0 deletions bin/build_lambdas.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

(cd lambdas/presign; rm -f lambda.zip; zip lambda.zip handler.py)

(cd lambdas/list; rm -f lambda.zip; zip lambda.zip handler.py)

os=$(uname -s)
if [ "$os" == "Darwin" ]; then
(
cd lambdas/resize
rm -rf libs lambda.zip
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"

cd libs && zip -r ../lambda.zip . && cd ..
zip lambda.zip handler.py
rm -rf libs
)
else
(
cd lambdas/resize
rm -rf package lambda.zip
mkdir package
pip3 install -r requirements.txt --platform manylinux2014_x86_64 --only-binary=:all: -t package
zip lambda.zip handler.py
cd package
zip -r ../lambda.zip *;
)
fi
3 changes: 2 additions & 1 deletion buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ phases:
- localstack start -d
pre_build:
commands:
- bin/deploy.sh
- bin/build_lambdas.sh
- deployment/awslocal/deploy.sh
build:
commands:
- pytest tests
Expand Down
24 changes: 0 additions & 24 deletions bin/deploy.sh → deployment/awslocal/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ awslocal sns subscribe \
--protocol email \
--notification-endpoint [email protected]

(cd lambdas/presign; rm -f lambda.zip; zip lambda.zip handler.py)
awslocal lambda create-function \
--function-name presign \
--runtime python3.11 \
Expand All @@ -30,7 +29,6 @@ awslocal lambda create-function-url-config \
--function-name presign \
--auth-type NONE

(cd lambdas/list; rm -f lambda.zip; zip lambda.zip handler.py)
awslocal lambda create-function \
--function-name list \
--runtime python3.11 \
Expand All @@ -46,28 +44,6 @@ awslocal lambda create-function-url-config \
--function-name list \
--auth-type NONE

os=$(uname -s)
if [ "$os" == "Darwin" ]; then
(
cd lambdas/resize
rm -rf libs lambda.zip
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"

cd libs && zip -r ../lambda.zip . && cd ..
zip lambda.zip handler.py
rm -rf libs
)
else
(
cd lambdas/resize
rm -rf package lambda.zip
mkdir package
pip3 install -r requirements.txt --platform manylinux2014_x86_64 --only-binary=:all: -t package
zip lambda.zip handler.py
cd package
zip -r ../lambda.zip *;
)
fi

awslocal lambda create-function \
--function-name resize \
Expand Down
Loading
Loading