Bird
Raised Fist0
Terraformcloud~10 mins

Resource types and names in Terraform - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an AWS S3 bucket resource.

Terraform
resource "aws_s3_bucket" "[1]" {
  bucket = "my-example-bucket"
  acl    = "private"
}
Drag options to blanks, or click blank then click option'
As3bucket
Bbucket_name
Cmy_bucket
Dexample
Attempts:
3 left
💡 Hint
Common Mistakes
Using the bucket name as the resource name (they are different).
Including spaces or special characters in the resource name.
2fill in blank
medium

Complete the code to declare an AWS EC2 instance resource with the name 'web_server'.

Terraform
resource "aws_instance" "[1]" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}
Drag options to blanks, or click blank then click option'
Aserver1
Binstance
Cec2_instance
Dweb_server
Attempts:
3 left
💡 Hint
Common Mistakes
Using a generic name instead of the specified one.
Confusing resource type with resource name.
3fill in blank
hard

Fix the error in the resource declaration by completing the resource name correctly.

Terraform
resource "aws_vpc" "[1]" {
  cidr_block = "10.0.0.0/16"
}
Drag options to blanks, or click blank then click option'
Amain_vpc
Bvpc-1
C10.0.0.0/16
Dvpc main
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid characters like dashes or spaces in the resource name.
Using the CIDR block as the resource name.
4fill in blank
hard

Fill both blanks to declare an AWS security group resource named 'web_sg' with description 'Allow HTTP traffic'.

Terraform
resource "aws_security_group" "[1]" {
  name        = "[2]"
  description = "Allow HTTP traffic"
  vpc_id      = "vpc-123456"
}
Drag options to blanks, or click blank then click option'
Aweb_sg
Bweb-security-group
Dhttp_sg
Attempts:
3 left
💡 Hint
Common Mistakes
Using dashes in the resource name (invalid).
Confusing resource name with the name attribute.
5fill in blank
hard

Fill all three blanks to declare an AWS Lambda function resource named 'process_data' with runtime 'python3.8' and handler 'lambda_function.lambda_handler'.

Terraform
resource "aws_lambda_function" "[1]" {
  function_name = "[2]"
  runtime       = "[3]"
  handler       = "lambda_function.lambda_handler"
  role          = "arn:aws:iam::123456789012:role/lambda_exec_role"
  filename      = "function.zip"
}
Drag options to blanks, or click blank then click option'
Aprocess_data
Cpython3.8
Dpython3.9
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for resource and function_name.
Using wrong runtime version.

Practice

(1/5)
1. What does the resource block in Terraform define?
easy
A. A provider configuration
B. A variable to store user input
C. A cloud service to create and manage
D. A function to run code

Solution

  1. Step 1: Understand Terraform resource block purpose

    The resource block tells Terraform what cloud service to create and manage.
  2. Step 2: Differentiate from other blocks

    Variables store input, functions run code, and providers configure cloud access, but resources define actual services.
  3. Final Answer:

    A cloud service to create and manage -> Option C
  4. Quick Check:

    Resource block = cloud service definition [OK]
Hint: Resource blocks create services, not variables or functions [OK]
Common Mistakes:
  • Confusing resource with variable or provider blocks
  • Thinking resource defines code logic
  • Mixing resource with output blocks
2. Which of the following is the correct syntax to declare an AWS S3 bucket resource named my_bucket in Terraform?
easy
A. resource aws_s3_bucket "my_bucket" {}
B. resource aws_s3_bucket my_bucket {}
C. resource "aws_s3_bucket" my_bucket {}
D. resource "aws_s3_bucket" "my_bucket" {}

Solution

  1. Step 1: Recall Terraform resource syntax

    Terraform resource syntax requires resource type and name as strings inside quotes: resource "type" "name" {}
  2. Step 2: Match correct option

    Only resource "aws_s3_bucket" "my_bucket" {} uses quotes correctly around both type and name.
  3. Final Answer:

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

    Resource type and name must be quoted [OK]
Hint: Always quote resource type and name in Terraform [OK]
Common Mistakes:
  • Omitting quotes around type or name
  • Using quotes inconsistently
  • Swapping order of type and name
3. Given this Terraform code snippet:
resource "aws_instance" "web" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
}

What is the resource type and name?
medium
A. Type: aws_instance, Name: web
B. Type: web, Name: aws_instance
C. Type: ami-123456, Name: t2.micro
D. Type: aws_instance, Name: ami-123456

Solution

  1. Step 1: Identify resource type and name in block

    The first quoted string after resource is the type: "aws_instance". The second is the name: "web".
  2. Step 2: Confirm values are correct

    AMI and instance_type are attributes, not resource type or name.
  3. Final Answer:

    Type: aws_instance, Name: web -> Option A
  4. Quick Check:

    Resource type and name = aws_instance, web [OK]
Hint: First quoted string = type, second = name [OK]
Common Mistakes:
  • Confusing attributes with resource type or name
  • Swapping type and name
  • Using attribute values as names
4. What is wrong with this Terraform resource declaration?
resource "aws_s3_bucket" my_bucket {
  bucket = "my-bucket-name"
}
medium
A. Resource name is not quoted
B. Resource type is missing
C. Bucket attribute is invalid
D. Missing provider block

Solution

  1. Step 1: Check resource declaration syntax

    Terraform requires both resource type and name to be quoted strings.
  2. Step 2: Identify the error

    The resource name my_bucket is not quoted, causing a syntax error.
  3. Final Answer:

    Resource name is not quoted -> Option A
  4. Quick Check:

    Resource names must be quoted strings [OK]
Hint: Always quote resource names to avoid syntax errors [OK]
Common Mistakes:
  • Forgetting quotes around resource names
  • Assuming attributes cause errors
  • Thinking provider block is mandatory here
5. You want to create two AWS EC2 instances with different names but the same type in Terraform. Which is the best way to name the resources?
hard
A. resource "aws_instance" "aws_instance" {} and resource "aws_instance" "aws_instance" {}
B. resource "aws_instance" "app1" {} and resource "aws_instance" "app2" {}
C. resource "aws_instance" "1" {} and resource "aws_instance" "2" {}
D. resource "aws_instance" "instance" {} and resource "aws_instance" "instance" {}

Solution

  1. Step 1: Understand resource naming rules

    Resource names must be unique within the same type to avoid conflicts.
  2. Step 2: Evaluate naming options

    resource "aws_instance" "app1" {} and resource "aws_instance" "app2" {} uses unique, descriptive names app1 and app2. Choices that reuse the same name cause errors. The choice using numeric names is valid but less descriptive.
  3. Final Answer:

    resource "aws_instance" "app1" {} and resource "aws_instance" "app2" {} -> Option B
  4. Quick Check:

    Unique descriptive names prevent conflicts [OK]
Hint: Use unique, clear names for each resource [OK]
Common Mistakes:
  • Using duplicate resource names
  • Using unclear or numeric-only names
  • Ignoring naming uniqueness rules