A Golang Lambda Function

We start by creating a Golang lambda (in the web UI.

The Function

We create a function, using the Golang1.x runtime. We call it foo_func_go. This also generates an IAM role for us (giving the function the ability to write logs to CloudWatch). This role it generates looks like this:

1{
2 "Version": "2012-10-17",
3 "Statement": [
4 {
5 "Effect": "Allow",
6 "Action": "logs:CreateLogGroup",
7 "Resource": "arn:aws:logs:us-east-2:601364042938:*"
8 },
9 {
10 "Effect": "Allow",
11 "Action": [
12 "logs:CreateLogStream",
13 "logs:PutLogEvents"
14 ],
15 "Resource": [
16 "arn:aws:logs:us-east-2:601364042938:log-group:/aws/lambda/foo_func_go:*"
17 ]
18 }
19 ]
20}

Coding the Function

We then start writing some Golang.

1mkdir foo_func_go
2cd foo_func_go
3 
4go mod init github.com/cloudcastsapp/foo-func-go
5go get github.com/aws/aws-lambda-go/lambda

Then we create file main.go:

1package main
2
3import (
4 "fmt"
5 "github.com/aws/aws-lambda-go/lambda"
6)
7
8type MyEvent struct {
9 Name string `json:"name"`
10 Message string `json:"message"`
11}
12
13type MyResponse struct {
14 Answer string `json:"answer"`
15}
16
17func HandleLambdaEvent(event MyEvent) (MyResponse, error) {
18 return MyResponse{Answer: fmt.Sprintf("Hi %s! Here is your message: %s", event.Name, event.Message)}, nil
19}
20
21func main() {
22 lambda.Start(HandleLambdaEvent)
23}

Compile it, zip it, upload it.

We have a basic function that will work in Lambda, but we need to compile the Go problem, package it up (zip archive), and then upload it!

1# We chose the x86_64 environment (instead of ARM64),
2# so we need to compile this of linux AMD64
3# Generates file "main"
4GOOS=linux GOARCH=amd64 go build main.go
5 
6# Zip that file up
7zip main.zip main
8 
9# Confirm the Lambda function exists
10# https://awscli.amazonaws.com/v2/documentation/api/latest/reference/lambda/get-function.html
11aws --profile cloudcasts lambda get-function \
12 --function-name foo_func_go
13 
14# Upload the zip
15# https://awscli.amazonaws.com/v2/documentation/api/latest/reference/lambda/update-function-code.html
16# Note: The --publish flag tells it to publish this as the latest / used version
17aws --profile cloudcasts lambda update-function-code \
18 --function-name foo_func_go \
19 --zip-file fileb://main.zip \
20 --publish

The newly uploaded version appears in the versions tab in the web UI.

Invoke the Function

We can now invoke the function and see the results!

We use the RequestResponse invocation type, which tells the command to wait for a response before returning.

1aws --profile cloudcasts lambda invoke \
2 --function-name foo_func_go \
3 --invocation-type RequestResponse \
4 --cli-binary-format raw-in-base64-out \
5 --payload '{"name": "Chris", "message": "People like you!"}' \
6 output.json
7 
8cat output.json
9# {"answer":"Hi Chris! Here is your message: People like you!"}%

We can also check CloudWatch to see the logs under log group /aws/lambda/foo_func_go.

Anything we write to stderr/stdout should get captured into the CloudWatch logs as well. I suggest logging out in JSON format when sending to CloudWatch.

Don't miss out

Sign up to learn when new content is released! Courses are in production now.