Bird
Raised Fist0
Terraformcloud~5 mins

What is Infrastructure as Code in Terraform - CLI Guide

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Infrastructure as Code lets you create and manage computer resources using files instead of clicking buttons. This helps you set up servers, networks, and storage automatically and repeatably.
When you want to create a new server without manually logging into a cloud console.
When you need to set up the same environment multiple times for testing or production.
When you want to keep track of your infrastructure changes like you do with code.
When you want to avoid mistakes from manual setup by automating the process.
When you want to share your infrastructure setup with teammates easily.
Config File - main.tf
main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
  required_version = ">= 1.0"
}

provider "aws" {
  region = "us-east-1"
}

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

  tags = {
    Name = "example-instance"
  }
}

This file tells Terraform to use the AWS provider in the us-east-1 region. It creates one small server (t2.micro) using a common Amazon Machine Image (AMI). The tags section names the server for easy identification.

Commands
This command sets up Terraform in your folder by downloading the AWS provider plugin. It prepares Terraform to work with your configuration.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "~> 4.0"... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes required for your infrastructure.
This command shows what Terraform will do before making any changes. It helps you check your setup and avoid mistakes.
Terminal
terraform plan
Expected OutputExpected
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_instance.example will be created + resource "aws_instance" "example" { + ami = "ami-0c55b159cbfafe1f0" + instance_type = "t2.micro" + tags = { + "Name" = "example-instance" } } Plan: 1 to add, 0 to change, 0 to destroy. ───────────────────────────────────────────────────────────────────────────── Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
This command creates the server as described in the file. The -auto-approve flag skips the confirmation step to speed up the process.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Creation complete after 15s [id=i-0abcdef1234567890] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
→
-auto-approve - Skip manual approval to apply changes immediately
This command displays the current state of your infrastructure as Terraform sees it. It confirms the server was created.
Terminal
terraform show
Expected OutputExpected
aws_instance.example: id = i-0abcdef1234567890 ami = ami-0c55b159cbfafe1f0 instance_type = t2.micro tags = { Name = example-instance }
Key Concept

If you remember nothing else from this pattern, remember: Infrastructure as Code lets you create and manage your servers and resources using files so you can automate and repeat your setups safely.

Common Mistakes
Not running 'terraform init' before other commands
Terraform needs to download provider plugins first; without init, commands fail.
Always run 'terraform init' once before planning or applying changes.
Applying changes without reviewing the plan
You might create or destroy resources unintentionally.
Run 'terraform plan' first to see what will happen before 'terraform apply'.
Using incorrect or outdated AMI IDs
The server creation will fail if the AMI ID does not exist in the chosen region.
Use a valid AMI ID for your AWS region; check AWS console or documentation.
Summary
Write a Terraform file to describe your infrastructure resources.
Run 'terraform init' to prepare Terraform with needed plugins.
Use 'terraform plan' to preview changes before applying.
Apply changes with 'terraform apply' to create or update resources.
Check the current infrastructure state with 'terraform show'.

Practice

(1/5)
1. What does Infrastructure as Code mean in simple terms?
easy
A. Using physical servers instead of cloud
B. Manually clicking buttons in the cloud console
C. Writing text files to create and manage cloud resources
D. Writing code only for applications, not infrastructure

Solution

  1. Step 1: Understand the concept of Infrastructure as Code

    It means using code or text files to define and manage infrastructure instead of manual steps.
  2. Step 2: Compare options with this definition

    Only Writing text files to create and manage cloud resources describes writing text files to manage cloud resources, matching the concept.
  3. Final Answer:

    Writing text files to create and manage cloud resources -> Option C
  4. Quick Check:

    Infrastructure as Code = writing text files [OK]
Hint: Think: code controls infrastructure, not manual clicks [OK]
Common Mistakes:
  • Confusing manual setup with code-based setup
  • Thinking Infrastructure as Code means only coding apps
  • Assuming physical servers are Infrastructure as Code
