This guide details how to set up a GitHub Action for deploying an AWS Lambda function from a Docker container image hosted in Amazon ECR.
- An AWS account with access key and secret access key configured.
- A Docker container or dummy container image for initializing your Lambda function.
-
Configure Secrets:
- Navigate to
Repo settings->Security->Secrets and Variables->Actions->Repository secrets. - Add your
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEY.
- Navigate to
-
Initial Lambda Setup with Dummy Image:
- You need to create your Lambda function in AWS using a Docker container. Initially, you can use a "dummy" Docker container. This is just a workaround to get your actual Lambda function created and configured in AWS.
- For pushing a dummy image to AWS's ECR, refer to Amazon ECR Docker Basics.
- Once your Lambda function is created, you will replace this dummy container with your actual application container through this GitHub Action.
-
Prepare and Place the GitHub Action YAML File:
- Create a
.github/workflowsdirectory in your repository if it doesn't already exist. - Place the
github_action_lambda.yamlfile inside this directory. This is where GitHub Actions expects to find YAML files defining workflows.
- Create a
-
Modify GitHub Action Configuration:
- Update the
arn:aws:lambda:YOUR_REGION:YOUR_ACCOUNT_ID:function:YOUR_FUNCTION_NAMEin the YAML file with your Lambda function's ARN. - Replace
YOUR_DOCKER_REPOSITORY_NAMEwith the name of your Docker repository in the ECR. - Adjust
AWS_REGIONas necessary. The default is set toeu-central-1. - Replace
YOUR_LAMBDA_FUNCTION_NAME
- Update the
-
Update Docker Platform (if necessary):
- For ARM-based Lambda functions, replace
--platform linux/amd64with--platform linux/arm64. - To specify the Dockerfile location, modify the build step to include
-f path/to/your/Dockerfile.
- For ARM-based Lambda functions, replace
-
Environment Variables and Secrets:
- Upload a JSON file containing your Lambda function's environment variables (e.g.,
{"SLACK_BOT_TOKEN": "XXX"}) to an S3 bucket. - Replace
s3://bucket-name/secrets.jsonwith the path to your JSON file in the YAML configuration.
- Upload a JSON file containing your Lambda function's environment variables (e.g.,
- This setup includes an automated update for Lambda function environment variables using the uploaded JSON file.
- Remember to review and test each component of your workflow to ensure it operates as expected within your AWS and GitHub environments.