Skip to content

HCL parses fails on Maps when a value of a key is an expression #87

Open
@wzur-r3

Description

I have a following code:

resource "aws_iam_role" "jenkins_orchestrator" {
  name_prefix = "jenkins-orchestrator"
  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Sid    = ""
        Effect = "Allow"
        Principal = {
          Federated = data.aws_iam_openid_connect_provider.cluster.arn
        }
        Action = "sts:AssumeRoleWithWebIdentity"
        Condition = {
          StringEquals = {
            "${replace(data.aws_iam_openid_connect_provider.cluster.arn, "arn:aws:iam::${data.aws_caller_identity.current.account_id}:oidc-provider/", "")}:sub" = "system:serviceaccount:${var.kubernetes_namespace}:${var.helm_release_name}"
          }
        }
      }
    ]
  })

  tags = var.tags
}

HCL4J fails to parse the map passed to StringEquals. The value of the key in that map has to be calculated, so it cannot be a constant.

HCL4J complains the ) character was unexpected, which was the one in oidc-provider/", "")}:sub

I noticed the same somewhere else, when I tried to create a map with a common prefix for the keys:

locals {
   mapExample = {
      "${var.prefix}-suffix-1": "value1",
      "${var.prefix}-suffix-2": "value1",
      "${var.prefix}-suffix-3": "value1",
  }
}

but I was able to refactor that to something HCL4J could consume.

HCL has following syntax for the keys in maps:

  • id - which is a string of value of id
  • (some-function()) - which allows to use a function and it has to be surrounded by () to distinguish it from the first case
  • "some-string-possible-with-${interpolation}" - which is what I have in examples above

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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