How to build an API using AWS Lambda and API Gateway (Written with Go)
Introduction
Overview
In this article I will outline the steps to build an API using AWS Lambda and API Gateway using my favorite language at the moment, Go.
Why
Sometimes you just want to perform a simple operation that is accessible through a RESTful API and don’t want to have manage/pay for a server.
Now the steps
Pre-requisite
Sample Lambda function
Go to amazon’s documentation to see some sample code. Otherwise, you can use mine.
// main.go
package main
import (
"github.com/aws/aws-lambda-go/lambda"
)
type Request struct {
Message string `json:"message"`
}
type Response struct {
Message string `json:"message"`
}
func HandleLambdaEvent(r Request) (Response, error) {
return Response{Message: r.Message}, nil
}
func main() {
lambda.Start(HandleLambdaEvent)
}This is a basic function that returns what’s passed in the message key of the Request in the message key of the Response body.
First we import github.com/aws/aws-lambda-go/lambda package.
We can get this Go package by running this in the terminal:
$ go get github.com/aws/aws-lambda-go/lambdaNext we define the struct that serves as the type that holds the request’s body.
The name for the struct is completely arbitrary.
For this example we have Request. In the AWS documentation above, they use MyEvent.
Build app
Run:
$ GOOS=linux go build main.goThis will generate a main application.
Next we’ll need to zip the app.
$ zip function.zip mainThe name of the zip file is arbitrary.
Create Lambda function
Go to AWS Lambda
Click Create function.
Name your function. I choose createPost as the name because the configuration that we are going to build is very similar to how an API endpoint would look like if we are to create a Post record. More on that later.
Select Go 1.x.
You can set other things like VPC etc but for this exercise we can just use the default settings.
Click Create function.
Next upload the zip file that you just created earlier.
Once you’re done, click Edit on Runtime settings.
Change the Handler to main. This tells AWS which function to invoke in the main package. Click Save.
Now let’s publish the function. Click Publish new version and Publish for the dialog after that.
API Gateway
Create a new API
Name your API and click create API.
Create Resource
With the / highlighted, click on Actions and select Create Resource.
What is a Resource
A resource typically refers to a collection. You can name it whatever you want.
The resource path (according to best practice) would usually be a noun.
In this example, we would have the resource name identical to the endpoint path.
example-domain-name.com/postsYou can check Enable API Gateway CORS or not. There are some optimizations but nothing worth mentioning for this exercise.
Create a method
A method here simply refers to a HTTP method.
Have /posts highlighted and click Actions then select Create Method.
Select POST. With a POST method on /posts endpoint, this resembles an endpoint that creates a record on a traditional backend API application. We are not creating a record here but this API design language should be familiar to RESTful conventions.
Select Lambda Function and key in the name of the lambda function that we just created. Click Save. A dialog would pop up to inform you about permissions, click Ok.
Test our endpoint
We can test our API endpoint. Click TEST.
In the Request Body, put a simple JSON body.
{
"message": "Hello World"
}and click Test.
We should the response like:
It’s working! Try changing the message in the Request and testing it again.
Deploy API
With the / highlighted, click Actions and Deploy API after that.
Choose [New Stage]. Or if you have an existing stage you can choose that instead.
I will provide a new stage with a stage name and click Deploy.
Now we have an endpoint we can send our requests to.
cURL
We can now cURL it! Do note that the base URL has the stage name on it. Your resource name comes after that.
https://{base-url}/{stage-name}/postsDone!
We are done! Now you have a Go function that is accessible on the internet. What you can do with it is only limited by your imagination.
Improvements
The URL is that amazon generated for us is kinda ugly. Next, we will explore how to have a custom domain name for our API endpoints.
Good luck, have fun!


















