admin管理员组

文章数量:1384310

I'm using Terraform to make modifications to my Cloudflare (that part works....)

Where I'm running into a problem is in the flatten (setting up a tfvar for each domain) I've tried using flatten object made of nested list in terraform Which I think get me close but I'm getting the following errors:

    ╷
    │ Error: Invalid reference
    │
    │   on cloudflare.tf line 621, in resource "cloudflare_record" "DNSRecord":
    │  621:     ]]) : DNSRecord.type => silly}
    │
    │ A reference to a resource type must be followed by at least one attribute   access, specifying the resource name.
    ╵
    

So here's truncated (and redacted) pertinent files...

cloudflare.tf (truncated)

    resource "cloudflare_record" "DNSRecord" {
        zone_id  = var.zone_id
        for_each = {
            for DNSRecord in flatten ([
            for type, groups in var.DNS_Values : [
                for group in groups : [
                for name in group.name : [
                    {
                        type     = type
                        content  = group.content
                        name     = group.name
                        comment  = groupment
                        proxied  = group.proxied
                        priority = group.priority
                    }
                ]
            ]
        ]]) : DNSRecord.type => silly}
        type = each.value.type
        content = each.value.content
        name = each.value.name
        comment = each.valuement
        proxied = each.value.proxied
        priority = each.value.priority
    }
    

variables.tf (truncated)

    variable "DNS_Values" {
        type = object(
        {
          MX = list(object({
            content  = string,
            priority = string,
            name     = string,
            comment  = string,
            proxied  = string
          })),
          CNAME = list(object({
            content  = string,
            name     = string,
            comment  = string,
            proxied  = string
          }))
        }
      )
    }

domain.tfvar (truncated and redacted)

    DNS_VALUES = {
        CNAME = [
            {
                content  = "targethostA.domain.name",
                name     = "overflow",
                comment  = "Updated by Terraform @ ${local.newtimestamp}",
                proxied  = false
            },
            {
                content  = "targethost7.domain.name",
                name     = "redacted",
                comment  = "Updated by Terraform @ ${local.newtimestamp}",
                proxied  = false
            },
            {
                content  = "targethost3.domain.name",
                name     = "madeup",
                comment  = "Updated by Terraform @ ${local.newtimestamp}",
                proxied  = false
            },
            {
                content  = "targethost4.domain.name",
                name     = "needsleep",
                comment  = "Updated by Terraform @ ${local.newtimestamp}",
                proxied  = false
            },
            {
                content  = "targethost2.domain.name",
                name     = "needcoffee",
                comment  = "Updated by Terraform @ ${local.newtimestamp}",
                proxied  = false
            },
            {
                content  = "targethost1.domain.name",
                name     = "SubdomainA",
                comment  = "Updated by Terraform @ ${local.newtimestamp}",
                proxied  = false
            },
        ],
        MX = [
            {
                content  = "mail1.server",
                priority = "10",
                name     = "domain.name",
                comment  = "Updated by Terraform @ ${local.newtimestamp}",
                proxied  = false
            },
            {
                content  = "mail2.server",
                priority = "1",
                name     = "domain.name",
                comment  = "Updated by Terraform @ ${local.newtimestamp}",
                proxied  = false
            },
            {
                content  = "mail3.server",
                priority = "5",
                name     = "domain.name",
                comment  = "Updated by Terraform @ ${local.newtimestamp}",
                proxied  = false
            },
            {
                content  = "mail4.server",
                priority = "5",
                name     = "domain.name",
                comment  = "Updated by Terraform @ ${local.newtimestamp}",
                proxied  = false
            },
            {
                content  = "mail5.server",
                priority = "10",
                name     = "domain.name",
                comment  = "Updated by Terraform @ ${local.newtimestamp}",
                proxied  = false
            },
        ]
    }

main,tf is not added as there's nothing related in it (other than local.newtimestamp).

Basically every record should look like this

    resource "cloudflare_record" "DNSRecord" {
      zone_id  = var.zone_id
      name     = "domain.name"
      comment  = "Updated by Terraform @ ${local.newtimestamp}"
      content  = "mail5.server"
      type     = "MX"
      priority = "10"
      proxied  = false
    }

本文标签: Terraform assistance needed with Flatten to build resourcesStack Overflow