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

已提問 10 個月前檢視次數 646 次
1 個回答
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
專家
已回答 10 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南