You build serverless applications in Go by compiling a static binary and running it on a function platform such as AWS Lambda, Google Cloud Run functions or Azure Functions, or in a container on Cloud Run. Go suits serverless well: it starts fast, uses little memory and produces a single self-contained executable.
The standard library supplies the HTTP server, JSON handling, context and logging; the platform SDK supplies the handler glue. This guide uses AWS Lambda as the main example and covers the patterns that apply everywhere.
| Platform | How Go is deployed |
|---|---|
| AWS Lambda | Compiled binary named bootstrap on the provided.al2023 runtime (the old managed go1.x runtime is deprecated) |
| Google Cloud | Go runtime for Cloud Run functions using the Functions Framework, or any container on Cloud Run |
| Azure Functions | Custom handler: your Go HTTP server, started by the Functions host |
| Any container platform | A normal net/http server in a small container image |
The container route is the most portable, because a plain http.Server that listens on the port from the PORT environment variable runs unchanged on Cloud Run and similar services.
Install the library with go get github.com/aws/aws-lambda-go. A handler is a function that takes a context.Context and an event and returns a response and an error.
Return an HTTP error response for client or business errors; returning a Go error signals a function failure to Lambda.
Cross-compile for Lambda's Linux runtime, naming the output bootstrap:
Create the function with runtime provided.al2023 and architecture arm64 (usually cheaper and fast for Go), then upload function.zip. The lambda.norpc build tag drops legacy RPC code that provided runtimes do not need, producing a smaller binary. Infrastructure tools such as AWS SAM, CDK and Terraform can automate the same steps.
init, so warm invocations reuse them.-ldflags="-s -w" to strip debug information.ctx into HTTP and database calls so work stops before the platform times out.log/slog so platform log search works well.For platforms that run containers, no SDK is required:
Build it into a multi-stage image that ends in a scratch or distroless base, and the container is typically only a few megabytes. Handle SIGTERM with http.Server.Shutdown so in-flight requests finish when instances scale down.
Because handlers are ordinary functions, call them directly in unit tests with a hand-built event. For integration tests, use the AWS SAM CLI (sam local invoke) or run the container image locally. Keep business logic in packages that do not import the Lambda library, so the same code works as an HTTP service, a Lambda function or a CLI.
Use provided.al2023 with a binary named bootstrap. The older go1.x managed runtime is deprecated.
Yes. Static binaries start in a few milliseconds of runtime initialization, so cold starts are mostly dominated by your own initialization code and network setup.
For a function handler, yes. Alternatively, run your HTTP server behind an adapter, or use a container image on a platform with a native HTTP model such as Cloud Run.
Keep business logic in plain packages and make the handler a thin adapter. That lets you move between Lambda, Cloud Run and a long-running server.
Go is a strong fit for serverless: compile a static binary, deploy it as bootstrap on provided.al2023 for Lambda or as a container for Cloud Run, initialize clients once, honor context deadlines and keep the handler thin. Measure cold starts and memory rather than guessing, and use idempotent handlers.