0
0
Terraformcloud~10 mins

Data source vs resource difference in Terraform - Interactive Practice

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

Complete the code to declare a resource that creates an AWS S3 bucket.

Terraform
resource "aws_s3_bucket" "my_bucket" {
  bucket = [1]
}
Drag options to blanks, or click blank then click option'
A"my-unique-bucket-name"
Baws_s3_bucket.my_bucket
C"data.aws_s3_bucket.my_bucket"
Dbucket_name
Attempts:
3 left
💡 Hint
Common Mistakes
Using a data source reference instead of a string for the bucket name.
Omitting quotes around the bucket name.
2fill in blank
medium

Complete the code to declare a data source that reads an existing AWS S3 bucket.

Terraform
data "aws_s3_bucket" "existing_bucket" {
  bucket = [1]
}
Drag options to blanks, or click blank then click option'
Abucket_name
Baws_s3_bucket.my_bucket
C"my-existing-bucket"
Dexisting_bucket_name
Attempts:
3 left
💡 Hint
Common Mistakes
Using a resource reference instead of a string for the bucket name.
Forgetting to put quotes around the bucket name.
3fill in blank
hard

Fix the error in referencing the bucket name from a data source in the resource block.

Terraform
resource "aws_s3_bucket_policy" "policy" {
  bucket = [1].bucket
  policy = jsonencode({
    Statement = []
  })
}
Drag options to blanks, or click blank then click option'
Adata.aws_s3_bucket_policy.existing_policy
Baws_s3_bucket.my_bucket
Caws_s3_bucket_policy.policy
Ddata.aws_s3_bucket.existing_bucket
Attempts:
3 left
💡 Hint
Common Mistakes
Referencing a resource instead of a data source.
Using the wrong attribute name.
4fill in blank
hard

Fill both blanks to create a resource that uses a data source to get the VPC ID.

Terraform
data "aws_vpc" "selected" {
  filter {
    name   = [1]
    values = ["default"]
  }
}

resource "aws_subnet" "example" {
  vpc_id     = [2].id
  cidr_block = "10.0.1.0/24"
}
Drag options to blanks, or click blank then click option'
A"tag:Name"
Bdata.aws_vpc.selected
C"vpc-id"
Daws_vpc.default
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect filter names.
Referencing resources instead of data sources.
5fill in blank
hard

Fill all three blanks to create a resource that uses a data source to get an AMI ID and launch an EC2 instance.

Terraform
data "aws_ami" "ubuntu" {
  most_recent = true
  filter {
    name   = [1]
    values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
  }
  owners = [[2]]
}

resource "aws_instance" "web" {
  ami           = [3].id
  instance_type = "t2.micro"
}
Drag options to blanks, or click blank then click option'
A"name"
B"099720109477"
Cdata.aws_ami.ubuntu
D"owner-id"
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect filter or owner keys.
Referencing the resource instead of the data source.