Bird
Raised Fist0
Terraformcloud~20 mins

Creating your first resource in Terraform - Practice Exercises

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 Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Terraform resource blocks

Which of the following Terraform resource blocks correctly creates an AWS S3 bucket named my-unique-bucket-123?

A
resource "aws_s3_bucket" "bucket" {
  name = "my-unique-bucket-123"
  acl  = "public-read"
}
B
resource "aws_s3_bucket" "bucket" {
  bucket_name = "my-unique-bucket-123"
  acl         = "private"
}
C
resource "aws_s3_bucket" "bucket" {
  bucket = "my-unique-bucket-123"
  acl    = "private"
}
D
}
"etavirp" =    lca  
"321-tekcub-euqinu-ym" = tekcub  
{ "tekcub" "tekcub_3s_swa" ecruoser
Attempts:
2 left
💡 Hint

Look for the correct attribute name to specify the bucket's name and proper string syntax.

❓ Configuration
intermediate
2:00remaining
Terraform variable usage in resource creation

Given the variable bucket_name defined as "example-bucket-456", which resource block correctly uses this variable to create an AWS S3 bucket?

Terraform
variable "bucket_name" {
  type    = string
  default = "example-bucket-456"
}
A
resource "aws_s3_bucket" "bucket" {
  bucket = var.bucket_name
  acl    = "private"
}
B
resource "aws_s3_bucket" "bucket" {
  bucket = "var.bucket_name"
  acl    = "private"
}
C
resource "aws_s3_bucket" "bucket" {
  bucket = "${var.bucket_name}"
  acl    = "private"
}
D
resource "aws_s3_bucket" "bucket" {
  bucket = $var.bucket_name
  acl    = "private"
}
Attempts:
2 left
💡 Hint

Terraform variables are referenced without quotes and without interpolation syntax in modern versions.

❓ Architecture
advanced
2:00remaining
Choosing the right resource for infrastructure setup

You want to create a virtual machine in AWS using Terraform. Which resource type should you use to create an EC2 instance?

Aaws_virtual_machine
Baws_ec2_machine
Caws_vm
Daws_instance
Attempts:
2 left
💡 Hint

Look for the official Terraform AWS provider resource name for EC2 instances.

❓ security
advanced
2:00remaining
Securing AWS S3 bucket with Terraform

Which Terraform resource block correctly creates an AWS S3 bucket with public access blocked?

A
resource "aws_s3_bucket" "bucket" {
  bucket = "secure-bucket-789"
  acl    = "private"

  public_access_block = true
}
B
resource "aws_s3_bucket" "bucket" {
  bucket = "secure-bucket-789"
  acl    = "private"

  public_access_block {
    block_public_acls       = true
    block_public_policy     = true
    ignore_public_acls      = true
    restrict_public_buckets = true
  }
}
C
resource "aws_s3_bucket" "bucket" {
  bucket = "secure-bucket-789"
  acl    = "private"

  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}
D
resource "aws_s3_bucket" "bucket" {
  bucket = "secure-bucket-789"
  acl    = "private"

  block_public_access = true
}
Attempts:
2 left
💡 Hint

Public access block settings are configured inside a nested block named public_access_block.

❓ service_behavior
expert
2:00remaining
Terraform apply behavior with resource changes

You have a Terraform configuration that creates an AWS S3 bucket named my-bucket. You change the bucket name in the configuration to my-new-bucket and run terraform apply. What will happen?

Terraform
resource "aws_s3_bucket" "bucket" {
  bucket = "my-new-bucket"
  acl    = "private"
}
ATerraform will delete the old bucket named 'my-bucket' and create a new bucket named 'my-new-bucket'.
BTerraform will rename the existing bucket from 'my-bucket' to 'my-new-bucket' without downtime.
CTerraform will update the bucket name in place without deleting the old bucket.
DTerraform will throw an error because bucket names cannot be changed.
Attempts:
2 left
💡 Hint

Consider how Terraform handles changes to resource identifiers that are immutable in the cloud provider.

Practice

(1/5)
1. What is the main purpose of a resource block in Terraform?
easy
A. To write a script to run on a server
B. To define a cloud component to create and manage
C. To store variables for reuse
D. To output values after deployment

Solution

  1. Step 1: Understand Terraform resource blocks

    A resource block tells Terraform what cloud component to create, like a server or database.
  2. Step 2: Differentiate resource from other blocks

    Variables store data, outputs show results, scripts run commands. Only resource creates infrastructure.
  3. Final Answer:

    To define a cloud component to create and manage -> Option B
  4. Quick Check:

    Resource block = cloud component definition [OK]
Hint: Resource blocks create cloud parts, not scripts or variables [OK]
Common Mistakes:
  • Confusing resource with variable or output blocks
  • Thinking resource runs code on servers
  • Assuming resource stores data only
2. Which of the following is the correct syntax to create an AWS S3 bucket resource named my_bucket in Terraform?
easy
A. resource "my_bucket" "aws_s3_bucket" {}
B. resource aws_s3_bucket my_bucket {}
C. resource "aws_s3_bucket" {}
D. resource "aws_s3_bucket" "my_bucket" {}

Solution

  1. Step 1: Recall Terraform resource syntax

    The syntax is: resource "resource_type" "resource_name" { ... } with quotes around type and name.
  2. Step 2: Match correct syntax

    resource "aws_s3_bucket" "my_bucket" {} matches this exactly with resource type aws_s3_bucket and name my_bucket in quotes.
  3. Final Answer:

    resource "aws_s3_bucket" "my_bucket" {} -> Option D
  4. Quick Check:

    Correct resource syntax uses quotes for type and name [OK]
Hint: Resource type and name must be in quotes inside resource block [OK]
Common Mistakes:
  • Omitting quotes around resource type or name
  • Swapping resource type and name order
  • Leaving resource name empty
3. Given this Terraform resource block:
resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
}

