Serverless Functions in Go: AWS Lambda from A to Z — Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Serverless Functions in Go: AWS Lambda from A to Z



Serverless architecture has become the de facto standard for building scalable and cost-effective applications in the cloud. AWS Lambda is one of the most popular services for running code without managing servers. In this article, we will cover how to create, configure, and deploy serverless functions in Go (Golang) on AWS Lambda, from the very basics to advanced techniques.



Why Go for AWS Lambda?



Go is an ideal language for serverless functions. It compiles into a single binary file, has minimal cold start time (often less than 100 ms), and low memory consumption. This makes it one of the fastest and cheapest options for Lambda.



  • Fast startup: A compiled binary launches instantly.
  • Low cost: Minimal resource consumption reduces cloud bills.
  • Easy deployment: One file — no server-side dependencies.
  • Excellent integration: Native support for the AWS SDK for Go.


Environment Setup and First Handler



Before writing code, make sure you have installed:

  • Go version 1.19+
  • AWS CLI utility (configured with access keys)
  • Make or any build system (optional)


Create a new module and install the official Lambda package:



mkdir my-lambda-functioncd my-lambda-functiongo mod init my-lambda-functiongo get github.com/aws/aws-lambda-go/lambda


Now let's write a simple function that returns a greeting:



package main

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

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

type MyResponse struct { Message string `json:"message"`}

func HandleRequest(ctx context.Context, event MyEvent) (MyResponse, error) { if event.Name == "" { event.Name = "Guest" } return MyResponse{Message: "Hello, " + event.Name + "!"}, nil}

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


This code accepts JSON with a name field and returns a personalized greeting. Note: the function must be exported (capital letter), and the signature must strictly match the runtime expectations.



Building and Deploying to AWS Lambda



For a Linux-compatible binary (which is what Lambda expects), use cross-compilation:



GOOS=linux GOARCH=amd64 go build -o bootstrap main.gozip function.zip bootstrap


The output file name must be bootstrap — this is a requirement of the Go runtime on AWS Lambda.



Now let's create the function via AWS CLI:



aws lambda create-function \    --function-name my-go-function \    --runtime provided.al2023 \    --role arn:aws:iam::123456789012:role/lambda-execution-role \    --handler bootstrap \    --zip-file fileb://function.zip


Note the --runtime provided.al2023 — this is a special runtime for Go (based on Amazon Linux 2023). If you have an older version, use provided.al2.



After creation, test the function:



aws lambda invoke --function-name my-go-function \    --payload '{"name": "World"}' output.jsoncat output.json


You should see: {"message":"Hello, World!"}



Working with AWS Events (S3, DynamoDB, API Gateway)



The true power of Lambda is revealed when integrating with other AWS services. Let's look at an example handler for S3 events:



package main

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

func HandleS3Event(ctx context.Context, s3Event even

Blogs

Book Recommendations