amazon-web-servicesterraformterraform-provider-awsaws-acm

Terraform : How to obtain value from list containing map


I have a terraform output for acm_certificate domain_validation_options

the output is something of this type. I want to extract resource_record_value and resource_record_name

     ~ base_module_outputs = {
          + acm_certificate_name = [
              + {
                  + domain_name           = "xxxx"
                  + resource_record_name  = "xxxxx"
                  + resource_record_type  = "CNAME"
                  + resource_record_value = "xxxx"
                },
            ]
            # (2 unchanged elements hidden)
        }

root module outputs.tf

output "base_module_outputs" {
  value = module.base
}

base module outputs.tf

output "acm_certificate_name" {
  value = aws_acm_certificate.xx.domain_validation_options
}

Solution

  • Since the output value needs to be present in the base module outputs, here's how to get it:

    output "acm_certificate_name" {
      value = tolist(aws_acm_certificate.xx.domain_validation_options)[0].resource_record_value
    }
    

    The explicit conversion to a list (using tolist built-in function) is required here as the domain_validation_options attribute is a set:

    domain_validation_options - Set of domain validation objects which can be used to complete certificate validation. [...]

    Hence domain_validation_options has no indexes. The same type of logic can be applied to the second required output which references the resource_record_name.


    EDIT: As suggest in the comments (h/t: @Matt Schuchard), the safer way to fetch the values is by using the one built-in function. The above code would then change to:

    output "acm_certificate_name" {
      value = one(aws_acm_certificate.xx.domain_validation_options).resource_record_value
    }