Skip to content

Instantly share code, notes, and snippets.

@af-inet
Created May 24, 2018 17:10
Show Gist options
  • Select an option

  • Save af-inet/15a6e9df6d4ebddd0a2cb05306238941 to your computer and use it in GitHub Desktop.

Select an option

Save af-inet/15a6e9df6d4ebddd0a2cb05306238941 to your computer and use it in GitHub Desktop.
Create routes from an AWS VPC to a Virtual Private Gateway.
#!/usr/bin/env python
#
# https://boto3.readthedocs.io/en/latest/reference/services/ec2.html
#
import argparse
import boto3
client = boto3.client("ec2")
def parse_args():
parser = argparse.ArgumentParser(
description='create routes for a peering connection between 2 VPCs')
parser.add_argument('VPC_ID',
type=str,
help='VPC id you want to add routes for')
parser.add_argument('VPG_ID',
type=str,
help='Virtual Private Gateway id you want to add routes for')
parser.add_argument('CIDR',
type=str,
help='CIDR to add routes for, ex: 192.168.0.0/16')
return parser.parse_args()
def get_routetables_by_vpc(vpc_id):
route_list = client.describe_route_tables(Filters=[
{
"Name": "vpc-id",
"Values": [vpc_id]
}
])
if route_list.get('RouteTables'):
return route_list['RouteTables']
else:
raise Exception("No Route Tables Found for VPC: %s" % vpc_id)
def main():
args = parse_args()
tables_1 = get_routetables_by_vpc(args.VPC_ID)
print("[*] creating routes...")
# routes VPC to VPG
for table in tables_1:
response = client.create_route(
DestinationCidrBlock=args.CIDR,
DryRun=False,
RouteTableId=table["RouteTableId"],
GatewayId=args.VPG_ID)
print(response)
print("[*] done!")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment