Skip to content

Instantly share code, notes, and snippets.

@managedkaos
Created November 24, 2020 03:23
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 managedkaos/d7c1d4e4a34920927ad57d7698ccd9e4 to your computer and use it in GitHub Desktop.
Save managedkaos/d7c1d4e4a34920927ad57d7698ccd9e4 to your computer and use it in GitHub Desktop.
Import an AWS VPC using just the VPC ID. Public and Private subnets are imported dynamically using filtering.
variable "vpc_id" {
type = string
default = "vpc-12345678"
}
data "aws_vpc" "vpc" {
id = var.vpc_id
}
data "aws_subnet_ids" "public" {
vpc_id = data.aws_vpc.vpc.id
filter {
name = "tag:Name"
values = ["*public*"]
}
}
data "aws_subnet" "public" {
for_each = data.aws_subnet_ids.public.ids
id = each.value
}
data "aws_subnet_ids" "private" {
vpc_id = data.aws_vpc.vpc.id
filter {
name = "tag:Name"
values = ["*private*"]
}
}
data "aws_subnet" "private" {
for_each = data.aws_subnet_ids.private.ids
id = each.value
}
locals {
private = [for i in data.aws_subnet.private : i]
}
output "private" {
value = local.private[0].availability_zone
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment