YouTube Summaries | Complete Terraform Course - From Beginner to Pro (Parts 1 - 3)
October 28th, 2023
This summary will cover the first 3 parts of the Complete Terraform Course video seen above. As with other summaries, I like to create these to further cement the learnings from the original video or article discussed, so while this summary will hopefully be somewhat helpful for you, I always recommend that you watch the original video discussed.
TL;DW:
- Terraform automates cloud infrastructure with code.
- Basic terraform commands include
init,planapplyanddestroy. - Providers link to cloud platforms.
- State files store infrastructure status.
Planshows changes before applying.Applymakes changes.Destroyremoves resources.- Terraform Cloud offers ease but may have a cost.
- Self-managed backends, like AWS S3, offer control.
Introduction to Terraform:
- Terraform as an Infrastructure as Code (IaC) tool used to automate the provisioning and management of cloud resources.
- Terraform is declarative, which allows users to define the desired infrastructure state in code.
- Terraform supports various cloud providers, making it a versatile tool for infrastructure management.
Understanding Terraform Concepts:
- Terraform configurations are written in HashiCorp Configuration Language (HCL).
- Providers are introduced as plugins that enable Terraform to interact with various cloud platforms.
- Terraform state files store information about the managed infrastructure.
Terraform Init Command:
-
Initialization of a Working Directory: When you start a new Terraform project or work on an existing one, terraform
initis the first step. It sets up your working directory for Terraform operations. -
Downloading Providers: Terraform uses “providers” to interact with different cloud or infrastructure platforms (e.g., AWS, Azure, or VMware). During the
initprocess, it downloads the required provider plugins from the official Terraform Registry or other configured sources. -
Local Backend Initialization: If you’ve configured a local backend for your project (not recommended for collaborative or production use),
initprepares the backend settings and structure. The local backend stores your state file on your local machine. -
Remote Backend Initialization: If you’re using a remote backend (a more robust approach, suitable for teams and production environments),
initestablishes the connection to the remote storage location. This may involve configuring settings such as AWS S3, Google Cloud Storage, or Terraform Cloud, depending on the backend you’re using. -
Downloading Modules: If your Terraform project includes modules (reusable configurations),
initretrieves and installs these modules and their dependencies. -
Creating Hidden Directories:
initgenerates a.terraformdirectory in your working directory, which includes subdirectories for providers, modules, and other relevant Terraform files. These directories are used during the planning and applying phases. -
Initializing Workspace: In Terraform Cloud,
initassociates your local project with a remote workspace, allowing for collaboration, state storage, and version history.
Terraform State File:
- The Terraform state file is the representation of the infrastructure’s current state, using JSON format.
- It contains information about resources and data objects managed by Terraform.
- Securing and encrypting the state file is important, especially due to sensitive data that it stores.
Terraform Plan and Apply Commands:
- The
plancommand provides a comparison between the desired state defined in Terraform configuration and the actual state in the Terraform state. - An execution plan shows the differences between the desired and actual states.
- The
applycommand actually makes changes to the infrastructure based on the plan.
Terraform Destroy Command:
- The
destroycommand is a way to clean up and remove all resources associated with a project. destroyshould not be run for live projects but only for cleanup at the end.
Local vs. Remote Backend:
- Remote backends allow for encryption, collaboration, and automation possibilities.
- Terraform Cloud and self-managed backends (e.g., AWS with S3 and DynamoDB) are two primary remote backend options
- Bootstrapping allows the provisioning of remote backend resources with Terraform.