简体   繁体   中英

Reference variable in a for loop in terraform

I have a list of accounts declared under variable "acct". I need to create as many ARNs as the accounts below using this list. How to do it? The below code gives me attribute error:

A reference to a resource type must be followed by at least one attribute access, specifying the resource name.


variable "acct"{
    type    = list(string)
    default = ["111111111111","22222222222",.....]
}

data "aws_arn" "SrcArn" {
    account = [for acc in var.acct : acc ]
    arn = ["arn:aws:logs:us-east-1:${account}:*"]
}

I need to create a list of arns which then can be used further down in the code. Can these then be referenced below like this:

condition {
                test = "ArnLike"
                values = data.aws_arn.SrcArn
                variable = "aws:SourceArn"
            }

If you want to use aws_arn, you can use to this.

variable "acct"{
    type    = list(string)
    default = ["111111111111","22222222222",.....]
}

data "aws_arn" "SrcArn" {
    count = var.acct
    arn = ["arn:aws:logs:us-east-1:${var.acct[count.index]}:*"]
}

And if you wanna make arn_list,

locals {
    # only use to variable.
    # not use to data "aws_arn"
    arn_list_only_use_to_variable = [for account in var.acct: "arn:aws:logs:us-east-1:${account}:*"]

    # use to data "aws_arn"
    src_arn = data.aws_arn.SrcArn
    arn_list_2_use_to_data_aws_arn = [for account in src_arn: account]
}

...
condition {
                test = "ArnLike"
                values = local.arn_list_only_use_to_variable 
                variable = "aws:SourceArn"
            }
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM