Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang, and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

About a year or so ago, when functions as a service (FaaS) had just started to become popular, I had written a few tutorials around developing functions that made use of Couchbase as the NoSQL database. For example, Use AWS Lambda and API Gateway with Node.js and Couchbase NoSQL focused on Node.js, which is one of my favorite programming technologies. However, at the time, my other favorite programming technology, Golang, was not yet supported with Amazon Web Services (AWS) Lambda. Fast forward a bit, Golang is now supported by Lambda and it is incredibly easy to use in combination with Couchbase because of the well-made Go SDK for Couchbase.

In this tutorial, we’re going to take a look at how to build a function on AWS Lambda that communicates with Couchbase using the Go programming language.

Creating a Go Lambda Function for Creating and Querying NoSQL Data

Since Go is now officially supported by AWS, we can make use of the official SDK rather than trying to build something of our own. Within your **$GOPATH**, create a new project and execute the following commands:

The above commands will download the Lambda SDK as well as the Couchbase SDK for Golang. We are also downloading a UUID package that will allow us to create unique values that will represent our NoSQL document keys.

With the dependencies available, create a **main.go** file and include the following boilerplate code:

Before we get into the function design, let’s break down the above code.

Beyond importing our dependencies, we are creating two native data structures. The first Todo data structure will represent the data that we plan to work with within the application. For clarity, we’re going to create a simple todo list type application that will allow us to save todo items and query for them. The second data structure will represent our incoming requests to the function. The incoming requests in our example should either contain JSON with a todo property or nothing at all.

We are also creating a global bucket variable which will be accessible from everywhere else in our code. Within the main function we can connect to our Couchbase instance and open that bucket. It is important that your Couchbase instance is not running on localhost because Lambda, a remote service, needs to be able to reach it.

After configuring the database, we can start Lambda and point it at our Handler function. In general, each function should accomplish a specific task, but our function will handle querying as well as data creation.

Take a look at the Handler function code below:

When the Lambda function is executed we want to look at the request that comes in. If it is empty or only contains properties that we’re not interested in, we are going to query for our todo items. Using N1QL we can query our bucket and return all the items as a stringified JSON response.

If the incoming request variable is not empty, it means we want to create some data in our database. Using a UUID, we can take the request and create a new document without an expiration. Then the data itself is returned.

Our overall function could be more elegantly designed, but the point of what we’re doing should be clear. The setup and usage of Couchbase isn’t really any different from how we use it in a RESTful or GraphQL application, but this time around we designed our code as a Lambda function.

With the function ready to go, we can work toward our deployment.

Deploying and Testing the AWS Lambda Function

When it comes to AWS Lambda there are certain requirements that differ from how we might run our application locally. For one, Lambda uses a particular operating system and a potentially different architecture from what we’re doing locally. For this reason, we need to cross compile our application and then bundle it.

From the command line, execute the following:

The above commands will build for Linux and create an archive of the binary. Just make sure to change binary-name with that of your actual binary name. If the zip command doesn’t work on your operating system, just archive it manually.

For more information on cross compiling Go applications, check out my previous tutorial on the subject: Cross Compiling Golang Applications for Use on a Raspberry Pi.

With the **** file available, go to the AWS Lambda Console where we can create a new function.

When creating the function, choose the defaults, but make sure that you choose a Go 1.x application because that is what we’re going to deploy. When you’re at your function dashboard, don’t worry about adding a trigger for this example. Instead, choose to upload your function code which is the **** file. For the **Handler** make sure to use the name of your application binary.

To test our deployment, we can create a test right in the dashboard. Toward the top of the screen, choose to configure a new test event. You can name the test event whatever you want, but add the following as the request content:

After saving the test event, execute it from the dashboard and look at the results. It should have run successfully and you should have a new document in your Couchbase database.


You just saw how to create a new AWS Lambda function with the Go programming language that communicates with Couchbase Server. The process is quite simple, and can be extended with triggers like AWS Gateway to make it a little more useful from a usage perspective.

In the next tutorial, we’re going to take a look at expanding this tutorial to support Amazon Alexa-powered devices. This way, we can use Alexa as our trigger and create or query documents with our voice.

This post is part of the Couchbase Community Writing Program


Posted by Laura Czajkowski, Developer Community Manager, Couchbase

Laura Czajkowski is the Snr. Developer Community Manager at Couchbase overseeing the community. She’s responsible for our monthly developer newsletter.

Leave a reply