Skip to content

Instantly share code, notes, and snippets.

@imbriaco
Last active June 8, 2016 22:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imbriaco/594540615487eb9b59f54286e3d08f5c to your computer and use it in GitHub Desktop.
Save imbriaco/594540615487eb9b59f54286e3d08f5c to your computer and use it in GitHub Desktop.
I'll take stupid Terraform tricks for $500, Alex
module "my_instance" {
source = "../path/to/crazy_module.tf"
...
count = 1
include_eip = "true"
}
resource "aws_instance" "instance" {
...
count = "${var.count}"
}
resource "aws_eip" "instance" {
depends_on = [ "aws_security_group.instance" ]
count = "${replace(replace(var.include_eip, "/\\Atrue\\z/", "1"), "/\\A[^1].*\\z/", "0") * var.count}"
}
resource "aws_eip_association" "instance" {
instance_id = "${element(aws_instance.instance.*.id, count.index)}"
allocation_id = "${element(aws_eip.instance.*.id, count.index)}"
count = "${replace(replace(var.include_eip, "/\\Atrue\\z/", "1"), "/\\A[^1].*\\z/", "0") * var.count}"
depends_on = ["aws_eip.instance"]
}
@jc-asdf
Copy link

jc-asdf commented Jun 8, 2016

my variation:

variable "truefalse_map" {
  type = "map"
  default = {
    "1"     = ""
    "0"     = 0
    "true"  = ""
    "false" = 0
  }
}
...
# in a resource ...
...
  count = "${coalesce(lookup(var.truefalse_map, var.attach_ebs), var.quantity)}"
...

Then I'm using 'attach_ebs' as a flag, haha

@imbriaco
Copy link
Author

imbriaco commented Jun 8, 2016

@jc-asdf: Hah! I almost did something like that too. The reason I didn't was that I didn't want to have to define that truefalse map in every module that used my hacky conditional logic, even though I think your approach is a little more readable. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment