Skip to content

Variable string concatenation #109

Description

@ilons

I'm trying to perform string concatenation with variables, but are having some trouble to get it to work.

Actual Terraform:

variable "env_prefix" {
  type  = string
  description = "my env prefix"
}
resource "azurerm_resource_group" "rg_demo" {
  name = "${var.env_prefix}-infra-demo"
  location = "North Europe"
}

Expected Terrascript:

import terrascript

script = terrascript.Terrascript()
env_prefix = script.add(terrascript.Variable("env_prefix", type="string", description="my env prefix"))
# Sorry for the convulated resource syntax, what is the proper way to do it?
script += type("azurerm_resource_group", (terrascript.Resource,), {})(
    "rg_demo",
    name=f"${{{env_prefix}}}-infra-demo",
    location="North Europe",
)

print(script)

Expected result:

{
  "variable": {
    "env_prefix": {
      "type": "string",
      "description": "my env prefix"
    }
  },
  "resource": {
    "azurerm_resource_group": {
      "rg_demo": {
        "name": "${var.env_prefix}-infra-demo",
        "location": "North Europe"
      }
    }
  }
}

Actual result:

{
  "variable": {
    "env_prefix": {
      "type": "string",
      "description": "my env prefix"
    }
  },
  "resource": {
    "azurerm_resource_group": {
      "rg_demo": {
        "name": "${{'type': 'string', 'description': 'my env prefix'}}-infra-demo",
        "location": "North Europe"
      }
    }
  }
}

Without string interpolation during the assignmen (name=env_prefix,, it gets almost right:

{
  "variable": {
    "env_prefix": {
      "type": "string",
      "description": "my env prefix"
    }
  },
  "resource": {
    "azurerm_resource_group": {
      "rg_demo": {
        "name": "var.env_prefix",
        "location": "North Europe"
      }
    }
  }
}

But should in fact be:

{
  "resource": {
    "azurerm_resource_group": {
      "rg_demo": {
        "name": "${var.env_prefix}",
        "location": "North Europe"
      }
    }
  }
}

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions