Building Serverless Applications with Terraform1. Introduction4 min

Infrastructure-as-code (IaC) is a methodology of applying techniques commonly used for source code to infrastructure templates. IaC allows engineering teams to define required infrastructure in either a declarative or imperative way, and enables capabilities such as version control, audit, and CI/CD.

Hashicorp Terraform is an IaC tool that allows engineering teams to define and deploy their applications using a declarative, human-readable syntax. This guide is targeting engineering teams that are looking for guidance and samples for using Terraform to build serverless applications.

While this guide covers some of the Terraform basics, it assumes you do have previous experience and some level of proficiency with Terraform.

Overview

One of the major benefits of using IaC is the fact that it allows to easily create multiple environments from the same infrastructure template. This helps engineering teams to ensure that different application environments can be easily replicated and kept in sync, reducing the risk of application functionality breaking. This is applicable to multiple scenarios. One common scenario is using IaC to deploy development, testing, and production environments. Another example is using IaC to deploy a set of dedicated data plane assets in a SaaS application, where each tenant has their own set of single-tenant infrastructure resources. IaC reduces the risk of human error and allows to automate replicating changes across environments.

Over the years IaC practices and tools evolved to cover thousands of various resource types, many of them going beyond the original definition of "infrastructure". For example, today you can use IaC to manage resources such as user accounts or even application settings. Serverless applications are no different. Building Serverless applications with IaC tools is a common task, and we highly recommend you use IaC tools for building your Serverless applications on AWS.

Infrastructure-as-code tools

There are various IaC tools engineering teams use for building Serverless applications. AWS provides IaC tools such as AWS Serverless Application Model (SAM), and AWS Cloud Development Kit (CDK). Other IaC tools, such as Hashicorp Terraform, are provided by AWS Partners or 3rd party vendors. Selecting which tool to use depends on several factors, such as whether you want to use declarative or imperative approach, or whether your organization is already using a particular IaC methodology and you'd like to reuse the knowledge.

AWS SAM is an open-source framework for building serverless applications. SAM is a superset of AWS CloudFormation, and provides a shorthand syntax to express multiple types of serverless resources, such as functions, APIs, databases, and event source mappings. With SAM, you model your environments using YAML, which is automatically converted to CloudFormation syntax and deployed to AWS using the SAM CLI. SAM also provides local tools, allowing engineers to test their functions locally or simulate an API Gateway.

AWS CDK takes a programmatic approach. With CDK you model your environments using one of the supported programming languages, such as TypeScript, Python, or Java. Similar to SAM, the CDK CLI will automatically convert your environment definition to CloudFormation syntax and deploy it to AWS. CDK can be used together with SAM, allowing engineers to use SAM for local testing when their environments are defined with CDK.

Hashicorp Terraform is a popular IaC tool coming from an AWS Partner, widely adopted in the industry. With Terraform you use the HashiCorp Terraform Language (HCL) to write Terraform configuration files (.tf). You use those files to model your environments. Similar to CDK, Terraform can also integrate with SAM when building Serverless applications for ease of local testing, as you will see in this guide.

Throughout this guide you will learn how to use Terraform for building serverless applications. You will follow a process of gradually building and evolving serverless application architecture. You will learn how to address IAM and observability, and how to use SAM together with Terraform for testing your applications locally. Towards the end of this guide you will learn how to scale-up your Terraform templates for serverless applications by adding support for multiple environments and modularizing infrastructure and application code.


Created by:

Anton Aleksandrov
Anton AleksandrovAnton Aleksandrov is a Principal Solutions Architect for AWS Serverless and Event-Driven architectures.
Debasis Rath
Debasis RathSenior Solutions Architect, Serverless Specialist