This repository was archived by the owner on Apr 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_dns.py
More file actions
71 lines (57 loc) · 2.03 KB
/
update_dns.py
File metadata and controls
71 lines (57 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
# https://gist.github.com/chrisguitarguy/e9cb271f6ac882627d0d61efe03dc8ae was used as inspiration
import argparse
import os
import time
import boto3 as aws
def _parse_args(args=None):
p = argparse.ArgumentParser(description='Update a hostname record in route53 with the current IP address of instances in cluster')
p.add_argument("cluster_name", help="Cluster name" )
p.add_argument('zone_id', help='The DNS zone id to update')
p.add_argument('hostname', help='The DNS name to update')
return p.parse_args(args)
def _get_local_ipv4s(cluster_name):
client = aws.client('ecs')
ec2_client = aws.client('ec2')
ips = []
ec2_instances_ids = []
r = client.list_container_instances(
cluster = cluster_name
)
r = client.describe_container_instances(
cluster = cluster_name,
containerInstances = r["containerInstanceArns"]
)
for instance in r["containerInstances"]:
ec2_instances_ids.append(instance["ec2InstanceId"])
r = ec2_client.describe_instances(
InstanceIds=ec2_instances_ids
)
for reservation in r["Reservations"]:
for instance in reservation["Instances"]:
ips.append(instance["PrivateIpAddress"])
return ips
def _update_dns(ips, zone_id, hostname):
dns = aws.client('route53')
dns.change_resource_record_sets(
HostedZoneId=zone_id,
ChangeBatch={
'Comment': 'Update {} record from ASG'.format(hostname),
'Changes': [{
'Action': 'UPSERT',
'ResourceRecordSet': {
'Name': hostname,
'Type': 'A',
'TTL': 60,
'ResourceRecords': [{'Value':ip} for ip in ips],
},
}],
},
)
def main(args=None):
args = _parse_args(args)
ips = _get_local_ipv4s(args.cluster_name)
_update_dns(ips, args.zone_id, args.hostname)
if __name__ == '__main__':
time.sleep(60) # needs a pause otherwise there is no ip
main()