Which of the following Terraform data source blocks is correctly written to fetch an AWS AMI by its name?
Remember that filter is a block, not an assignment, and values must be a list.
Option B correctly uses the data block syntax with the filter block and values as a list. Option B misses quotes around the data source type. Option B incorrectly assigns filter as a map instead of a block. Option B uses a string instead of a list for values.
Given the following Terraform data source block, what will be the value of data.aws_ami.example.id after apply?
data "aws_ami" "example" { most_recent = true filter { name = "name" values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] } owners = ["099720109477"] }
Check the meaning of most_recent and the type of owners.
The data source fetches the most recent AMI matching the filter and owned by the specified owner ID. owners accepts a list of strings. The result is a single AMI ID, not a list. most_recent is a valid argument.
You want to reference an existing AWS VPC by its tag Environment=Production in Terraform. Which data source block correctly achieves this?
Remember that filter is a block and tags is not a valid argument for aws_vpc data source.
Option C correctly uses a filter block with the tag name and value. Option C incorrectly uses tags argument which is not supported. Option C uses aws_vpcs which returns multiple VPCs, not a single one. Option C incorrectly assigns filter as a map instead of a block.
Consider this Terraform data source block fetching AWS credentials from the environment. What is the main security risk?
data "aws_caller_identity" "current" {}
Think about what information aws_caller_identity exposes and where Terraform stores state.
The aws_caller_identity data source reveals AWS account and user details. If Terraform state files are not securely stored or encrypted, this sensitive information could be exposed. It does not modify credentials or grant privileges.
Given this Terraform data source block, what will be the behavior when applied?
data "aws_subnet" "example" { filter { name = "tag:Name" values = ["subnet-1"] } filter { name = "availability-zone" values = ["us-east-1a"] } }
Consider how multiple filter blocks combine in Terraform data sources.
Multiple filter blocks in a data source combine with AND logic, so the data source returns resources matching all filters. It does not cause an error and does not return resources matching only one filter.