This is a template for an AWS Lambda function written in Node.js that connects to a PostgreSQL database through AWS RDS Proxy.
- AWS Lambda function with API Gateway trigger
- PostgreSQL database connection through RDS Proxy
- Serverless Framework deployment configuration
- Environment variables management
- AWS Secrets Manager integration
- VPC configuration
- Node.js installed (v18.x recommended)
- AWS CLI configured with appropriate credentials
- Serverless Framework installed (
npm install -g serverless) - PostgreSQL database and RDS Proxy set up in AWS
-
Clone this repository:
git clone <repository-url> cd node-template-lambda
-
Install dependencies:
npm install
-
Create a
.envfile in the root directory with the following variables:DB_PROXY_ENDPOINT=your-rds-proxy-endpoint DB_PORT=5432 DB_NAME=your-database-name DB_USERNAME=your-database-username DB_PASSWORD=your-database-password SECURITY_GROUP_ID=sg-xxxxxxxx SUBNET_ID_1=subnet-xxxxxxxx SUBNET_ID_2=subnet-xxxxxxxx SUBNET_ID_3=subnet-xxxxxxxx TEST_ENV=development -
Configure VPC Settings:
- Ensure your Lambda has access to your VPC by configuring the security groups and subnet IDs in the
.envfile - The security group should allow outbound traffic to your RDS Proxy
- Subnets should be private subnets with NAT Gateway access
- Ensure your Lambda has access to your VPC by configuring the security groups and subnet IDs in the
-
Configure AWS Secrets Manager:
- The template automatically creates a Secrets Manager secret during deployment
- The secret name will be
node-template-lambda-{stage}-secrets - Values will be populated from your environment variables
-
Deploy to development environment:
serverless deploy --stage dev
-
Deploy to production environment:
serverless deploy --stage prod
-
Deploy to a specific region:
serverless deploy --stage prod --region us-east-1
-
Test the function locally:
serverless invoke local --function main -
Test the deployed function:
serverless invoke --function main --stage dev
To remove all deployed resources:
-
Remove from development environment:
serverless remove --stage dev
-
Remove from production environment:
serverless remove --stage prod
-
Remove from a specific region:
serverless remove --stage prod --region us-east-1
Note: This will remove all AWS resources created by this service, including:
- Lambda functions
- API Gateway endpoints
- IAM roles and policies
- Secrets Manager secrets
- CloudWatch log groups