Lesson 54 of 60 · Go
Building a Serverless Function with AWS Lambda (Go Runtime)
Duration: 20 minutes
AWS Lambda in Go
This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.
Add the AWS SDK:
$ go get github.com/aws/aws-lambda-go/lambda
package main
import (
"context"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"encoding/json"
)
type Response struct { Message string `json:"message"` }
func handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
var payload map[string]string
json.Unmarshal([]byte(req.Body), &payload)
resp := Response{Message: "Hello, " + payload["name"]}
body, _ := json.Marshal(resp)
return events.APIGatewayProxyResponse{StatusCode:200, Body:string(body)}, nil
}
func main() { lambda.Start(handler) }
Build for Linux (GOOS=linux):
$ env GOOS=linux GOARCH=amd64 go build -o main main.go
Zip and upload to AWS, or use the Serverless Framework.