Skip to content

Commit cdb4990

Browse files
committed
initial commit
0 parents  commit cdb4990

File tree

2 files changed

+189
-0
lines changed

2 files changed

+189
-0
lines changed

.gitignore

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
*.pyc
2+
*.pyo
3+
*.class
4+
*.log
5+
*.iml
6+
.coverage.*
7+
htmlcov
8+
9+
.cache
10+
.filesystem
11+
/infra/
12+
localstack/infra/
13+
14+
/node_modules/
15+
package-lock.json
16+
/nosetests.xml
17+
18+
.env
19+
venv
20+
/.venv*
21+
/.coverage
22+
.settings/
23+
.project
24+
.classpath
25+
.DS_Store
26+
*.egg-info/
27+
.eggs/
28+
.vagrant/
29+
*.sw*
30+
~*
31+
*~
32+
33+
node_modules/
34+
/build/
35+
/dist/
36+
/target/
37+
38+
.idea
39+
.vscode
40+
41+
**/obj/**
42+
**/lib/**
43+
44+
!bin/docker-entrypoint.sh
45+
46+
requirements.copy.txt
47+
.serverless/
48+
49+
**/.terraform/*
50+
*.tfstate
51+
*.tfstate.*
52+
*tfplan
53+
*.terraform.lock.hcl
54+
55+
.python-version
56+
57+
venv
58+
api_states
59+
60+
/integration/lambdas/golang/handler.zip
61+
/tests/integration/lambdas/golang/handler.zip
62+
tmp/
63+
64+
volume/

README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Serverless image resizer
2+
3+
Make sure you use the same version as the Python Lambdas to make Pillow work (3.9)
4+
5+
## Overview
6+
7+
TODO: image
8+
9+
## Use Terraform
10+
11+
```bash
12+
tflocal init && tflocal apply
13+
```
14+
15+
## Create the infrastructure manually
16+
17+
### Create the buckets
18+
19+
The names are completely configurable via SSM:
20+
21+
```bash
22+
awslocal s3 mb s3://localstack-thumbnails-app-images
23+
awslocal s3 mb s3://localstack-thumbnails-app-resized
24+
```
25+
26+
### Put the bucket names into the parameter store
27+
28+
```bash
29+
awslocal ssm put-parameter --name /localstack-thumbnail-app/buckets/images --value "localstack-thumbnails-app-images"
30+
awslocal ssm put-parameter --name /localstack-thumbnail-app/buckets/resized --value "localstack-thumbnails-app-resized"
31+
```
32+
33+
### Create the DLQ Topic for failed lambda invokes
34+
35+
```bash
36+
awslocal sns create-topic --name failed-resize-topic
37+
```
38+
39+
Subscribe an email address to it (to alert us immediately if an image resize fails!).
40+
41+
```bash
42+
awslocal sns subscribe \
43+
--topic-arn arn:aws:sns:us-east-1:000000000000:failed-resize-topic \
44+
--protocol email \
45+
--notification-endpoint my-email@example.com
46+
```
47+
48+
`awslocal sns list-topics | jq -r '.Topics[] | select(.TopicArn|test("failed-resize-topic")).TopicArn'`
49+
50+
51+
### Create the lambdas
52+
53+
#### S3 pre-signed POST URL generator
54+
55+
```bash
56+
(cd lambdas/presign; rm -f lambda.zip; zip lambda.zip handler.py)
57+
awslocal lambda create-function \
58+
--function-name presign \
59+
--runtime python3.9 \
60+
--timeout 10 \
61+
--zip-file fileb://lambdas/presign/lambda.zip \
62+
--handler handler.handler \
63+
--role arn:aws:iam::000000000000:role/lambda-role
64+
```
65+
66+
Create the function URL:
67+
68+
```bash
69+
awslocal lambda create-function-url-config \
70+
--function-name presign \
71+
--auth-type NONE
72+
```
73+
74+
Copy the `FunctionUrl` from the response!
75+
76+
### Resizer Lambda
77+
78+
```bash
79+
(
80+
cd lambdas/resize
81+
rm -rf package lambda.zip
82+
mkdir package
83+
pip install -r requirements.txt -t package
84+
zip lambda.zip handler.py
85+
cd package
86+
zip -r ../lambda.zip *;
87+
)
88+
awslocal lambda create-function \
89+
--function-name resize \
90+
--runtime python3.9 \
91+
--timeout 10 \
92+
--zip-file fileb://lambdas/resize/lambda.zip \
93+
--handler handler.handler \
94+
--dead-letter-config TargetArn=arn:aws:sns:us-east-1:000000000000:failed-resize-topic \
95+
--role arn:aws:iam::000000000000:role/lambda-role
96+
```
97+
98+
### Connect the S3 bucket to the resizer lambda
99+
100+
```bash
101+
awslocal s3api put-bucket-notification-configuration \
102+
--bucket localstack-thumbnails-app-images \
103+
--notification-configuration "{\"LambdaFunctionConfigurations\": [{\"LambdaFunctionArn\": \"$(awslocal lambda get-function --function-name resize | jq -r .Configuration.FunctionArn)\", \"Events\": [\"s3:ObjectCreated:*\"]}]}"
104+
```
105+
106+
### Create the static s3 webapp
107+
108+
```bash
109+
awslocal s3 mb s3://webapp
110+
awslocal s3 sync --delete ./website s3://webapp
111+
awslocal s3 website s3://webapp --index-document index.html
112+
```
113+
114+
Then visit http://webapp.s3-website.localhost.localstack.cloud:4566
115+
116+
117+
## Develop locally
118+
119+
Create a virtualenv and install all the dependencies there:
120+
121+
```bash
122+
python3 -m venv .venv
123+
source .venv/bin/activate
124+
pip install -r requirements-dev.txt
125+
```

0 commit comments

Comments
 (0)