Get startedGet started for free

Terraform Overview

1. Terraform Overview

You now know the basics of infrastructure as code, but what exactly is Terraform? Terraform is an infrastructure as code tool created by HashiCorp, and is currently licensed under the HashiCorp Business Source License v1.1. It lets you provision Google Cloud resources with declarative configuration files. Examples of resources include virtual machines, containers, storage, and networks. Terraform allows infrastructure to be expressed as code in a simple, human-readable language called HashiCorp Configuration Language, or HCL. Terraform reads configuration files and provides an execution plan of changes, which can be reviewed, applied, and provisioned. At a high level, Terraform lets operators author files containing resource definitions on the Google Cloud provider, and automates the creation of those resources. Terraform features include: Multi-cloud and multi-API support. Terraform supports all major cloud providers, in addition to Google Cloud and API-exposed services such as GitHub and Kubernetes. Three different editions that range from self-hosted to fully managed, including enterprise support. A large community, including a registry with publicly available modules for Google Cloud deployments. And infrastructure provisioning, rather than configuration. You can use Terraform for Google Cloud to provision resources, meaning you can use resource blocks to define infrastructure elements such as VMs, networks, and firewalls. You can create explicit dependencies between resources, so that a given resource can only be created after the creation of another resource. You can standardize how a given resource is created by creating reusable modules. And you can limit the values provided for a given resource argument using validation rules. Next let’s view the standard IaC configuration workflow. Before jumping into the Terraform workflow, you must determine what resources should be created for a project. For example, for a common 2-tier architecture, you need a pool of web servers that use a database tier. Therefore during the “scope” phase, you scope the requirements for the Google Cloud resources and plan how they should be connected. In this case scope the type of compute and database instances needed. The Terraform workflow begins with the author phase, where you author the configuration code for the infrastructure you want to create. Referring to our example, during this phase, you code the configuration of the instances and the VPC network. You then organize your code in configuration files, such as variables, main, and tfvars. The next phase is initialize, where any plugins or modules are installed. During this phase, you run the terraform init command, which initializes the Terraform configuration directory and installs Google Cloud as the provider. In the plan phase, you run the terraform plan command. The terraform plan command provides an execution plan for the resources created, changed, or destroyed as per the configuration defined in the author phase. You can then review the plan before applying it to your Google Cloud infrastructure. After you review the configuration described in the generated plan, apply it to create the infrastructure and a state file. Later in the course you will explore the Terraform workflow in more detail. Now let’s examine common Terraform use cases. Terraform is used to manage infrastructure. It takes an immutable approach, meaning you write code to reduce the complexity involved in upgrading or modifying the services and infrastructure. Terraform is also used to track infrastructure changes. Whenever a new change is planned or applied, you will be prompted to approve the change before Terraform modifies the state of the infrastructure. When an infrastructure is created using Terraform, a state file is automatically generated. State files reflect the current state of your infrastructure, and provide the amount and type of Google Cloud resources modified in your configuration. Terraform is used to automate changes. Because configuration files are declarative in nature, you do not have to write detailed instructions to build the infrastructure; you only define the end state. Terraform manages the dependencies and provisions the resources. It’s also used to standardize configurations. You can use modules to save time and implement best practices. You can also leverage publicly available modules from the Terraform Registry. Terraform can also be used to automate the enforcement of policies on the types of resources teams can provision and use.

2. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.