Reference multiple resource instances

100 views
Skip to first unread message

Will Black

unread,
Jan 4, 2021, 2:02:57 PM1/4/21
to Terraform
All,

I have a resource that loops thru a map and creates multiple resources.

resource "aws_instance" "this" { 
   for_each = var.instance_map
    ...
}

I now want to reference all the arns for backup.  Something like the following.  Is there a way to reference all the arns without having to loop back through all the instances using 'each.key'?

#Backup Selection (Instance) Resource
resource "aws_backup_selection" "selection" {
...
resources = [
         aws_instance.ths[each.key].arn,
        ]
}

Will Black

unread,
Jan 4, 2021, 2:34:24 PM1/4/21
to Terraform
I actually figured out that I can use a variable reference to accomplish this.



#Backup Selection (Instance) Resource
resource "aws_backup_selection" "selection" {
...
resources = [
          resources = [ for ec2 in aws_instance.this: ec2.arn ]  
        ]
Reply all
Reply to author
Forward
0 new messages