Terraform Cookbook
eBook - ePub

Terraform Cookbook

Mikael Krief

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

Terraform Cookbook

Mikael Krief

Book details
Book preview
Table of contents
Citations

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.

]]>

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
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 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
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 here.
Is Terraform Cookbook an online PDF/ePUB?
Yes, you can access Terraform Cookbook by Mikael Krief 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.

Information

Year
2020
ISBN
9781800209626
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