This project provides a solution for connecting AWS Lambda functions to PostgreSQL databases using custom container runtimes. It overcomes the default runtime limitations, enabling seamless integration between Lambda and PostgreSQL RDS instances.
AWS Python Lambda functions, by default, face challenges when connecting to PostgreSQL RDS instances. While Lambda layers can extend the Python runtime with libraries to enable connections to PostgreSQL databases, the in-console code editor's runtime lacks the dependencies needed to utilize the libraries. This solution leverages custom container runtimes to include all necessary dependencies, allowing Lambda functions to establish PostgreSQL connections efficiently.
- Docker file that creates container image for AWS Lambda container runtime support
- PostgreSQL connectivity using pg8000
- Instructions to create container and deploy to AWS ECR
- AWS Account
- Docker installed on your local machine
- AWS CLI configured with appropriate permissions
- Basic knowledge of AWS Lambda and PostgreSQL
-
Clone this repository:
git clone https://github.com/aws-samples/postgres-container-runtime-function.git cd postgres-container-runtime-function
-
Build the Docker image:
docker build --platform linux/amd64 -t postgres-container-runtime-function .
-
Authenticate 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
-
Tag and push the image to ECR:
docker tag postgres-container-runtime-functione:latest your-account-id.dkr.ecr.your-region.amazonaws.com/postgres-container-runtime-function:latest docker push your-account-id.dkr.ecr.your-region.amazonaws.com/postgres-container-runtime-function:latest
- Create a new Lambda function in the AWS Console.
- Choose "Container image" as the deployment package type.
- Select the image you pushed to ECR.
- Configure the function's environment variables with your PostgreSQL connection details.
- Update the function's execution role to allow access to your RDS instance.
- Deploy and test your function.
Ensure you set the following environment variables in your Lambda function:
RDS_HOST
: Your PostgreSQL RDS endpointRDS_NAME
: RDS nameRDS_USER
: RDS usernameRDS_PASSWORD
: RDS passwordRDS_PORT
: RDS port
This project is licensed under the MIT License - see the LICENSE file for details.
- AWS Documentation on Lambda Container Images
- pg8000 project
If you encounter any problems or have any questions, please open an issue in this repository.
Happy coding!
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.