Getting persistence in Serverless

PoC Layout
PoC Layout to our post

Serverless is one of the hottest 🔥 terms lately, but what exactly is it?

Serverless is a cloud computing model whose goal is to get developers to focus only on the code and forget everything else. One of its implementations is FaaS (function as a service). It is based on dividing the software into independent functions allowing to execute code in response to events or requests. Therefore, the functionality could be delegated to the cloud. It is very useful in the construction of microservices.

The main platforms provide this service such as AWS Lambda, Google Cloud Functions, Azure Functions, IBM Open Whisk or the interesting and free Open Lambda. Of course, like the rest of the services on these platforms, we are in a cost-per-use model. This can already give us some direct advantage.

So … is Serverless perfect? Why isn’t everyone already using it? Are the developers outdated?
Not exactly, Serverless has a weak point. It only has a local scope. In other words, it has no persistence and its scope is lost at the end of its execution.
In this post we are going to see how to solve this. Let’s get to it.

Solution 💡

  • Console interface and frontend: developed with Golang.
  • Backend and services: developed using serverless technology deployed in Amazon Web Services Lambda using Node.js.
  • Database: simple storage using JSON format in Amazon Web Services S3.

The complete solution is in the following Github repository.

Amazon Web Services

Amazon S3

Private S3 Bucket with JSON file

AWS IAM

Test user for SDK

In addition, within this service we need to create a new role.
This role is for the Lambda execution. The services to be allowed are S3 and CloudWatch Logs (CloudWatch Logs is only used to help us to debug our Lambda functions).

Policies and services in new role.

The role will allow us to connect the AWS Lambda service to the S3 private bucket. We’ll use it to configure the following functions.

AWS Lambda

From AWS Lambda you have to create 2 new functions. While creating them we must select the role previously (in this case: lambda-s3-general) created in the IAM service:

Creating a new Lambda function in AWS Lambda

The code of both functions of our POC application is very simple. It only consists of obtaining or updating the data of a JSON object.
Important: Here you have to replace the name of the bucket and file (serverlesspocbucket and data.json) with the ones created previously.

The first returns the number of current coins:

The second one updates the number of current coins:

Go code

  • Setting up the Go environment according to their instructions.
  • Replace the false AWS values in the credentials.go file with your own.
    Important: now we have to use the creedenciales obtained in AWS IAM. It is also necessary to replace the AWS region we are using and the “arn” of the created Lambda functions.
  • Include AWS dependency for Go:
go get github.com/aws/aws-sdk-go

Now we can start to see the code and how it connects to Lambda.

The interface of the application consists of simple menu through the console (as will be seen in the examples) so that part will be ignored in the explanation. Let’s focus on the main functions and the call to AWS.

As we have seen we have a lambda function that is used for both operations. The only difference is the parameters we use for the invocation.
It’s easy: arnGet for CurrentState and arnSet for AddCoins.

First prepare the parameters and get the credentials (as we’ll see later).
Then we invoke the Lambda function through the SDK, check its exceptions and parse the answer.

Finally, this is the function we use to obtain the credentials:

Running 🚀

To start using the project and be able to play with it you have to clone the repository under the src folder of the Go workspace of your computer.
Reminder: you should also replace the false AWS values in the credentials.go file with your own.

Once the previous steps have been completed, it is now possible to execute it:

go run main.go aws.go constants.go credentials.go functions.go

Or build an executable and run it later from wherever we want:

go install POCServerless

Typically this executable is located under the /bin folder of our workspace. Remember that it is very useful to include the /bin folder in our own path. So we have access to the executable from anywhere:

MBP-Dani:~ daniortiz$ POCServerless 

*****************************
**Welcome to POC Serverless**
*****************************

Choose one of the following options:
1. View the current status of the wallet
2. Save coins
3. Exit
>

Let’s check the initial status:

> 1
The wallet has 0.00 coins

Choose one of the following options:
1. View the current status of the wallet
2. Save coins
3. Exit
>

Then we’re going to keep new coins in our wallet:

> 2
Coins to save:
5
Saving 5 coins...
5 coins saved in your wallet!

Choose one of the following options:
1. View the current status of the wallet
2. Save coins
3. Exit
> 2
Coins to save:
8
Saving 8 coins...
8 coins saved in your wallet!

Choose one of the following options:
1. View the current status of the wallet
2. Save coins
3. Exit
>

We checked the state of our purse again and exit the aplication:

> 1
The wallet has 13.00 coins

Choose one of the following options:
1. View the current status of the wallet
2. Save coins
3. Exit
> 3
See you soon!

As expected we have received the coins that we have kept previously and we can verify that our Serverless application works successfully!

Software engineer, geek and newbie writer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store