Serverless Computing with Go on AWS Lambda

Introduction

Serverless computing is a fairly new way of building and managing software applications in the cloud-native world. This article will teach you what going Serverless means and how to set up, build, and deploy a serverless application with the Go programming language on AWS Lambda.

You will learn why Serverless technology is easy and advisable to adopt, especially for swift software development. You will learn how to build and deploy Serverless software by building one in the most suitable programming language yourself.

Cloud Computing Models

All Cloud Service Providers (CSPs) offer cloud computing services in four main models:

  • Infrastructure as a Service (IaaS)
  • Platform as a Service (PaaS)
  • Containers as a Service (CaaS)
  • Function as a Service (Function as a Service)

IaaS is a cloud consumption model in which the CSP abstracts away the hardware resources needed, allowing the cloud user to focus only on managing the virtual machines and their applications running in the machines. AWS dominates the IaaS public cloud market.

PaaS provides developers with a structural framework for developing their applications. It makes the building process easy, fast, and budget-friendly.

PaaS also provides the facilities needed for developing, testing, and application deployment while abstracting away the implementation details like server management, load balancing, and database configurations. Heroku, being the first, is an example of a PaaS solution.

CaaS is a defining solution adopted in DevOps and Site Reliability Engineering. CaaS makes it possible to spawn multiple containers on a single virtual machine instead of one per application. It is also possible to achieve fault tolerance and scalability with orchestration tools like AWS ECS, EKS, and Fargate. AWS cloud tools are the most adopted CaaS tools in the Cloud Native world.

FaaS is a model that allows cloud engineers to run their software without provisioning or managing a complex infrastructure. Software like this is called functions and abstracts away the need to worry about scaling or managing infrastructure. This is the bedrock of Serverless technology.

Serverless Computing Architecture

Server lessness (or Functions) as a Service is the newest cloud computing model — although first instituted by AWS in 2014. It strips the developer of the burden of spawning, managing, and fixing servers. It delegates it to the CSP, allowing the developer enough time to focus on improving the features of their product.

AWS uses the Pay-As-You-Go paradigm, so the developer is only charged for cloud computing time. The benefits of going serverless include:

  1. Faster development time
  2. Support for autoscaling
  3. No need for a System/Server Administrator
  4. Cost-friendly
  5. Polyglot — support for several programming language runtimes

Some of the popular CSPs who provide Serverless technology include:

  • AWS Lambda
  • Google Cloud Functions
  • Microsoft Azure Functions

AWS Lambda and Serverless Go

AWS Lambda is one of the most adopted Serverless solutions launched by AWS at re:Invent 2014. In addition, AWS announced their support for Go on AWS Lambda in 2018. While other languages like Node.js, Java, Python, and .NET are suitable for serverless development on AWS Lambda, minimizing cold starts and improving the application’s performance requires a powerful Go language.

Google’s Go language is powerful because:

  • It is the most suitable language for Cloud Native computing
  • It is fast and error-efficient
  • It supports scalability, concurrency, microservice architecture, and cross-platform interoperability
  • It has a growing community

Development Environment

To continue with this article, you should have the following set up:

  • an AWS account (free tier or paid)
  • Python environment
  • Go environment

Setting Up AWS Environment

We will set up AWS CLI for managing services and resources via the terminal. The installation on your local machine requires the pip package from Python. Open your terminal and run the command:

$ pip install awscli
# upgrade to the latest version
$ pip install --upgrade awscli

If you use a Windows instance or machine, add the path to the folder where the CLI binary is installed to the environment variable. Also, do the equivalent using a Linux, Mac, or any UNIX machine.

Run the command below to verify you have it set up properly:

$ aws --version

AWS requires user credentials to be set to authenticate AWS services. It is unnecessary if you are using a Cloud IDE like AWS Cloud 9. Log in to the AWS Management Console.

Sign in to the IAM console. From the navigation pane, select Users and click Add User. Create a new user, Set Permissions to Attach existing policies directly with the Policy name as AWSLambda_FullAccess ticked. Click on Add Tags and add a new key. Click Review and click Create User. Copy and Store the Access Key ID and the Secret access key. Click on Close and head to the local or remote terminal you install “awscli” on.

We will proceed to configure the CLI there. Run the command below and paste the credentials as they are requested:

$ aws configure

Your screen should be similar to this:

Screenshot of AWS Environment setup.

Setting up the Go environment

We will assume you already have the Go environment set up. If not, download the Go runtime and set the GOROOT environment variable in its installed directory. However, if you are on the cloud, you only have to download the runtime. Using a local machine, you can learn more about setting environment variables for AWS CLI here.

Setting up the Development Environment for Local Development

You can use Visual Studio Code or JetBrains‘ “GoLand IDE” for your development environment. If you use VSCode, installing some extensions will make writing Go enjoyable. You may need extensions like the following:

  • autocompletion
  • hovering signature help
  • code formatting
  • auto importing of packages
  • test runner

Developing a Serverless Go Application on AWS Lambda

In this section, we will build a Lambda function in Go and deploy it. Please create a new project folder and open it on your code editor. Create a “main.go” file and add the following code to it:

package main

import “github.com/aws/aws-lambda-go/lambda”

func Handler() (string, error) {
    return “Serverless Software Development using AWS Lambda with Go”, nil
}

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

Run the following command on the CLI before running the code:

$ go get -t github.com/aws/aws-lambda-go/lambda

This will install the package for Lambda with Go. Next, search for Lambda in the AWS Compute services and click “Create Function“.

Leave the default Author from Scratch, add a function name, select “Choose an existing role”, select the Go Runtime, leave the x86_64 architecture, and open another tab. In this tab, navigate to the Create Role** page to set up a new Role. Leave the AWS service as the default entity type, select Lambda** as the use case, and click Next. Search for CloudWatchFullAccess, tick the checkbox, and click Next. Add a Role name and click Create Role.

Now, return to the previous tab with the Lambda form open, refresh, and then select the new role you just created. Your screen should look like this:

Screenshot of role creation for Developing a Serverless Go Application on AWS Lambda.

Next, click on Create Function. The AWS Lambda console will display a success screen after this.

Deploying the Serverless Application

We will proceed to deployment now that we have a Lambda function configured. The deployment package is either a .zip file or an S3 bucket with the application code already in it.

On your terminal, input this command to zip our main.go file:

$ goos=linux go build -o main main.go

When you run ls you will see a new archive.zip file in the directory. Next, head to the AWS Lambda dashboard and click the Upload from toggle button at the bottom left.

Screen shot of AWS Lambda dashboard.

Upload the new zip file and move on to Configure a new test event. You can alternatively upload the zip file to an AWS S3 bucket. After this, we are going to do Event testing. Switch to the Test section and type in a new event name. Next, empty the JSON object — just the curly braces — and click Save. Click on the Test button and see the message returned from the Handler function.

Congratulations, you have successfully deployed a Lambda Function.

Conclusion

In this article, we learned about the different cloud computing models. We saw what the Function as a Service model is all about, and we learned about the architecture and architecture of Serverless computing.

Moreover, we looked at the AWS Lambda Serverless service and how to configure it locally and globally for developing serverless applications. Finally, we built a simple API in Go and deployed it as a zip file on AWS Lambda.

MacBobby Chibuzor is a Robotics Hardware Engineer and a Tech Polyglot. He also has practical experience in Software Engineering and Machine Learning, with an interest in embedded systems and Blockchain technology. In addition, Mac loves to spend his free time in technical writing.

Need help?

Let us know about your question or problem and we will reach out to you.