How to Leverage AWS Lambda Go as an IT System Administrator

Published:5 September 2022 - 6 min. read

Azure Cloud Labs: these FREE, on‑demand Azure Cloud Labs will get you into a real‑world environment and account, walking you through step‑by‑step how to best protect, secure, and recover Azure data.

Looking for a way to deploy applications in the AWS cloud without worrying about provisioning servers? Consider deploying AWS Lambda go code for serverless computing that allows developers, like yourself, to run code “without” a server.

Not a reader? Watch this related video tutorial!
Not seeing the video? Make sure your ad blocker is disabled.

You can choose between multiple languages such as Java, Go, Python, node.js, and many more, but one of the most common ways to deploy an application is using Go Language. In this tutorial, you’ll build and run a configuration to invoke an AWS Lambda Go function from scratch!

Read on and discover how a serverless computing service can change how you deploy your applications!

Prerequisites

This tutorial comprises step-by-step demonstrations. If you’d like to follow along, ensure you have the following in place:

  • A Windows 10+ device.
  • An IAM user and role – This tutorial assumes you already have an IAM user and role with EC2FullAccess and LambdaFullAccess privileges.

Crafting an AWS Lambda Go Program

AWS Lambda function supports many languages such as Python, Nodejs, .net, and Java. But creating a program using Go is another viable option. So why not kick off this tutorial by creating a Go-based program? You’ll later invoke this program using the AWS Lambda Go function.

1. Log in to the Ubuntu machine with your favorite SSH client.

2. Next, run the commands below to create a working directory called ~/lambda_go. This directory will contain all configuration files you’ll be using in this tutorial.

mkdir ~/lambda_go
cd ~/lambda_go

3. Run the apt update command below to ensure that Ubuntu has all the latest package sources.

sudo apt update 
Updating the system package repository
Updating the system package repository

4. Now, run the below apt install command to install the go software (golang-go) on your machine.

sudo apt install golang-go
Installing the go software
Installing the go software

5. Create a file called main.go in your preferred code editor, add the following code to the file and save the changes.

This main.go file contains the code in the Go language with the following details:

  • package main - To implement the handler function, you need a main() function, and to implement the main function, you need the main package.
  • func HandleRequest - The Lambda function handler is a method that allows you to process events. When your Lambda function is invoked, Lambda runs this handler method.
  • github.com/aws/aws-lambda-go/lambda – Implements the Lambda programming model for the Go language.
package main

import (
        "fmt"
        "context"
        "github.com/aws/aws-lambda-go/lambda"
)

type MyEvent struct {
        Name string `json:"name"`
}

func HandleRequest(ctx context.Context, name MyEvent) (string, error) {
        return fmt.Sprintf("Hello %s!", name.Name ), nil
}

func main() {
        lambda.Start(HandleRequest)
}

6. Next, run the go build command below to compile the go program (main.go).

GOOS=linux go build -o main main.go

After executing the program, you’ll notice two new files (one main directory and one main.go file).

Compiling the main.go program
Compiling the main.go program

7. Finally, run the below commands to zip and save the go program to your local Ubuntu machine.

This zip file will later need to be uploaded in the Lambda function. You can also copy this zip file from the Linux machine to your Windows machine.

./main
zip archive.zip main
ls
Zipping the go program
Zipping the go program

Creating an AWS Lambda Go Function

Now that you have the Go-based function ready, it’s time to create an AWS Lambda Go function. AWS Lambda function can use multiple languages to deploy the application, and one of the most widely used is the Go language.

To provision a Lambda function from the AWS Management console:

1. Open your favorite web browser, navigate the AWS Management Console, and log in to your AWS account.

2. While in the AWS Management Console, search for, and select Lambda from the search results to access the Lambda services page.

Navigating to AWS Lambda
Navigating to AWS Lambda

3. Next, click on Functions from the left panel and Create Function (top-right) to initiate creating a new AWS Lambda function.

Initiating creating a new AWS Lambda function
Initiating creating a new AWS Lambda function

4. Now, specify parameters for the new AWS Lambda function with the following:

  • Choose the Author from scratch option as you will define the name and other parameters in upcoming steps.
  • Provide a unique and descriptive Function name. But this tutorial’s choice is ata_lambda_function.
  • Set the Runtime to Go 1.x language as you use the Go language program.
  • Click Create function (bottom-right) to finalize creating the new AWS Lambda function.
Specifying all the parameters to create the Lambda function
Specifying all the parameters to create the Lambda function

5. Click on the newly-created Lambda function to access the function’s information page (step six).

Accessing the newly-created Lambda function’s page
Accessing the newly-created Lambda function’s page

6. Next, click the Upload dropdown button and select .zip file under the Code tab to initiate uploading your go program.

Your AWS Lambda function’s code consists of scripts or compiled programs and their dependencies. You use a deployment package to deploy your function code to Lambda as container images or .zip file archives.

Uploading the zipped go program to the Lambda function
Uploading the zipped go program to the Lambda function

7. Finally, click Upload and locate the file (archive.zip) you zipped in the last step of the “Creating a Go Program Function” section and click on Save to finalize uploading your code.

Uploading your zip file saves it in the AWS Lambda function.

If the code is larger than 3MB or you need to add libraries, you must upload your function code as a .zip archive.

Saving the go program to the Lambda function
Saving the go program to the Lambda function

Executing and Verifying the AWS Lambda Go Function

You have successfully created your Lambda function and uploaded your go program to the function. But you can’t expect the function to run perfectly without testing it first.

To test and verify the Lambda function:

1. Navigate to your Lambda function’s information page again, and click on Edit (top-right of the Runtime settings) to edit the runtime settings.

Ensure you rename the handler name to main, as shown below since your Go code (main.go) has main as the handler.

Updating the handler in the Lambda function
Updating the handler in the Lambda function

2. After updating the Handler, navigate to the Test tab, and configure a new event as follows:

  • Choose the Create new event option, and specify a unique Event name. But this tutorial’s choice is Event1.
  • Keep the Event sharing settings as Private.
  • Add the below Event JSON to the editor. Feel free to change the value Sagar to your liking, and click Save (top-right) to finalize creating the new event.
{
"Name": "Sagar"
}
Configuring the test events in the Lambda function
Configuring the test events in the Lambda function

3. Finally, click the Test button (top-right) to test your Lambda function.

You’ll be prompted to configure test event details if you hit the Test button for the first time.

Testing the Lambda function
Testing the Lambda function

After you click the Test button, the Lambda function takes seconds to execute and provides the execution logs.

You can see below that the Lambda function has been executed successfully, and the output prints Hello Sagar!. The output also shows how long the function took to complete, the memory size consumed, and how much time AWS billed you.

Viewing the output of the Lambda function
Viewing the output of the Lambda function

Monitoring an AWS Lambda Go Function

You’ve successfully executed your Lambda function, which is a significant milestone. But monitoring your Lambda function is equally important to check the resource utilization, application performance, and operational health of the AWS infrastructure.

How do you monitor a Lambda function? The best way to monitor all metrics and logs of the Lambda function is by verifying in the AWS Cloud Watch service.

To monitor your Lambda function:

1. On the AWS Management Console, search for and select CloudWatch to access the CloudWatch dashboard.

Navigating to the AWS CloudWatch Service dashboard
Navigating to the AWS CloudWatch Service dashboard

2. Next, click All metrics on the left panel, and you’ll see all metrics, such as EBS, EC2, etc., as you can see below.

Look for, and click on Lambda to view the Lambda function metrics.

Accessing the Lambda function monitoring
Accessing the Lambda function monitoring

3. Under the All metrics tab, select the By Function Name option to list the metrics categorized by function name.

Choosing Lambda function metrics based on the function name
Choosing Lambda function metrics based on the function name

4. Ultimately, on the FunctionName column, tick the boxes in all the metrics of ata_lamabda_function to view their details.

As you can see below, the CloudWatch displays all the metrics of the selected function, such as Errors, Duration, Throttles, ConcurrentExecutions, and Invocations.

Viewing all metrics of the ata_lamabda_function on CloudWatch
Viewing all metrics of the ata_lamabda_function on CloudWatch

Conclusion

Building a serverless application with Lambda allows you to scale when needed and is a quick task. And in this tutorial, you’ve learned to create a Go Language-based AWS Lambda Go function and execute and monitor an AWS Lambda function.

AWS Lambda, a Function as a Service (FaaS), has been making noise in serverless and cloud computing. Companies are already turning to serverless architecture to bring their products to the market the quickest without compromising efficiency. So why not use this newfound knowledge with other AWS services and build robust services?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!