DevOps Engineering Cloud and infrastructure as code21 / 21

SECTION 06 · 30 MIN READ & EXPLORE

Read infrastructure as code with Terraform

Connect providers, resources, variables, state, plans, and outputs without hiding the infrastructure lifecycle.

The original cloud lesson ends with Terraform snippets. Read them as an illustration of the configuration model, then complete the planning exercise below.

Infrastructure as Code

2.1 Introduction to IaC

  • Definition: Managing and provisioning computer data centers through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools.

  • Declarative vs. Imperative:

  • Imperative (Scripting): “First do A, then do B, then do C.” (Focus on how).

  • Declarative (Terraform): “I want the final state to look like X.” (Focus on what).

  • Benefits: Version control (Git), reproducibility, documentation as code.

2.2 Terraform Core Concepts

  • HCL (HashiCorp Configuration Language): The syntax used to write Terraform. It is designed to be human-readable.
  • Providers: Plugins that allow Terraform to interact with cloud APIs (AWS, Azure, Docker, etc.).
  • Resources: The specific infrastructure objects you want to create (a VM, a bucket, a VPC).
  • State (terraform.tfstate): A file where Terraform keeps track of what it has created. It maps your code to the real-world resources. Crucial for tracking changes.
  • Plan vs. Apply:
  • terraform plan: A dry run. “Here is what I will do.”
  • terraform apply: Execution. “I am making the changes now.”

Example Snippets (HCL)

Here is a practical example of a main.tf file that provisions a simple server.

1. Defining the Provider

Tells Terraform which cloud we are talking to.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = "us-east-1"
}

2. Creating a Resource (Compute)

This creates a virtual machine (EC2 instance).

resource "aws_instance" "web_server" {
  ami           = "ami-0c55b159cbfafe1f0" # ID of the OS image (Ubuntu, etc.)
  instance_type = "t2.micro"              # Hardware size

  tags = {
    Name = "Student-DevOps-Instance"
    Environment = "Dev"
  }
}

3. Using Variables (Best Practice)

Avoid hardcoding values by using input variables.

variable "instance_size" {
  description = "Size of the VM"
  type        = string
  default     = "t2.micro"
}

resource "aws_instance" "web_server" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = var.instance_size   # Using the variable
}

4. Outputs

Get information back after the infrastructure is created (e.g., the IP address).

output "instance_ip_addr" {
  value = aws_instance.web_server.public_ip
  description = "The public IP address of the web server"
}
HANDS-ON LAB

Trace a proposed infrastructure change

Your mission

  • Identify each resource and its provider.
  • Explain what belongs in state and what belongs in version control.

Combine the provider, one instance resource, its variables, and its output into one configuration. The two instance snippets are alternatives; do not declare both with the same resource name. Replace historical values before attempting initialization.

Describe the sequence init → fmt → validate → plan → apply → destroy. For each step, explain whether it changes live infrastructure. If you choose to provision in your own account, inspect both creation and destruction plans and verify resources are gone afterward.

How to know it worked

  • You can explain a plan without applying it.
  • You have written a cleanup plan for any resources you choose to create.
CHECK YOUR UNDERSTANDING

Pause and predict

Does deleting a resource block immediately delete a live server?

Try your own explanation first. These notes stay on this page and are not saved or graded.

Show explanation

No. Terraform must evaluate the changed configuration against state and the provider, then apply the proposed destruction. State must remain available to manage that lifecycle.

A LITTLE CLEARER?

Take a moment to explain the idea in your own words.

Progress is saved in this browser.

DevOps Engineering

Course overview ↗

01Introduction to DevOps

  1. What is DevOps?20m
  2. DevOps Roles20m