First, you'll create and compile a simple AWS Lambda function on your local machine. Then you'll upload that compiled function using the AWS Management Console. Finally, you'll test your function in the Management Console using a simple test event.
In an empty folder, create a file main.go
and paste the following contents:
package main
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
)
type Input struct {
Name string `json:"name"`
}
func handler(ctx context.Context, name Input) (string, error) {
return fmt.Sprintf("Hello, %s!", name.Name), nil
}
func main() {
lambda.Start(handler)
}
The highlighted lines show you how Lambda functions are both similar to and different from standalone programs written in Go:
package main
main
package.import (
"github.com/aws/aws-lambda-go/lambda"
)
github.com/aws/aws-lambda-go/lambda
to execute lambda.Start()
, which tells the Lambda runtime which function will handle the event that triggers invocation of your function.type Input struct {
Name string `json:"name"`
}
func handler(ctx context.Context, name Input) (string, error) {
ctx
. In this case, you see a generic user-defined Input
event that is passed into the function. You will learn more about these events, their types, and how they are serialized and deserialized later in the workshop.lambda.Start(handler)
main
function calls lambda.Start()
with a single parameter, a function that will be invoked each time an event is received.The following code block lists valid handler signatures. TIn
and TOut
represent types compatible with the encoding/json standard library. For more information, see AWS Lambda Function Handler in Go in the AWS Lambda Documentation.
func ()
func () error
func (TIn), error
func () (TOut, error)
func (context.Context) error
func (context.Context, TIn) error
func (context.Context) (TOut, error)
func (context.Context, TIn) (TOut, error)
In the same folder where you created main.go
, create a file Makefile
and paste the following contents:
.PHONY: clean build package
clean:
rm -rf ./hello package.zip
build:
go get github.com/aws/aws-lambda-go/lambda
GOOS=linux GOARCH=amd64 go build -o hello .
package:
zip -r package.zip hello
This simplified Makefile gives you three basic commands you will use to compile and package your Lambda function:
make clean
: remove the compiled executable and zipped packagemake build
: compile the program to run in the Lambda go1.x runtime.make package
: zip the compiled function for upload to the Management ConsoleThe GOOS=linux
and GOARCH=amd64
environment variables in the make build
step allow you to build a binary that will run in the Lambda environment regardless of whether you develop locally in macOS, Linux, or Windows.
Run the following commands in a terminal in the directory where you created main.go
and Makefile
:
make clean
make build
make package
If you receive the error go: cannot find main module; see ‘go help modules’ you probably still have GO111MODULE=on set in your environment. If you're using a sh-like shell, unset GO111MODULE
or export GO111MODULE=auto
should put things back into auto mode. See this GitHub issue for additional information.
Below you can see the results of running the make build
and make package
commands on macOS, showing that the built binary is a Linux ELF 64-bit executable, not a Mach-O executable.
Now that you've built and packaged your function, you must upload it to the AWS Management Console.
hello
Upload
and select the package.zip
file you created previouslySave
Before running your Lambda function for the first time, it's helpful to understand the Lambda invocation cycle. A Lambda is executed in three steps:
An event source emits an event that triggers the Lambda function to begin execution. The event source can be either:
A Lambda function receives the event and performs some sort of processing that implements your business logic.
The Lambda function sends its result to a destination. The destination can be:
The invocation cycle looks as follows:
For your first Lambda function, you will create a basic test event in the Management Console and use that event to synchronously invoke your function.
In the function Configuration page, activate the “Select a test event” menu and choose “Configure test events”.
In the “Configure test event” screen that appears, give your test event a name, paste the following JSON into the text box, and choose “Create”.
{
"name": "GoDays Berlin"
}
Now you can invoke your function for the first time! Ensure that the event you created is selected and choose “Test”.
Expand the “Execution result” area that appears. You should see the following:
In this section you learned how to: