This is a collection of the Terraform tutorials provided by HashiCorp for the three main cloud service providers: AWS, Azure, and GCP (along with a simple Docker container example). This link goes to the associated documentation - please reference the "Get Started" section to access the tutorials. Please refer to the tutorials to learn more about setting variables, outputs, and hooking up a remote backend for state management via Terraform Cloud.
Terraform is an infrastructure as code (IaC) tool that uses a standardized workflow for provisioning resources across a variety of third-party platforms. The workflow is defined below:
- Infrastructure is defined in Terraform configuration (~.tf) files that uses a declarative configuration language
- Project is initialized with relevant resources and plugins (providers) downloaded
- Before deploying changes, an execution plan is made that describes all resources that will be created/modified/deleted
- A resource graph is created and creates/modifies/deletes non-dependent resources in parallel when the execution plan is executed
In order to create an execution plan, Terraform is dependent on the project's state - this is the actual infrastructure that currently exists. When creating an execution plan, the Terraform core engine essentially creates a diff between the existing infrastructure (if it exists) and the newly declared resources. By default, this project state is stored locally in the project directory - Terraform Cloud allows for using a remote state instead to ensure consistency for collaboration. Please refer to the HashiCorp tutorials to learn more.
- Provider: Plugins that allow Terraform with cloud platforms and other services via APIs. These are listed in the Terraform Registry - in each project, one needs a provider to actual perform an action. E.g., to deploy AWS resources, one must use the AWS Provider.
- Terraform Configuration: set of files used to describe the infrastructure in Terraform Language.
The infrastructure configuration is defined in Terraform files that all end in .tf
. The infra is then defined in the Terraform Language. Some key components are defined within the configuration - each represented as blocks analogous to JSON:
terraform {}
: Contains the Terraform settings, including the required providers to provision infrastructureprovider "{provider_name}" {}
: Configures a specified provider - multiple provider blocks can be included in a configurationresource "{resource_type}" "{resource_name}" {}
: Defines the individual components of the infrastructure, declaring a unique resource_type and resource_name combination that can be used to reference the resource within the configuration.
In each project, there will be certain files that are relevant. A description is provided of each:
{name}.tf
: A Terraform configuration file - this defines the desired intrastructure..terraform
: Directory that contains downloaded providers' files and modules.terraform.lock.hcl
: This file records the provider versions to ensure consistency in project creation and sharingterraform.tfstate
: This file contain's the local project's state - this is used for tracking the generated resources and creation of execution plansterraform.tfstate.lock.info
: A temporary file created during applying/deleting a configuration.terraform.tfvars
: Essentially an env file - loads contained variables when running Terraform operations. Can place private information and omit with .gitignore.
It should be noted that all files that end in .tf
are automatically loaded when running terraform plan/apply
.
This assumes that the appropriate credentials and Terraform CLI have been downloaded locally. The following steps are used for creating a basic project:
- Define a Terraform configuration in a (or many)
.tf
file terraform init
to download providers and setup stateterraform validate
to check for syntax and/or logical errorsterraform apply
to view the execution plan and approve it in order to deploy infrastrucutreterraform destroy
to delete the infrastructure components at end of lifecycle
Other useful commands:
terraform show
: Display the current stateterraform state list
: Lists all the resources in the project's stateterraform graph
: Visualize the execute plan to show dependencies