What will Terraform do when you run terraform apply?
medium
A. Output the AMI ID without creating resources
B. Delete all EC2 instances in the account
C. Create an AWS EC2 instance with the specified AMI and type
D. Create an S3 bucket named example

Solution

  1. Step 1: Identify resource type and attributes

    The resource is aws_instance named example with AMI and instance type set.
  2. Step 2: Understand Terraform apply behavior

    Terraform will create the EC2 instance with given AMI and instance type when applied.
  3. Final Answer:

    Create an AWS EC2 instance with the specified AMI and type -> Option C
  4. Quick Check:

    Resource block creates EC2 instance as defined [OK]
Hint: Resource type aws_instance creates EC2 instances [OK]
Common Mistakes:
  • Confusing resource type with S3 bucket
  • Thinking terraform apply deletes resources by default
  • Assuming terraform only outputs values
4. You wrote this Terraform resource:
resource "aws_s3_bucket" "mybucket" {
  bucket = mybucket
}

When running terraform plan, you get an error. What is the problem?
medium
A. The bucket name must be a quoted string, not a bare word
B. Resource type aws_s3_bucket does not exist
C. Resource name cannot be mybucket
D. Missing required provider block

Solution

  1. Step 1: Check bucket attribute syntax

    The bucket attribute expects a string value, so it must be in quotes like "mybucket".
  2. Step 2: Identify error cause

    Using bare word mybucket without quotes causes syntax error in Terraform.
  3. Final Answer:

    The bucket name must be a quoted string, not a bare word -> Option A
  4. Quick Check:

    String values require quotes in Terraform config [OK]
Hint: Always quote string values in resource attributes [OK]
Common Mistakes:
  • Forgetting quotes around string values
  • Assuming resource name restrictions cause error
  • Thinking provider block is missing for this error
5. You want to create two AWS EC2 instances with Terraform using the same configuration but different names. Which is the best way to do this?
hard
A. Use a resource block with a count or for_each to create multiple instances
B. Use a single resource block with a list of names in the bucket attribute
C. Create one resource block and duplicate it manually in the config file
D. Write two separate resource blocks with different names

Solution

  1. Step 1: Understand multiple resource creation methods

    You can create multiple similar resources by repeating blocks or using count/for_each.
  2. Step 2: Identify best practice

    Using count or for_each is efficient and scalable compared to manual duplication or separate blocks.
  3. Final Answer:

    Use a resource block with a count or for_each to create multiple instances -> Option A
  4. Quick Check:

    count/for_each = best for multiple similar resources [OK]
Hint: Use count or for_each for multiple similar resources [OK]
Common Mistakes:
  • Manually duplicating resource blocks causes errors
  • Trying to pass list to unrelated attribute
  • Not using count or for_each for multiple resources