Last active
June 10, 2019 18:17
-
-
Save pasternak/446f4b9c1a8188322edef9215a07c2ad to your computer and use it in GitHub Desktop.
given list of subnets, find out which one are public and which are private
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
variable "subnets" { | |
default = "subnet-xxxxx,subnet-yyyyy,subnet-zzzzzz" | |
} | |
locals { | |
subnets = ["${split(",", replace(var.subnets, "/(,$|\\s)/", ""))}"] | |
private_subnets = "${compact(null_resource.subnets.*.triggers.private)}" | |
public_subnets = "${compact(null_resource.subnets.*.triggers.public)}" | |
} | |
data "aws_route_table" "selected" { | |
count = "${length(local.subnets)}" | |
subnet_id = "${element(local.subnets, count.index)}" | |
} | |
data "aws_route" "selected" { | |
count = "${length(local.subnets)}" | |
route_table_id = "${element(data.aws_route_table.selected.*.id, count.index)}" | |
destination_cidr_block = "0.0.0.0/0" | |
} | |
resource "null_resource" "subnets" { | |
count = "${length(local.subnets)}" | |
triggers = { | |
public = "${element(data.aws_route.selected.*.gateway_id, count.index) != "" ? "${element(local.subnets, count.index)}" : ""}" | |
private = "${element(data.aws_route.selected.*.gateway_id, count.index) == "" ? "${element(local.subnets, count.index)}" : ""}" | |
} | |
} | |
output "sub" { | |
value = "${data.aws_route.selected.*.gateway_id}" | |
} | |
output "private_subnets" { | |
value = "${local.private_subnets}" | |
} | |
output "public_subnets" { | |
value = "${local.public_subnets}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment