35,99 €
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.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 570
Veröffentlichungsjahr: 2023
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: 1250823
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
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
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.
Jack Lee is a Microsoft MVP and an Azure Certified Solutions Architect with a passion for software development, the cloud, and DevOps innovation. He is an active Microsoft Tech Community contributor and has presented at various user groups and conferences, including the Global Azure Bootcamp at Microsoft Canada. Jack is an experienced mentor and judge at hackathons and is also the president of a user group that focuses on Azure, DevOps, and software development. He is the co-author of Azure for Architects, Azure Strategy and Implementation Guide, and Cloud Analytics with Microsoft Azure from Packt Publishing. You can follow Jack on Twitter at @jlee_consulting.
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
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
Cover
Index
