简体   繁体   English

Terraform:如何连接 AWS arns 列表?

[英]Terraform: how to concatenate lists of AWS arns?

I want to create a iam_policy_arn_list in terraform where the list consists of the "FullAccess" arns of existing AWS policies, and the arn of a policy that I create on the fly.我想在 terraform 中创建一个iam_policy_arn_list ,其中该列表包含现有 AWS 策略的“FullAccess”arn,以及我动态创建的策略的 arn。 (I'm trying to create a Lambda function that can read/write to only a specified bucket.) If I only use existing AWS policies, then the following ingredients in my setup work: (我正在尝试创建一个只能读取/写入指定存储桶的 Lambda function。)如果我使用现有的 AWS 策略,那么我的设置中的以下成分可以工作:

variable "iam_policy_arn_list" {
  type        = list(string)
  description = "IAM Policies to be attached to role"
  default = [
    "arn:aws:iam::aws:policy/CloudWatchFullAccess",
    "arn:aws:iam::aws:policy/AmazonSESFullAccess",
    "arn:aws:iam::aws:policy/AmazonS3FullAccess"
  ]
}

resource "aws_iam_role_policy_attachment" "role-policy-attachment" {
  role       = "${var.prefix}${var.role_name}"
  count      = length(var.iam_policy_arn_list)
  policy_arn = var.iam_policy_arn_list[count.index]
  depends_on = [aws_iam_role.iam_for_lambda]
}

But now I want to remove "arn:aws:iam::aws:policy/AmazonS3FullAccess" and replace it with the arn of a policy that I create on the fly that lets the Lambda function only access a specified S3 bucket.但现在我想删除"arn:aws:iam::aws:policy/AmazonS3FullAccess"并将其替换为我动态创建的策略的 arn,该策略允许 Lambda function 仅访问指定的 S3 存储桶。 Where I am stuck is how to end up with a list variable of the rough form:我被卡住的地方是如何最终得到一个粗略形式的列表变量:

variable "iam_policy_arn_list" {
  type        = list(string)
  description = "IAM Policies to be attached to role"
  default = [
    "arn:aws:iam::aws:policy/CloudWatchFullAccess",
    "arn:aws:iam::aws:policy/AmazonSESFullAccess",
    arn_of_the_policy_I_create_on_the_fly
  ]
}

... because the concat function will not work when defining variables. ...因为 concat function 在定义变量时将不起作用。 I have tried using the concat function elsewhere, but nothing seems to work.我曾尝试在其他地方使用 concat function,但似乎没有任何效果。 Eg I tried:例如我试过:

resource "aws_iam_policy" "specific_s3_bucket_policy" {
  name        = "my_name"
  description = "Grant access to one specific S3 bucket"
  policy = jsonencode({
    "Version" : "2012-10-17",
    "Statement" : [
      {
        "Effect" : "Allow",
        "Action" : [
          "s3:ListAllMyBuckets",
          "s3:ListBucket"
        ],
        "Resource" : "*"
      },
      {
        "Effect" : "Allow",
        "Action" : [
          "s3:PutObject",
          "s3:PutObjectAcl",
          "s3:GetObject",
          "s3:GetObjectAcl",
          "s3:DeleteObject"
        ],
        "Resource" : "arn:aws:s3:::${var.S3_BUCKET_NAME}/*"
      }
    ]
  })
}



resource "aws_iam_role_policy_attachment" "role-policy-attachment" {
  role       = "${var.prefix}${var.role_name}"
  count      = length(var.iam_policy_arn_list)
  policy_arn = concat(var.iam_policy_arn_list, [aws_iam_policy.specific_s3_bucket_policy.arn])[count.index]
  depends_on = [aws_iam_role.iam_for_lambda]
}

... but this does not work. ...但这不起作用。 Suggestions?建议?

Given the following iam_policy_arn_list :鉴于以下iam_policy_arn_list

variable "iam_policy_arn_list" {
  type        = list(string)
  description = "IAM Policies to be attached to role"
  default = [
    "arn:aws:iam::aws:policy/CloudWatchFullAccess",
    "arn:aws:iam::aws:policy/AmazonSESFullAccess",
  ]
}

Then create a local value like this:然后像这样创建一个本地值:

locals {
  combined_iam_policy_arn_list = concat(var.iam_policy_arn_list, [aws_iam_policy.specific_s3_bucket_policy.arn])
}

And then apply it like this:然后像这样应用它:

resource "aws_iam_role_policy_attachment" "role-policy-attachment" {
  role       = "${var.prefix}${var.role_name}"
  count      = length(local.combined_iam_policy_arn_list)
  policy_arn = local.combined_iam_policy_arn_list[count.index]
  depends_on = [aws_iam_role.iam_for_lambda]
}

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

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