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
}
...
]
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.
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 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.
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.
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 {}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With