Terraform & Serverless framework, a match made in heaven? (Part I)
By: Adam Jasinski
Tags: Serverless, Nodejs, AWS, Serverless Architecture, Terraform

TL;DR

The general belief is that Terraform is the de facto way of deploying infrastructure across multiple platforms. On the other hand the Serverless Framework is more specific and aims to aid deployments of Serverless applications across multiple platforms.

There are clear benefits of being generic, and there are benefits of being specific. However, I am going to attempt to convince you why I believe that deployment of Function-as-a-Service (FaaS) and infrastructure resources on AWS ought to be deployed using the Serverless framework in conjunction with Terraform and they are not mutually exclusive.

First, we need to appreciate the fact that a multitude of tools exists when thinking about deploying infrastructure resources. Each of these tools seems to offer a slightly different approach to solve the same problem. Unsurprisingly, it seems to boil down to personal preference or market demands when it comes to picking the right tool. Let me clarify; the tool is right when it fits your use case, the current skills and the audience.

There are two families of tools that have emerged over the past few years (which is an eternity in the modern IT), one being the more declarative and more straightforward to understand with the second being the full-featured, programming geared.

To fully appreciate the variety in the tooling, we must understand that the origin of the userbase has driven the genesis of both families. Declarative tools aim to support professionals from the more traditional ops backgrounds by providing them with an easy-to-learn tool that provides value as quickly as possible. Some examples of that group include CloudFormation, Terraform or ARM templates. On the other hand, there are the more developer-friendly options such as Cloud Development Kit, Pulumi or the Serverless framework that are much more featureful and easier to adopt by the target userbase.

This series focuses on the comparison of tools from both families and their suitability in traditional enterprise environments.

And our candidates are CloudFormation, Terraform and the Serverless framework. My judgment will be slightly biased towards deployments of Lambda functions from the perspective of a DevOps engineer developing event-driven software to run on AWS.

Let’s see how the competitors compare:

Comparison between Terraform, CloudFormation and the Serverless Framework

I would strongly recommend checking out some of the mentions in the above matrix: checkov, terraform-compliance, cloudformation-guard, terragrunt and also the Serverless Framework Plugins and Terraform Custom Provider Development Program, these are super useful tools to help you to expand the possibilities when deploying serverless applications and infrastructure in general.

So, here comes the question: why exactly you say that terraform and the Serverless framework are the best choices when deploying serverless applications and more specifically lambdas on AWS? CloudFormation would be the way. (pun intended) it is worth noting that the Serverless framework transpiles to CloudFormation!

And the answer is two-fold:

unopinionated — Terraform allows you to deploy to any cloud

— Terraform allows you to deploy to any cloud flexible — Serverless framework is flexible aside from app definition

The approach here is to utilise as many benefits of both tools to obtain as much value as possible.

Exercising the flexibility of the Serverless framework and standardising approaches of deploying our apps on a multitude of FaaS platforms (knative, AWS Lambda, Azure Functions, kubeless) is an excellent boost to our productivity. As an added benefit, we are becoming more agnostic by reducing the number of tools required for the devs to learn. Besides single file deployments, the plugins, and the overall heavy lifting the framework does for us make it better suited than CloudFormation.

On the other hand, similar value points are being gained by Terraform when it comes to deploying infrastructure. It is the tool where we can easily choose the provider of choice and invest in a single deployment tool of choice. Also, the security, testing and the compliance being the more critical aspects within the enterprise environments make Terraform the perfect candidate.

Let’s take an example.

We have two AWS Lambda functions, and each one uses an SQS queue as its event source and forwards the events. We have three distinct choices of deploying the whole stack, including the lambda functions:

Deploy supporting resources and the functions using Terraform

Deploy supporting resources and the functions using Serverless Framework

Deploy selected supporting resources using Terraform and functions using the Serverless Framework

The issues arise, when we want to move to a different FaaS provider (kubeless, knative), the transition with the Serverless framework only option may not be possible due to inconsistent provider support, especially for the supporting infrastructure resources.

The Terraform-only option seems to be a better choice, especially when considering the variety of providers available.

I would recommend that you go with a third option: