Terraform 中模块引用的值

问题描述 投票:0回答:1

当我对其端点进行更改时,我试图重新部署我的 api 网关部署。每个端点都是在模块中定义的,因此通过遵循此文档,我最终得到如下所示的内容:

resource "aws_api_gateway_deployment" "rest_api" {
  rest_api_id = aws_api_gateway_rest_api.rest_api.id
  
  triggers = {
    redeployment = sha1(jsonencode([
      module.api_endpoint[*],
      aws_api_gateway_integration.webapp,
      aws_api_gateway_integration_response.webapp_404,
      aws_api_gateway_integration.root_redirect
    ]))
  }

  lifecycle { create_before_destroy = true }
}

module api_endpoint {
  source = "./modules/api_endpoint"

  for_each = local.paths

  api_id = aws_api_gateway_rest_api.rest_api.id
  root_resource_id = aws_api_gateway_rest_api.rest_api.root_resource_id
  path = each.key
  methods = each.value

  authoriser_id = aws_api_gateway_authorizer.rest_api.id
  lambda_arn = aws_lambda_function.rest_api.invoke_arn
}

但是,当我添加其他 api_endpoints 时,它不会更新。我是否遗漏了

module.api_endpoint[*]
的价值?我原以为它会像下面的资源一样工作。我需要让它输出一些会相应改变的东西吗?

terraform aws-api-gateway
1个回答
0
投票

正如评论中提到的,使用

for_each
时 splat 表达式不起作用。或者,
values
内置函数
可用于获取模块提供的特定输出的所有值。另一种选择是引用其中一个键的单个输出。代码看起来像这样:

resource "aws_api_gateway_deployment" "rest_api" {
  rest_api_id = aws_api_gateway_rest_api.rest_api.id
  
  triggers = {
    redeployment = sha1(jsonencode([
      values(module.api_endpoint)[*].<name_of_the_output>,
      aws_api_gateway_integration.webapp,
      aws_api_gateway_integration_response.webapp_404,
      aws_api_gateway_integration.root_redirect
    ]))
  }

  lifecycle { create_before_destroy = true }
}

其中

<name_of_the_output>
是一个占位符,应替换为模块中定义的真实输出名称。

© www.soinside.com 2019 - 2024. All rights reserved.