Creating a Serverless API on AWS APIGW And Lambda with Golang

Tram Ho

What is Serverless?

When I first heard about the keyword “Serverless”, I also thought deeply. But when I have a lot of exposure to Public Cloud platforms such as AWS or GCP, I shortened myself, to tell myself a sentence when I encounter this keyword, and I also tell the Freshers that I mentor. in company. The comments below are my subjective views only.

“Any service that when I go to install it but I don’t see the operating system (Windows Server or Linux), it means it is hidden underneath so I don’t need to manage the OS, just let it be Serverless”

Because I use AWS in today’s article, I take AWS’s service as an example:

  • AWS API Gateway: as a place to receive input requests, handle authentication and take care of packet structure conversion (request/response). You see this service uses a lot of computing power to process the input of the API, but when installing, I just need to focus on the Logic I want to process, no need to care about the Server configuration below Ram, CPU can it run? Of course, AWS will still charge you, how do you go to this page to learn more.
  • AWS Lambda: This will be the protagonist of today’s article. This service of AWS allows me to upload my code or build package and run it directly, I can’t see its underlying operating system is Windows Server or Linux anywhere. so just put it in the Serverless category.

It is possible that in some Public Cloud or Private Cloud providers, there will be some forms of services that do not apply their above formula, but most of the services of the major Public Clouds that I have used are mostly applicable.

Why use Serverless, are using as traditional, why change?

Surely those of you who have used the hosting service already know, when I rent a VPS or on AWS with EC2 service, the price list will be calculated according to the time of use, the higher the configuration, the higher the cost per hour. big. For large enterprises, this initial investment cost for server clusters is even more terrible.

Back to Serverless it is often these services, for example AWS Lambda, our protagonist today. Then the cost will be calculated on the number of requests and processing time of each request. The fact that I build but the number of requests is small or temporarily has no users, the cost is almost nothing for AWS, unless I use the Provisioned package, AWS will provide me with Lambdas with fast start time. a little more than usual. Scroll down to the cons of Serverless I added.

In addition, the implementation of Lambda helps to reduce a lot of time spent on setting up the appropriate environment, operating system, and runtime for each programming language. Or if I use Docker, I also have to make sure the Docker runtime works continuously without interruption. Service interruptions due to air conditioning failures and power outages actually happen quite often, so it’s also a factor when using Serverless to think less about those problems.

Cons of Serverless in General and Lambda (Nobody’s Life Is Perfect)

  1. According to my test, when there is no request for a period of time, Lambda will enter the “rest” mode, which means there is no instance waiting to receive the request anymore. This leads to when switching from “rest” to “active” mode, it takes a longer time when there is a continuous request, I see it is about 300-800ms. But in return for continuous requests, Lambda’s response speed is quite fast, about <50ms.
  2. Not suitable for Workloads that receive small and continuous packets, this time the cost for Lambda compared to EC2 or external VPS will be higher. You can calculate and compare by yourself using this link.
  3. The cost model “starting small” helps businesses and startups save a lot of money, but this is also a double-edged sword if planning for the future of your company has not been properly planned. For example, there are about a few million requests a month in the early stages of the project, then the company develops more microservices inside, the system is more cumbersome, and if abused AWS Lambda applies to workloads as well. If running for a long time, the cost will be very sour at this time.

Enough talk for you to grasp the outside of Serverless or here the main character is AWS Lambda, now I go into the main work of programming!

Prepare the environment for API deployment

  1. Install Nodejs (Because item 2 is installed with NPM), details are at this link
  2. Install Serverless Framework, details at this link
  3. Install AWS CLI, details at this link
  4. Install Golang, details in this link
  5. Since the example uses the make command, I use Ubuntu/MacOS.

Install AWS CLI to Connect Your AWS Account

You install Client ID and Client Secret as below, the remaining 2 parameters are optional you can not install.

Let’s see the main structure of the sourcecode!

I always use the sourcecode of Serverless Framework as an example, first let’s clone the project

Then I went to the example written in Golang to create the HTTP API.

Before running, I introduce to you the main components of a Serverless Framework project:

serverless.yml

This file defines the services we will create on AWS after deployment, including AWS APIGateway and AWS Lambda along with the necessary IAM Roles. This yml file uses bullet points similar to Python, so you should note when editing, remember to indent to match.

In this section, you define the Public Cloud that you want to deploy your API to, here using AWS and Go 1.x.

The functions section you see has 3 functions, Get, GetQuerry and Post, which are also the 3 APIs I will publicize.

/getFolder/getExample.go

This is where I write my handlers in Golang, here I enter the simplest file, getExample.

This code takes the queryString “name” from the URL, then inserts and prints the text “Hello <name passed in>” . You can try to modify the logic in this function such as changing the message printed or trying to connect to the database.

Let’s run!

If you use Ubuntu and MacOS just run the command below outside of root:

You access your AWS account, then go to Cloudformation and select the stack whose name starts with “goservice”.

Go to Output tab to get API Endpoint.

image.png

Good luck !

Share the news now

Source : Viblo