Created
July 8, 2016 09:23
-
-
Save masanoriyamaguchi/c8d3a06fe0e8c708946212155da464c4 to your computer and use it in GitHub Desktop.
AWS VPCに関する情報をxmlで吐き出すpythonスクリプト
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
# -*- coding: utf-8 -*- | |
import boto3 | |
import json | |
import xmltodict | |
from datetime import datetime | |
client = boto3.client('ec2') | |
TARGET_VPC_ID = 'YOUR VPC ID' | |
root = {} | |
root['VPC'] = {} | |
def main(): | |
print('***** main start *****') | |
root['VPC'] = describe_vpcs() | |
root['VPC']['Attributes'] = {} | |
root['VPC']['Attributes']['Attribute'] = describe_vpc_attribute() | |
root['VPC']['VpcEndpointServices'] = describe_vpc_endpoint_services() | |
root['VPC']['Endpoints'] = describe_vpc_endpoints() | |
root['VPC']['AttachedInternetGateways'] = describe_internet_gateways() | |
root['VPC']['AttachedNatGateways'] = describe_nat_gateways() | |
root['VPC']['NetworkAcls'] = describe_network_acls() | |
root['VPC']['RouteTables'] = describe_route_tables() | |
root['VPC']['DhcpOptions'] = describe_dhcp_options() | |
root['VPC']['Subnets'] = describe_subnets() | |
#root['VPC']['PeeringConnections'] = describe_vpc_peering_connections() | |
response = xmltodict.unparse(root, pretty=True) | |
print response | |
print('***** main finish *****') | |
# VPCピアリング情報を取得 | |
#def describe_vpc_peering_connections(): | |
# desc_vpc_pcs = client.describe_vpc_peering_connections() | |
# | |
# del desc_vpc_pcs["ResponseMetadata"] | |
# | |
# return desc_vpc_pcs | |
# DHCPオプションを取得 | |
def describe_dhcp_options(): | |
desc_dhcp_opt = client.describe_dhcp_options() | |
del desc_dhcp_opt["ResponseMetadata"] | |
desc_dhcp_opt["DhcpOption"] = desc_dhcp_opt.pop("DhcpOptions") | |
return desc_dhcp_opt | |
# Route Tableの情報取得 | |
def describe_route_tables(): | |
desc_routes = client.describe_route_tables( | |
Filters=[ | |
{ | |
'Name': 'vpc-id', | |
'Values': [TARGET_VPC_ID] | |
}, | |
] | |
) | |
del desc_routes["ResponseMetadata"] | |
desc_routes["RouteTable"] = desc_routes.pop("RouteTables") | |
for desc_routes_tmp in desc_routes["RouteTable"]: | |
del desc_routes_tmp["VpcId"] | |
return desc_routes | |
# Network ACLの情報取得 | |
def describe_network_acls(): | |
desc_nacls = client.describe_network_acls( | |
Filters=[ | |
{ | |
'Name': 'vpc-id', | |
'Values': [TARGET_VPC_ID] | |
}, | |
] | |
) | |
del desc_nacls["ResponseMetadata"] | |
desc_nacls["NetworkAcl"] = desc_nacls.pop("NetworkAcls") | |
for desc_nacls_tmp in desc_nacls["NetworkAcl"]: | |
del desc_nacls_tmp["VpcId"] | |
return desc_nacls | |
# NAT Gatewayの情報取得 | |
def describe_nat_gateways(): | |
desc_nats = client.describe_nat_gateways( | |
Filters=[ | |
{ | |
'Name': 'vpc-id', | |
'Values': [TARGET_VPC_ID] | |
}, | |
] | |
) | |
del desc_nats["ResponseMetadata"] | |
for desc_nats_aft in desc_nats["NatGateways"]: | |
del desc_nats_aft["VpcId"] | |
return desc_nats_aft | |
# VPCにアタッチされているIntenet Gateway の一覧を取得 | |
def describe_internet_gateways(): | |
desc_igws = client.describe_internet_gateways( | |
Filters=[ | |
{ | |
'Name': 'attachment.vpc-id', | |
'Values': [TARGET_VPC_ID] | |
}, | |
] | |
) | |
del desc_igws["ResponseMetadata"] | |
return desc_igws | |
# VPCエンドポイントの一覧および詳細情報取得 | |
def describe_vpc_endpoints(): | |
desc_vpc_endpoints = client.describe_vpc_endpoints( | |
Filters=[ | |
{ | |
'Name': 'vpc-id', | |
'Values': [TARGET_VPC_ID] | |
}, | |
] | |
) | |
del desc_vpc_endpoints["ResponseMetadata"] | |
return desc_vpc_endpoints | |
# サポートされているVPCエンドポイントの一覧取得 | |
def describe_vpc_endpoint_services(): | |
desc_vpc_es = client.describe_vpc_endpoint_services() | |
del desc_vpc_es["ResponseMetadata"] | |
return desc_vpc_es | |
# VPC情報を取得 | |
def describe_vpcs(): | |
desc_vpcs = client.describe_vpcs( | |
VpcIds=[ | |
TARGET_VPC_ID, | |
], | |
Filters=[ | |
{ | |
'Name': 'vpc-id', | |
'Values': [TARGET_VPC_ID] | |
}, | |
] | |
) | |
del desc_vpcs["ResponseMetadata"] | |
return desc_vpcs | |
# VPC enablednshostnames, enablednssupportの設定状態を取得 | |
def describe_vpc_attribute(): | |
vpc_attrs = ['enableDnsHostnames','enableDnsSupport'] | |
vpc_attr_results = [] | |
for vpc_attr in vpc_attrs: | |
vpc_attr_result = client.describe_vpc_attribute( | |
VpcId = TARGET_VPC_ID, | |
Attribute = vpc_attr | |
) | |
del vpc_attr_result["ResponseMetadata"] | |
del vpc_attr_result["VpcId"] | |
vpc_attr_results.append(vpc_attr_result) | |
return vpc_attr_results | |
# サブネットの一覧情報を取得 | |
def describe_subnets(): | |
desc_subnets = client.describe_subnets( | |
Filters=[ | |
{ | |
'Name': 'vpc-id', | |
'Values': [ | |
'vpc-99e5b1fc', | |
] | |
}, | |
] | |
) | |
del desc_subnets["ResponseMetadata"] | |
desc_subnets["Subnet"] = desc_subnets.pop("Subnets") | |
for desc_subnets_tmp in desc_subnets["Subnet"]: | |
del desc_subnets_tmp["VpcId"] | |
return desc_subnets | |
def output(name, output_data): | |
f = open(name, 'w') | |
f.write(output_data) | |
f.close() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment