-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
67 lines (59 loc) · 2.6 KB
/
lambda_function.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
58
59
60
61
62
63
64
65
66
67
import boto3
import datetime
import json
import logging
import os
def defaultconverter(o):
if isinstance(o, datetime.datetime):
return o.__str__()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
instance_id = os.environ.get('EC2_INSTANCE_ID')
route53_hosted_zone_id = os.environ.get('ROUTE53_HOSTED_ZONE_ID')
route53_dns_record_name = os.environ.get('ROUTE53_DNS_RECORD_NAME')
route53_dns_record_ttl = os.environ.get('ROUTE53_DNS_RECORD_TTL')
route53_dns_record_type = os.environ.get('ROUTE53_DNS_RECORD_TYPE')
session = boto3.Session()
def update_route_53(event):
event_detail = event.get('detail')
event_instance_id = event_detail.get('instance-id')
event_state = event_detail.get('state')
logger.info('Received EC2 state change notification, instance {event_instance_id} transistion to {event_state} state.'.format(
event_instance_id=event_instance_id, event_state=event_state))
if event_instance_id == instance_id and event_state == 'running':
ec2_client = session.client('ec2')
# fetch EC2 public ip
descriptions = ec2_client.describe_instances(
InstanceIds=[event_instance_id])
reservations = descriptions.get('Reservations')
instances = reservations[0].get('Instances')
public_ip_address = instances[0].get('PublicIpAddress')
logger.info('Public IP address of instance {event_instance_id} is {public_ip_address}'.format(
event_instance_id=event_instance_id, public_ip_address=public_ip_address))
logger.info('Updating Route53 record {route53_dns_record_name}'.format(
route53_dns_record_name=route53_dns_record_name))
# update route53 record
route53_client = session.client('route53')
response = route53_client.change_resource_record_sets(
ChangeBatch={
'Changes': [
{
'Action': 'UPSERT',
'ResourceRecordSet': {
'Name': route53_dns_record_name,
'ResourceRecords': [
{
'Value': public_ip_address,
},
],
'TTL': int(route53_dns_record_ttl),
'Type': route53_dns_record_type
},
},
]
},
HostedZoneId=route53_hosted_zone_id,
)
return json.dumps(response, default=defaultconverter)
def lambda_handler(event, _):
return update_route_53(event)