13
13
dynamodb_client = boto3 .client ('dynamodb' )
14
14
dynamodb_resource = boto3 .resource ('dynamodb' )
15
15
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
+
16
18
def lambda_handler (event , context ):
17
19
""" Check to see whether a DynamoDB table already exists. If not, create it. This table is used to keep a record of
18
20
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):
131
133
# need to do this when you create the reverse lookup zone because the association is done automatically.
132
134
if filter (lambda record : record ['Name' ] == reversed_lookup_zone , hosted_zones ['HostedZones' ]):
133
135
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' )
135
137
reverse_hosted_zone_properties = get_hosted_zone_properties (reverse_lookup_zone_id )
136
138
if vpc_id in map (lambda x : x ['VPCId' ], reverse_hosted_zone_properties ['VPCs' ]):
137
139
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):
146
148
# create private hosted zone for reverse lookups
147
149
if state == 'running' :
148
150
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' )
150
152
# Wait a random amount of time. This is a poor-mans back-off if a lot of instances are launched all at once.
151
153
time .sleep (random .random ())
152
154
@@ -158,7 +160,7 @@ def lambda_handler(event, context):
158
160
if tag .get ('Value' ).lstrip ().lower () in private_hosted_zone_collection :
159
161
print 'Private zone found:' , tag .get ('Value' )
160
162
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' )
162
164
private_hosted_zone_properties = get_hosted_zone_properties (private_hosted_zone_id )
163
165
if state == 'running' :
164
166
if vpc_id in map (lambda x : x ['VPCId' ], private_hosted_zone_properties ['VPCs' ]):
@@ -182,10 +184,11 @@ def lambda_handler(event, context):
182
184
except BaseException as e :
183
185
print e
184
186
# 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 :
186
189
print 'Public zone found' , tag .get ('Value' )
187
190
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' )
189
192
# create A record in public zone
190
193
if state == 'running' :
191
194
try :
@@ -197,8 +200,8 @@ def lambda_handler(event, context):
197
200
delete_resource_record (public_hosted_zone_id , public_host_name , public_hosted_zone_name , 'A' , public_ip )
198
201
except BaseException as e :
199
202
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')
202
205
else :
203
206
print '%s is not a valid host name' % tag .get ('Value' )
204
207
# Consider making this an elif CNAME
@@ -209,9 +212,9 @@ def lambda_handler(event, context):
209
212
cname = tag .get ('Value' ).lstrip ().lower ()
210
213
10000
cname_host_name = cname .split ('.' )[0 ]
211
214
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' )
213
216
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' )
215
218
if cname_domain_suffix_id == cname_private_hosted_zone_id :
216
219
if cname .endswith (cname_private_hosted_zone ):
217
220
#create CNAME record in private zone
@@ -227,7 +230,7 @@ def lambda_handler(event, context):
227
230
print e
228
231
for cname_public_hosted_zone in public_hosted_zones_collection :
229
232
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' )
231
234
#create CNAME record in public zone
232
235
if state == 'running' :
233
236
try :
@@ -255,7 +258,7 @@ def lambda_handler(event, context):
255
258
private_hosted_zone_name = configuration [0 ]
256
259
print 'Private zone found %s' % private_hosted_zone_name
257
260
# 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' )
259
262
private_hosted_zone_properties = get_hosted_zone_properties (private_hosted_zone_id )
260
263
# create A records and PTR records
261
264
if state == 'running' :
@@ -335,12 +338,16 @@ def delete_resource_record(zone_id, host_name, hosted_zone_name, type, value):
335
338
]
336
339
}
337
340
)
338
- def get_zone_id (zone_name ):
341
+ def get_zone_id (zone_name , zone_type ):
339
342
"""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
340
344
if zone_name [- 1 ] != '.' :
341
345
zone_name = zone_name + '.'
342
346
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' ])
344
351
try :
345
352
zone_id_long = x [0 ]['Id' ]
346
353
zone_id = str .split (str (zone_id_long ),'/' )[2 ]
@@ -452,3 +459,5 @@ def get_hosted_zone_properties(zone_id):
452
459
hosted_zone_properties = route53 .get_hosted_zone (Id = zone_id )
453
460
hosted_zone_properties .pop ('ResponseMetadata' )
454
461
return hosted_zone_properties
462
+
463
+ #lambda_handler(event)
0 commit comments