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
Code Review for Infrastructure Changes with Terraform
📖 Scenario: You are part of a cloud team managing infrastructure using Terraform. Before applying any changes, you need to review the Terraform configuration files carefully to ensure they follow best practices and meet the project requirements.This project will guide you through creating a simple Terraform configuration, adding variables for flexibility, writing the main resource block, and finally adding output to review the infrastructure state.
🎯 Goal: Build a Terraform configuration step-by-step that defines an AWS S3 bucket with a variable for the bucket name, includes a resource block to create the bucket, and outputs the bucket ARN for review.
📋 What You'll Learn
Create a Terraform variable for the S3 bucket name
Define an AWS S3 bucket resource using the variable
Add an output block to display the bucket ARN
Use valid Terraform syntax and best practices
💡 Why This Matters
🌍 Real World
Terraform is widely used to manage cloud infrastructure as code. Reviewing configuration files carefully before applying changes helps prevent mistakes and ensures infrastructure is created as intended.
💼 Career
Cloud engineers and DevOps professionals regularly write and review Terraform code to manage infrastructure safely and efficiently. This project builds foundational skills for those roles.
Progress0 / 4 steps
1
Create a Terraform variable for the S3 bucket name
Write a Terraform variable block named bucket_name with type string and default value "my-unique-bucket-12345".
Terraform
Hint
Use the variable block with type and default attributes.
2
Add a provider configuration for AWS
Add a Terraform provider block for aws with region set to us-east-1.
Terraform
Hint
The provider block specifies which cloud provider and region Terraform will use.
3
Define an AWS S3 bucket resource using the variable
Create a Terraform resource block named aws_s3_bucket with resource name my_bucket. Set the bucket attribute to use the variable bucket_name.
Terraform
Hint
Use resource "aws_s3_bucket" "my_bucket" and set bucket = var.bucket_name.
4
Add an output block to display the bucket ARN
Add a Terraform output block named bucket_arn that outputs the ARN of the S3 bucket resource aws_s3_bucket.my_bucket.arn.
Terraform
Hint
The output block helps you see important information after Terraform applies changes.
Practice
(1/5)
1. What is the main purpose of running terraform plan before applying changes?
easy
A. To apply the changes directly to the cloud resources
B. To preview the changes Terraform will make to the infrastructure
C. To delete all existing infrastructure
D. To create a backup of the current infrastructure state
Solution
Step 1: Understand the role of terraform plan
This command shows what changes Terraform will perform without making any actual changes.
Step 2: Differentiate from other commands
terraform apply makes changes, while terraform plan previews them safely.
Final Answer:
To preview the changes Terraform will make to the infrastructure -> Option B
Quick Check:
Preview changes = terraform plan [OK]
Hint: Remember: plan previews, apply executes changes [OK]
Common Mistakes:
Confusing plan with apply
Thinking plan deletes resources
Assuming plan creates backups
2. Which of the following is the correct syntax to initialize a Terraform working directory?
easy
A. terraform init
B. terraform start
C. terraform setup
D. terraform configure
Solution
Step 1: Identify the initialization command
terraform init sets up the working directory by downloading providers and preparing backend.
Step 2: Verify other options
Commands like terraform start, terraform setup, and terraform configure do not exist in Terraform CLI.
Final Answer:
terraform init -> Option A
Quick Check:
Initialize = terraform init [OK]
Hint: Init means start setup in Terraform [OK]
Common Mistakes:
Using non-existent commands
Confusing init with apply
Assuming configure is a Terraform command
3. Given this Terraform snippet:
resource "aws_instance" "example" {
ami = "ami-123456"
instance_type = "t2.micro"
}
output "instance_id" {
value = aws_instance.example.id
}
What will terraform apply output after successful deployment?
medium
A. The ID of the created AWS instance
B. The AMI ID used in the instance
C. The instance type string
D. An error because output is missing
Solution
Step 1: Understand the output block
The output named instance_id returns the ID of the created AWS instance resource.
Step 2: Confirm output value
The value is set to aws_instance.example.id, which is the unique instance ID assigned by AWS.
Final Answer:
The ID of the created AWS instance -> Option A
Quick Check:
Output shows instance ID = The ID of the created AWS instance [OK]
Hint: Output shows resource attributes, not input values [OK]
Common Mistakes:
Confusing output value with input AMI
Expecting instance type as output
Thinking output block is missing or invalid
4. You see this Terraform code snippet in a pull request: