Terraform Video - Learn Terraform for GCP & Learn Terraform for AWS

Terraform.video

At Terraform.video, our mission is to provide a comprehensive resource for developers and IT professionals to learn about and master the art of declarative deployment using Terraform and cloud technologies. We strive to offer high-quality, up-to-date content that is accessible to beginners and advanced users alike. Our goal is to empower our community to build scalable, resilient, and secure infrastructure with ease and confidence.

Video Introduction Course Tutorial

Terraform Cheatsheet

This cheatsheet is a reference guide for anyone getting started with Terraform, a tool for declarative deployment using cloud infrastructure. The topics covered in this cheatsheet are related to the concepts, topics, and categories on the website terraform.video.

Table of Contents

Introduction to Terraform

Terraform is an open-source tool for building, changing, and versioning infrastructure safely and efficiently. It allows you to define your infrastructure as code, which can be versioned, reviewed, and shared among team members. Terraform supports a wide range of cloud providers, including AWS, Azure, Google Cloud, and more.

Terraform Basics

Resources

Resources are the building blocks of your infrastructure in Terraform. They represent the various components of your infrastructure, such as virtual machines, databases, and load balancers. Each resource is defined using a resource block, which specifies the resource type, name, and configuration.

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

Variables

Variables allow you to parameterize your Terraform configuration, making it more flexible and reusable. You can define variables in a variables block, and then reference them in your resource blocks using the ${var.variable_name} syntax.

variable "region" {
  default = "us-west-2"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  region        = "${var.region}"
}

Outputs

Outputs allow you to export values from your Terraform configuration, which can be useful for sharing information with other parts of your infrastructure or external systems. You can define outputs in an output block, and then reference them using the ${output.output_name} syntax.

output "public_ip" {
  value = "${aws_instance.example.public_ip}"
}

Providers

Providers are the plugins that Terraform uses to interact with various cloud providers. Each provider has its own set of resources and configuration options. You can configure a provider in a provider block, and then reference it in your resource blocks using the provider attribute.

provider "aws" {
  region = "us-west-2"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  provider      = "aws"
}

Modules

Modules allow you to organize your Terraform configuration into reusable components, which can be shared among different projects or teams. A module is defined using a module block, which specifies the module source and any input variables.

module "example" {
  source = "github.com/example/module"
  region = "us-west-2"
}

State

Terraform uses a state file to keep track of the current state of your infrastructure. This file contains information about the resources that Terraform has created, as well as any dependencies between them. You can view the current state of your infrastructure using the terraform state command.

Terraform Configuration Language

The Terraform configuration language is used to define your infrastructure as code. It is a declarative language that allows you to specify the desired state of your infrastructure, rather than the steps required to achieve that state.

Variables

Variables allow you to parameterize your Terraform configuration, making it more flexible and reusable. You can define variables in a variables block, and then reference them in your resource blocks using the ${var.variable_name} syntax.

variable "region" {
  default = "us-west-2"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  region        = "${var.region}"
}

Conditionals

Conditionals allow you to control the behavior of your Terraform configuration based on certain conditions. You can use the if and for_each keywords to define conditionals.

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  count = var.create_instance ? 1 : 0
}

Loops

Loops allow you to repeat a block of code multiple times, based on a certain condition. You can use the for_each keyword to define loops.

variable "instances" {
  default = {
    "example-1" = "t2.micro"
    "example-2" = "t2.small"
  }
}

resource "aws_instance" "example" {
  for_each = var.instances

  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = each.value
  tags = {
    Name = each.key
  }
}

Functions

Functions allow you to manipulate data within your Terraform configuration. Terraform provides a wide range of built-in functions, such as string manipulation, math operations, and data formatting.

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  tags = {
    Name = format("example-%02d", count.index + 1)
  }
}

Terraform Providers

Terraform providers are the plugins that Terraform uses to interact with various cloud providers. Each provider has its own set of resources and configuration options.

AWS Provider

The AWS provider allows you to manage resources in Amazon Web Services (AWS), such as EC2 instances, S3 buckets, and RDS databases.

provider "aws" {
  region = "us-west-2"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  provider      = "aws"
}

Azure Provider

The Azure provider allows you to manage resources in Microsoft Azure, such as virtual machines, storage accounts, and SQL databases.

provider "azurerm" {
  features {}
}

resource "azurerm_virtual_machine" "example" {
  name                  = "example-vm"
  location              = "westus2"
  resource_group_name   = "example-rg"
  network_interface_ids = [azurerm_network_interface.example.id]

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"
    version   = "latest"
  }

  storage_os_disk {
    name              = "example-osdisk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }

  os_profile {
    computer_name  = "example-vm"
    admin_username = "adminuser"
    admin_password = "Password1234!"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }
}

Google Cloud Provider

