0
votes

I've been working on some terraform code in a single file (main.tf). Now that it is working as expected I want build out a module for it.

I want to make this hard-coded variable an input variable. So I copy/pasted into modules variables.tf file.

variable "subnet-map" {
  type = map
  default = {
        aznum = 0, bits = 8, netnum = 0, desc = "some description" },
      { aznum = 1, bits = 8, netnum = 1, desc = "some description" },
      { aznum = 2, bits = 8, netnum = 2, desc = "some description" },
      { aznum = 0, bits = 8, netnum = 3, desc = "some description" },
      { aznum = 1, bits = 8, netnum = 4, desc = "some description" },
      { aznum = 2, bits = 8, netnum = 5, desc = "some description" },
      { aznum = 0, bits = 8, netnum = 6, desc = "some description" },
      { aznum = 1, bits = 8, netnum = 7, desc = "some description" },
      { aznum = 2, bits = 8, netnum = 8, desc = "some description" },
      { aznum = 0, bits = 8, netnum = 9, desc = "some description" },
      { aznum = 1, bits = 8, netnum = 10, desc = "some description" },
      { aznum = 2, bits = 8, netnum = 11, desc = "some description" },
  
}

To confirm, yes it works with no problems in a single 'main.tf' file. When I try it as an input variable I get the following error.

$ terraform apply

Error: Unexpected comma after argument

  on modules/my-aws-subnet/variables.tf line 17, in variable "subnet-map":
  16:   default = {
  17:         aznum = 0, bits = 8, netnum = 0, desc = "some description" },

Argument definitions must be separated by newlines, not commas. An argument
definition must end with a newline.

I'm not sure why this is treated differently when defined in variables.tf. Any thoughts?

3
In all my iterations of this, it seems i made a copy paste error here, sorry. In the end I got it working like so:dc12078

3 Answers

1
votes

The problem here is that your default variable value is not a valid type. It must be one of String, Number, Bool, Map, Object, List, etc. or some complex type. If you want it to be a map like you specified in type, then you can add a key to the default value:

variable "subnet-map" {
  type = map
  default = { "my-map" =
    { aznum = 0, bits = 8, netnum = 0, desc = "some description" },
    { aznum = 1, bits = 8, netnum = 1, desc = "some description" },
    { aznum = 2, bits = 8, netnum = 2, desc = "some description" },
    { aznum = 0, bits = 8, netnum = 3, desc = "some description" },
    { aznum = 1, bits = 8, netnum = 4, desc = "some description" },
    { aznum = 2, bits = 8, netnum = 5, desc = "some description" },
    { aznum = 0, bits = 8, netnum = 6, desc = "some description" },
    { aznum = 1, bits = 8, netnum = 7, desc = "some description" },
    { aznum = 2, bits = 8, netnum = 8, desc = "some description" },
    { aznum = 0, bits = 8, netnum = 9, desc = "some description" },
    { aznum = 1, bits = 8, netnum = 10, desc = "some description" },
    { aznum = 2, bits = 8, netnum = 11, desc = "some description" },
  }
}

Alternatively, if you wanted a list of maps, then you can also specify that in your updated type and default:

variable "subnet-map" {
  type = list(map)
  default = [
    { aznum = 0, bits = 8, netnum = 0, desc = "some description" },
    { aznum = 1, bits = 8, netnum = 1, desc = "some description" },
    { aznum = 2, bits = 8, netnum = 2, desc = "some description" },
    { aznum = 0, bits = 8, netnum = 3, desc = "some description" },
    { aznum = 1, bits = 8, netnum = 4, desc = "some description" },
    { aznum = 2, bits = 8, netnum = 5, desc = "some description" },
    { aznum = 0, bits = 8, netnum = 6, desc = "some description" },
    { aznum = 1, bits = 8, netnum = 7, desc = "some description" },
    { aznum = 2, bits = 8, netnum = 8, desc = "some description" },
    { aznum = 0, bits = 8, netnum = 9, desc = "some description" },
    { aznum = 1, bits = 8, netnum = 10, desc = "some description" },
    { aznum = 2, bits = 8, netnum = 11, desc = "some description" },
  ]
}
0
votes

Youre missing the first { bracket next to aznum = 0.

0
votes

This is the solution I ended with that works.

variable "subnet-map" {
  default = {
    pub = [
      { aznum = 0, bits = 8, netnum = 0, desc = "some description" },
      { aznum = 1, bits = 8, netnum = 1, desc = "some description" },
      { aznum = 2, bits = 8, netnum = 2, desc = "some description" },
      { aznum = 0, bits = 8, netnum = 3, desc = "some description" },
      { aznum = 1, bits = 8, netnum = 4, desc = "some description" },
      { aznum = 2, bits = 8, netnum = 5, desc = "some description" },
    ],
    int = [
      { aznum = 0, bits = 8, netnum = 6, desc =  "some description" },
      { aznum = 1, bits = 8, netnum = 7, desc =  "some description" },
      { aznum = 2, bits = 8, netnum = 8, desc =  "some description" },
      { aznum = 0, bits = 8, netnum = 9, desc =  "some description" },
      { aznum = 1, bits = 8, netnum = 10, desc = "some description" },
      { aznum = 2, bits = 8, netnum = 11, desc = "some description" },
    ],
  }
}