Skip to content

gogo-boot/cdk-lambda-go

Repository files navigation

AWS Lambda Example with CDK

This project contains AWS Lambda functions written in Go, including a simple "Hello World" function as an example.

Prerequisites

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

CDK CLI on Mac

You can install the AWS CDK CLI using Homebrew:

brew install aws-cdk

Project Structure

The 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

Development Workflow

To deploy Lambda functions, there are two main options: CDK or SAM. The image below illustrates the workflow:

Development Workflow

Debug on Local

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.

Getting Started for Development

1. Install Dependencies

npm install

CDK Synth

Generate 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 synth

Relevant Environment Variables for CDK

export CDK_DOCKER=podman   # In case you want to use Podman instead of Docker

2. Build the Lambda Functions

make -C example-lambda build

Deployment

CDK Deployment

The project uses AWS CDK for infrastructure deployment with TypeScript definitions.

1. Standard Deployment

Deploy the complete infrastructure stack:

cdk deploy

2. Quick Lambda Code Updates

For 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

⚠️ Important: --hotswap should only be used in development environments. Always use standard cdk deploy for production.

3. Cleanup/Destroy Stack

# Remove all deployed resources
cdk destroy

References

  • AWS Lambda Go Package Documentation
    AWS Lambda Go Package Documentation
    Explains the use of lambda.norpc build tags and why the Go binary must be named bootstrap for 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.

About

AWS CDK Example to deploy Lambda which is written in Go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published