How to auto build lambda go code in go CDK?

0

I'm using CDK go to create the lambda resources with go language. As we know the go lambda need to be build to binary before upload to lambda. The SAM support auto-build function when using sam build & deploy with Makefile:

Here is an example:

  MyLambda:
    Type: AWS::Serverless::Function # More info about Function Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
    Properties:
      Description: "Code of MyLambda"
      CodeUri: lambda-code/
      Handler: bin/main
      Runtime: go1.x
      Architectures:
        - x86_64
      MemorySize: 128

But for CDK, I need to build lambda code in separate step before using CDK deploy:

Here is an example:

	fn := awslambda.NewFunction(stack, jsii.String("ddbreader"), &awslambda.FunctionProps{
		Runtime:    awslambda.Runtime_GO_1_X(),
		Handler:    jsii.String("bin/reader"),
		MemorySize: jsii.Number(128),
		Timeout:    awscdk.Duration_Seconds(jsii.Number(10)),
		Code:       awslambda.Code_FromAsset(jsii.String("app/ddbreader"), nil),
		CurrentVersionOptions: &awslambda.VersionOptions{
			RemovalPolicy: awscdk.RemovalPolicy_DESTROY,
			RetryAttempts: jsii.Number(1),
		},
		LogRetention: awslogs.RetentionDays_FIVE_DAYS,
	})

The CDK code for lambda creation is here: https://github.com/zhang1980s/ddb-stream-latency/blob/main/app/app_main.go

I'm planing to use CDK to create a codepipeline to deploy all AWS resources including lambda code. I understand the lambda build step could be done by codebuild but I would understand if the go code building has been encapsulated in CDK so that I don't need to write the buildspec logic separately. Please guide me how to perform lambda go auto build function in CDK. Thanks!

The CDK code for codepipeline creation. https://github.com/zhang1980s/ddb-stream-latency/blob/main/pipeline/pipeline.go

1 Answer
0

Hi,

The article explains how to use https://github.com/aws/aws-cdk-go/tree/main/awscdklambdagoalpha to build you Go lambdas automatically with CDK before deploying:

Article: https://adrianhesketh.com/2021/07/17/go-cdk-building-go-lambda-functions/

Best,

Didier

profile pictureAWS
EXPERT
answered 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions