Skip to content

Instantly share code, notes, and snippets.

@alastairhm
Last active March 6, 2019 10:52
Show Gist options
  • Save alastairhm/f0792d143be31f26d715d8e29055e715 to your computer and use it in GitHub Desktop.
Save alastairhm/f0792d143be31f26d715d8e29055e715 to your computer and use it in GitHub Desktop.
Get AWS VPC tags using Boto3, for use as an external Terraform data source
import json
import boto3
import sys
data = json.load(sys.stdin)
ec2 = boto3.resource('ec2', region_name=data["region"])
client = boto3.client('ec2', region_name=data["region"])
filters = [{'Name':'tag:Name', 'Values':[data["vpc_name"]]}]
vpcs = list(ec2.vpcs.filter(Filters=filters))
tags = {}
for vpc in vpcs:
response = client.describe_vpcs(
VpcIds=[
vpc.id,
]
)
for tag in response["Vpcs"][0]["Tags"]:
tags[tag["Key"]] = tag["Value"]
sys.stdout.write(json.dumps(tags))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment