
Developing an AWS Lambda function in Go is a great choice for building scalable and reliable serverless applications.
AWS Lambda supports Go runtime versions 1.14 and later, which includes a wide range of libraries and tools for building efficient functions.
To get started with AWS Lambda Golang function development, you'll need to install the AWS SAM CLI, which provides a set of tools for building and testing serverless applications.
The AWS SAM CLI allows you to package and deploy your Lambda function with a single command, making it easy to test and iterate on your code.
For another approach, see: Golang Go
Getting Started
To get started with AWS Lambda in Go, you'll need to have Go installed on your computer.
You'll also need to install AWS SAM, which will help you manage your AWS resources.
Be aware that if you deploy your project to AWS, you might be billed for the resources you're creating, so be sure to clean up after yourself when you're done.
For database management, I've found supabase to be a reliable and efficient choice.
Worth a look: Go vs Golang
Building and Deploying
Building your AWS Lambda function requires compiling your Go code for Linux and placing it in a .zip file. The executable within the .zip file should be named bootstrap.
To deploy your function, you can use the SAM CLI to test it locally before deploying to AWS. This can save you time and debugging headaches. You can also run your deployment with a simple command, regardless of whether you're using API Gateway or not.
For a more complex setup, you can define your infrastructure using the Typescript CDK and attach an API Gateway to your Lambda function. This will spit out a URL to call the Lambda function.
Here are the steps to deploy your Go function:
- Build your Go function for Linux: `GOARCH=amd64 go build main.go`
- Package your application: `aws s3 cp packaged.yaml s3://YOUR_S3_BUCKET_NAME/`
- Deploy your application: `sam deploy --guided --stack-name YOUR_STACK_NAME --region YOUR_AWS_REGION`
Note that you can also hot swap your Go Lambda logic without redeploying your infrastructure by recompiling your Golang binary bootstrap and running `sam deploy --guided --stack-name YOUR_STACK_NAME --region YOUR_AWS_REGION` with the `--watch` flag.
Building Your Function
To deploy your function to AWS Lambda, you need to compile it for Linux and package it in a .zip file. The executable within the .zip file should be named bootstrap, as that's the default expected by Lambda.
The default architecture of Lambda is x86_64, so if you're cross-compiling from a non-x86 environment, make sure to build your executable with GOARCH=amd64. If you're targeting ARM architecture, use GOARCH=arm64 instead.
Alternatively, you can deploy your function as a container image, which is a more modern approach to deployment.
Worth a look: Aws Lambda S3 Api Gateway Upload File Typescript
Steps
Building and deploying a serverless application can be a complex process, but breaking it down into smaller steps makes it more manageable.
First, you need to build your Go function for Linux by navigating to your Go project directory where main.go resides and running the command: `go build -o bootstrap main.go`.
You can then package your application by running `sam package --template template.yaml --s3-bucket YOUR_S3_BUCKET_NAME` from the root directory of your project.
You might like: Golang Test Main
To deploy your application, you'll need to run `sam deploy --guided --stack-name YOUR_STACK_NAME --region YOUR_AWS_REGION`. This will create a new CloudFormation stack or update an existing one.
If you need to redeploy your application quickly, you can use the `sam deploy --guided --stack-name YOUR_STACK_NAME --region YOUR_AWS_REGION` command again, or use the `sam deploy --guided --stack-name YOUR_STACK_NAME --region YOUR_AWS_REGION --no-fail-on-s3-errors` command to skip S3 error checks.
Here's a summary of the deployment process:
Function Development
Building your AWS Lambda function in Go requires some specific settings. To deploy your function to AWS Lambda, it needs to be compiled for Linux and placed into a .zip file.
The executable within the .zip file should be named bootstrap. This is a requirement for Lambda's default architecture, which is x86_64. If you're cross-compiling from a non-x86 environment, you'll need to build your executable with GOARCH=amd64.
Alternatively, you can use a container image as a deployment package. This is a great option if you're already familiar with containerization.
Project Structure

