简体   繁体   English

如何在 terraform 中运行 forloop inside 字符串模板?

[英]How do I run a forloop inside string template in terraform?

My resource looks like below, how to run a forloop for below usecase where I am putting each index of aws_account_ids variable manually.我的资源如下所示,如何为下面的用例运行 forloop,我手动放置aws_account_ids变量的每个索引。

resource "aws_ecr_repository_policy" "ecr_image_pull_access" {
  repository = aws_ecr_repository.ecr_repo.name
  policy     = <<EOF
{
  "Version": "2008-10-17",
  "Statement": [
    {
      "Sid": "AllowPull",
      "Effect": "Allow",
      "Principal": {
        "AWS": [
          "arn:aws:iam::${var.aws_account_ids[0]}:root",
          "arn:aws:iam::${var.aws_account_ids[1]}:root",
          "arn:aws:iam::${var.aws_account_ids[2]}:root"
        ]
      },
      "Action": [
        "ecr:BatchCheckLayerAvailability",
        "ecr:BatchGetImage",
        "ecr:GetDownloadUrlForLayer"
      ]
    }
  ]
}
EOF
}

I tried following this https://discuss.hashicorp.com/t/dynamic-policy-generation-error-policy-contains-an-invalid-json-invalid-character-after-array-element/38881/5 but getting error我尝试遵循此https://discuss.hashicorp.com/t/dynamic-policy-generation-error-policy-contains-an-invalid-json-invalid-character-after-array-element/38881/5但出现错误

| var.aws_account_ids is list of string with 3 element
│ 
│ Cannot include the given value in a string template: string required.

The usual way is to wrap everything in jsonencode and use regular TF expressions, instead of json string:通常的方法是将所有内容包装在jsonencode中并使用正则 TF 表达式,而不是 json 字符串:

resource "aws_ecr_repository_policy" "ecr_image_pull_access" {
  repository = aws_ecr_repository.ecr_repo.name
  policy     = jsonencode({
    Version = "2008-10-17"
    Statement = [{
      Sid = "AllowPull",
      Effect = "Allow"
      Principal = {
        AWS = [for acc_id in var.aws_account_ids: "arn:aws:iam::${acc_id}:root"]
      },
      Action = [
        "ecr:BatchCheckLayerAvailability",
        "ecr:BatchGetImage",
        "ecr:GetDownloadUrlForLayer"
      ]
    }]
    }
  )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 VSCode 中更改 Terraform 版本? - How do I Change the Terraform Version in VSCode? 如何为 Terraform 配置 AWS MFA? - How do I configure AWS MFA for Terraform? 如何强制 terraform 重新创建资源? - How do I force terraform to recreate a resource? 如何在 terraform 中将 rds 与弹性 beantalk 连接 - How do I connect rds with elastic beanstalk in terraform 如何配置 Terraform 以在不销毁和重新创建的情况下更新 GCP 计算引擎实例模板? - How can I configure Terraform to update a GCP compute engine instance template without destroying and re-creating? 如何使用 Terraform 将 EC2 实例启动到现有 VPC 中? - How do I launch an EC2 instance into an existing VPC using Terraform? 如何将各种密钥名称关联到 terraform 中的 ec2 实例? - How do I associate various key names to an ec2 instance in terraform? 这个 terraform 模板有什么问题? - What is wrong with this terraform template? 如何设置Terraform中elasticsearch索引的总字段数限制? - How do I set the total field limit of an elastic search index in Terraform? 如何将生命周期规则应用于 Terraform 中的现有 s3 存储桶? - How do I apply a lifecycle rule to an EXISTING s3 bucket in Terraform?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM