Friday, October 24, 2025

A Practical Guide to Deploying Go Lambdas with AWS CDK

I needed to create a Lambda function with low latency and fast computation. To achieve this, I chose Go, as it is easy to learn and consumes fewer resources compared to other languages.

However, I found that there are very few examples available for building Lambda functions in Go, especially when using AWS CDK or SAM for deployment. Even AI tools couldn't provide sufficient guidance, likely because there isn’t much related content available online.

As a result, I decided to build the solution myself.

Below is a simple example, It is deployed using AWS CDK and utilizes an AWS Linux image, which is lightweight. The CDK makes it straightforward to deploy and manage the Lambda function end to end.

import * as cdk from 'aws-cdk-lib';
import {Duration, RemovalPolicy} from "aws-cdk-lib";
import { Construct } from 'constructs';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as path from "path";


export interface MyLambdaStackProps extends cdk.StackProps {}

export class MyLambdaStack extends cdk.Stack {

    constructor(scope: Construct, id: string, props: MyLambdaStackProps) {
        super(scope, id, props);

        // Define the ExampleLambda Lambda function
        const goLambda = new lambda.Function(this, "ExampleLambda", {
            runtime: lambda.Runtime.PROVIDED_AL2023, // Use the custom runtime
            handler: "bootstrap", // Go binary name
            architecture: lambda.Architecture.ARM_64, // Use ARM architecture, cheaper and better performance

            currentVersionOptions: {
                removalPolicy: RemovalPolicy.RETAIN
            },

            // lambda.Code.fromAsset will pack artifact from "/asset-output" directory.
            // therefor the "bootstrap" file must be located in this directory.
            code: lambda.Code.fromAsset(path.join(__dirname, '../example-lambda'), {  // Path to Go binary
                bundling: {
                    image: cdk.DockerImage.fromRegistry("golang:1.25"),
                    command: [
                        'bash', '-c',
                        'GOARCH=arm64 GOOS=linux CGO_ENABLED=0 go build -tags lambda.norpc -o /asset-output/bootstrap main.go'
                    ],
                },
            }),
            // environment: {
            //     'DB_HOST': '',
            // },
            // vpc: this.vpc,
            // vpcSubnets: {
            //     subnets: [subnet1, subnet2, subnet3]
            // },
            // securityGroups: [MySecurityGroup],
            // timeout: cdk.Duration.seconds(10) // Limit timeout to 10 seconds
        });
    }
}


you can have full example code https://github.com/gogo-boot/cdk-lambda-go It has better description in README.md.

You may put the Eventsource `goLambda.addEventSource` for triggering the Lambda by event.

also Provision the Lambda and Auto Scale by `goLambda.addAutoScaling({ minCapacity: 1, maxCapacity: 5 });`, so you lambda will be pre provisioned and run without cold start. so it responses very fast.

No comments:

Post a Comment