Skip to content

Instantly share code, notes, and snippets.

@gswallow
Last active February 22, 2018 18:19
Show Gist options
  • Save gswallow/de465bd3579520cc8e072483b913c5c2 to your computer and use it in GitHub Desktop.
Save gswallow/de465bd3579520cc8e072483b913c5c2 to your computer and use it in GitHub Desktop.
LOL nested loops in HCL
irb(main):001:0> 3 - 0 % 3 - 1
=> 2
irb(main):002:0> 3 - 1 % 3 - 1
=> 1
irb(main):003:0> 3 - 2 % 3 - 1
=> 0
irb(main):004:0> 3 - 3 % 3 - 1
=> 2
irb(main):005:0> 3 - 4 % 3 - 1
=> 1
irb(main):006:0> 3 - 5 % 3 - 1
=> 0
irb(main):007:0> 0 % 2
=> 0
irb(main):008:0> 1 % 2
=> 1
irb(main):009:0> 2 % 2
=> 0
irb(main):010:0> 3 % 2
=> 1
irb(main):011:0> 4 % 2
=> 0
irb(main):012:0> 5 % 2
=> 1
resource "aws_route" "datacenter_public" {
count = "${length(distinct(data.aws_route_table.public.*.id)) * length(var.VPN_ROUTES)}"
route_table_id = "${data.aws_route_table.public.*.id[length(distinct(data.aws_route_table.public.*.id)) - count.index % length(distinct(data.aws_route_table.public.*.id)) - 1]}"
destination_cidr_block = "${var.VPN_ROUTES[count.index % length(data.aws_route_table.public.*.id) % length(var.VPN_ROUTES)]}"
gateway_id = "${aws_vpn_gateway.vpn_gw.id}"
}
resource "aws_route" "datacenter_nat" {
count = "${length(distinct(data.aws_route_table.nat.*.id)) * length(var.VPN_ROUTES)}"
route_table_id = "${data.aws_route_table.nat.*.id[length(distinct(data.aws_route_table.nat.*.id)) - count.index % length(distinct(data.aws_route_table.nat.*.id)) - 1]}"
destination_cidr_block = "${var.VPN_ROUTES[count.index % length(var.VPN_ROUTES)]}"
gateway_id = "${aws_vpn_gateway.vpn_gw.id}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment