Skip to content

Unresolvable data references can cause a merged tag set to be null #205

@ajkerrigan

Description

@ajkerrigan

It looks like using merge() can produce an empty map if any of its elements have a direct reference that can't be resolved at scan time. For example, given the following Terraform file:

# main.tf

data "aws_region" "current" {}

variable "tags" {
  type = map(string)
  default = {
    VarTag1 = data.aws_region.current.name
  }
}

locals {
  tags = {
    LocalTag1 = data.aws_region.current.name
  }
}

resource "aws_s3_bucket" "ok1" {
  bucket = "ok1"

  tags = merge(
    var.tags,
    local.tags
  )
}

resource "aws_s3_bucket" "ok2" {
  bucket = "ok2"

  tags = {
    "ResourceTag1" = data.aws_region.current.name
  }
}

resource "aws_s3_bucket" "bug1" {
  bucket = "bug1"

  tags = merge(
    var.tags,
    local.tags,
    {
      "ResourceTag1" = data.aws_region.current.name
    }
  )
}

There are 3 buckets with tag definitions that refer to data.aws_region.current.name, which tfparse resolves to a null/empty value. The ok1 and ok2 buckets end up with tags that have null/empty values, but the bug1 bucket produces an entirely null set of tags.

Results of a tfparse.load_from_path() for this sample module
{
    "aws_region": [
        {
            "__tfmeta": {
                "filename": "main.tf",
                "label": "aws_region",
                "line_end": 1,
                "line_start": 1,
                "path": "data.aws_region.current",
                "type": "data"
            },
            "id": "6b85d52d-5153-48bc-849e-2b8db532fb52"
        }
    ],
    "aws_s3_bucket": [
        {
            "__tfmeta": {
                "filename": "main.tf",
                "label": "aws_s3_bucket",
                "line_end": 43,
                "line_start": 33,
                "path": "aws_s3_bucket.bug1",
                "type": "resource"
            },
            "bucket": "bug1",
            "id": "adb04e60-9d00-46a0-b876-a79be0925e40",
            "tags": null
        },
        {
            "__tfmeta": {
                "filename": "main.tf",
                "label": "aws_s3_bucket",
                "line_end": 23,
                "line_start": 16,
                "path": "aws_s3_bucket.ok1",
                "type": "resource"
            },
            "bucket": "ok1",
            "id": "a0de7f46-546a-46b3-89a1-02a5906111f6",
            "tags": {
                "LocalTag1": null,
                "VarTag1": null
            }
        },
        {
            "__tfmeta": {
                "filename": "main.tf",
                "label": "aws_s3_bucket",
                "line_end": 31,
                "line_start": 25,
                "path": "aws_s3_bucket.ok2",
                "type": "resource"
            },
            "bucket": "ok2",
            "id": "8581a2e6-8016-4f4b-8885-0d56bb973d85",
            "tags": {
                "ResourceTag1": null
            }
        }
    ],
    "locals": [
        {
            "__tfmeta": {
                "filename": "main.tf",
                "line_end": 14,
                "line_start": 10,
                "path": "locals"
            },
            "id": "6ac38550-c83e-4fa4-bc8c-04cc0303a257",
            "tags": {
                "LocalTag1": null
            }
        }
    ],
    "variable": [
        {
            "__tfmeta": {
                "filename": "main.tf",
                "label": "tags",
                "line_end": 8,
                "line_start": 3,
                "path": "variable.tags"
            },
            "default": {
                "VarTag1": null
            },
            "id": "8ca4495d-36da-4dfc-bad0-cd78ff373fb7",
            "type": null
        }
    ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions