-
Notifications
You must be signed in to change notification settings - Fork 19
/
ec2_ip_route53.py
executable file
·57 lines (52 loc) · 1.49 KB
/
ec2_ip_route53.py
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
#!/usr/bin/python
'''
This script finds an instance by its ID.
Then, it finds out its public IP address and changes Route53 'A' record.
'''
import boto3
def find_ip(id):
'''
Finds a public IP address based on instance ID.
Returns a public IP address.
'''
ec2 = boto3.resource('ec2')
instance = ec2.Instance(id)
ip = instance.public_ip_address
return ip
def change_route53_record(zone_id, domain, ip):
'''
Changes Route53 type A record.
'''
r53 = boto3.client('route53')
r53.change_resource_record_sets(
HostedZoneId=zone_id,
ChangeBatch={
'Comment': 'test',
'Changes': [
{
'Action': 'UPSERT',
'ResourceRecordSet': {
'Name': domain,
'ResourceRecords': [
{
'Value': ip
}
],
'Type': 'A',
'TTL': 300
}
},
]
}
)
# set instance ID
instance_id = '' # Instance ID, e.g. 'i-0111112233'
# set Hosted Zone ID
zone_id = '' # Hosted Zone ID, e.g. 'ZBDAAABBBCCC'
# domain
domain = '' # Domain, e.g. technoff.eu
# find the public IP address
ip_address = find_ip(instance_id)
# change 'A' record
change_a_record = change_route53_record(zone_id, domain, ip_address)
print(domain + ' record was changed to: ' + ip_address)