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
Terraform Block Syntax and Structure
📖 Scenario: You are setting up a simple cloud infrastructure using Terraform. You will create a configuration file that defines a provider and a resource using proper block syntax and structure.
🎯 Goal: Build a valid Terraform configuration with a provider block for AWS and a resource block to create an AWS S3 bucket.
📋 What You'll Learn
Use correct Terraform block syntax with curly braces
Define a provider block named aws with region us-east-1
Define a resource block of type aws_s3_bucket named my_bucket
Set the bucket attribute inside the resource block to my-unique-bucket-12345
💡 Why This Matters
🌍 Real World
Terraform is widely used to define and manage cloud infrastructure as code, making deployments repeatable and consistent.
💼 Career
Understanding Terraform block syntax is essential for cloud engineers and DevOps professionals to write and maintain infrastructure code.
Progress0 / 4 steps
1
Create the provider block
Write a Terraform provider block named aws with the attribute region set to "us-east-1". Use proper block syntax with curly braces.
Terraform
Hint
Remember to open and close the block with curly braces and indent the attribute inside.
2
Add the resource block skeleton
Add a Terraform resource block of type aws_s3_bucket named my_bucket. Use proper block syntax with curly braces.
Terraform
Hint
Use the keyword resource followed by the resource type and name in quotes, then open and close curly braces.
3
Set the bucket attribute inside the resource block
Inside the resource "aws_s3_bucket" "my_bucket" block, add the attribute bucket and set it to "my-unique-bucket-12345". Use proper indentation.
Terraform
Hint
Indent the attribute inside the resource block and use an equals sign to assign the value.
4
Complete the Terraform configuration
Ensure the Terraform configuration has the provider "aws" block with region "us-east-1" and the resource "aws_s3_bucket" "my_bucket" block with the bucket attribute set to "my-unique-bucket-12345". The blocks must be properly closed with curly braces.
Terraform
Hint
Check that all blocks are properly opened and closed with curly braces and attributes are correctly set.
Practice
(1/5)
1. What is the main purpose of a block in Terraform configuration?
easy
A. To store secret values securely
B. To write comments in the code
C. To execute commands on the cloud provider
D. To group related settings and resources together
Solution
Step 1: Understand Terraform block role
Blocks in Terraform group related configuration settings and resources logically.
Step 2: Differentiate from other uses
Blocks are not for comments, commands, or secrets but for organizing configuration.
Final Answer:
To group related settings and resources together -> Option D
Quick Check:
Blocks = Group related settings [OK]
Hint: Blocks group settings inside braces {} [OK]
Common Mistakes:
Thinking blocks run commands
Confusing blocks with comments
Assuming blocks store secrets
2. Which of the following is the correct syntax to start a Terraform block named resource with label aws_instance and name web?
easy
A. resource aws_instance web {
B. resource "aws_instance" "web" {
C. resource: aws_instance: web {
D. resource(aws_instance, web) {
Solution
Step 1: Recall Terraform block syntax
Terraform blocks start with block type, then labels in quotes, then braces.
C. An AWS S3 bucket named mybucket with private access
D. A local file named mybucket with private permissions
Solution
Step 1: Identify block type and labels
The block is a resource of type aws_s3_bucket with name mybucket.
Step 2: Understand block arguments
Arguments set bucket name and access control list (acl) to private.
Final Answer:
An AWS S3 bucket named mybucket with private access -> Option C
Quick Check:
resource block creates AWS S3 bucket [OK]
Hint: resource blocks define cloud resources with settings [OK]
Common Mistakes:
Confusing resource with variable
Thinking acl is file permission
Assuming provider config instead of resource
4. Which of the following Terraform blocks has a syntax error?
medium
A. resource "aws_instance" "web"
ami = "ami-123456"
instance_type = "t2.micro"
}
B. provider "aws" {
region = "us-east-1"
}
C. variable "region" {
default = "us-west-2"
}
D. output "instance_ip" {
value = aws_instance.web.public_ip
}
Solution
Step 1: Check block opening and braces
resource "aws_instance" "web"
ami = "ami-123456"
instance_type = "t2.micro"
} misses the opening brace '{' after the block header.
Step 2: Confirm other blocks are correct
Options B, C, and D have proper braces and syntax.
Final Answer:
Missing opening brace in resource block -> Option A
Quick Check:
Blocks need opening and closing braces [OK]
Hint: Always check for matching braces after block header [OK]
Common Mistakes:
Forgetting opening or closing braces
Misplacing block labels without quotes
Incorrect indentation causing confusion
5. You want to create a Terraform configuration that defines a resource block for an AWS EC2 instance named app_server and inside it, add a nested tags block with key-value pairs. Which is the correct way to structure this?
hard
A. resource "aws_instance" "app_server" {
ami = "ami-abc123"
instance_type = "t3.micro"
tags = {
Name = "AppServer"
Environment = "Production"
}
}
B. resource "aws_instance" "app_server" {
ami = "ami-abc123"
instance_type = "t3.micro"
tags {
Name = "AppServer"
Environment = "Production"
}
}
C. resource "aws_instance" "app_server" {
ami = "ami-abc123"
instance_type = "t3.micro"
tags: {
Name = "AppServer"
Environment = "Production"
}
}
D. resource "aws_instance" "app_server" {
ami = "ami-abc123"
instance_type = "t3.micro"
tags = [
Name = "AppServer",
Environment = "Production"
]
}
Solution
Step 1: Understand tags argument type
In Terraform, tags are a map (key-value pairs) assigned with = and braces { }.