Loading...
Loading...
This guide will walk you through deploying a NestJS application to AWS Lambda using serverless framework, configuring environment variables, setting up AWS credentials, and using AWS Systems Manager Parameter Store for secure configuration management.
npm install -g serverlessgithub-actions-deployer)Attach the following policies (for production, create a custom policy with minimal required permissions):
Security Note: For production, create custom IAM policies with only the minimum required permissions instead of using full access policies.
⚠️ Important: Save these credentials securely. The Secret Access Key cannot be retrieved again if lost.
For each environment variable, create a parameter with the following structure:
Example Parameters:
/nestjs/prod/DATABASE_URL/nestjs/prod/JWT_SECRET/nestjs/prod/API_KEY/nestjs/prod/NODE_ENVUse a hierarchical naming structure:
Examples:
/nestjs/prod/ - Production environment/nestjs/staging/ - Staging environment/nestjs/dev/ - Development environmentInstall required packages:
Create serverless.yml in your project root:
Note: The ~true suffix decrypts SecureString parameters. Make sure your Lambda execution role has permissions to access SSM Parameter Store.
Create src/lambda.ts:
Ensure your tsconfig.json compiles to dist folder:
Secrets to add:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_REGIONus-east-1 (or your preferred region)Security Best Practice: Never commit AWS credentials to your repository. Always use GitHub Secrets for sensitive information.
Create .github/workflows/deploy.yml:
Note: Modify the --stage parameter to match your environment (prod, staging, dev). The workflow will automatically fetch environment variables from Parameter Store based on the stage.
For multiple environments, you can use matrix strategy or branch-based deployment:
After deployment, test your API:
✅ Your NestJS application is now deployed to AWS Lambda and accessible via API Gateway!
ssm:GetParameterpermissions for the Parameter Store path used in your serverless.yml.package.jsonand use npm ci instead of npm installfor consistent builds.serverless.yml(default is 6 seconds, max is 900 seconds).