Terraform Cookbook
eBook - ePub

Terraform Cookbook

  1. 366 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Terraform Cookbook

About this book

Discover how to manage and scale your infrastructure using Infrastructure as Code (IaC) with Terraform

Key Features

  • Get up and running with the latest version of Terraform, v0.13
  • Design and manage infrastructure that can be shared, tested, modified, provisioned, and deployed
  • Work through practical recipes to achieve zero-downtime deployment and scale your infrastructure effectively

Book Description

HashiCorp Configuration Language (HCL) has changed how we define and provision a data center infrastructure with the launch of Terraform—one of the most popular and powerful products for building Infrastructure as Code. This practical guide will show you how to leverage HashiCorp's Terraform tool to manage a complex infrastructure with ease.Starting with recipes for setting up the environment, this book will gradually guide you in configuring, provisioning, collaborating, and building a multi-environment architecture. Unlike other books, you'll also be able to explore recipes with real-world examples to provision your Azure infrastructure with Terraform. Once you've covered topics such as Azure Template, Azure CLI, Terraform configuration, and Terragrunt, you'll delve into manual and automated testing with Terraform configurations. The next set of chapters will show you how to manage a balanced and efficient infrastructure and create reusable infrastructure with Terraform modules. Finally, you'll explore the latest DevOps trends such as continuous integration and continuous delivery (CI/CD) and zero-downtime deployments.By the end of this book, you'll have developed the skills you need to get the most value out of Terraform and manage your infrastructure effectively.

What you will learn

  • Understand how to install Terraform for local development
  • Get to grips with writing Terraform configuration for infrastructure provisioning
  • Use Terraform for advanced infrastructure use cases
  • Understand how to write and use Terraform modules
  • Discover how to use Terraform for Azure infrastructure provisioning
  • Become well-versed in testing Terraform configuration
  • Execute Terraform configuration in CI/CD pipelines
  • Explore how to use Terraform Cloud

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.

]]>

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Sharing Terraform Configuration with Modules
The real challenge for developers and software factories in recent years has been to stop writing the portions of code that are repeated between applications and even between teams. Hence the emergence of language, framework, and software packages that are easily reusable in several applications and that can be shared between several teams (such as NuGet, NPM, Bower, PyPI, RubyGems, and many others). In Infrastructure as Code (IaC) in general, we also encounter the same problems of code structure, its homogenization, and its sharing in the company.
We learned in the Provisioning infrastructure in multiple environments recipe of Chapter 2, Writing Terraform Configuration, some topologies of the structure of the Terraform configuration, that gave us a partial answer to the question of how to structure a Terraform configuration well. But that doesn't stop there—Terraform also allows you to create modules with which you can share Terraform configuration between several applications and several teams.
In this chapter, we will study the main stages of the modules, which are: the creation, use, and publishing of Terraform modules. We will learn about the creation of a Terraform module and its local use, as well as the rapid bootstrapping of the code of a module. We will also study the use of Terraform modules using the public registry or a Git repository. Finally, we will study how to test a module as well as an example of a CI/CD pipeline of a Terraform module in Azure Pipelines and GitHub Actions.
In this chapter, we cover the following recipes:
  • Creating a Terraform module and using it locally
  • Using modules from the public registry
  • Sharing a Terraform module using GitHub
  • Using another file inside a custom module
  • Using the Terraform module generator
  • Generating module documentation
  • Using a private Git repository for sharing Terraform modules
  • Applying a Terrafile pattern for using modules
  • Testing Terraform module code with Terratest
  • Building CI/CD for Terraform modules in Azure Pipelines
  • Building a workflow for Terraform modules using GitHub Actions

Technical requirements

In this chapter, for some recipes, we will need certain prerequisites, which are as follows:
  • To have Node.js and NPM installed on your computer: The download website is here: https://nodejs.org/en/.
  • To have a GitHub account: If you don't have one, the creation of the account is free and can be done here: https://github.com/.
  • To have an Azure DevOps organization: You can create one with a Live or GitHub account here: https://azure.microsoft.com/en-in/services/devops/.
  • To have a basic knowledge of Git commands and workflow: The documentation is available here: https://git-scm.com/doc.
  • To know about Docker: The documentation is here: https://docs.docker.com/.
  • To install Golang on our workstation: The documentation is here: https://golang.org/doc/install. We will see the main steps of its installation in the Testing a Terraform module using Terratest recipe.
The complete source code for this chapter is available on GitHub at https://github.com/PacktPublishing/Terraform-Cookbook/tree/master/CHAP05.
Check out the following video to see the Code in Action: https://bit.ly/3ibKgH2

Creating a Terraform module and using it locally

A Terraform module is a Terraform configuration that contains one or more Terraform resource. Once created, this module can be used in several Terraform configuration files either locally or even remotely.
In this recipe, we will look at the basics of a Terraform module with the steps involved in creating a module and using it locally.

Getting ready

To start this recipe, we will use the Terraform configuration that we have already written in the Provisioning infrastructure in multiple environments recipe in Chapter 2, Writing Terraform Configuration, and whose sources can be found at https://github.com/PacktPublishing/Terraform-Cookbook/tree/master/CHAP05/sample-app.
The module we will create in this recipe will be in charge of providing a Service Plan, one App Service, and an Application Insights resource in Azure. Its source code is available here: https://github.com/PacktPublishing/Terraform-Cookbook/tree/master/CHAP05/moduledemo/Modules/webapp. Then, we will write a Terraform configuration that uses this module and the code is here: https://github.com/PacktPublishing/Terraform-Cookbook/tree/master/CHAP05/moduledemo/MyApp.

How to do it…

To create the module, perform the following steps:
  1. In a new folder called moduledemo, create the Modules and webapp folders.
  1. In the webapp folder, create a new variables.tf file with the following code:
variable "resource_group_name" {
description = "Resource group name"
}

variable "location" {
description = "Location of Azure resource"
default = "West Europe"
}

variable "service_plan_name" {
description = "Service plan name"
}

variable "app_name" {
description = "Name of application"
}
  1. Then, create the main.tf file with the following code:
resource "azurerm_app_service_plan" "plan-app" {
name = var.service_plan_name
...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Foreword
  4. Dedication
  5. About Packt
  6. Contributors
  7. Preface
  8. Setting Up the Terraform Environment
  9. Writing Terraform Configuration
  10. Building Dynamic Environments with Terraform
  11. Using the Terraform CLI
  12. Sharing Terraform Configuration with Modules
  13. Provisioning Azure Infrastructure with Terraform
  14. Deep Diving into Terraform
  15. Using Terraform Cloud to Improve Collaboration
  16. Other Books You May Enjoy

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Terraform Cookbook by Mikael Krief,Mitchell Hashimoto in PDF and/or ePUB format, as well as other popular books in Computer Science & Software Development. We have over one million books available in our catalogue for you to explore.