This project contains AWS Lambda functions written in Go, including a simple "Hello World" function as an example.
| Requirement | Local Development | Deployment |
|---|---|---|
| Go 1.21 or later | required | |
| Docker/Podman | required | required |
| Node.js | required (for AWS CDK) | |
| AWS CDK | required | |
| AWS CLI | required |
You can install the AWS CDK CLI using Homebrew:
brew install aws-cdkThe directory structure is a mixture of AWS CDK and AWS Lambda:
├── README.md # Project documentation
├── bin/
│ └── my-lambda.ts # CDK app entry point
├── lib/ # CDK stack definitions
├── example-lambda/ # Example Lambda function in Go
└── docs/ # Documentation assets
To deploy Lambda functions, there are two main options: CDK or SAM. The image below illustrates the workflow:
The main purpose of using SAM is for executing and debugging Lambda functions locally during development.
However, AWS SAM is not fully supported with the Go runtime. Therefore, SAM won't be used in this project.
You can work around this by implementing a handler() function in a separate file and calling it from main_test.go if you want to debug locally.
npm installGenerate the CloudFormation template from the CDK code. It also builds the assets (Lambda code) and prepares everything for deployment.
It builds Go Lambda functions using Docker or Podman to ensure a consistent build environment. The Docker image for compilation is specified in my-lambda-stack.ts.
cdk synthexport CDK_DOCKER=podman # In case you want to use Podman instead of Docker
make -C example-lambda buildThe project uses AWS CDK for infrastructure deployment with TypeScript definitions.
Deploy the complete infrastructure stack:
cdk deployFor rapid development cycles when only Lambda function code changes:
# Hot-swap deployment - bypasses CloudFormation for faster updates
cdk deploy --hotswap Benefits of --hotswap:
- Faster deployment: Updates Lambda code directly without CloudFormation stack updates
- Development efficiency: Ideal for iterative Go code changes
- Reduced deployment time: From minutes to seconds for Lambda-only changes
- Safe for development: Automatically falls back to standard deployment if infrastructure changes are detected
--hotswap should only be used in development environments. Always use standard cdk deploy for production.
# Remove all deployed resources
cdk destroy-
AWS Lambda Go Package Documentation
AWS Lambda Go Package Documentation
Explains the use oflambda.norpcbuild tags and why the Go binary must be namedbootstrapfor deployment. -
CDK Bundling Options for Go Lambdas
CDK Asset Bundling Options
Details why the compiled binary should be placed in the/asset-output/directory when using CDK bundling.
