Last active
August 26, 2017 16:11
-
-
Save sloppycoder/e2c8e9cb442d4cfc5fc9b443f6f817b7 to your computer and use it in GitHub Desktop.
Register EC2 instance public IP address with Route53
This file contains hidden or 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 | |
| # | |
| # EC2Instance must have an IAM role assign to it which has access to Route53 services | |
| # in this script the role is called 'route53role' | |
| # | |
| from boto import utils, ec2, route53 | |
| instance_metadata = utils.get_instance_metadata(timeout=0.5, num_retries=1) | |
| region = instance_metadata['placement']['availability-zone'][:-1] | |
| instance_id = instance_metadata['instance-id'] | |
| access_key_id = instance_metadata['iam']['security-credentials']['route53role']['AccessKeyId'] | |
| secret_access_key = instance_metadata['iam']['security-credentials']['route53role']['SecretAccessKey'] | |
| public_ip = instance_metadata['network']['interfaces']['macs'].values()[0]['public-ipv4s'] | |
| # retrieve tag 'name' | |
| #ec2_conn = ec2.connect_to_region(region, aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key) | |
| #tags = ec2_conn.get_all_tags(filters={'resource-id': instance_id, 'key': 'ddns_hostname'}) | |
| #print tags | |
| hostname = 's'# get_all_tags not working yet, so hard code hostname for now | |
| domain='mydomain.com' | |
| # register 'name' as A record | |
| r53_conn = route53.connect_to_region(region) | |
| zone = r53_conn.get_zone(domain) | |
| change_set = route53.record.ResourceRecordSets(r53_conn, zone.id) | |
| changes1 = change_set.add_change("UPSERT", hostname + '.' + domain, type="A", ttl=3600) | |
| changes1.add_value(public_ip) | |
| change_set.commit() |
This file contains hidden or 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
| # copy the content to file etc/systemd/system/ddns-route53.service | |
| # then | |
| # systemctl daemon-reload | |
| # systemctl enable ddns-route53 | |
| # systemctl start ddns-route53 | |
| [Unit] | |
| Description=python script to register public IP to route53 DNS server | |
| [Service] | |
| WorkingDirectory=/tmp | |
| ExecStart=/usr/local/bin/ddns_route53 | |
| Restart=always | |
| [Install] | |
| WantedBy=multi-user.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment