Create a Serverless Application in Golang With AWS

Monday, August 10, 2020 • 4 minutes to read


In this tutorial, we are going to create a golang serverless application and deploy it to the AWS cloud.

Table of Content


  • Serverless Installed
  • AWS account
  • Connect AWS account to serverless

All these are covered in detail in the last tutorial.

Getting Started

Create a new directory hello-go-serverless.
Open the terminal inside the project.

Run the below command to initiate the serverless application using aws-go-mod template.
This will create a basic scaffolding of the serverless project.

1serverless create --template aws-go-mod


This template has created 2 application hello and world, and serverless.yml where serverless deployment configuration is defined. It also created a couple of deployment scripts.

Open hello/main.go

 1package main
 3import (
 4    "bytes"
 5    "context"
 6    "encoding/json"
 8    ""
 9    ""
12// Response is of type APIGatewayProxyResponse since we're leveraging the
13// AWS Lambda Proxy Request functionality (default behavior)
16type Response events.APIGatewayProxyResponse
18// Handler is our lambda handler invoked by the `lambda.Start` function call
19func Handler(ctx context.Context) (Response, error) {
20    var buf bytes.Buffer
22    body, err := json.Marshal(map[string]interface{}{
23        "message": "Go Serverless v1.0! Your function executed successfully!",
24    })
25    if err != nil {
26        return Response{StatusCode: 404}, err
27    }
28    json.HTMLEscape(&buf, body)
30    resp := Response{
31        StatusCode:      200,
32        IsBase64Encoded: false,
33        Body:            buf.String(),
34        Headers: map[string]string{
35            "Content-Type":           "application/json",
36            "X-MyCompany-Func-Reply": "hello-handler",
37        },
38    }
40    return resp, nil
43func main() {
44    lambda.Start(Handler)

This function is creating a JSON object using json.Marshal and then sent as a response.
The lambda package is initiating the Handler function and events package is used to create aws APIGatewayProxyResponse object.

This function is exposed as an API using the AWS APIGateway service. These APIs are configured in the serverless.yml file.

Check this to learn more about JSON in golang.

Open serverless.yml

In the provider section, the provider is aws, the runtime is go1.x, by default its region is us-east-1 and stage is dev. Using the staging option, you can deploy the project in different environments like dev, prod or test.

Update the provider section.

2  name: aws
3  runtime: go1.x
4  stage: dev
5  region: us-east-1

You don't have to if you're okay with the defaults. It is just good practice.

In the package section, it is configuring what to include and exclude in the lambda function deployment.
It is excluding all the files except bin folder. After building the application, the function binary will save in the bin folder.

2  exclude:
3    - ./**
4  include:
5    - ./bin/**

In the functions, bin/hello is the entry point of hello function. The function can be triggered using the hello route. After deployment, it will create a new route in the AWS API gateway and link it to the AWS lambda function in which hello function will deploy.

2  hello:
3    handler: bin/hello
4    events:
5      - http:
6          path: hello
7          method: get

world function is the same as hello.

You can use the Makefile for the below steps. It is a deployment script, which will run all the below steps for you.

Build the application

Open the terminal in the project and execute the file.

Windows User


Linux User or other UNIX based OS users First, make it executable and then run it.

1chmod u+x

This will initiate go modules go.mod in the project and add all the project dependencies in it. It is similar to the package.json in the node.js.


Create binaries of hello and world application for Linux OS.

1export GO111MODULE=on
2env GOOS=linux go build -ldflags="-s -w" -o bin/hello hello/main.go
3env GOOS=linux go build -ldflags="-s -w" -o bin/world world/main.go

It will generate binaries of the application and save them in the bin folder.


I am assuming that you have an AWS account and it is connected to the Serverless.
If you not check this to setup.

Run the below command to deploy it to the AWS lambda.

1serverless deploy

You can use sls for serverless. It is a shorthand notation.


Test the application

Copy the endpoint from the deployment output and either try it on a browser or use the curl command to check.

$ curl


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    70  100    70    0     0     63      0  0:00:01  0:00:01 --:--:--    63
{"message":"Go Serverless v1.0! Your function executed successfully!"}


$ curl


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    69  100    69    0     0     76      0 --:--:-- --:--:-- --:--:--    76
{"message":"Okay so your other function also executed successfully!"}


Serverless is the perfect companion for the golang. In the upcoming tutorials, we will integrate multiple services in serverless like DB, SNS etc.

Thanks for reading.

Cover is designed in Canva


PreviousBasic validations every nodejs developer should know

NextCreate your first Serverless application