# ================================================================================== # Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. # Permission is hereby granted, free of charge, to any person obtaining a copy of this # software and associated documentation files (the "Software"), to deal in the Software # without restriction, including without limitation the rights to use, copy, modify, # merge, publish, distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # ================================================================================== # # stvblogPrepareProcess # by: Rob Dachowski # For questions or feedback, please contact robdac@amazon.com # # Purpose: The labmda does the initial setup for running the process # # Change Log: # 3/1/21: Initial version # # ================================================================================== import json import uuid import boto3 from botocore.exceptions import ClientError import os import ssmparms as sp import stmparms as stm import datetime import stverrors # ================================================================================== # Function:lambda_handler # Purpose: This is the main function for this lambda. I # Parameters: # event - the input json structure # context - lambda context # ================================================================================== def lambda_handler(event, context): print("===> stvblogCheckMediaConvertJobStatus: " + "\nEvent:" + str(event)) print("\t---> Boto Version: ", boto3.__version__) # Load the parms from DynamoDB parms = stm.get_stm_parms(event['Outputs']['process']['ProcessName']) if not parms: # We have an issue, so get out raise stvDynamoDBError("*** Unable to load parms from DynamoDB ***") pc = parms['Item']['Config'] pi = parms['Item']['Inputs'] po = parms['Item']['Outputs'] # # pull the MediaConvert endpoint URL and Job ID from the event JSON and check the status # region = pc['region'] mc_endpoint_url = po['mediaConvert']['mediaConvertEndpointUrl'] mediaconvert = boto3.client('mediaconvert', region_name=region, endpoint_url=mc_endpoint_url, verify=True) try: # # Check on the MediaConvert Job # job_id = po['mediaConvert']['mediaConvertJobId'] print( "\t---> Checking on status of MediaConvert Job: ", job_id) job_details = mediaconvert.get_job(Id=job_id) status = job_details['Job']['Status'] except ClientError as e: print("*** Error reading Checking on the MediaConvert Job ", po['mediaConvert']['mediaConvertJobId'] + " ***") raise stvError("*** Error Code: ", e.response['Error']['Message'] + " ***") print( "\t---> Status of MediaConvert Job: ", status) # # Set up the response # response = event response['Outputs']['mediaConvert']['mediaConvertJobStatus'] = status response['Outputs']['mediaConvert']['startTimeStamp'] = datetime.datetime.now().strftime("%Y-%m-%d %H-%M-%S") # # Now update the status # if status == 'COMPLETE': response['Outputs']['mediaConvert']['completionTime'] = job_details['Job']['Timing']['FinishTime'].strftime("%Y-%m-%d %H-%M-%S") # # Load system parameter for CloudFront URL # print( "\t---> Getting Parameters from Parameter Store" ) cloudfront_url = sp.get_parameter('/stvblog/CloudFrontUrl')['Parameter']['Value'] print( "\t---> Retrieved Parameters from Parameter Store", cloudfront_url ) # # Assemble the complete video playback URL # playback_url = "https://" + cloudfront_url + "/" + po['process']['uuid'] + "/" + pc['finalOutput'] + "/" + os.path.splitext(pi['mediaFile'])[0] + ".m3u8" # # Write the video playback URL to JSON # response['Outputs']['process']['videoPlaybackUrl'] = playback_url if stm.update_stm_parms(response['Outputs']['process']['ProcessName'], response['Config'], response['Inputs'], response['Outputs'], event['Targets']): print('===>stvblogCheckMediaConvertJobStatus Complete') return response else: raise stvError("*** Error writing to the stvblog table ***")