AWS Lambda function with Golang

Aman Agarwal
3 min readApr 14, 2021

What is Lambda Function

It is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources for you. You can use AWS Lambda to extend other AWS services with custom logic or create your back-end services that operate at AWS scale, performance, and security.

Why use Lambda Function

Instead of doing everything by hand lambda function has automated everything. You can run your code on it without having to manage servers or even containers. It’ll automatically scale depending on how much work you feed into it. You can use it in data pipelines or to respond to web requests or even to compose and send emails.

How to create Lambda Function

There are several methods for creating Lambda Function!

  1. Through zip file. (From scratch)
  2. Using Sample Code. (Use Blueprint)
  3. Container Image
  4. Through Serverless application

Writing Lambda Function In Go!

You need to include the github.com/aws/aws-lambda-go/lambda package, which implements the Lambda programming model for Go. Besides, you need to implement a handler function code and a main() function.

So moving step by step:-

$ go mod init lambda-example.com$ go get github.com/aws/aws-lambda-go
  1. create file main.go
package mainimport (
“fmt”
“github.com/aws/aws-lambda-go/lambda”
)
func main() {​​​​​
lambda.Start(handler)
}​​​​​
//where handler is name of function where are code will be written.type LambdaInp struct {​​​​​​​​​​
Name string `json:”name”`
}​​​​​​​​​​​​
func handler(event LambdaInp) {​​​​​​​​​​​​
fmt.Println(“hellow “ + event.Name + “ from handler func”)
}​​​​​​​
$ go build

It will create an object file name lambda-example.com, we will zip that particular object file.

 $ zip archive.zip lambda-example.com

After this, our Zip file is created! will be used to create our Lambda function.​​​​​​​

Creating lambda function

Create Lambda Function where you will find the above mention method for creating function, we will be choosing Auth from scratch.

In the Basic Information section, we will give our function name as well as the Runtime parameter which will be Go 1.x for golang.

After providing a basic setting for creating a new Lambda function, hit Create Function.

Upload our zip file in Code Source section (we can also upload zip file from our S3 bucket if we have one)

After this change the Handler name from Runtime Setting to “lambda-example.com” in our case ( name of the object file we have defined in our code ).

Then moving to the Test section , define the test case in JSON formate , in our case

{​​​​​​​​“name”:” world”}​​​​​

After the execution of the test, it will display its complete log where we can see the result.

START RequestId: 4bad89d9–93ac-4289–9413–428698eac3fb Version: $LATEST
hellow value1 from handler func
END RequestId: 4bad89d9–93ac-4289–9413–428698eac3fb
REPORT RequestId: 4bad89d9–93ac-4289–9413–428698eac3fb Duration: 0.88 ms Billed Duration: 1 ms Memory Size: 512 MB Max Memory Used: 31 MB Init Duration: 68.47 ms

--

--

Aman Agarwal

Engineer | Explorer | Blockchain | Golang | JavaScript developer