如何设置Terraform模块的列表变量。

huangapple go评论58阅读模式
英文:

how to set terraform module list variables

问题

由于我是一个新的Terraform机器人,我正在尝试使用Terraform创建多个Lambda函数的权限。以下是`main.tf`文件的一部分:

模块 "lambda1_s3_events" {
  source       = "./terraform-aws-modules/lambda/aws"
  statement_id = "AllowS3Invoke"
  action       = "lambda:InvokeFunction"
  function_name = aws_lambda_function.lambda1.function_name
  principal    = "s3.amazonaws.com"
  source_arn   = "arn:aws:s3:::${module.s3_bucket.name}"
}

模块 "lambda2_s3_events" {
  source       = "./terraform-aws-modules/lambda/aws"
  statement_id = "AllowS3Invoke"
  action       = "lambda:InvokeFunction"
  function_name = aws_lambda_function.lambda2.function_name
  principal    = "s3.amazonaws.com"
  source_arn   = "arn:aws:s3:::${module.s3_bucket.name}"
}

模块 "lambda3_s3_events" {
  source       = "./terraform-aws-modules/lambda/aws"
  statement_id = "AllowS3Invoke"
  action       = "lambda:InvokeFunction"
  function_name = aws_lambda_function.lambda3.function_name
  principal    = "s3.amazonaws.com"
  source_arn   = "arn:aws:s3:::${module.s3_bucket.name}"
}

而不是创建多个Lambda权限模块,如上所示,您可以如何将这三个合并为一个模块?

我尝试过添加三个块来配置...


<details>
<summary>英文:</summary>

as i am a new bot to terraform, i am trying to create lambda permissions to multiple lambda functions using terraform. 

**main.tf**

module "lambda1_s3_events" {
source = "./terraform-aws-modules/lambda/aws"
statement_id = "AllowS3Invoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda1.function_name
principal = "s3.amazonaws.com"
source_arn = "arn:aws:s3:::${module.s3_bucket.name}"
}

module "lambda2_s3_events" {
source = "./terraform-aws-modules/lambda/aws"
statement_id = "AllowS3Invoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda2.function_name
principal = "s3.amazonaws.com"
source_arn = "arn:aws:s3:::${module.s3_bucket.name}"
}

module "lambda3_s3_events" {
source = "./terraform-aws-modules/lambda/aws"
statement_id = "AllowS3Invoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda3.function_name
principal = "s3.amazonaws.com"
source_arn = "arn:aws:s3:::${module.s3_bucket.name}"
}

and instead of creating multiple lambda permission modules as showed above. how can we create this three as one ?



i have tried add three blocks to configure

</details>


# 答案1
**得分**: 1

您可以在模块上使用[`for_each`](https://developer.hashicorp.com/terraform/language/meta-arguments/for_each)元参数,类似于资源。

```hcl
module "lambda1_s3_events" {
  source   = "./terraform-aws-modules/lambda/aws"
  for_each = toset(local.lambda_functions)

  statement_id  = "AllowS3Invoke"
  action        = "lambda:InvokeFunction"
  function_name = each.value
  principal     = "s3.amazonaws.com"
  source_arn    = "arn:aws:s3:::${module.s3_bucket.name}"
}
locals {
  lambda_functions = [
    aws_lambda_function.lambda1.function_name,
    aws_lambda_function.lambda2.function_name,
    aws_lambda_function.lambda3.function_name,
    ## 使用您的 Lambda 函数名称更新此列表 ##
  ]
}

在这里,您还可以使用 for_each 进行更多的可能性,但我希望您能明白。这将使您能够仅调用一个模块,但循环遍历所有 Lambda 函数。

> 注意: Terraform 0.13 中添加了对模块的 for_each 支持;以前的版本只能在资源上使用它。

英文:

you can use a for_each meta argument on modules also, similar like resources.

module &quot;lambda1_s3_events&quot; {
  source   = &quot;./terraform-aws-modules/lambda/aws&quot;
  for_each = toset(local.lambda_functions)

  statement_id  = &quot;AllowS3Invoke&quot;
  action        = &quot;lambda:InvokeFunction&quot;
  function_name = each.value
  principal     = &quot;s3.amazonaws.com&quot;
  source_arn    = &quot;arn:aws:s3:::${module.s3_bucket.name}&quot;
}
locals {
  lambda_functions = [
    aws_lambda_function.lambda1.function_name,
    aws_lambda_function.lambda2.function_name,
    aws_lambda_function.lambda3.function_name,
    ## update this list with your lambda function names ##
  ]
}

you can do a couple of more possibilities with for_each here but I hope you get an idea.
This will enable you to use only one module call but loop over all all lambda functions.

> CONSIDERATION: Module support for for_each was added in Terraform 0.13; previous versions can only use it with resources.

huangapple
  • 本文由 发表于 2023年1月9日 13:13:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定