Skip to content

Instantly share code, notes, and snippets.

@kclinden
Last active October 13, 2020 21:19
Show Gist options
  • Save kclinden/415cf85fe90457a40017bf5add370045 to your computer and use it in GitHub Desktop.
Save kclinden/415cf85fe90457a40017bf5add370045 to your computer and use it in GitHub Desktop.
Get Subnet ARNs by Tag
#This was an interesting thing I was trying to figure out. I used this to pass into a IAM Policy Resource Limit
# Get all of the subents with tag packer; returns a set of ids
data "aws_subnet_ids" "this" {
vpc_id = var.vpc_id
tags = {
tag_name = "tag_value"
}
}
#Get the arns for each subnet
data "aws_subnet" "this" {
for_each = data.aws_subnet_ids.this.ids
id = each.key
}
output "subnet_ids" {
value = data.aws_subnet_ids.this.ids
}
# This value here can then be sent to another resource for use as a list of arns
output "subnet_arns" {
value = [for s in data.aws_subnet.this : s.arn]
}
data "aws_iam_policy_document" "document" {
statement {
effect = "Allow"
actions = ["ec2:CreateNetworkInterfacePermission"]
resources = ["${data.aws_caller_identity.current.arn}:network-interface/*"]
condition {
test = "StringEquals"
variable = "ec2:Subnet"
values = var.subnets
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment