10000 Added support for split horizon DNS · alxsey/aws-lambda-ddns-function@88f9d71 · GitHub
[go: up one dir, main page]

Skip to content

Commit 88f9d71

Browse files
committed
Added support for split horizon DNS
1 parent 480d48c commit 88f9d71

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

union.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
dynamodb_client = boto3.client('dynamodb')
1414
dynamodb_resource = boto3.resource('dynamodb')
1515

16+
#event = { "id": "ee376907-2647-4179-9203-343cfb3017a4", "detail-type": "EC2 Instance State-change Notification", "source": "aws.ec2", "account": "123456789012", "time": "2015-11-11T21:30:34Z", "region": "us-east-1", "resources": [ "arn:aws:ec2:us-east-1:123456789012:instance/i-04308bdc" ], "detail": { "instance-id": "i-04308bdc", "state": "stopped" }}
17+
1618
def lambda_handler(event, context):
1719
""" Check to see whether a DynamoDB table already exists. If not, create it. This table is used to keep a record of
1820
instances that have been created along with their attributes. This is necessary because when you terminate an instance
@@ -131,7 +133,7 @@ def lambda_handler(event, context):
131133
# need to do this when you create the reverse lookup zone because the association is done automatically.
132134
if filter(lambda record: record['Name'] == reversed_lookup_zone, hosted_zones['HostedZones']):
133135
print 'Reverse lookup zone found:', reversed_lookup_zone
134-
reverse_lookup_zone_id = get_zone_id(reversed_lookup_zone)
136+
reverse_lookup_zone_id = get_zone_id(reversed_lookup_zone, 'Private')
135137
reverse_hosted_zone_properties = get_hosted_zone_properties(reverse_lookup_zone_id)
136138
if vpc_id in map(lambda x: x['VPCId'], reverse_hosted_zone_properties['VPCs']):
137139
print 'Reverse lookup zone %s is associated with VPC %s' % (reverse_lookup_zone_id, vpc_id)
@@ -146,7 +148,7 @@ def lambda_handler(event, context):
146148
# create private hosted zone for reverse lookups
147149
if state == 'running':
148150
create_reverse_lookup_zone(instance, reversed_domain_prefix, region)
149-
reverse_lookup_zone_id = get_zone_id(reversed_lookup_zone)
151+
reverse_lookup_zone_id = get_zone_id(reversed_lookup_zone, 'Private')
150152
# Wait a random amount of time. This is a poor-mans back-off if a lot of instances are launched all at once.
151153
time.sleep(random.random())
152154

@@ -158,7 +160,7 @@ def lambda_handler(event, context):
158160
if tag.get('Value').lstrip().lower() in private_hosted_zone_collection:
159161
print 'Private zone found:', tag.get('Value')
160162
private_hosted_zone_name = tag.get('Value').lstrip().lower()
161-
private_hosted_zone_id = get_zone_id(private_hosted_zone_name)
163+
private_hosted_zone_id = get_zone_id(private_hosted_zone_name, 'Private')
162164
private_hosted_zone_properties = get_hosted_zone_properties(private_hosted_zone_id)
163165
if state == 'running':
164166
if vpc_id in map(lambda x: x['VPCId'], private_hosted_zone_properties['VPCs']):
@@ -182,10 +184,11 @@ def lambda_handler(event, context):
182184
except BaseException as e:
183185
print e
184186
# create PTR record
185-
elif tag.get('Value').lstrip().lower() in public_hosted_zones_collection:
187+
# Changed from elif to if
188+
if tag.get('Value').lstrip().lower() in public_hosted_zones_collection:
186189
print 'Public zone found', tag.get('Value')
187190
public_hosted_zone_name = tag.get('Value').lstrip().lower()
188-
public_hosted_zone_id = get_zone_id(public_hosted_zone_name)
191+
public_hosted_zone_id = get_zone_id(public_hosted_zone_name, 'Public')
189192
# create A record in public zone
190193
if state =='running':
191194
try:
@@ -197,8 +200,8 @@ def lambda_handler(event, context):
197200
delete_resource_record(public_hosted_zone_id, public_host_name, public_hosted_zone_name, 'A', public_ip)
198201
except BaseException as e:
199202
print e
200-
else:
201-
print 'No matching zone found for %s' % tag.get('Value')
203+
#else:
204+
# print 'No matching zone found for %s' % tag.get('Value')
202205
else:
203206
print '%s is not a valid host name' % tag.get('Value')
204207
# Consider making this an elif CNAME
@@ -209,9 +212,9 @@ def lambda_handler(event, context):
209212
cname = tag.get('Value').lstrip().lower()
210213 10000
cname_host_name = cname.split('.')[0]
211214
cname_domain_suffix = cname[cname.find('.')+1:]
212-
cname_domain_suffix_id = get_zone_id(cname_domain_suffix)
215+
cname_domain_suffix_id = get_zone_id(cname_domain_suffix, 'Private')
213216
for cname_private_hosted_zone in private_hosted_zone_collection:
214-
cname_private_hosted_zone_id = get_zone_id(cname_private_hosted_zone)
217+
cname_private_hosted_zone_id = get_zone_id(cname_private_hosted_zone, 'Private')
215218
if cname_domain_suffix_id == cname_private_hosted_zone_id:
216219
if cname.endswith(cname_private_hosted_zone):
217220
#create CNAME record in private zone
@@ -227,7 +230,7 @@ def lambda_handler(event, context):
227230
print e
228231
for cname_public_hosted_zone in public_hosted_zones_collection:
229232
if cname.endswith(cname_public_hosted_zone):
230-
cname_public_hosted_zone_id = get_zone_id(cname_public_hosted_zone)
233+
cname_public_hosted_zone_id = get_zone_id(cname_public_hosted_zone, 'Public')
231234
#create CNAME record in public zone
232235
if state == 'running':
233236
try:
@@ -255,7 +258,7 @@ def lambda_handler(event, context):
255258
private_hosted_zone_name = configuration[0]
256259
print 'Private zone found %s' % private_hosted_zone_name
257260
# TODO need a way to prevent overlapping subdomains
258-
private_hosted_zone_id = get_zone_id(private_hosted_zone_name)
261+
private_hosted_zone_id = get_zone_id(private_hosted_zone_name, 'Private')
259262
private_hosted_zone_properties = get_hosted_zone_properties(private_hosted_zone_id)
260263
# create A records and PTR records
261264
if state == 'running':
@@ -335,12 +338,16 @@ def delete_resource_record(zone_id, host_name, hosted_zone_name, type, value):
335338
]
336339
}
337340
)
338-
def get_zone_id(zone_name):
341+
def get_zone_id(zone_name, zone_type):
339342
"""This function returns the zone id for the zone name that's passed into the function."""
343+
#TODO determine which zone ID to return based on the calling function
340344
if zone_name[-1] != '.':
341345
zone_name = zone_name + '.'
342346
hosted_zones = route53.list_hosted_zones()
343-
x = filter(lambda record: record['Name'] == zone_name, hosted_zones['HostedZones'])
347+
if zone_type == 'Private':
348+
x = filter(lambda record: record['Name'] == zone_name and record['Config']['PrivateZone'] == True, hosted_zones['HostedZones'])
349+
if zone_type == 'Public':
350+
x = filter(lambda record: record['Name'] == zone_name and record['Config']['PrivateZone'] == False, hosted_zones['HostedZones'])
344351
try:
345352
zone_id_long = x[0]['Id']
346353
zone_id = str.split(str(zone_id_long),'/')[2]
@@ -452,3 +459,5 @@ def get_hosted_zone_properties(zone_id):
452459
hosted_zone_properties = route53.get_hosted_zone(Id=zone_id)
453460
hosted_zone_properties.pop('ResponseMetadata')
454461
return hosted_zone_properties
462+
463+
#lambda_handler(event)

0 commit comments

Comments
 (0)
0