Advantages of golang for AWS Lambda.

Advantages of golang for AWS Lambda.

  • Last Modified: 11 Sep, 2021

I am using golang 1.17. I assume you already know golang and you have basic hands-on experience with AWS Lambda.


Get Yours Today

Discover our wide range of products designed for IT professionals. From stylish t-shirts to cutting-edge tech gadgets, we've got you covered.

Explore Our Collection 🚀


I am using golang 1.17. I assume you already know golang and you have basic hands-on experience with AWS Lambda.

  • Runtime: Go runtime version in Lambda is different from every other runtime available today. While all other languages have support only for some specific versions i.e. Python 3.8, 3.7 or 2.7. Golang’s runtime supports any 1.x release. You can use any golang’s version from day one.
  • Cold Start: Go lang has the fastest cols start times
    • When Col start happens?
      • When the first request comes in after deployment.
      • When Lambda functions haven’t been invoked for a while, they it will become inactive
  • Pricing: The pricing model for AWS lambda is based on the number of requests and the duration of time to execute the code. Golang has better CPU performance than scripting languages

How you can start?

Lambda functions don’t have an editor for the golang like other languages. You will need to use local editor.

Write code

Initialize go mod project

go mod init aws-lambda-in-go-lang

Now create main.go and create executable

package main

import (
        "fmt"
        "context"
        "github.com/aws/aws-lambda-go/lambda"
)

type MyEvent struct {
        Name string `json:"name"`
}

func HandleRequest(ctx context.Context, name MyEvent) (string, error) {
        return fmt.Sprintf("Hi %s!", name.Name ), nil
}

func main() {
        lambda.Start(HandleRequest)
}

If you try to run this, it will fail with exit status 1

go run main.go

2021/09/11 23:21:46 expected AWS Lambda environment variables [_LAMBDA_SERVER_PORT AWS_LAMBDA_RUNTIME_API] are not defined exit status 1

You can either test your handler using tests

package main

import (
        "testing"
)

func TestHandler(t *testing.T) {
        myEvent := MyEvent{
                Name: "KayD",
        }
        results, err := HandleRequest(nil, myEvent)
        if(err != nil) {
                t.Errorf("There is an error in the request %s", err)
        }
        if(results != "Hi KayD!") {
                t.Errorf("\nExpected Result: `Hi KayD!`,\nActual Result: `%s`",  results)
        }
}

Output

go test PASS ok main/simple 0.177s

Build an executable

GOOS=linux go build main

Create a Zip File

zip function.zip main
  adding: main (deflated 47%)

Create a Function

Change handler name to main

Deploy the zip file

Upload the zip file

Test

Go to the Test tab and edit the JSON as required

...
Get Yours Today

Discover our wide range of products designed for IT professionals. From stylish t-shirts to cutting-edge tech gadgets, we've got you covered.

Explore Our Collection 🚀


comments powered by Disqus