Preface
Infrastructure as Code has emerged as the de facto method for provisioning and maintaining cloud infrastructure. While this approach has been around for a while, it has matured and refined over the years. Initially, it followed largely imperative programming models and employed agents to facilitate change. However, with the advent of API-based cloud services, it has transformed into a primarily declarative configuration of desired states that are translated into cloud services and their configuration-of which Terraform is the gold standard.
Terraform is crucial for both application development teams, who create applications and services that delight end users, and platform teams, who empower enterprises and organizations with smooth and efficient operations. As a result, Terraform has become the preferred way for these teams to build and manage the environments that support their goals.
This book, Mastering Terraform, recognizes that to truly master Terraform, one must first have a deep understanding of the cloud services and architectures that will be automated through the use of Terraform and Infrastructure as Code. Throughout this book, the focus will be on solving real-world problems-whether by building new environments or managing existing ones-using Terraform.
We live in a multi-cloud world. That's why each of the largest hyperscale clouds-AWS, Azure, and Google Cloud-will receive equal treatment within this book. No matter which of these clouds you plan on working with-one, two, or all of them-this book will treat you as a first-class citizen.
Another reality embraced in this book is that Terraform is not the only tool in the tool shed. Practitioners often need to integrate multiple tools to accomplish their goals. Therefore, we'll explore three different cloud computing paradigms: virtual machines, containers, and serverless. Each has its own design characteristics, deployment mechanics, and toolchain dynamics that you need to be aware of when working with them.
Who this book is for
This book is intended for cloud, DevOps, platform, and infrastructure engineers, SREs, developers, and cloud architects who want to use Terraform to automate cloud infrastructures and streamline software delivery. You will benefit from having a basic understanding of Infrastructure as Code (such as Terraform, Ansible, and AWS CDK), cloud architecture, development tools, and platforms. This foundation will help you elevate your skills through the concepts and practices covered in the book.
What this book covers
Chapter 1, Understanding Terraform Architecture, provides an in-depth look at the core tenants of Terraform's architecture, focusing on Terraform state, modularity, the command-line tool, and the configuration language that make up what we know as Terraform.
Chapter 2, Using HashiCorp Configuration Language, provides an in-depth look at the critical language structures used by Terraform's functional language, HashiCorp Configuration Language (HCL). It shares best practices and real-world use cases for common scenarios.
Chapter 3, Harnessing HashiCorp Utility Providers, provides an in-depth look at the utility providers that extend Terraform core and your cloud provider of choice. It shares the best practices and common use cases to help you streamline common cloud-agnostic operations no matter what cloud platform you're using.
Chapter 4, Foundations of Cloud Architecture - Virtual Machines and Infrastructure-as-a-Service, provides an overview of core concepts needed to start designing and building Infrastructure as Code solutions using Terraform. These critical concepts transcend cloud platforms, which will help prepare you to automate this category of solution no matter what your cloud of choice.
Chapter 5, Beyond VMs - Core Concepts of Containers and Kubernetes, provides an overview of core concepts needed to start designing and building Infrastructure as Code solutions that integrate with Docker and Kubernetes-a scenario that is becoming more and more popular these days. The chapter explores Docker and Kubernetes integration strategies using both native client tools and relevant Terraform providers for Kubernetes and Helm.
Chapter 6, Connecting It All Together - GitFlow, GitOps, and CI/CD, provides an overview of software development processes using GitFlow and the impact of this approach on Infrastructure as Code operations. Finally, we explore using GitHub Actions to implement CI/CD pipelines for virtual machines, Kubernetes, and serverless workloads.
Chapter 7, Getting Started on AWS - Building Solutions with AWS EC2, provides an end-to-end solution developed for AWS using virtual machines powered by EC2. The chapter explores the usage of Packer to automate OS-level configuration, Terraform to provision the infrastructure and deploy the workload, and finally, GitHub Actions to automate the entire process.
Chapter 8, Containerize with AWS - Building Solutions with AWS EKS, provides an end-to-end solution developed for AWS using Kubernetes powered by EKS. The chapter explores the usage of Docker to automate OS-level configuration, Terraform to provision the infrastructure and deploy the workload, and finally, GitHub Actions to automate the entire process.
Chapter 9, Go Serverless with AWS - Building Solutions with AWS Lambda, provides an end-to-end serverless solution developed for AWS Lambda. The chapter explores the necessary application code changes to conform to AWS Lambda's framework, Terraform to provision the infrastructure and deploy the workload, and finally, GitHub Actions to automate the entire process.
Chapter 10, Getting Started on Azure - Building Solutions with Azure Virtual Machines, provides an end-to-end solution developed using Azure Virtual Machines. The chapter explores the usage of Packer to automate OS-level configuration, Terraform to provision the infrastructure and deploy the workload, and finally, GitHub Actions to automate the entire process.
Chapter 11, Containerize on Azure - Building Solutions with Azure Kubernetes Service, provides an end-to-end solution developed for Azure using Kubernetes powered by AKS. The chapter explores the usage of Docker to automate OS-level configuration, Terraform to provision the infrastructure and deploy the workload, and finally, GitHub Actions to automate the entire process.
Chapter 12, Go Serverless on Azure - Building Solutions with Azure Functions, provides an end-to-end serverless solution developed for Azure Functions. The chapter explores the necessary application code changes to conform to the Azure Functions framework, Terraform to provision the infrastructure and deploy the workload, and finally, GitHub Actions to automate the entire process.
Chapter 13, Getting Started on Google Cloud - Building Solutions with GCE, provides an end-to-end solution developed for GCP using virtual machines powered by GCE. The chapter explores the usage of Packer to automate OS-level configuration, Terraform to provision the infrastructure and deploy the workload, and finally, GitHub Actions to automate the entire process.
Chapter 14, Containerize on Google Cloud - Building Solutions with GKE, provides an end-to-end solution developed for GCP using Kubernetes powered by GKE. The chapter explores the usage of Docker to automate OS-level configuration, Terraform to provision the infrastructure and deploy the workload, and finally, GitHub Actions to automate the entire process.
Chapter 15, Go Serverless on Google Cloud - Building Solutions with Google Cloud Functions, provides an end-to-end serverless solution developed for Google Cloud Functions. The chapter explores the necessary application code changes to conform to the Google Cloud Functions framework, Terraform to provision the infrastructure and deploy the workload, and finally, GitHub Actions to automate the entire process.
Chapter 16, Already Provisioned? Strategies for Importing Existing Environments, provides an in-depth look at different approaches for bringing existing resources and environments under Terraform management. The chapter explores the usage of built-in import capabilities as well as strategies using third-party tools and provides real-world recommendations for when and how to use them and what the trade-offs are to employing these techniques.
Chapter 17, Managing Production Environments with Terraform, provides in-depth guidance for how to manage long-lived environments using Infrastructure as Code with Terraform. The chapter explores different real-world operating...