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

Add build script for Lambda functions #1595

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
93 changes: 50 additions & 43 deletions content/en/getting-started/quickstart/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,14 @@ Another Lambda function lists and provides pre-signed URLs for browser display.
The application also handles Lambda failures through SNS and SES email notifications.

The sample application uses AWS CLI and our `awslocal` wrapper to deploy the application to LocalStack.
You can build and deploy the sample application on LocalStack by running the following command:
Before going further, you need to build your Lambda functions.
You can use the following script that will cover all three of them:

{{< command >}}
$ deployment/build-lambdas.sh
{{< / command >}}

You can now deploy the sample application on LocalStack by running the following command:

{{< command >}}
$ deployment/awslocal/deploy.sh
Expand All @@ -136,13 +143,13 @@ $ awslocal s3 mb s3://localstack-thumbnails-app-resized

{{< command >}}
$ awslocal ssm put-parameter \
--name /localstack-thumbnail-app/buckets/images \
--type "String" \
--value "localstack-thumbnails-app-images"
--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"
--name /localstack-thumbnail-app/buckets/resized \
--type "String" \
--value "localstack-thumbnails-app-resized"
{{< / command >}}

#### Create SNS DLQ Topic for failed lambda invocations
Expand All @@ -156,45 +163,45 @@ You can use the following command to subscribe an email address to the SNS topic

{{< command >}}
$ awslocal sns subscribe \
--topic-arn arn:aws:sns:us-east-1:000000000000:failed-resize-topic \
--protocol email \
--notification-endpoint [email protected]
--topic-arn arn:aws:sns:us-east-1:000000000000:failed-resize-topic \
--protocol email \
--notification-endpoint [email protected]
{{< / command >}}

#### Create the Presign Lambda

{{< command >}}
$ (cd lambdas/presign; rm -f lambda.zip; zip lambda.zip handler.py)
$ awslocal lambda create-function \
--function-name presign \
--runtime python3.9 \
--timeout 10 \
--zip-file fileb://lambdas/presign/lambda.zip \
--handler handler.handler \
--role arn:aws:iam::000000000000:role/lambda-role \
--environment Variables="{STAGE=local}"
--function-name presign \
--runtime python3.9 \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor suggestion: Would it make sense to upgrade to python3.12 because 3.9 might get deprecated next year (it's the latest non-deprecation Python version) https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html

Thank you @tinyg210 for tackling the missing build instructions, that's indeed a common issue 👍

--timeout 10 \
--zip-file fileb://lambdas/presign/lambda.zip \
--handler handler.handler \
--role arn:aws:iam::000000000000:role/lambda-role \
--environment Variables="{STAGE=local}"
$ awslocal lambda wait function-active-v2 --function-name presign
$ awslocal lambda create-function-url-config \
--function-name presign \
--auth-type NONE
--function-name presign \
--auth-type NONE
{{< / command >}}

#### Create the Image List Lambda

{{< command >}}
$ (cd lambdas/list; rm -f lambda.zip; zip lambda.zip handler.py)
$ awslocal lambda create-function \
--function-name list \
--handler handler.handler \
--zip-file fileb://lambdas/list/lambda.zip \
--runtime python3.9 \
--timeout 10 \
--role arn:aws:iam::000000000000:role/lambda-role \
--environment Variables="{STAGE=local}"
--function-name list \
--handler handler.handler \
--zip-file fileb://lambdas/list/lambda.zip \
--runtime python3.9 \
--timeout 10 \
--role arn:aws:iam::000000000000:role/lambda-role \
--environment Variables="{STAGE=local}"
$ awslocal lambda wait function-active-v2 --function-name list
$ awslocal lambda create-function-url-config \
--function-name list \
--auth-type NONE
--function-name list \
--auth-type NONE
{{< / command >}}

#### Build the Image Resizer Lambda
Expand All @@ -203,7 +210,7 @@ $ awslocal lambda create-function-url-config \
{{< tab header="macOS" lang="shell" >}}
cd lambdas/resize
rm -rf libs lambda.zip
docker run --platform linux/x86_64 -v "$PWD":/var/task "public.ecr.aws/sam/build-python3.9" /bin/sh -c "pip install -r requirements.txt -t libs; exit"
docker run --platform linux/x86*64 -v "$PWD":/var/task "public.ecr.aws/sam/build-python3.9" /bin/sh -c "pip install -r requirements.txt -t libs; exit"
cd libs && zip -r ../lambda.zip . && cd ..
zip lambda.zip handler.py
rm -rf libs
Expand All @@ -226,7 +233,7 @@ mkdir package
pip install -r requirements.txt -t package
zip lambda.zip handler.py
cd package
zip -r ../lambda.zip*;
zip -r ../lambda.zip\_;
cd ../..
{{< /tab >}}
{{< /tabpane >}}
Expand All @@ -235,27 +242,27 @@ cd ../..

{{< command >}}
$ awslocal lambda create-function \
--function-name resize \
--runtime python3.9 \
--timeout 10 \
--zip-file fileb://lambdas/resize/lambda.zip \
--handler handler.handler \
--dead-letter-config TargetArn=arn:aws:sns:us-east-1:000000000000:failed-resize-topic \
--role arn:aws:iam::000000000000:role/lambda-role \
--environment Variables="{STAGE=local}"
--function-name resize \
--runtime python3.9 \
--timeout 10 \
--zip-file fileb://lambdas/resize/lambda.zip \
--handler handler.handler \
--dead-letter-config TargetArn=arn:aws:sns:us-east-1:000000000000:failed-resize-topic \
--role arn:aws:iam::000000000000:role/lambda-role \
--environment Variables="{STAGE=local}"
$ awslocal lambda wait function-active-v2 --function-name resize
$ awslocal lambda put-function-event-invoke-config \
--function-name resize \
--maximum-event-age-in-seconds 3600 \
--maximum-retry-attempts 0
--function-name resize \
--maximum-event-age-in-seconds 3600 \
--maximum-retry-attempts 0
{{< / command >}}

#### Connect S3 bucket to Resizer Lambda

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

#### Create the S3 static website
Expand Down
Loading