Mastering Terraform
local graph
Mastering Terraform
Section titled “Mastering Terraform”chapter_01
Section titled “chapter_01”node-book_mastering-terraform_chapter-01-understanding-terraform-architecture — Understanding Terraform Architecture
chapter_02
Section titled “chapter_02”node-book_mastering-terraform_chapter-02-using-hashicorp-configuration-language — Using HashiCorp Configuration Language
chapter_03
Section titled “chapter_03”node-book_mastering-terraform_chapter-03-harnessing-hashicorp-utility-providers — Harnessing HashiCorp Utility Providers
chapter_04
Section titled “chapter_04”node-book_mastering-terraform_chapter-04-foundations-of-cloud-architecture-virtual-machines-and-infra — Foundations of Cloud Architecture – Virtual Machines and Infrastructure-as-a-Service
chapter_05
Section titled “chapter_05”node-book_mastering-terraform_chapter-05-beyond-vms-core-concepts-of-containers-and-kubernetes — Beyond VMs – Core Concepts of Containers and Kubernetes
chapter_06
Section titled “chapter_06”node-book_mastering-terraform_chapter-06-connecting-it-all-together-gitflow-gitops-and-ci-cd — Connecting It All Together – GitFlow, GitOps, and CI/CD
chapter_07
Section titled “chapter_07”node-book_mastering-terraform_chapter-07-getting-started-on-aws-building-solutions-with-aws-ec2 — Getting Started on AWS – Building Solutions with AWS EC2
chapter_08
Section titled “chapter_08”node-book_mastering-terraform_chapter-08-containerize-with-aws-building-solutions-with-aws-eks — Containerize with AWS – Building Solutions with AWS EKS
chapter_09
Section titled “chapter_09”node-book_mastering-terraform_chapter-09-go-serverless-with-aws-building-solutions-with-aws-lambda — Go Serverless with AWS – Building Solutions with AWS Lambda
chapter_10
Section titled “chapter_10”node-book_mastering-terraform_chapter-10-getting-started-on-azure-building-solutions-with-azure-virtu — Getting Started on Azure – Building Solutions with Azure Virtual Machines
chapter_11
Section titled “chapter_11”node-book_mastering-terraform_chapter-11-containerize-on-azure-building-solutions-with-azure-kubernet — Containerize on Azure – Building Solutions with Azure Kubernetes Service
chapter_12
Section titled “chapter_12”node-book_mastering-terraform_chapter-12-go-serverless-on-azure-building-solutions-with-azure-functio — Go Serverless on Azure – Building Solutions with Azure Functions
chapter_13
Section titled “chapter_13”node-book_mastering-terraform_chapter-13-getting-started-on-google-cloud-building-solutions-with-gce — Getting Started on Google Cloud – Building Solutions with GCE
chapter_14
Section titled “chapter_14”node-book_mastering-terraform_chapter-14-containerize-on-google-cloud-building-solutions-with-gke — Containerize on Google Cloud – Building Solutions with GKE
chapter_15
Section titled “chapter_15”node-book_mastering-terraform_chapter-15-go-serverless-on-google-cloud-building-solutions-with-google — Go Serverless on Google Cloud – Building Solutions with Google Cloud Functions
chapter_16
Section titled “chapter_16”node-book_mastering-terraform_chapter-16-already-provisioned-strategies-for-importing-existing-enviro — Already Provisioned? Strategies for Importing Existing Environments
chapter_17
Section titled “chapter_17”node-book_mastering-terraform_chapter-17-managing-production-environments-with-terraform — Managing Production Environments with Terraform
chapter_18
Section titled “chapter_18”node-book_mastering-terraform_chapter-18-looking-ahead-certification-emerging-trends-and-next-steps — Looking Ahead – Certification, Emerging Trends, and Next Steps
Preface
Section titled “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
Section titled “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
Section titled “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 models that help organizations better coordinate Infrastructure as Code at scale. Then, it delves into Day 2 operations with best practices for managing change and break-fixing when things go awry.
Chapter 18, Looking Ahead – Certification, Emerging Trends, and Next Steps, provides a practical guide to preparing for and sitting for a Terraform Certification exam. The chapter also explores emerging trends and potential next steps for those of you who want to take your mastery of Terraform to the next level.
To get the most out of this book
Section titled “To get the most out of this book”You should be comfortable using a command-line interface on either Windows or Linux, a code editor such as Visual Studio Code, and a Git source code repository such as GitHub.
While the book offers a conceptual overview of cloud computing concepts such as virtual networks, virtual machines, Docker, and Kubernetes, familiarity with these technologies will help fill in gaps and enhance your learning experience.
Additionally, a basic understanding of an imperative and object-oriented programming language such as Java, C#, or Python will be useful for the serverless chapters.
| Software/hardware covered in the book | Operating system requirements |
|---|---|
| Terraform v1.8.4 | Windows, macOS, or Linux |
| Packer v1.10.3 | Windows, macOS, or Linux |
| Kubectl v1.26.2 | Windows, macOS, or Linux |
| Helm v3.13.2 | Windows, macOS, or Linux |
| .NET 6 | Windows, macOS, or Linux |
| AWS CLI v2.15.58 | |
| Azure CLI v2.58.0 | |
| Google Cloud SDK v469.0.0 |
Download the example code files
Section titled “Download the example code files”This book provides three comprehensive end-to-end solutions, one for each major cloud platform: AWS, Azure, and Google Cloud. Each solution is accompanied by its own dedicated GitHub repository, containing all the necessary code to help you get up and running quickly with each platform:
- Amazon Web Services:
- Chapter 7 - Virtual Machine Solution: https://github.com/markti/aws-vm-demo
- Chapter 8 - Kubernetes Solution: https://github.com/markti/aws-k8s-demo
- Chapter 9 - Serverless Solution: https://github.com/markti/aws-serverless-demo
- Chapter 7 - Virtual Machine Solution: https://github.com/markti/aws-vm-demo
- Microsoft Azure:
- Chapter 10 - Virtual Machine Solution: https://github.com/markti/azure-vm-demo
- Chapter 11 - Kubernetes Solution: https://github.com/markti/azure-k8s-demo
- Chapter 12 - Serverless Solution: https://github.com/markti/azure-serverless-demo
- Chapter 10 - Virtual Machine Solution: https://github.com/markti/azure-vm-demo
- Google Cloud:
- Chapter 13 - Virtual Machine Solution: https://github.com/markti/gcp-vm-demo
- Chapter 14 - Kubernetes Solution: https://github.com/markti/gcp-k8s-demo
- Chapter 15 - Serverless Solution: https://github.com/markti/gcp-serverless-demo
- Chapter 13 - Virtual Machine Solution: https://github.com/markti/gcp-vm-demo
You can download the additional example code files for this book from GitHub at https://github.com/PacktPublishing/Mastering-Terraform. If there’s an update to the code, it will be updated in the GitHub repository.
Conventions used
Section titled “Conventions used”There are a number of text conventions used throughout this book.
Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter/X handles. Here is an example: “When you run terraform init, Terraform transforms the current working directory into the root module of the workspace.”
A block of code is set as follows:
app_settings = { "SCM_DO_BUILD_DURING_DEPLOYMENT" = "false" "WEBSITE_RUN_FROM_PACKAGE" = "1"When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
app_settings = { "SCM_DO_BUILD_DURING_DEPLOYMENT" = "false" "WEBSITE_RUN_FROM_PACKAGE" = "1"Any command-line input or output is written as follows:
terraform import 'ADDRESS["key"]' IDBold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: “The two major cloud platforms, Amazon Web Services (AWS) and Microsoft Azure, had already adopted resource typing in their respective IaC solutions.”
Tips or important notes
Appear like this.
Step 1
Section titled “Step 1”Have your purchase invoice for this book ready, as you’ll need it in Step 3. If you received a physical invoice, scan it on your phone and have it ready as either a PDF, JPG, or PNG.
For more help on finding your invoice, visit
Note
Bought this book directly from Packt? You don’t need an invoice. After completing Step 2, you can jump straight to your exclusive content.
| ## Step 2 Scan the following QR code or visit | ![]() |
|---|
Step 3
Section titled “Step 3”Sign in to your Packt account or create a new one for free. Once you’re logged in, upload your invoice. It can be in PDF, PNG, or JPG format and must be no larger than 10 MB. Follow the rest of the instructions on the screen to complete the process.
Need help?
Section titled “Need help?”If you get stuck and need help, visit for a detailed FAQ on how to find your invoices and more. The following QR code will take you to the help page directly:
Note
If you are still facing issues, reach out to .
Source:
