Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i build a list of maps in terraform

i would like to build a list of maps in terraform. Is there an operation that would let me do that.

eg: i would like to build the structure (from aws_instance.test.*.private_ip)

addresses = [
   {
     address = private_ip-1
   },
   {
     address = private_ip-2
   }
   ...
]
like image 819
suppandi g Avatar asked Nov 13 '17 21:11

suppandi g


People also ask

What is MAP Variable terraform?

terraform-map-variable.tfA variable can have a map type assigned explicitly, or it can be implicitly declared as a map by specifying a default value that is a map. The above demonstrates both. Then, replace the aws_instance with the following: resource "aws_instance" "example" { ami = var.

Are terraform maps ordered?

The important thing here is that, as you've noticed, Terraform's map type is an unordered map which identifies elements only by their keys, not by permission. Therefore if you have a situation where you need to preserve the order of a sequence then a map is not a suitable data structure to use.

How do you use count terraform?

How to use Terraform Count. We use count to deploy multiple resources. The count meta-argument generates numerous resources of a module. You use the count argument within the block and assign a whole number (could by variable or expression), and Terraform generates the number of resources.

What is try in terraform?

The try function can only catch and handle dynamic errors resulting from access to data that isn't known until runtime. It will not catch errors relating to expressions that can be proven to be invalid for any input, such as a malformed resource reference.


1 Answers

Apologies if this has already been resolved, I see that it's an older question.

I'm not sure if this is best practice, but this is the way I would attempt to reach the desired state.

Using the null_resource you'd be able to do the following:

variable "addresses" {
  type = "list"

  default = [
    "private_ip-1",
    "private_ip-2",
    "private_ip-3"
  ]
}

resource "null_resource" "test" {
  count = "${length(var.addresses)}"

  triggers {
    address = "${element(var.addresses, count.index)}"
  }
}

output "addresses" {
  value = "${null_resource.test.*.triggers}"
}

And have this output:

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

addresses = [
    {
        address = private_ip-1
    },
    {
        address = private_ip-2
    },
    {
        address = private_ip-3
    }
]

I'm currently on terraform 0.11.5, null_resource appears to have been added in 0.6.7

There are limitations to the null_resource triggers though. interpolated variables can only result in strings. So, unfortunately you won't be able to interpolate a value that would result in a list or a map; for example:

resource "null_resource" "test" {
  triggers {
    mylist = "${var.addresses}"
  }
}

will result in an error

Error: null_resource.test: triggers (address): '' expected type 'string', got unconvertible type '[]interface {}'

like image 120
mxlan Avatar answered Sep 27 '22 18:09

mxlan