Bird
Raised Fist0
Terraformcloud~20 mins

Resource block syntax in Terraform - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Terraform Resource Block Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Configuration
intermediate
2:00remaining
Identify the correct resource block syntax for creating an AWS S3 bucket
Which resource block correctly defines an AWS S3 bucket named "my_bucket" with versioning enabled?
A
resource "aws_s3_bucket" "my_bucket" {
  bucket = "my_bucket"
  versioning = true
}
B
resource aws_s3_bucket my_bucket {
  bucket = "my_bucket"
  versioning = true
}
C
resource "aws_s3_bucket" "my_bucket" {
  name = "my_bucket"
  versioning = {
    enabled = true
  }
}
D
resource "aws_s3_bucket" "my_bucket" {
  bucket = "my_bucket"
  versioning {
    enabled = true
  }
}
Attempts:
2 left
💡 Hint
Remember that nested blocks like versioning require a block syntax, not just an attribute assignment.
❓ service_behavior
intermediate
2:00remaining
Effect of missing required attribute in resource block
What happens if you define an AWS EC2 instance resource block without specifying the required attribute "ami"?
Terraform
resource "aws_instance" "example" {
  instance_type = "t2.micro"
}
ATerraform will ignore the missing attribute and create the instance with instance_type only.
BTerraform will create the instance with a default AMI automatically.
CTerraform plan will fail with an error about missing required attribute "ami".
DTerraform will create the instance but it will be stopped by default.
Attempts:
2 left
💡 Hint
Think about how Terraform validates resource blocks before applying.
❓ Architecture
advanced
2:00remaining
Correct resource block to create a VPC with CIDR block and enable DNS support
Which resource block correctly creates an AWS VPC with CIDR block "10.0.0.0/16" and enables DNS support?
A
resource "aws_vpc" "main" {
  cidr = "10.0.0.0/16"
  enable_dns = true
}
B
resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
  enable_dns_support = true
}
C
resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
  dns_support = true
}
D
resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
  enable_dns_hostnames = true
}
Attempts:
2 left
💡 Hint
Check the exact attribute names required by the AWS VPC resource.
❓ security
advanced
2:00remaining
Resource block to create a security group allowing inbound HTTP traffic
Which resource block correctly creates an AWS security group allowing inbound HTTP (port 80) traffic from anywhere?
A
resource "aws_security_group" "web_sg" {
  name = "web_sg"
  ingress {
    from_port = 80
    to_port = 80
    protocol = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}
B
resource "aws_security_group" "web_sg" {
  name = "web_sg"
  ingress = [{
    from_port = 80
    to_port = 80
    protocol = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }]
}
C
resource "aws_security_group" "web_sg" {
  name = "web_sg"
  ingress {
    port = 80
    protocol = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}
D
resource "aws_security_group" "web_sg" {
  name = "web_sg"
  ingress {
    from_port = 80
    to_port = 80
    protocol = "tcp"
    cidr_block = "0.0.0.0/0"
  }
}
Attempts:
2 left
💡 Hint
Remember the correct attribute names and types for ingress rules.
✅ Best Practice
expert
2:00remaining
Correct way to reference another resource's attribute in a resource block
Given a resource "aws_vpc" "main" with attribute "id", which resource block correctly references this VPC ID when creating a subnet?
Terraform
resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "subnet1" {
  vpc_id = ???
  cidr_block = "10.0.1.0/24"
}
Avpc_id = aws_vpc.main.id
Bvpc_id = "aws_vpc.main.id"
Cvpc_id = ${aws_vpc.main.id}
Dvpc_id = $aws_vpc.main.id
Attempts:
2 left
💡 Hint
Terraform 0.12+ allows direct references without interpolation syntax.

Practice

(1/5)
1. What is the correct way to start a resource block in Terraform?
easy
A. resource "aws_instance" { example }
B. resource aws_instance example { }
C. resource "example" "aws_instance" { }
D. resource "aws_instance" "example" { }

Solution

  1. Step 1: Understand resource block syntax

    A resource block starts with the keyword resource, followed by the resource type in quotes, then the resource name in quotes.
  2. Step 2: Match the correct syntax

    resource "aws_instance" "example" { } correctly uses resource "aws_instance" "example" { }, which matches the required syntax.
  3. Final Answer:

    resource "aws_instance" "example" { } -> Option D
  4. Quick Check:

    Resource block syntax = resource "type" "name" { } [OK]
Hint: Resource block always uses resource "type" "name" [OK]
Common Mistakes:
  • Omitting quotes around type or name
  • Swapping type and name order
  • Using braces incorrectly
2. Which of the following is a valid property assignment inside a Terraform resource block?
easy
A. instance_type = t2.micro
B. instance_type => "t2.micro"
C. instance_type = "t2.micro"
D. instance_type: "t2.micro"

Solution

  1. Step 1: Identify correct property syntax

    Terraform uses = to assign values, and string values must be in quotes.
  2. Step 2: Check each option

    instance_type = "t2.micro" uses instance_type = "t2.micro", which is correct. instance_type = t2.micro misses quotes, B and C use invalid assignment symbols.
  3. Final Answer:

    instance_type = "t2.micro" -> Option C
  4. Quick Check:

    Property assignment uses = and quotes for strings [OK]
Hint: Use = and quotes for string values inside resource blocks [OK]
Common Mistakes:
  • Forgetting quotes around string values
  • Using colon or arrow instead of =
  • Missing equal sign
3. Given this resource block snippet, what will Terraform create?
resource "aws_s3_bucket" "mybucket" {
  bucket = "my-unique-bucket-123"
  acl    = "private"
}
medium
A. An S3 bucket named "mybucket" with public access
B. An S3 bucket named "my-unique-bucket-123" with private access
C. An S3 bucket named "aws_s3_bucket" with private access
D. A storage container with default public access

Solution

  1. Step 1: Identify resource type and name

    The resource type is aws_s3_bucket, and the resource name is mybucket. The bucket property sets the actual bucket name.
  2. Step 2: Understand properties

    The bucket property sets the bucket's real name to "my-unique-bucket-123". The acl is set to "private", meaning no public access.
  3. Final Answer:

    An S3 bucket named "my-unique-bucket-123" with private access -> Option B
  4. Quick Check:

    Bucket name = property value, ACL = private [OK]
Hint: Bucket name is from bucket property, not resource name [OK]
Common Mistakes:
  • Confusing resource name with actual bucket name
  • Assuming default ACL is public
  • Ignoring property values inside the block
4. Identify the error in this Terraform resource block:
resource "aws_instance" "web" {
  ami = "ami-123456"
  instance_type = t2.micro
}
medium
A. Missing quotes around the instance_type value
B. Resource block missing closing brace
C. Incorrect resource type name
D. Missing resource name

Solution

  1. Step 1: Check property value syntax

    The instance_type property value t2.micro is missing quotes, which is required for string values.
  2. Step 2: Verify block structure

    The resource block has correct braces and resource type/name, so no errors there.
  3. Final Answer:

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

    String values must be quoted [OK]
Hint: Always quote string values inside resource blocks [OK]
Common Mistakes:
  • Forgetting quotes around string properties
  • Assuming braces are optional
  • Mixing resource type and name order
5. You want to create two AWS EC2 instances with different names but the same AMI and instance type using resource blocks. Which is the best way to write this in Terraform?
hard
A.
resource "aws_instance" "web1" {
  ami = "ami-abc123"
  instance_type = "t2.micro"
}
resource "aws_instance" "web2" {
  ami = "ami-abc123"
  instance_type = "t2.micro"
}
B.
resource "aws_instance" "web" {
  count = 2
  ami = "ami-abc123"
  instance_type = "t2.micro"
}
C.
resource "aws_instance" "web" {
  ami = "ami-abc123"
  instance_type = "t2.micro"
  name = ["web1", "web2"]
}
D.
resource "aws_instance" "web" {
  ami = "ami-abc123"
  instance_type = "t2.micro"
  for_each = ["web1", "web2"]
}

Solution

  1. Step 1: Understand resource naming and duplication

    Each resource block must have a unique name.
    resource "aws_instance" "web1" {
      ami = "ami-abc123"
      instance_type = "t2.micro"
    }
    resource "aws_instance" "web2" {
      ami = "ami-abc123"
      instance_type = "t2.micro"
    }
    defines two separate resources with different names.
  2. Step 2: Evaluate other options

    resource "aws_instance" "web" {
      count = 2
      ami = "ami-abc123"
      instance_type = "t2.micro"
    }
    uses count but does not differentiate names.
    resource "aws_instance" "web" {
      ami = "ami-abc123"
      instance_type = "t2.micro"
      name = ["web1", "web2"]
    }
    tries to assign a list to name property, which is invalid.
    resource "aws_instance" "web" {
      ami = "ami-abc123"
      instance_type = "t2.micro"
      for_each = ["web1", "web2"]
    }
    uses for_each incorrectly without mapping names.
  3. Final Answer:

    Define two separate resource blocks with unique names -> Option A
  4. Quick Check:

    Unique resource names needed for multiple instances [OK]
Hint: Use separate resource blocks with unique names for multiple instances [OK]
Common Mistakes:
  • Using count without unique names
  • Assigning list to single property
  • Misusing for_each without proper keys