This project demonstrates how to deploy Go Lambda functions using AWS CDK (Cloud Development Kit). It creates a serverless API with AWS Lambda (using Go runtime) and API Gateway, with infrastructure defined as code using TypeScript.
- Infrastructure: AWS CDK with TypeScript
- Runtime: Go on AWS Lambda (ARM64 architecture)
- API: AWS API Gateway REST API
- Environments: Supports both development and production deployments
The project includes a simple Go Lambda function that returns a JSON response when called through API Gateway.
- Node.js and npm
- Go 1.22 or later
- AWS CLI configured with appropriate credentials
- AWS CDK v2 installed (
npm install -g aws-cdk) - Docker (for local development and multi-architecture support)
# Install CDK dependencies
npm install
# Install Go dependencies
cd lambdas/function1
go mod tidy
cd ../..You can deploy to different environments (development or production) using either npm scripts or direct CDK commands:
# Deploy to development environment
npm run deploy:dev -- --profile your-dev-profile
# Deploy to production environment
npm run deploy:prod -- --profile your-prod-profile# Deploy to development environment
# Using environment variables for account and region
AWS_ACCOUNT=your-dev-account-id AWS_REGION=your-dev-region cdk deploy --context stage=dev --profile your-dev-profile
# Or using the context configuration from cdk.json
cdk deploy --context stage=dev --profile your-dev-profile
# Deploy to production environment
cdk deploy --context stage=prod --profile your-prod-profileThe deployment will create only the stack for the specified stage. The account and region information will be taken from:
- Environment variables (if provided)
- Context configuration in cdk.json
- Default AWS profile configuration
You can test the API locally using AWS SAM. First, synthesize the CDK stack for your desired environment:
# Synthesize the development stack
npm run synth:dev
# Or synthesize the production stack
npm run synth:prod# Synthesize the development stack
cdk synth --context stage=dev
# Or synthesize the production stack
cdk synth --context stage=prodThen start the local API Gateway using the generated CloudFormation template:
# For development environment
sam local start-api -t cdk.out/AwsCdkLambdaGoStackDev.template.json --profile your-dev-profile
# For production environment
sam local start-api -t cdk.out/AwsCdkLambdaGoStackProd.template.json --profile your-prod-profileThis starts a local API Gateway that invokes your Lambda function for the specified environment.
This project uses ARM64 architecture for Lambda functions, which offers better performance and cost efficiency. To build and test ARM64 binaries on x86 machines, you need QEMU support.
Run this command to enable ARM64 emulation:
docker run --rm --privileged multiarch/qemu-user-static --reset -p yesTo make ARM64 support persistent across reboots, create a systemd service:
- Create the service file:
sudo vim /etc/systemd/system/qemu-user-static-persistent.service- Add the following content:
[Unit]
Description=Register QEMU user-mode static binaries for cross-architecture
After=docker.service
Requires=docker.service
[Service]
Type=oneshot
User=root
Privileges=yes
WorkingDirectory=/root
ExecStart=/usr/bin/docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
- Enable and start the service:
sudo systemctl enable qemu-user-static-persistent.service
sudo systemctl start qemu-user-static-persistent.service
sudo systemctl status qemu-user-static-persistent.serviceCheck if ARM64 emulation is properly configured:
ls -l /proc/sys/fs/binfmt_misc/
cat /proc/sys/fs/binfmt_misc/qemu-aarch64This project supports multiple deployment environments (development and production) using AWS CDK's context mechanism. The environment selection works as follows:
-
The deployment stage is determined by the
stagecontext parameter:cdk deploy --context stage=dev # For development cdk deploy --context stage=prod # For production
-
If no stage is specified, it defaults to
dev(development). -
Based on the selected stage, only one stack will be synthesized and deployed:
- For
dev:AwsCdkLambdaGoStackDev - For
prod:AwsCdkLambdaGoStackProd
- For
-
Each stack uses environment-specific configurations from:
- Environment variables
- Context values in
cdk.json - Default AWS profile settings
This approach ensures that only the stack for the specified environment is processed during deployment, avoiding the issue of multiple stacks being synthesized simultaneously.
bin/: CDK app entry pointlib/: CDK stack definitionslambdas/: Go Lambda function codecdk.out/: Generated CloudFormation templates