
Amazon lambda with golang
- Tutorial
In mid-January of this year, Amazon announced Go support in its lambdas.
Great news, but now I am writing these lines, having no experience writing code in Golang, so that, going through the stumps and potholes in parallel with the writing of the article, I will come to my first working lambda on Go.
Actually, you'll have to start with the Go installation . My machine is Windows 10 on an Intel processor. Nothing complicated here: download, run. We check that Go has registered the path to <installation path> \ bin in the PATH environment variable or we start go through cli (cmd, for example) for verification.
Create a project folder:
To edit the code, I use Sublime Text 3 , so I need to adapt it to Go. To do this, install package control and use it ( Preferences -> Package Control -> Install Package -> GoSublime ) to download the GoSublime plugin .
Restart Sublime Text.
More information on setting up Sublime Text can be found here .
Now try Go with the first Hello, world!

Let's try to write and test a lambda, which takes an input object of two fields in json format and gives a response in json format with an identifier of successful completion.
Inquiry:
Answer:
This example is described in the article .
To build the archive for lambda, you need to download the aws-lambda-go library . There you can see what steps to build the archive you need to perform on Linux, macOS and Windows.
Let's do it all in steps:
After these steps, you should see the main.zip archive, which you must download to the lambda.

I will create a lambda through the AWS console.
To do this, you need:
Test the lambda. To do this, create a test by clicking the Test button at the top of the console. Add test input and save.

Now we can call this test script at the top of the console by clicking the Test button.

Environment variables not set
Check them out via cmd:
1. main is not written in the Handler field of the Function code block, hello is there by default.
2. The test json is incorrectly set.
It should look like this:
Great news, but now I am writing these lines, having no experience writing code in Golang, so that, going through the stumps and potholes in parallel with the writing of the article, I will come to my first working lambda on Go.
Install Go and set up workspace
Actually, you'll have to start with the Go installation . My machine is Windows 10 on an Intel processor. Nothing complicated here: download, run. We check that Go has registered the path to <installation path> \ bin in the PATH environment variable or we start go through cli (cmd, for example) for verification.
Create a project folder:
C:\Users\a_gol\go>cd C:\Go
C:\Go>mkdir workspace
C:\Go>set GOPATH=C:\Go\workspace
C:\Go>cd %GOPATH%
C:\Go\workspace>
To edit the code, I use Sublime Text 3 , so I need to adapt it to Go. To do this, install package control and use it ( Preferences -> Package Control -> Install Package -> GoSublime ) to download the GoSublime plugin .
Restart Sublime Text.
More information on setting up Sublime Text can be found here .
Now try Go with the first Hello, world!
- Open the project folder% GOPATH% (I have this workplace) in Sublime Text
- Create a new hello.go file
- Add code to it.
package main import "fmt" func main() { fmt.Printf("hello, world\n") }
- Launch the Go console (ctrl + b)
- We launch the program through the console: go run hello.go

Lambda on Go
Let's try to write and test a lambda, which takes an input object of two fields in json format and gives a response in json format with an identifier of successful completion.
Inquiry:
{
"id": 12345,
"value": "some-value"
}
Answer:
{
"message": "processed request ID 12345",
"ok": true
}
This example is described in the article .
package main
import (
"fmt"
"github.com/aws/aws-lambda-go/lambda"
)
type Request struct {
ID float64 `json:"id"`
Value string `json:"value"`
}
type Response struct {
Message string `json:"message"`
Ok bool `json:"ok"`
}
func Handler(request Request) (Response, error) {
return Response{
Message: fmt.Sprintf("Processed request ID %f", request.ID),
Ok: true,
}, nil
}
func main() {
lambda.Start(Handler)
}
To build the archive for lambda, you need to download the aws-lambda-go library . There you can see what steps to build the archive you need to perform on Linux, macOS and Windows.
Let's do it all in steps:
- Download the package:
go get -u github.com/aws/aws-lambda-go/cmd/build-lambda-zip
- Set environment variables via cmd:
set GOOS=linux set GOARCH=amd64
- In the Go console, you can run the following commands to collect the archive, which we will deploy to the lambda (these commands are for Windows, for other axes here ).
go build -o main main.go %USERPROFILE%\Go\bin\build-lambda-zip.exe -o main.zip main
After these steps, you should see the main.zip archive, which you must download to the lambda.

Making Lambda in Amazon
I will create a lambda through the AWS console.
To do this, you need:
- Select Lambda Service in the desired region
- Click Create function
- Fill in the fields
- After creation, the lambda console will open, where in the Function code block you need to click on the Upload button and load the main.zip archive. Remember to specify main in the Handler field.
- At the top of the console, click Save.
Test the lambda. To do this, create a test by clicking the Test button at the top of the console. Add test input and save.
{
"id": 12345,
"value": "some-value"
}

Now we can call this test script at the top of the console by clicking the Test button.

Potholes
Environment variables not set
{
"errorMessage": "fork/exec /var/task/main: exec format error",
"errorType": "PathError"
}
set GOOS=linux
set GOARCH=amd64
Check them out via cmd:
echo %GOOS%
echo %GOARCH%
Possible bumps
1. main is not written in the Handler field of the Function code block, hello is there by default.
{
"errorMessage": "fork/exec /var/task/hello: no such file or directory",
"errorType": "PathError"
}
2. The test json is incorrectly set.
It should look like this:
{
"id": 12345,
"value": "some-value"
}