Write a `for` expression that builds a map of subnet IDs by AZ and a filtered list.
IntermediateHands-onWrite-Terraform tasks
Answer
locals {
subnet_by_az = { for s in aws_subnet.private : s.availability_zone => s.id }
big_instances = [for name, inst in var.instances : name if inst.size != "small"]
upper_names = [for n in var.names : upper(n)]
}
Key trick → { for k, v in ... : key => value } builds a map and [ ... ] builds a list. A trailing if filters. Duplicate map keys are an error unless you add ... to group them.
More on Write-Terraform tasks
Write Terraform for an S3 bucket with versioning, encryption and public access blocked.BeginnerWrite the remote backend configuration with S3 and state locking.BeginnerWrite a VPC with public and private subnets across 3 AZs using `count` or `for_each`.IntermediateWrite a reusable module for an EC2 instance with inputs, outputs and validation.AdvancedWrite a security group that allows HTTPS from anywhere and SSH only from an office CIDR.BeginnerCreate multiple IAM users from a list with `for_each`, and output their ARNs as a map.Beginner