Lambda Construct

In the “infra” subdirectory we have the infra\main\main.go file which creates the stack.

The Lambda construct is defined in infra\hello-world.go. See the full file on github

    fn := lambda.NewFunction(stack, aws.String("simplelambda"),
   &lambda.FunctionProps{
     Description:                  aws.String("Simple Lambda says hello"),
     FunctionName:                 aws.String("sayhello"),
     LogRetention:                 logs.RetentionDays_THREE_MONTHS,
     MemorySize:                   aws.Float64(1024),
     Timeout:                      awscdk.Duration_Seconds(aws.Float64(10)),
     Code: lambda.Code_FromAsset(&lambdaPath, &awss3assets.AssetOptions{}),
     Handler: aws.String("main"),
     Runtime:                      lambda.Runtime_GO_1_X(),
   })
 36   fn := lambda.NewFunction(stack, aws.String("simplelambda"),
 37   &lambda.FunctionProps{
 38     Description:                  aws.String("Simple Lambda says hello"),
 39     FunctionName:                 aws.String("sayhello"),
 40     LogRetention:                 logs.RetentionDays_THREE_MONTHS,
 41     MemorySize:                   aws.Float64(1024),
 42     Timeout:                      awscdk.Duration_Seconds(aws.Float64(10)),
 43     Code: lambda.Code_FromAsset(&lambdaPath, &awss3assets.AssetOptions{}),
 44     Handler: aws.String("main"),
 45     Runtime:                      lambda.Runtime_GO_1_X(),
 46   })

As all constructs in line 36 the stack is referenced and the construct gets a name as a string pointer.

Common parameters

Line Name Description
39  FunctionName The function name is vital, because we need the correct name for executing and deploying the function
40 LogRetention  a second lambda, which sets the CloudWatch Logs to three month, so we do not keep logs forever
41 MemorySite  Always starting with 1024, because this is faster - and cheaper in most cases
43 Timeout  Keep the timeout short so that errors will occur fast
44 Handler always main
45 Runtime With all GO 1 Versions, this is the same Runtime. In Node.JS or Python this changes with each new language release

The Code itself: Code Paramater

With GO we compile (build) a file for an X86 Linux operation system. This is zipped. CDK will copy the dist/main.zip to the CDK S3 deployment Bucket and CloudFormation will update the lambda function with the code.

See also

Source

See the full source on github.

Sources