0
0
Terraformcloud~10 mins

Why data sources query existing infrastructure in Terraform - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to query an existing AWS VPC using a data source.

Terraform
data "aws_vpc" "example" {
  id = [1]
}
Drag options to blanks, or click blank then click option'
A"subnet-12345678"
B"my-vpc"
C"vpc-12345678"
D"instance-12345678"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a subnet or instance ID instead of a VPC ID.
Using the VPC name instead of the ID.
2fill in blank
medium

Complete the code to get the CIDR block of an existing AWS VPC using a data source.

Terraform
output "vpc_cidr" {
  value = data.aws_vpc.example.[1]
}
Drag options to blanks, or click blank then click option'
Acidr_block
Binstance_type
Csubnet_ids
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'id' instead of 'cidr_block'.
Using unrelated attributes like 'instance_type'.
3fill in blank
hard

Fix the error in the data source block to correctly query an existing AWS subnet by its ID.

Terraform
data "aws_subnet" "example" {
  [1] = "subnet-87654321"
}
Drag options to blanks, or click blank then click option'
Aid
Bvpc_id
Ccidr_block
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' instead of 'id'.
Using 'vpc_id' which is not the subnet's ID.
4fill in blank
hard

Fill both blanks to query an existing AWS security group by its name and output its ID.

Terraform
data "aws_security_group" "example" {
  [1] = "my-security-group"
}

output "sg_id" {
  value = data.aws_security_group.example.[2]
}
Drag options to blanks, or click blank then click option'
Aname
Bid
Cdescription
Dvpc_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'description' or 'vpc_id' instead of 'name' or 'id'.
Mixing up the attributes for querying and output.
5fill in blank
hard

Fill all three blanks to query an existing AWS EC2 instance by its ID, get its subnet ID, and output the subnet ID.

Terraform
data "aws_instance" "example" {
  [1] = "i-0abcdef1234567890"
}

output "instance_subnet" {
  value = data.aws_instance.example.[2]
}

resource "aws_security_group_rule" "allow_http" {
  type              = "ingress"
  from_port         = 80
  to_port           = 80
  protocol          = "tcp"
  security_group_id = data.aws_instance.example.[3][0]
  cidr_blocks       = ["0.0.0.0/0"]
}
Drag options to blanks, or click blank then click option'
Aid
Bsubnet_id
Cvpc_security_group_ids
Dinstance_type
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'instance_type' instead of 'id' for querying.
Using 'id' instead of 'subnet_id' for subnet output.
Using 'id' instead of 'vpc_security_group_ids' for security group IDs.