Skip to content

Instantly share code, notes, and snippets.

@pasternak
Last active June 10, 2019 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pasternak/446f4b9c1a8188322edef9215a07c2ad to your computer and use it in GitHub Desktop.
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
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