Experimenting Locally with Terraform

Terraform supports a bunch of providers, but the vast majority of them are public cloud based. However, you could set up a local VMware vSphere cluster and use the vSphere provider to interact with that to get you going. There’s also a provider for OpenStack if you want to set up an OpenStack cluster. Alternatively … Read more

How to get an object from a list of objects in Terraform?

You get the map with id=”name2″ with the following expression: var.objects[index(var.objects.*.id, “name2”)] For a quick test, run the following one-liner in terraform console: [{id = “name1”, attribute = “a”}, {id = “name2”, attribute = “a,b”}, {id = “name3”, attribute = “d”}][index([{id = “name1”, attribute = “a”}, {id = “name2”, attribute = “a,b”}, {id = “name3”, … Read more

I want to identify the public ip of the terraform execution environment and add it to the security group

There’s an easier way to do that without any scripts. The trick is having a website such as icanhazip.com which retrieve your IP, so set it in your terraform file as data: data “http” “myip” { url = “http://ipv4.icanhazip.com” } And whenever you want to place your IP just use data.http.myip.body, example: ingress { from_port … Read more

How to write an if, else, elsif conditional statement in Terraform

This is one way using the coalesce() function: locals{ prod = “${var.environment == “PROD” ? “east” : “”}” prod2 = “${var.environment == “PROD2” ? “west2” : “”}” nonprod = “${var.environment != “PROD” && var.environment != “PROD2” ? “west” : “”}” region = “${coalesce(local.prod,local.prod2, local.nonprod)}” }

Is there a way AND/OR conditional operator in terraform?

This is more appropriate in the actual version (0.12.X) The supported operators are: Equality: == and != Numerical comparison: >, <, >=, <= Boolean logic: &&, ||, unary ! https://www.terraform.io/docs/configuration/interpolation.html#conditionals condition_one and condition two: count = var.condition_one && var.condition_two ? 1 : 0 condition_one and NOT condition_two: count = var.condition_one && !var.condition_two ? 1 : … Read more