2. Which of these is a correct Terraform resource block syntax?
easy
A. resource "aws_instance" "web" { ami = "ami-123" instance_type = "t2.micro" }
B. resource aws_instance web { ami = ami-123 instance_type = t2.micro }
C. resource "aws_instance" web { ami = "ami-123" instance_type = "t2.micro" }
D. resource "aws_instance" "web" ( ami = "ami-123", instance_type = "t2.micro" )

Solution

  1. Step 1: Recall Terraform resource block syntax

    It requires the keyword resource, resource type and name in quotes, then braces with key-value pairs.
  2. Step 2: Check each option for correct syntax

    resource "aws_instance" "web" { ami = "ami-123" instance_type = "t2.micro" } uses quotes for type and name, braces, and key-value pairs with equals and quotes correctly.
  3. Final Answer:

    resource "aws_instance" "web" { ami = "ami-123" instance_type = "t2.micro" } -> Option A
  4. Quick Check:

    Correct Terraform resource syntax = resource "aws_instance" "web" { ami = "ami-123" instance_type = "t2.micro" } [OK]
Hint: Look for quotes around type and name, and braces {} [OK]
Common Mistakes:
  • Missing quotes around resource type or name
  • Using parentheses instead of braces
  • Not quoting string values
3. Given this Terraform snippet:
resource "aws_s3_bucket" "bucket" {
  bucket = "my-bucket"
  acl    = "private"
}

What will Terraform do when applied?
medium
A. Create a public S3 bucket named 'my-bucket'
B. Throw a syntax error due to missing region
C. Delete the S3 bucket named 'my-bucket'
D. Create a private S3 bucket named 'my-bucket'

Solution

  1. Step 1: Analyze the resource block

    The resource defines an AWS S3 bucket named 'my-bucket' with ACL set to 'private'.
  2. Step 2: Understand the effect of ACL 'private'

    This means the bucket will be created with private access, not public.
  3. Final Answer:

    Create a private S3 bucket named 'my-bucket' -> Option D
  4. Quick Check:

    ACL 'private' means private bucket [OK]
Hint: ACL 'private' means bucket is not public [OK]
Common Mistakes:
  • Assuming default is public
  • Thinking missing region causes syntax error
  • Confusing create with delete actions
4. This Terraform code has an error:
resource "aws_instance" "web" {
  ami = "ami-123"
  instance_type = t2.micro
}

What is the error?
medium
A. Missing resource name
B. Missing quotes around instance_type value
C. Wrong resource type
D. Missing equals sign after ami

Solution

  1. Step 1: Check the instance_type line

    The value t2.micro is not in quotes, but it should be a string.
  2. Step 2: Confirm correct syntax for string values

    Terraform requires string values to be in quotes, so missing quotes cause an error.
  3. Final Answer:

    Missing quotes around instance_type value -> Option B
  4. Quick Check:

    String values need quotes in Terraform [OK]
Hint: String values always need quotes in Terraform [OK]
Common Mistakes:
  • Forgetting quotes around string values
  • Thinking resource name is missing
  • Assuming equals sign is missing
5. You want to create two identical AWS EC2 instances using Terraform without repeating code. Which feature helps you do this efficiently?
hard
A. Using a for_each or count loop in the resource block
B. Manually copying the resource block twice
C. Writing separate Terraform files for each instance
D. Using different resource types for each instance

Solution

  1. Step 1: Understand the goal to avoid repetition

    We want to create multiple similar resources without copying code.
  2. Step 2: Identify Terraform features for repetition

    Terraform supports 'count' and 'for_each' to create multiple instances from one block.
  3. Step 3: Evaluate options

    Using a for_each or count loop in the resource block uses these features, while others involve manual repetition or incorrect methods.
  4. Final Answer:

    Using a for_each or count loop in the resource block -> Option A
  5. Quick Check:

    Loops in Terraform = efficient resource creation [OK]
Hint: Use count or for_each to repeat resources without copy-paste [OK]
Common Mistakes:
  • Copy-pasting resource blocks manually
  • Creating separate files unnecessarily
  • Using different resource types for same resource