- Ensure you have an AWS account
FROM public.ecr.aws/lambda/python:3.12
COPY requirements.txt ${LAMBDA_TASK_ROOT}
RUN pip install uv
RUN uv venv
RUN uv pip install -r requirements.txt
COPY .prefect/ ${LAMBDA_TASK_ROOT}/.prefect/
COPY handlers.py ${LAMBDA_TASK_ROOT}
ENV PREFECT_HOME=${LAMBDA_TASK_ROOT}
CMD [ "handlers.lambda_handler" ]
Note
What is uv
you ask? Change your life today.
- Create a new file named
lambda_function.py
(or any desired name) and write your Lambda function code. - Example Lambda function code:
from prefect import flow
@flow(log_prints=True)
def lambda_handler(event, context):
"""AWS Lambda function entrypoint"""
print(f"{event=}, {context=}")
return {"message": "Hello, World!"}
Tip
To do the rest of the steps automatically, see this GitHub Action. Also, use gh secret set -f .env
to set all environment variables in your .env
file as GitHub Actions secrets.
Manual steps
- Build the Docker image using the Dockerfile.
docker build \
--platform linux/amd64 \
--tag your-account-id.dkr.ecr.your-region.amazonaws.com/my-lambda-function:latest \
.
- Create a new Amazon ECR repository to store your Docker image.
aws ecr create-repository --repository-name my-lambda-function
- Authenticate your Docker client with Amazon ECR.
aws ecr get-login-password --region your-region | docker login --username AWS --password-stdin your-account-id.dkr.ecr.your-region.amazonaws.com
- Push the tagged Docker image to your Amazon ECR repository.
docker push your-account-id.dkr.ecr.your-region.amazonaws.com/my-lambda-function:latest
- Create the Lambda function using the AWS CLI, specifying the function name, package type, code image URI, and IAM role.
aws lambda create-function --function-name my-lambda-function \
--package-type Image \
--code ImageUri=your-account-id.dkr.ecr.your-region.amazonaws.com/my-lambda-function:latest \
--role arn:aws:iam::your-account-id:role/my-lambda-role
- Set the environment variables for your Lambda function using the AWS CLI.
aws lambda update-function-configuration --function-name my-lambda-function \
--environment "Variables={ENV_VAR1=value1,ENV_VAR2=value2}"
- Invoke the Lambda function to test its functionality.
aws lambda invoke --function-name my-lambda-function response.json
- Check the
response.json
file for the function's output.
- If you make changes to your Lambda function code or environment variables, rebuild the Docker image, push it to Amazon ECR, and update the function using the AWS CLI.
docker build \
--platform linux/amd64 \
--tag your-account-id.dkr.ecr.your-region.amazonaws.com/my-lambda-function:latest .
docker push your-account-id.dkr.ecr.your-region.amazonaws.com/my-lambda-function:latest
aws lambda update-function-code \
--function-name my-lambda-function \
--image-uri your-account-id.dkr.ecr.your-region.amazonaws.com/my-lambda-function:latest
aws lambda update-function-configuration \
--function-name my-lambda-function \
--environment "Variables={ENV_VAR1=value1,ENV_VAR2=value2}"
Remember to replace your-region
, your-account-id
, and other placeholders with your actual AWS region, account ID, and other relevant information. Also, ensure that you have the necessary IAM permissions and follow best practices for securing your Lambda function and associated resources.