Skip
Skip

Reputation: 6531

Terraform module and square brackets syntax

I have the following module usage syntax, which I can not understand

module.cloudfront_function_route_host[0].cloudfront_function_arn

Questions:

Secondary question:


Module definition in subfolder .\cloudfront-functions-route-host\

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

File main.tf

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

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

File output.tf

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

output "cloudfront_function_code" {
  value = local.function_code
}

Upvotes: 0

Views: 7003

Answers (1)

Ervin Szilagyi
Ervin Szilagyi

Reputation: 16805

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

The bracket notation is applied to the module itself. Probably you have the following module usage (or something similar) in your code somewhere:

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
  ...
}

This snippet declares a list of modules (it has a count attribute), for which it makes sense to have the bracket notation. Otherwise, this code module.cloudfront_function_route_host[0].cloudfront_function_arn does not work.

Does the module return a list?

No, it does not. As I said before, the bracket notation would apply to the module itself, not to the return.

Where in the module / terraform is this documented?

This is not something we could answer based on your question.

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

Returning to my previous snippet, note the folder (source) does not have to be the same as the module name.

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

I don't really understand this statement.

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

This is what exports the property of cloudfront_function_arn. For more information you would want to check out Terraform module outputs.

Upvotes: 3

Related Questions