Go and Serverless: How to Write AWS Lambda Functions That Python Developers Envy

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

Why Go is the New King of Serverless (Period)?

\n\n

When it comes to serverless architecture, most developers think of JavaScript (Node.js) or Python. It's logical: they are simple, popular, and tons of tutorials are written for them. But there is one problem: cold start.

\n\n

Imagine: you run a Lambda function in Python. The first request after idle time takes 3-5 seconds. The user waits. The business loses money. Now imagine the same function in Go: 100-200 milliseconds. Feel the difference?

\n\n

Go (Golang) is a language created within Google to solve problems where performance and simplicity matter. In the serverless world, it offers three key advantages:

\n
    \n
  • Instant cold start — a compiled binary starts in milliseconds.
  • \n
  • Low memory consumption — you pay less and get more.
  • \n
  • Single binary — no dependencies, just upload and forget.
  • \n
\n\n

In this article, we'll break down how to write serverless functions in Go for AWS Lambda, look at real code examples, and compare with alternatives. If you're tired of waiting for a Python function to "warm up" — welcome.

\n\n

How to Set Up AWS Lambda on Go: Step-by-Step Guide

\n\n

Before writing code, let's figure out the tooling. To work with Go on AWS Lambda, you will need:

\n
    \n
  • Go installed (version 1.21+).
  • \n
  • The aws-cli utility (or the AWS console).
  • \n
  • A basic understanding of what a Handler is.
  • \n
\n\n

Step 1. Create a project

\n

Create a new directory and initialize the module:

\n
mkdir go-lambda-example\ncd go-lambda-example\ngo mod init example.com/lambda
\n\n

Step 2. Write your first function

\n

Create a file main.go. Here is a minimal example of a serverless function in Go that processes JSON and returns a response:

\n
package main\n\nimport (\n    "context"\n    "fmt"\n\n    "github.com/aws/aws-lambda-go/lambda"\n)\n\n// MyEvent — incoming event structure\ntype MyEvent struct {\n    Name string `json:"name"`\n}\n\n// MyResponse — response structure\ntype MyResponse struct {\n    Message string `json:"message"`\n    Status  int    `json:"status"`\n}\n\n// HandleRequest — main Lambda handler\nfunc HandleRequest(ctx context.Context, event MyEvent) (MyResponse, error) {\n    if event.Name == "" {\n        return MyResponse{Message: "Hello, anonymous!", Status: 200}, nil\n    }\n    return MyResponse{\n        Message: fmt.Sprintf("Hello, %s! Welcome to serverless with Go.", event.Name),\n        Status:  200,\n    }, nil\n}\n\nfunc main() {\n    lambda.Start(HandleRequest)\n}
\n\n

Important: The function must accept context.Context and return (T, error). This is the standard for Go Lambda.

\n\n

Step 3. Compile for Linux

\n

AWS Lambda runs on Linux, so the binary must be built for that OS:

\n
GOOS=linux GOARCH=amd64 go build -o bootstrap main.go
\n

Note: the output file must be named bootstrap — this is an AWS Lambda requirement for Go.

\n\n

Step 4. Package and upload

\n

Package the binary into a ZIP:

\n
zip function.zip bootstrap
\n

Now upload it to AWS Lambda via the console or CLI. Done! Your first serverless function in Go is running.

\n\n

Advanced Example: Working with S3 and DynamoDB

\n

Now that the basics are covered, let's write something useful. Suppose we need to process file uploads to S3 and save metadata to DynamoDB. This is a classic serverless scenario.

\n\n

Install the necessary SDKs:

\n
go get github.com/aws/aws-lambda-go/events\ngo get github.com/aws/aws-sdk-go-v2/service/s3\ngo get github.com/aws/aws-sdk-go-v2/service/dynamodb
\n\

Blogs

Book Recommendations