Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

Terraform Cookbook: Provision, run, and scale cloud architecture with real-world examples using Terraform
Terraform Cookbook: Provision, run, and scale cloud architecture with real-world examples using Terraform
Terraform Cookbook: Provision, run, and scale cloud architecture with real-world examples using Terraform
Ebook1,444 pages6 hours

Terraform Cookbook: Provision, run, and scale cloud architecture with real-world examples using Terraform

Rating: 0 out of 5 stars

()

Read preview

About this ebook

HashiCorp Configuration Language (HCL) has changed how we define and provision data center infrastructure with the launch of Terraform, a top-tier product for building Infrastructure as Code (IaC). Terraform Cookbook shows you how to leverage Terraform to manage complex infrastructure with ease.

This new edition has been updated to include real-world examples for provisioning Azure, AWS and GCP infrastructure with Terraform. You'll delve into manual and automated testing with Terraform configurations, creating and managing a balanced, efficient, and reusable infrastructure with Terraform modules. You'll learn how to automate the deployment of Terraform configuration with continuous integration and continuous delivery (CI/CD).
Besides that, several new chapters have been added that describe the use of Terraform for Docker and Kubernetes, examine advanced topics on GitOps practices, and explain how to test Terraform configurations using different tools to check code and security compliance. The final chapter covers troubleshooting common Terraform issues and provides solutions for frequently encountered errors.

By the end of this book, you'll have developed the skills needed to get the most value out of Terraform and to effectively manage your infrastructure.

LanguageEnglish
Release dateAug 31, 2023
ISBN9781804619636
Terraform Cookbook: Provision, run, and scale cloud architecture with real-world examples using Terraform

Read more from Mikael Krief

Related to Terraform Cookbook

Related ebooks

System Administration For You

View More

Related articles

