Terraform 模块和方括号语法

Terraform module and square brackets syntax

我有以下模块使用语法,我看不懂

module.cloudfront_function_route_host[0].cloudfront_function_arn

问题:

第二个问题:


子文件夹中的模块定义 .\cloudfront-functions-route-host\

Observation: No "cloudfront_function_arn" property is exported, but still it is accessed via module

文件main.tf

locals {
  bucket_name   = ...
  function_code = ...
}

resource "aws_cloudfront_function" "route_host" {
  name    = ...
  runtime = ...
  comment = ...
  publish = true
  code    = local.function_code
}

文件output.tf

output "cloudfront_function_arn" {
  value = aws_cloudfront_function.route_host.arn
}

output "cloudfront_function_code" {
  value = local.function_code
}

How comes, that the CloudFront function resource - is accessed via the square-bracket notation?

括号符号应用于模块本身。可能您的代码某处有以下模块用法(或类似的东西):

module "cloudfront_function_route_host" {
  source = ".\cloudfront-functions-route-host"
  count = var.something ? 1 : 0 # probably you have some condition on which it is created or not
  ...
}

此代码段声明了一个 模块列表 (它具有 count 属性),因此使用括号表示法是有意义的。否则,此代码 module.cloudfront_function_route_host[0].cloudfront_function_arn 不起作用。

Does the module return a list?

不,不是。正如我之前所说,括号表示法适用于模块本身,而不适用于 return.

Where in the module / terraform is this documented?

根据您的问题,我们无法回答这个问题。

why in the module reference - underscores are used, but the module folder is called with dashes ("cloudfront-functions-route-host")

回到我之前的片段,注意文件夹 (source) 不必与模块名称相同。

Observation: No "cloudfront_function_arn" property is exported, but still it is accessed via module

这个说法我不是很理解

output "cloudfront_function_arn" {
  value = aws_cloudfront_function.route_host.arn
}

这就是导出 cloudfront_function_arn 的 属性 的内容。有关详细信息,您需要查看 Terraform module outputs.