|
| 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