AWS Lambda functions using Go

Software Engineer that works mainly with Go and Python. Loves music and beer.
Search for a command to run...

Software Engineer that works mainly with Go and Python. Loves music and beer.
Nice article!!
I don't work with these technologies but I loved reading the article Miguel. Do you think AWS Lambda is a cost effective solution for small startups?
I think it can be! Also how it helps to reduce complexity in terms of scalability, it can definitely be a good option to take into consideration.
Helpful post, Miguel!
If cold starts are not acceptable, it maybe a bit more expensive to eliminate them on AWS. They do provide several ways of doing it though.
A subdomain is basically a domain that’s part of another main domain and it is used a lot in many websites. I spent a decent amount of time figuring out how to do that and found this very helpful post and decided to extend it a little in a tutorial f...

I found myself very confortable working with Heroku, so I decided to use it as my main platform to deploy my applications. That, along with my Google domain, was a great combo to get my website up and running in no time, or so I thought. It wasn't to...

Remember those old jukeboxes where people used to insert a coin for it to take a record out and play the selected song? In this tutorial, we will implement our own digital jukebox that will allow you or your friends to search and add music to a playl...

Recently, I wanted to deploy a Go service into an AWS Lambda for the first time and found myself struggling a lot more than I expected.
Even though there are plenty of great and helpful posts with examples, there where minor aspects in which they differed (from each other and from the official documentation) given that the cloud seems to evolve so fast with constant improvement updates.
This is why I decided to create my own updated version using the official lambda library for go hoping it might help someone out.
Lambda is a serverless, event-driven compute service that lets you run your code without provisioning and/or managing servers, only paying for what you use which is extremely convenient.
Lambdas can be triggered with any of the other AWS services for things like:
File processing (S3)
Data and analytics (DynamoDB)
Log into your account, go to the search bar on top and type "lambda". Click on the first result.

Then click "Create function".

Type: Author from scratch
Function name: lambda-example-go
Runtime: Amazon Linux 2023
Architecture: arm64
According to the AWS documentation, "Functions that use arm64 architecture offer lower cost per Gb/s compared with the equivalent function running on an x86-based CPU." This is the one we'll be using for now.
Also, notice the note box that says "you must provide your own executable or script name 'bootstrap' as the entry point". We will be using this information in a few steps.
Click on "Create function" at the bottom and leave this page open.

Let's now create our simple Hello World service in Go.
mkdir lambda-example-go
cd lambda-example-go
We will need the lambda handler function to run our code. For more information, check out the official Developer Guide.
go get github.com/aws/aws-lambda-go/lambda
go mod init example.com/lambda-go
go mod tidy
Create a main.go file with the following content:
package main
import (
"log"
"github.com/aws/aws-lambda-go/lambda"
)
func main() {
lambda.Start(runService)
}
func runService() {
log.Println("Hello from AWS")
log.Println("exiting...")
}
We will now build our application, and for this, we need to remember a couple of things:
We need to use "bootstrap" as the name for our executable. This is very important, otherwise the function will fail on execution
We have to use arm64 architecture for a linux OS
Run the following build command inside your project directory:
GOOS=linux GOARCH=arm64 go build -v -o ./build/bootstrap main.go
Here, GOOS defines the operating system and GOARCH the architecture of the target system our code will run on.
Now let's zip our executable file. On macOS we can do that with zip:
zip -j lambda-example-go.zip ./build/bootstrap
Let's go back to our AWS page and click on "Upload from" on "Code Source" and choose ".zip file".

Click on "Upload" and choose your recently zipped file lambda-example-go.zip
Now go to the "Test" tab and just click "Test" on the right.

Execution should succeed and you will see a status box like this one:

Click on "logs" and open the log stream

You should be able to see our log messages

And that's about it.