The Google Cloud provider allows you to manage resources in Google Cloud Platform (GCP), such as virtual machines, storage buckets, and SQL databases.

provider "google" {
  project = "example-project"
  region  = "us-west1"
}

resource "google_compute_instance" "example" {
  name         = "example-instance"
  machine_type = "n1-standard-1"
  zone         = "us-west1-a"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }

  network_interface {
    network = "default"
  }

  metadata = {
    ssh-keys = "adminuser:${file("~/.ssh/id_rsa.pub")}"
  }
}

Terraform Modules

Terraform modules allow you to organize your Terraform configuration into reusable components, which can be shared among different projects or teams.

Module Sources

Module sources can be local or remote. Local modules are stored on your local filesystem, while remote modules are stored in a version control system or module registry.

module "example" {
  source = "./example-module"
}
module "example" {
  source = "github.com/example/example-module"
}

Module Inputs

Module inputs allow you to pass data into a module, which can be used to customize its behavior. You can define input variables in a module block, and then reference them in your module using the ${var.variable_name} syntax.

module "example" {
  source = "github.com/example/example-module"

  region = "us-west-2"
}

Module Outputs

Module outputs allow you to export values from a module, which can be used by other parts of your infrastructure or external systems. You can define output variables in a module block, and then reference them using the ${module.module_name.output_name} syntax.

module "example" {
  source = "github.com/example/example-module"

  region = "us-west-2"
}

output "example_output" {
  value = "${module.example.example_output}"
}

Terraform State

Terraform state is used to keep track of the current state of your infrastructure. This file contains information about the resources that Terraform has created, as well as any dependencies between them.

State File

The state file is a JSON file that contains information about the resources that Terraform has created. It is stored locally on your filesystem by default, but can also be stored remotely in a backend.

State Locking

State locking is used to prevent multiple users or processes from modifying the state file at the same time. Terraform supports several backend types that provide state locking, such as S3, Consul, and Azure Storage.

State Commands

Terraform provides several commands for working with state, such as terraform state list, terraform state show, and terraform state pull.

Terraform Commands

Terraform provides a wide range of commands for building, changing, and versioning infrastructure. Some of the most commonly used commands are:

Terraform Best Practices

Here are some best practices to follow when working with Terraform:

Common Terms, Definitions and Jargon

1. Terraform: An open-source tool used for building, changing, and versioning infrastructure safely and efficiently.
2. Declarative Deployment: A deployment approach that focuses on describing the desired state of the infrastructure rather than the steps to get there.
3. Cloud Infrastructure: A collection of computing resources, including servers, storage, and networking, that are available on-demand through the internet.
4. Infrastructure as Code (IaC): The practice of managing infrastructure using code, allowing for automation, version control, and collaboration.
5. Provider: A plugin for Terraform that allows it to interact with a specific cloud provider, such as AWS or Azure.
6. Resource: A component of infrastructure that Terraform manages, such as a virtual machine or a database.
7. Module: A reusable set of Terraform resources that can be shared across projects and teams.
8. State: The current configuration of infrastructure managed by Terraform, stored in a state file.
9. Plan: A preview of the changes that Terraform will make to the infrastructure based on the desired state.
10. Apply: The command used to apply changes to the infrastructure based on the desired state.
11. Destroy: The command used to remove infrastructure managed by Terraform.
12. Variables: Parameters that can be passed to Terraform to customize the configuration of infrastructure.
13. Outputs: Values that Terraform can return after applying changes to the infrastructure, such as IP addresses or DNS names.
14. Remote State: A feature of Terraform that allows the state file to be stored remotely, enabling collaboration and version control.
15. Terraform Cloud: A SaaS offering from HashiCorp that provides remote state storage, collaboration, and automation features for Terraform.
16. Terraform Enterprise: An on-premises version of Terraform Cloud with additional enterprise features.
17. HCL (HashiCorp Configuration Language): The language used to write Terraform configuration files.
18. AWS (Amazon Web Services): A cloud provider offering a wide range of computing, storage, and networking services.
19. Azure: Microsoft's cloud platform, offering similar services to AWS.
20. GCP (Google Cloud Platform): Google's cloud platform, offering similar services to AWS and Azure.

Editor Recommended Sites

AI and Tech News
Best Online AI Courses
Classic Writing Analysis
Tears of the Kingdom Roleplay
Ethereum Exchange: Ethereum based layer-2 network protocols for Exchanges. Decentralized exchanges supporting ETH
Learn Cloud SQL: Learn to use cloud SQL tools by AWS and GCP
Tech Debt - Steps to avoiding tech debt & tech debt reduction best practice: Learn about technical debt and best practice to avoid it
Dev Traceability: Trace data, errors, lineage and content flow across microservices and service oriented architecture apps
Kids Games: Online kids dev games