This template provides a complete setup for AWS Lambda functions written in Node.js, using Terraform for infrastructure management.
.
├── src/
│ ├── hello/ # Hello Lambda function
│ │ ├── index.js
│ │ └── package.json
│ └── users/ # Users Lambda function
│ ├── index.js
│ └── package.json
├── scripts/
│ ├── build.js # Build script for all functions
│ └── install-deps.js # Install dependencies script
├── terraform/ # Terraform infrastructure
│ ├── main.tf
│ ├── variables.tf
│ └── outputs.tf
├── .github/
│ └── workflows/
│ └── build.yml # GitHub Actions CI/CD
├── package.json # Root package.json
├── Makefile
└── README.md
- Node.js 18+
- npm or yarn
- Terraform >= 1.13
- AWS CLI configured
- Make (optional, for using Makefile commands)
-
Clone this template
git clone <this-repo> cd lambda-nodejs-template
-
Install dependencies
npm install npm run install:all # or make deps install-all
-
Build Lambda functions
npm run build # or make build
-
Deploy infrastructure
cd terraform terraform init terraform plan terraform apply # or make deploy
- Create a new directory under
src/
(e.g.,src/orders/
) - Add your
index.js
andpackage.json
files - Add the function to
terraform/main.tf
- The build process will automatically detect and build the new function
# Build all functions
npm run build
# or
make build
# The build script will:
# - Install production dependencies for each function
# - Create zip packages in the build/ directory
# Run tests for all functions
npm test
# or
make test
# Run tests for a specific function
cd src/hello
npm test
# Run linter
npm run lint
# or
make lint
The GitHub Actions workflow automatically:
- Detects changed functions
- Builds each function in parallel
- Runs tests and linting
- Performs security audits
- Creates deployment packages
- Uploads build artifacts
The infrastructure uses:
- terraform-aws-modules/lambda/aws for Lambda functions
- terraform-aws-modules/apigateway-v2/aws for API Gateway
- Pre-built packages (no building in Terraform)
Edit terraform/variables.tf
to customize:
- AWS region
- Function names
- Environment settings
After deployment, you'll get:
GET /hello
- Hello functionGET /users
- Users function
- Functions use Node.js 18.x runtime
- CloudWatch logs have 14-day retention
- API Gateway uses HTTP API (cheaper than REST API)
- IAM roles follow least privilege principle
- CloudWatch logs enabled for monitoring
- npm audit security scanning in CI/CD
- Dependency scanning for vulnerabilities