A well-organized project structure is essential for efficient function development. The main folder is named cmd, which contains the actual lambda handlers.
This is where the magic happens, and your functions will be deployed. The internal folder holds code that's shared between handlers, making it a great place to store reusable functions.
By separating shared code, you'll avoid duplication and make your project more maintainable. The tools folder defines additional tools to be used in the project, which can be a huge time-saver.
Having a dedicated space for tools keeps your project organized and easy to navigate. The api folder is used for OpenAPI generator config and generated models, which is especially useful for API-first development.
For more insights, see: Google Cloud Functions vs Aws Lambda
Using Cgo
To use Cgo in your function development, you need to ensure your build environment is compatible with the target Lambda runtime. This is crucial to avoid execution errors.
The compatibility of your build environment depends on the GNU libc version. You can check the table below for the corresponding GLIBC version for each Lambda runtime.
If you're using provided.al2023, you're good to go with GLIBC version 2.34. However, if you're using provided.al2, you'll need to stick with GLIBC version 2.26. And if you're using provided with go1.x, you're limited to GLIBC version 2.17.
On a similar theme: Golang Version Manager
Function Handler
When developing a function handler, one of the first questions to ask is where to put the initialization of AWS SDK clients, database connection, and other things we want to deal with during the cold start.
You have options here. One approach is to follow the pattern from AWS documentation example and initialize services inside the init() function, but this can make it harder to use the handler in unit tests.
Thanks to the fact that lambda.Start() method takes a function as an input, you can wrap it in a custom struct, and initialize it with the services you need. In your case, the code can look like this:
In the main() function (which runs during cold start) you get the secret from secretsmanager and then initialize the connection with DB. Both functionalities are defined inside internal folders as common helpers so they can be reused in other handlers. Finally your ItemsService is initialized with the created db connection, and used for creating a lambda handler.
Related reading: Invoke Aws Lambda Function Sam with S3 Trigger

HandleRequest parses ID from the path parameter, and calls ItemsService to get an item from DB.
Here are some valid function signatures for the handler:
The handler function parameter must satisfy the rules documented by Start. If the handler does not match one of the supported types, an appropriate error message will be returned to the caller.
Local Development
Local development is a good practice that can save you time and debugging headaches down the line.
Testing your Lambda functions locally before deploying them to AWS is a good idea. You can use the SAM CLI to test your functions in a Lambda-like execution environment.
The SAM CLI provides a way to work on your serverless applications in your local setup. It's a great tool for developers who want to test their functions before deploying them to the cloud.
By using the SAM CLI, you can run your Lambda function locally, which can help you catch errors and fix issues early on. This can save you a lot of time and hassle in the long run.
Function Management
In AWS SAM, you can define a Lambda function in Go by pointing the CodeUri to the folder with the lambda handler and setting the build method as go1.x.
This approach allows you to build a self-contained binary for your Lambda function, which is a single executable file.
With AWS SAM, you can easily establish a connection between the HTTP API Gateway and your Lambda function, along with the required permissions.
The provided.al2023 runtime is used by Lambda functions built in Go, making it a convenient choice for Go developers.
SAM Template
For Go developers working with AWS Lambda, the SAM template is a crucial part of the process.
The SAM template is created based on OpenApi, which means it's not specific to Go.
In this case, the HttpApi Gateway is created using OpenApi.
Testing is a big part of the development process, and for that, I've created a DB on supabase and seeded it with a few dummy records.
Frequently Asked Questions
Is Golang good for AWS Lambda?
Yes, Golang is a good choice for AWS Lambda due to its consistent performance superiority and potential cost savings. It offers speed and resource efficiency, making it a viable option for developers.
What language is used in Amazon Lambda?
AWS Lambda supports a variety of languages, including Java, Go, Node.js, Python, and more, allowing developers to choose the best fit for their project. With over 20 supported languages, AWS Lambda offers flexibility and versatility for cloud-based development.
Featured Images: pexels.com

