in source/lambda/iot-dr-create-r53-checker/lambda_function.py [0:0]
def lambda_handler(event, context):
logger.info('event: {}'.format(event))
responseData = {}
if event['RequestType'] == 'Update':
logger.info('nothing to do in update cycle')
responseData = {'Success': 'Update pass'}
cfnresponse_send(event, context, SUCCESS, responseData, 'CustomResourcePhysicalID')
if event['RequestType'] == 'Delete':
logger.info('nothing to do in delete cycle')
responseData = {'Success': 'Delete pass'}
cfnresponse_send(event, context, SUCCESS, responseData, 'CustomResourcePhysicalID')
if event['RequestType'] == 'Create':
cfn_result = FAILED
responseData = {}
try:
account_id = event['ResourceProperties']['ACCOUNT_ID']
region = event['ResourceProperties']['REGION']
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
tmp_dir = '/tmp/{}'.format(timestamp)
os.makedirs(tmp_dir, mode=0o755)
write_lambda_function(tmp_dir)
get_root_ca(tmp_dir)
create_thing(tmp_dir, timestamp, account_id, region, responseData)
rc = os.system('pip install awsiotsdk -q --no-cache-dir -t {}'.format(tmp_dir))
logger.info('rc: {}'.format(rc))
zip_file = shutil.make_archive('/tmp/iot-dr-r53-checker', 'zip', tmp_dir)
logger.info('zip_file: {}'.format(zip_file))
logger.info('uploading file: {} to s3 bucket: {}'.format(zip_file, S3_BUCKET))
s3 = boto3.resource('s3')
s3.meta.client.upload_file(zip_file, S3_BUCKET, zip_file.split('/')[-1])
endpoint = boto3.client('iot').describe_endpoint(endpointType='iot:Data-ATS')['endpointAddress']
responseData['ENDPOINT'] = endpoint
r=random.random();
query_string = hashlib.sha256(bytes(str(r).encode())).hexdigest()
logger.info('endpoint: {} query_string: {}'.format(endpoint, query_string))
responseData['QUERY_STRING'] = query_string
responseData['CA'] = 'root.ca.pem'
responseData['CLIENT_ID'] = 'r53-checker'
responseData['Success'] = 'R53 health checker lambda created'
logger.info('responseData: {}'.format(responseData))
cfn_result = SUCCESS
except Exception as e:
logger.error('{}'.format(e))
raise Exception(e)
cfnresponse_send(event, context, cfn_result, responseData, 'CustomResourcePhysicalID')