Reviews for Terraform Cookbook

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Terraform Cookbook - Mikael Krief

    cover.png

    Terraform Cookbook

    Second Edition

    Provision, run, and scale cloud architecture with real-world examples using Terraform

    Mikael Krief

    BIRMINGHAM—MUMBAI

    Terraform Cookbook

    Second Edition

    Copyright © 2023 Packt Publishing

    All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

    Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

    Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

    Senior Publishing Product Manager: Rahul Nair

    Acquisition Editor – Peer Reviews: Saby Dsilva

    Project Editor: Parvathy Nair

    Content Development Editors: Georgia Daisy van der Post, Matthew Davies

    Copy Editor: Safis Editing

    Technical Editor: Kushal Sharma

    Proofreader: Safis Editing

    Indexer: Rekha Nair

    Presentation Designer: Ganesh Bhadwalkar

    Developer Relations Marketing Executive: Priyadarshini Sharma

    First published: October 2020

    Second edition: August 2023

    Production reference: 3261223

    Published by Packt Publishing Ltd.

    Grosvenor House

    11 St Paul’s Square

    Birmingham

    B3 1RB, UK.

    ISBN 978-1-80461-642-0

    www.packt.com

    I would like to dedicate this book to my wife and children, who are my source of happiness.

    – Mikael Krief

    Foreword

    It was 2013 when Mitchell and I first conceived of Terraform, 10 years before the publication of the second edition of the Terraform Cookbook. At the time, we felt that the growing complexity of cloud environments required a radically different approach to management, and that Infrastructure as Code would play a critical enabling role. Since then, Terraform has grown to have a massive ecosystem of thousands of integrations, tens of thousands of modules, hundreds of thousands of users, and more than one billion downloads.

    When we first built Terraform, we were focused on the workflow and how we wanted it to be used. It was important that this be consistent and simple, regardless of what types of resource we were managing, whether public clouds, private clouds, network devices, or SaaS services. Given the complexity of modern cloud environments, we had to provide confidence to end users and ensure they were never surprised. We also knew there was an almost infinite surface area of integration, so it had to be easy to create plugins to extend Terraform.

    Terraform today delivers on all those goals. There are multiple ways to author Terraform, whether with HashiCorp Configuration Language (HCL), JavaScript Object Notation (JSON), or through programming languages such as TypeScript or Python using the Terraform CDK. Terraform is easily extensible through providers, which enables thousands of integrations across low-level hardware, cloud services, and SaaS. The rich ability to plan changes provides operators with the confidence they need around changes, which is why it’s used by thousands of organizations to manage their production environments.

    I was excited when Mikael Krief wrote the first version of the Terraform Cookbook to provide a practical guide for new users to learn the tool and apply it in a number of real-world situations. With the second edition, Mikael is providing an important refresh that covers many of the updates to the core Terraform product, which has evolved rapidly over the last few years. He also brings in many of the best practices that have evolved as the community has spent more time figuring out how to manage infrastructure at scale.

    This book starts with a very gentle introduction, including how to download and set up Terraform, and is perfect for users who are just getting started. From there, it introduces the basics of authoring Terraform code and using key features of Terraform. These lessons are brought together through more complex examples that present real-world use cases to help readers go from the basics of the tool to the practical usage of it.

    For new users just getting started with Terraform or Infrastructure as Code, this book will provide a valuable way to get started quickly. For users who haven’t used Terraform in a few years, this will provide an updated view of the new features and patterns that have emerged in the last few years.

    I hope you enjoy the book!

    Armon Dadgar CTO and Co-Founder, HashiCorp

    Contributors

    About the author

    Mikael Krief is a DevOps engineer who lives in France. He believes that Infrastructure as Code is a fundamental practice in DevOps culture. He is therefore interested in HashiCorp products and specializes in the use of Terraform. Mikael loves to share his passion through various communities, such as the HashiCorp User Groups. Over the years, he has contributed to many public projects, written various blog posts, published several books, and spoken at leading conferences. For his contributions and passion, he has been nominated and selected as a HashiCorp Ambassador since 2019, and he has been awarded the Microsoft Most Valuable Professional (MVP) award for 8 years.

    I would like to extend my thanks to my family for accepting that I needed to work long hours on this book during family time. I would like to thank Meeta Rajani for giving me the opportunity to write this second edition, which was a very enriching experience. Special thanks to Parvathy Nair, Georgia Daisy van der Post, and Radek Simko for their valuable input and time reviewing this book and to the entire Packt team for their support during the course of writing this book.

    About the reviewer

    Jack Lee is a Microsoft MVP and an Azure Certified Solutions Architect with a passion for software development, cloud, and DevOps innovations. He has earned multiple certifications including Microsoft Azure Solutions Architect Expert and Microsoft DevOps Engineer Expert. Jack has authored numerous books published by Packt, notably Azure for Architects, Azure Strategy and Implementation Guide, and Cloud Analytics with Microsoft Azure.

    Jack is an active Microsoft tech community contributor and has presented at various user groups and conferences, such as the Global Azure Bootcamp at Microsoft Canada. He is an experienced mentor and judge at hackathons. He is also the president of a user group that focuses on Azure, DevOps, and software development. You can follow Jack on Twitter at

    @jlee_consulting

    .

    Learn more on Discord

    To join the Discord community for this book – where you can share feedback, ask questions to the author, and learn about new releases – follow the QR code below:

    https://packt.link/cloudanddevops

    Contents

    Preface

    Who this book is for

    What this book covers

    To get the most out of this book

    Get in touch

    Setting Up the Terraform Environment

    Technical requirements

    Overviewing Terraform best practices

    Downloading and installing Terraform on Windows manually

    Getting ready

    How to do it…

    How it works…

    Installing Terraform using Chocolatey on Windows

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Installing Terraform on Linux using the APT package manager

    Getting ready

    How to do it…

    How it works…

    See also

    Installing Terraform using a script on Linux

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Executing Terraform in a Docker container

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Switching between multiple Terraform versions

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Upgrading Terraform providers

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Writing Terraform Configurations

    Technical requirements

    Configuring Terraform and the provider version to use

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Adding alias to a provider to use multiple instances of the same provider

    Getting ready

    How to do it…

    How it works…

    See also

    Manipulating variables

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Keeping sensitive variables safe

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using local variables for custom functions

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using outputs to expose Terraform provisioned data

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Calling Terraform’s built-in functions

    Getting ready

    How to do it…

    How it works…

    See also

    Using YAML files in Terraform configuration

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Writing conditional expressions

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Generating passwords with Terraform

    Getting ready

    How to do it…

    How it works…

    See also

    Managing Terraform resource dependencies

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Adding custom pre and postconditions

    Getting ready

    How to do it…

    How it works…

    See also

    Using checks for infrastructure validation

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Scaling Your Infrastructure with Terraform

    Technical requirements

    Provisioning infrastructure in multiple environments

    Getting ready

    How to do it…

    How it works…

    See also

    Provisioning multiple resources with the count meta-argument

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using maps

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Looping over a map of objects

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Generating multiple blocks with the dynamic block

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Filtering maps

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using Terraform with External Data

    Technical requirements

    Obtaining external data with data sources

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Querying external data with Terraform

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Manipulating local files with Terraform

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Executing local programs with Terraform

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Executing shell scripts using the Terraform Shell provider

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Managing Terraform State

    Using the local Terraform state

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Managing resources in Terraform state

    Getting ready

    How to do it…

    Displaying the content of state

    Listing Terraform resource names within state

    Showing detailed resource properties in state

    Deleting resources from state

    There’s more…

    See also

    Synchronizing Terraform state

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Importing existing resources

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using external resources from other Terraform state files

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Refactoring resources in configuration

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Applying a Basic Terraform Workflow

    Technical requirements

    Keeping your Terraform configuration clean

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Validating the code syntax

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Destroying infrastructure resources

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Displaying a list of providers used in a configuration

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Generating one Terraform lock file with Windows and Linux compatibility

    Getting ready

    How to do it…

    How it works…

    See also

    Copying a Terraform module configuration

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using workspaces to manage environments

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Exporting the output in JSON

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Tainting resources

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Generating the dependency graph

    Getting ready

    How to do it…

    How it works…

    See also

    Using different Terraform configuration directories

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Testing and evaluating a Terraform expression

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Debugging the Terraform execution

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Sharing Terraform Configuration with Modules

    Technical requirements

    Creating a Terraform module and using it locally

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Provisioning multiple instances of a Terraform module

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using modules from the public registry

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Sharing a Terraform module in the public registry using GitHub

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using another file inside a custom module

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using the Terraform module generator

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Generating module documentation

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using a private Git repository for sharing a Terraform module

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Applying a Terrafile pattern for using modules

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Provisioning Azure Infrastructure with Terraform

    Technical requirements

    Using Terraform in Azure Cloud Shell

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Protecting the Azure credential provider

    Getting ready

    How to do it…

    How it works…

    There’s more...

    See also

    Protecting the state file in the Azure remote backend

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Executing ARM templates in Terraform

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Executing Azure CLI commands in Terraform

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using Azure Key Vault with Terraform to protect secrets

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Provisioning and configuring an Azure VM with Terraform

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Building Azure serverless infrastructure with Terraform

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Generating a Terraform configuration for existing Azure infrastructure

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Enabling optional Azure features

    Getting ready

    How to do it…

    How it works…

    There’s more…

    Estimating Azure cost of infrastructure using Infracost

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using the AzApi Terraform provider

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Getting Starting to Provisioning AWS and GCP Infrastructure Using Terraform

    Technical requirements

    Getting started using Terraform for AWS

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using the S3 backend in AWS

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Getting started using Terraform for GCP

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using a GCS backend in GCP

    Getting ready

    How to do it…

    How it works…

    See also

    Executing Terraform in GCP Cloud Shell

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using Terraform for Docker and Kubernetes Deployment

    Technical requirements

    Creating a Docker container using Terraform

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Deploying Kubernetes resources using Terraform

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Deploying a Helm chart in Kubernetes using Terraform

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using a Kubernetes controller as a Terraform reconciliation loop

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Running Test and Compliance Security on Terraform Configuration

    Technical requirements

    Using PowerShell’s Pester framework to perform Terraform testing

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Testing the Terraform configuration using Python

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using OPA to check the Terraform configuration

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using tfsec to analyze the compliance of Terraform configuration

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Applying Terraform compliance using terraform-compliance

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Testing Terraform module code with Terratest

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Testing the Terraform configuration using Kitchen-Terraform

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using the new integrated Terraform module integration test

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Deep-Diving into Terraform

    Technical requirements

    Preventing resources from being destroyed

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Ignoring manual changes

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using Terraform’s templating feature

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Zero-downtime deployment with Terraform

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Managing Terraform configuration dependencies using Terragrunt

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using Terragrunt as a wrapper for Terraform

    Getting ready

    How to do it…

    How it works…

    See also

    Generating a self-signed SSL certificate using Terraform

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Checking the configuration before committing code using Git hooks

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Visualizing Terraform resource dependencies with Rover

    Getting ready

    How to do it…

    How it works…

    See also

    Using the Terraform CDK for developers

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Automating Terraform Execution in a CI/CD Pipeline

    Running Terraform in automation mode

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Displaying a summary of the execution of terraform plan

    Getting ready

    How to do it…

    There’s more…

    See also

    Building CI/CD pipelines to apply Terraform configurations in Azure Pipelines

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Automating Terraform execution in GitHub Actions

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Working with workspaces in CI/CD

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Building CI/CD for Terraform modules in Azure Pipelines

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Building a workflow for publishing Terraform modules using GitHub Actions

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using Terraform Cloud to Improve Team Collaboration

    Technical requirements

    Authenticating Terraform to Terraform Cloud

    Getting ready

    How to do it…

    There’s more…

    See also

    Managing workspaces in Terraform Cloud

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Using the remote backend in Terraform Cloud

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Migrating Terraform State to Terraform Cloud

    Getting ready

    How to do it…

    How it works…

    See also

    Using Terraform Cloud as a private module registry

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Executing Terraform configuration remotely in Terraform Cloud

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Checking the compliance of Terraform configurations using OPA in Terraform Cloud

    Getting ready

    How to do it…

    There’s more…

    See also

    Using integrated cost estimation for cloud resources

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Integrating the Infracost run task during the Terraform Cloud run

    Getting ready

    How to do it…

    How it works…

    There’s more…

    See also

    Configuring Terraform Cloud with the Terraform TFE provider

    Getting ready

    How to do it…

    There’s more…

    See also

    Troubleshooting Terraform Errors

    Fixing interpolation errors

    Getting ready

    How to do it…

    How it works…

    See also

    Fixing cycle errors

    Getting ready

    How to do it…

    How it works…

    There’s more…

    Fixing for_each errors

    Getting ready

    How to do it…

    There’s more…

    Fixing output errors

    Getting ready

    How to do it…

    How it works…

    There’s more…

    Appendix A: Terraform Cheat Sheet

    Basic commands

    Format Terraform configuration

    Terraform providers management

    Terraform dependency file

    Basic workflow commands

    Backend configuration

    Validate configuration

    Get outputs

    Import resources

    Terraform workspaces

    Terraform debug

    State management

    Display Terraform graph dependencies

    Taint/untaint resources

    Terraform Cloud/Enterprise

    Appendix B: Terraform Resources

    Terraform official resources

    Documentation

    Registry

    Providers development

    Terraform community resources

    Terraform news feed

    Terraform certifications and certification preparation

    Terraform certification program pages

    Terraform certification preparation

    Other Books You May Enjoy

    Index

    Landmarks

    Cover

    Index

    Preface

    Infrastructure as Code, more commonly known as IaC, is a practice that is a pillar of DevOps culture. IaC entails writing your desired architecture configuration in code. Among other advantages, IaC allows the automation of infrastructure deployments, which reduces or eliminates the need for manual intervention, and thus the risk of configuration errors, and the need to create templates and standardize infrastructure with modular and scalable code.

    Among all the DevOps tools, there are many that allow IaC. One of them is Terraform, from HashiCorp, which is very popular today because, in addition to being open source and multi-platform, it has the following advantages:

    It allows you to preview the changes that will be applied to your infrastructure.

    It allows the parallelization of operations, considering the management of dependencies.

    It has a multitude of providers.

    In this book dedicated to Terraform, we will first discuss the installation of Terraform, the writing of Terraform configurations, how to apply the Terraform workflow using the command-line interface (CLI), and how use Terraform modules.

    Once configuration writing and commands in Terraform are understood, we will discuss Terraform’s practical use for building infrastructure with the three leading cloud providers: Azure, AWS, and GCP. We will also explore how to use Terraform for Kubernetes in a chapter dedicated to this topic.

    Finally, we will finish this book by looking at advanced uses of Terraform, including Terraform testing, integrating Terraform into a continuous integration/continuous deployment (CI/CD) pipeline, and using Terraform Cloud, which is Terraform’s collaboration platform for teams and companies.

    This book will guide you through several recipes on best practices for writing Terraform configurations and commands, and it will also cover recipes on Terraform’s integration with other tools such as Terragrunt, kitchen-terraform, Tfsec, and Azure Pipelines.

    Most of the Terraform configurations described in this book are based on the Azure provider, for illustration, but you can apply these recipes to all other Terraform providers.

    In this second edition, the chapters have been completely redesigned, with over 50 new recipes and two brand new chapters: one on using Terraform with AWS and GCP, and another on Terraform and Kubernetes.

    In writing this cookbook, I wanted to share my experience of real and practical Terraform-based scenarios that I have encountered while working with customers and companies over the years.

    Who this book is for

    This book is for developers, operators, and DevOps engineers looking to improve their workflow and use Infrastructure as Code. Experience with Microsoft Azure, Jenkins, shell scripting, and DevOps practices is required to get the most out of this Terraform book.

    What this book covers

    Chapter 1, Setting Up the Terraform Environment, details the different ways of installing Terraform manually, with scripts, or by using a Docker container, and it also details the Terraform migration configuration process.

    Chapter 2, Writing Terraform Configurations, concerns the writing of Terraform configurations for a provider, variables, outputs, built-in functions, condition expressions, YAML file manipulation, and pre-and post-conditions.

    Chapter 3, Scaling Your Infrastructure with Terraform, shows you how to build dynamic environments by going further with Terraform configuration writing using loops, maps, and collections.

    Chapter 4, Using Terraform with External Data, explores how to use Terraform with external data and local files, and how to execute local programs and scripts with Terraform.

    Chapter 5, Managing Terraform State, explains Terraform state management, including reading, moving, deleting, and importing resources into the Terraform state.

    Chapter 6, Applying a Basic Terraform Workflow, explains the use of Terraform’s CLI to validate the configuration, use outputs, destroy resources provisioned by Terraform, use workspaces, generate dependency graphs, and debug the execution of Terraform.

    Chapter 7, Sharing Terraform Configuration with Modules, covers the creation, use, and sharing of Terraform modules, and shows testing module practices.

    Chapter 8, Provisioning Azure Infrastructure with Terraform, illustrates the use of Terraform in a practical scenario with the cloud service provider Azure. It covers topics such as authentication, remote backends, ARM templates, Azure CLI execution, and Terraform configuration generation for an existing infrastructure.

    Chapter 9, Getting Starting to Provisioning AWS and GCP Infrastructure Using Terraform, provides a starting point for provisioning AWS and GCP infrastructure using Terraform, and includes details on these providers, authentication, and remote backend storage.

    Chapter 10, Using Terraform for Docker and Kubernetes Deployment, explains how to use Terraform to create Docker containers and deploy Kubernetes resources.

    Chapter 11, Running Test and Compliance Security on Terraform Configuration, details Terraform configuration testing practices using several tools, including Tfsec, OPA,

    terraform-compliance

    , and Pester.

    Chapter 12, Deep-Diving into Terraform, discusses topics that go further with Terraform, such as the execution of Terraform configuration tests, zero-downtime deployment, Terraform wrappers with Terragrunt, checking configuration using Git-Hook, and using the Terraform CDK as a developer.

    Chapter 13, Automating Terraform Execution in a CI/CD Pipeline, explores local Terraform automation processes and implementing a CI/CD pipeline to apply Terraform configuration automatically.

    Chapter 14, Using Terraform Cloud to Improve Team Collaboration, explains how to use Terraform Cloud to run Terraform in a team with the sharing of Terraform modules in a private registry, the use of remote backends for Terraform state, migrating Terraform state, running Terraform remotely, and integrating cost estimation.

    Chapter 15, Troubleshooting Terraform Errors, lists several Terraform errors and explains how to resolve them.

    The Appendix A and B, contains a Terraform CLI cheat sheet and Terraform resources list.

    To get the most out of this book

    The following is the list of software/hardware prerequisites for this book:

    Download the example code files

    The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/Terraform-Cookbook-Second-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

    Download the color images

    We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/P7a3G.

    Conventions used

    There are a number of text conventions used throughout this book.

    CodeInText

    : Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. For example: "Execute the

    terraform graph

    command:"

    A block of code is set as follows:

    resource

    azurerm_resource_group

    "

    rg-app"

    { name =

    RG-APP-${terraform.workspace}

    location =

    westeurope

    When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

    terraform { backend

    azurerm

    { resource_group_name =

    RG-TFBACKEND

    storage_account_name =

    storagetfbackend

    container_name =

    tfstate

    key =

    myapp.tfstate

    access_key = xxxxxx-xxxxx-xxx-xxxxx

    } }

    Any command-line input or output is written as follows:

    terraform init

    Bold: Indicates a new term, an important word, or words that you see on the screen. For instance, words in menus or dialog boxes appear in the text like this. For example: "Select System info from the Administration panel."

    Warnings or important notes appear like this.

    Tips and tricks appear like this.

    Get in touch

    Feedback from our readers is always welcome.

    General feedback: Email

    [email protected]

    and mention the book’s title in the subject of your message. If you have questions about any aspect of this book, please email us at

    [email protected]

    .

    Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you reported this to us. Please visit http://www.packtpub.com/submit-errata, click Submit Errata, and fill in the form.

    Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at

    [email protected]

    with a link to the material.

    If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit http://authors.packtpub.com.

    Share your thoughts

    Once you’ve read Terraform Cookbook, Second Edition, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

    Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

    Download a free PDF copy of this book

    Thanks for purchasing this book!

    Do you like to read on the go but are unable to carry your print books everywhere?

    Is your eBook purchase not compatible with the device of your choice?

    Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

    Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.

    The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily

    Follow these simple steps to get the benefits:

    Scan the QR code or visit the link below

    https://packt.link/free-ebook/9781804616420

    Submit your proof of purchase

    That’s it! We’ll send your free PDF and other benefits to your email directly

    1

    Setting Up the Terraform Environment

    Before you start writing the Terraform configuration, it’s necessary to understand the best practices to write configuration for Infrastructure as Code (IaC). Then, we can install and configure a local development environment. This development environment will allow us to write the Terraform configurations file and apply changes with Terraform.

    In the recipes in this chapter, we will start to learn some of the most important IaC and Terraform best practices, then we will learn how to download and install Terraform manually on a Windows machine, as well as how to install it using a script on Windows and Linux. We will also learn how to use Terraform in a Docker container before learning how to upgrade Terraform providers.

    In this chapter, we’ll cover the following recipes:

    Overviewing Terraform best practices

    Downloading and installing Terraform on Windows manually

    Installing Terraform using Chocolatey on Windows

    Installing Terraform on Linux using the APT package manager

    Installing Terraform using a script on Linux

    Executing Terraform in a Docker container

    Writing Terraform configuration in Visual Studio Code

    Switching between multiple Terraform versions

    Upgrading Terraform providers

    Let’s get started!

    Technical requirements

    This chapter does not require that you have any specific technical knowledge. We will mainly use graphical user interfaces (GUIs) and simple Linux or Windows scripts executed in a terminal console. However, knowledge of Docker is recommended so that you can complete the Executing Terraform in a Docker container recipe.

    Finally, for the Integrated Development Environment (IDE), which is the software we use to write the Terraform configuration, we will use Visual Studio Code, which is available for free at https://code.visualstudio.com/.

    The source code for this chapter is available at https://github.com/PacktPublishing/Terraform-Cookbook-Second-Edition/tree/main/CHAP01.

    Overviewing Terraform best practices

    Before starting to learn how to install Terraform through the recipes presented in this chapter, it’s necessary to understand the most common best practices of IaC with Terraform.

    The first group of best practices is about IaC and are best practices for development in general:

    Store all Terraform configuration files and source code using a version control system such as GitHub, Azure DevOps, or Bitbucket.

    When the code is in Git, apply all good Git practices using branches, tags, commits, comments, and pull requests.

    Decouple your code file with multiple components; having a big monolithic code structure will make maintenance and deployment more difficult.

    Modularize and share common code for better reusability. For example, do not repeat the same code (as per the Don’t Repeat Yourself (DRY) principle) and share the same business logic between components. We will learn about Terraform module implementation in Chapter 7, Sharing Terraform Configuration with Modules.

    Automate your infrastructure changes using the CI/CD pipeline that we explore in detail in Chapter 13, Automating Terraform Execution in a CI/CD Pipeline.

    Then, we have best practices that are specific to Terraform:

    Write the required version of the Terraform binary explicitly in Terraform configuration and list all required providers with their required versions.

    Use remote backends to store and share the Terraform state file. We will learn how to do this in several recipes in Chapter 8, Provisioning Azure Infrastructure with Terraform, and in Chapter 9, Getting Started with Provisioning AWS and GCP Infrastructure Using Terraform.

    Don’t use hard-coded values in configuration; use variables. We will learn more about this in Chapter 2, Writing Terraform Configurations.

    Add a description property in variables

    Enjoying the preview?
    Page 1 of 1