cloudposse / cloudposse/atmos

generate varfiles in hcl format creates incorrect hcl when maps/lists are involved

Open
#334 1 comment 0 reactions 1 assignee Claimed by @aknysh View on GitHub
bug
Dominant language
Go
Stars
1.4k
Forks
175
Avg merge
2d 3h
Merged PRs (30d)
134

Description

### Describe the Bug

see 2 working conditions and 2 failing conditions below:

my component:
``` hcl
resource "local_file" "this" {
count = module.this.enabled ? 1 : 0

content = "did this work"
filename = "${path.module}/a-simple-test.txt"
}
variable "mymap" {
type = any
default = null
}
```

my vars - variation 1 - simple list
``` yaml
vars:
enabled: true
mymap:
mylist:
- 1
- 2
```

Result: TF Succeeds

```
enabled = true

environment = "ue2"

mymap = {
"mylist" = [1, 2]
}

region = "us-east-2"

stage = "test"

tenant = "self"
```

my vars - variation 2 - simple map
``` yaml
vars:
enabled: true
mymap:
somekey:
somevalue: true
```

Result: Variation 2: TF Plan fails with error on quoted argument name
```
enabled = true

environment = "ue2"

mymap somekey {
"somevalue" = true
}

region = "us-east-2"

stage = "test"

tenant = "self"
```

```

│ Error: Invalid argument name

│ on local.auto.tfvars line 6, in mymap "somekey":
│ 6: "somevalue" = true

│ Argument names must not be quoted.

exit status 1

```

my vars - variation 3 - map with list
``` yaml
vars:
enabled: true
mymap:
somekey:
somelist:
- item1
- item2
```

Result: Variation 3: TF Plan fails with error on quoted argument name

```
enabled = true

environment = "ue2"

mymap somekey {
"somelist" = ["item1", "item2"]
}

region = "us-east-2"

stage = "test"

tenant = "self"
```

```

│ Error: Invalid argument name

│ on local.auto.tfvars line 6, in mymap "somekey":
│ 6: "somelist" = ["item1", "item2"]

│ Argument names must not be quoted.

```

my vars - variation 4 - list with map
``` yaml
vars:
enabled: true
mymap:
mylist:
- 1
- akey:
didthiswork: false
```

```
Result: Variation #4 generate varfiles produces valid HCL; TF plans OK
```

``` hcl
enabled = true

environment = "ue2"

mymap = {
"mylist" = [
1,
{
"akey" = {
"didthiswork" = false
}
},
]
}

region = "us-east-2"

stage = "test"

tenant = "self"
```

### Expected Behavior

I'd expect valid HCL produced and a TF plan to succeed

### Steps to Reproduce

1. Create a simple component with an "any" variable
2. Set up the YAML with a list or a map, modeling after variations 2 and 3
3. Run the generate varfiles command, using type hcl
4. Run the plan

### Screenshots

_No response_

### Environment

Mac
Atmos 1.30.0
Terraform 1.3.7

### Additional Context

I also noticed when the hcl is incorrect, if the file already exists, it will not be cleanly re-written, creating duplicate entries on a subsequent run.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.