Created
December 27, 2017 17:14
-
-
Save keithmeng/f44c306f5f3dda3227b386b6f72b7da3 to your computer and use it in GitHub Desktop.
peering
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
#!/usr/bin/env python | |
import boto3 | |
import argparse | |
def list_private_zones(parsed_args): | |
route53_client = boto3.client('route53') | |
zones = route53_client.list_hosted_zones() | |
for i in range(len(zones['HostedZones'])): | |
z = zones['HostedZones'][i] | |
if z['Config']['PrivateZone'] is True: | |
print "Name: %s" % z['Name'] | |
print "Comment: %s" % z['Config']['Comment'] | |
print "HostedZoneId: %s\n" % z['Id'] | |
def create_peering_connection(parsed_args): | |
vpc_id = parsed_args.vpc_id | |
target_vpc_id = parsed_args.target_vpc_id | |
region = parsed_args.region | |
peered_account_number = parsed_args.peered_account_number | |
dryrun_set = parsed_args.dryrun_set | |
ec2_client = boto3.client('ec2', region_name=region) | |
if vpc_id and target_vpc_id and peered_account_number: | |
response = ec2_client.create_vpc_peering_connection(DryRun=dryrun_set, VpcId=vpc_id, PeerVpcId=target_vpc_id, PeerOwnerId=peered_account_number) | |
print response | |
print "The owner of vpc %s must accept this request" % target_vpc_id | |
else: | |
print "Missing one ore more of required arguments --vpc-id, --target-vpc-id" | |
def create_private_zone_authorization(parsed_args): | |
hosted_zone_id = parsed_args.hosted_zone_id | |
region = parsed_args.region | |
target_vpc_id = parsed_args.target_vpc_id | |
route53_client = boto3.client('route53') | |
if hosted_zone_id and target_vpc_id: | |
response = route53_client.create_vpc_association_authorization(HostedZoneId=hosted_zone_id, VPC={'VPCRegion': region,'VPCId': target_vpc_id}) | |
print response | |
print "Please have the owner of vpc %s run the following command: " % target_vpc_id | |
print "aws route53 associate-vpc-with-hosted-zone --hosted-zone-id %s \ | |
--vpc VPCRegion=%s,VPCId=%s" % (hosted_zone_id, region, target_vpc_id) | |
print "After they run that command, run this script with --delete-private-zone-authorization" | |
else: | |
print "Missing one ore more of required arguments --hosted-zone-id, --target-vpc-id" | |
def delete_private_zone_authorization(parsed_args): | |
hosted_zone_id = parsed_args.hosted_zone_id | |
region = parsed_args.region | |
target_vpc_id = parsed_args.target_vpc_id | |
route53_client = boto3.client('route53') | |
if hosted_zone_id and target_vpc_id: | |
response = route53_client.delete_vpc_association_authorization(HostedZoneId=hosted_zone_id, VPC={'VPCRegion': region,'VPCId': target_vpc_id}) | |
print response | |
else: | |
print "Missing one ore more of required arguments --hosted-zone-id, --target-vpc-id" | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
group = parser.add_mutually_exclusive_group(required=True) | |
group.add_argument('--list-private-zones', dest='action', action='store_const', | |
const=list_private_zones, help='Lists Private Route53 Zones') | |
group.add_argument('--create-peering-connection', dest='action', action='store_const', | |
const=create_peering_connection, | |
help='Initiates the peering request from one VPC to another') | |
group.add_argument('--create-private-zone-authorization', | |
dest='action', action='store_const', const=create_private_zone_authorization, | |
help='Create the authorization to associate a Private Route53 zone with a peered VPC') | |
group.add_argument('--delete-private-zone-authorization', | |
dest='action', action='store_const', const=delete_private_zone_authorization, | |
help='Deletes the authorization request to associate a Private Route53 zone with a peered VPC') | |
parser.add_argument('-r', '--region', default='us-east-1') | |
parser.add_argument('-vid', '--vpc-id', help='VPC Id of the Requesting VPC') | |
parser.add_argument('-tvid', '--target-vpc-id', help='VPC Id of the Target VPC') | |
parser.add_argument('-pan', '--peered-account-number', help='AWS Account number of the account you want to peer with') | |
parser.add_argument('-hzid', '--hosted-zone-id', help='HostZone ID of the Private Zone you want to associate to the peered VPC') | |
parser.add_argument('-dr', '--dryrun-set', default=False, type=bool, help='Set Dryrun flag for create peering connection') | |
parsed_args = parser.parse_args() | |
parsed_args.action(parsed_args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment