Creating an API with AWS Lambda, API Gateway and Go


The combination of AWS API Gateway and Lambdas is very attractive. As far as our industry is concerned, it’s a step closer to the dream of “code goes in, full environment comes out”. I’ve set up a few of these, and did another one today using Go. This post will offer a recap of that setup. My needs did not require a full CI/CD pipeline, so you won’t find that here. If you need that, I’ve used this guide with success.

The Go code

package main

import (


func main() {

type MyEvent struct {
	Name string

func HandleRequest(context context.Context, name MyEvent) (events.APIGatewayProxyResponse, error) {
	return events.APIGatewayProxyResponse{Body: "hello world", StatusCode: 200}, nil

One tricky thing here is the events.APIGatewayProxyResponse type. When the API Gateway calls your lambda, it needs the response from your lambda to be a certain shape. This little “gotcha” probably trips me up more than anything else in AWS – I do it just infrequently enough to always forget it.

Building the Lambda package

Packaging up our code for Lambda is pretty easy, we just need to build an executable and zip it up – go build && zip go-lambda. In this instance, the name of my program is go-lambda, which is the name the output of go build assumes.

Create your Lambda

I typically just log into the AWS Console in order to create Lambdas. Some people prefer the AWS CLI, ymmv. From the AWS console, navigate to the Lambda service and create a new function. I author mine from scratch (as opposed to using a template). Give your function a name and make sure that you select Go for your runtime. I won’t go be covering the Roles area of Lambdas in this post, but you can either create a new role or use an existing one. Once you’ve created your lambda, you can upload the zip file that we created earlier as your Lambda code.

Connect the API Gateway

From the AWS Console, you can specify a new API Gateway trigger for your Lambda. I like this approach, mainly because it creates the API Gateway for you (and all the bits necessary to integrate with Lambda). For this example, instruct the console to create a new API and leave the security open. Once you save the new trigger, it should present you with your new API endpoint. This setup allows for any HTTP method when calling your API, so just navigate to the endpoint within your browser to see our hello world message.

428 Words

2018-08-28 06:05 +0000