Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing of maps in JSON variable files is broken. #792

Closed
euliancom opened this issue Jun 21, 2019 · 2 comments
Closed

Parsing of maps in JSON variable files is broken. #792

euliancom opened this issue Jun 21, 2019 · 2 comments
Assignees
Labels
bug Something isn't working critical

Comments

@euliancom
Copy link
Contributor

Bug Report

Parsing of maps in JSON variable files is broken.

Describe the Bug

When declaring a map in JSON format in a Terraform variable file, it produces the error:

...
💡 [landing_zone_route_table_association] terraform plan -no-color -var-file='/root/.terrahub/cache/jit/landing_zone_route_table_association_eef16dcf/default.tfvars' -var-file='/root/.terrahub/cache/jit/landing_zone_route_table_association_eef16dcf/remote.tfvars' -out=/root/.terrahub/cache/jit/landing_zone_route_table_association_eef16dcf/terraform.tfplan -input=false
❌ [landing_zone_route_table_association] invalid value "/root/.terrahub/cache/jit/landing_zone_route_table_association_eef16dcf/default.tfvars" for flag -var-file: multiple map declarations not supported for variables

❌ [landing_zone_route_table_association] Usage: terraform plan [options] [DIR-OR-PLAN]

  Generates an execution plan for Terraform.

  This execution plan can be reviewed prior to running apply to get a
  sense for what Terraform will do. Optionally, the plan can be saved to
  a Terraform plan file, and apply can take this plan file to execute
  this plan exactly.

  If a saved plan is passed as an argument, this command will output
  the saved plan contents. It will not modify the given plan.
...

To Reproduce Error

component config file .terrahub.yml

component:
  name: landing_zone_route_table_association
  template:
    data:
      aws_vpc:
        selected:
          provider: 'aws.${tfvar.terrahub["provider_aliases"]["0"]}'
          count: '${length(var.${tfvar.terrahub["provider_aliases"]["0"]}_config)}'
          cidr_block: '${lookup(var.default_config["config_${count.index}"], "landing_zone_vpc_cidr_block")}'
terraform:
  varFile:
    - ../tfvars/landing_zone_route_table_association/default.tfvars

tfvars file ../tfvars/landing_zone_route_table_association/default.tfvars in HCL format

provider_aliases = [
    "default"
]

default_config = {
    config_0 = {
        landing_zone_vpc_cidr_block     = "x.x.x.x/16"
        landing_zone_subnet_cidr_block  = "x.x.x.x"
        landing_zone_route_table_filter = "Landing Zone Route Table|false"
    },
    config_1 = {
        landing_zone_vpc_cidr_block     = "x.x.x.x"
        landing_zone_subnet_cidr_block  = "x.x.x.x"
        landing_zone_route_table_filter = "Landing Zone Route Table|false"
    }
}

tfvars file default.tfvars in JSON format

{
  "tfstate_path": "/tmp/.terrahub/tfstate_local/terraform-aws-landing-zone",
  "region": "us-east-1",
  "account_id": xxxxxxxxxxxx,
  "provider_aliases": [
    "default"
  ],
  "default_config": {
    "config_0": {
      "landing_zone_vpc_cidr_block": "x.x.x.x/16",
      "landing_zone_subnet_cidr_block": "x.x.x.x/24",
      "landing_zone_route_table_filter": "Landing Zone Route Table|false"
    },
    "config_1": {
      "landing_zone_vpc_cidr_block": "x.x.x.x/16",
      "landing_zone_subnet_cidr_block": "x.x.x.x/24",
      "landing_zone_route_table_filter": "Landing Zone Route Table|false"
    }
  }
}

run command

  • terrahub run -i landing_zone_route_table_association

To Reproduce Without Error

main.tf

data "aws_vpc" "selected" {
  count = "${length(var.default_config)}"
  cidr_block = "${lookup(var.default_config["config_${count.index}"], "landing_zone_vpc_cidr_block")}"
}

provider "aws" {
  region = "${var.region}"
}

variable.tf

variable "default_config" {
  type = "map"
}

variable "region" {
  type = "string"
}

default.tfvars

region = "us-east-1"

default_config = {
    config_0 = {
        landing_zone_vpc_cidr_block     = "x.x.x.x/16"
        landing_zone_subnet_cidr_block  = "x.x.x.x/24"
        landing_zone_route_table_filter = "Landing Zone Route Table|false"
    }
    config_1 = {
        landing_zone_vpc_cidr_block     = "x.x.x.x/16"
        landing_zone_subnet_cidr_block  = "x.x.x.x/24"
        landing_zone_route_table_filter = "Landing Zone Route Table|false"
    }
}

run command

  • terraform plan -var-file=default.tfvars

output without error

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

data.aws_vpc.selected[1]: Refreshing state...
data.aws_vpc.selected[0]: Refreshing state...

------------------------------------------------------------------------

No changes. Infrastructure is up-to-date.

This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.
@euliancom euliancom added bug Something isn't working critical labels Jun 21, 2019
@euliancom
Copy link
Contributor Author

I have found a ticket to Hashicorp - hashicorp/terraform#15549

@eistrati
Copy link
Contributor

NOT relevant anymore and NOT waiting for this problem to be solved by terraform.

intermediary solutions:

  1. define JSON maps as empty objects in default.tfvars and pass map's values in HCL format using another tfvars file like config.tfvars; run terraform apply -var-file=default.tfvars -var-file=config.tfvars, respecting variables precedence (ONLY for terraform v0.11.x and below)
  2. switch from JSON to HCL or HCL2 (currently work in progress for terrahub cli; will be published soon, in the next couple of weeks)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working critical
Projects
None yet
Development

No branches or pull requests

3 participants