""" http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ from botocore.vendored import requests import boto3 import json import string import random import resource_tools def event_handler(event, context): """ Lambda entry point. Print the event first. """ print("Event Input: %s" % json.dumps(event)) try: mediapackage = boto3.client('mediapackage') if event["RequestType"] == "Create": result = create_channel(mediapackage, event, context) elif event["RequestType"] == "Update": result = update_channel(mediapackage, event, context) elif event["RequestType"] == "Delete": result = delete_channel(mediapackage, event, context) except Exception as exp: print("Exception: %s" % exp) result = { 'Status': 'FAILED', 'Data': {"Exception": str(exp)}, 'ResourceId': None } resource_tools.send(event, context, result['Status'], result['Data'], result['ResourceId']) return def create_channel(mediapackage, event, context, auto_id=True): """ Create a MediaPackage channel Return the channel URL, username and password generated by MediaPackage """ if auto_id: channel_id = "%s-%s" % (resource_tools.stack_name(event), event["LogicalResourceId"]) else: channel_id = event["PhysicalResourceId"] channel = { "Id": channel_id, "Description": "CloudFormation Stack ID %s" % event["StackId"], "HlsIngest": {} } try: response = mediapackage.create_channel( Id=channel_id, Description="CloudFormation Stack ID %s" % event["StackId"] ) print(json.dumps(response)) print(response["Arn"]) attributes = { "Arn": response["Arn"], "PrimaryUrl": response["HlsIngest"]["IngestEndpoints"][0]["Url"], "PrimaryUsername": response["HlsIngest"]["IngestEndpoints"][0]["Username"], "PrimaryPassword": response["HlsIngest"]["IngestEndpoints"][0]["Password"], "SecondaryUrl": response["HlsIngest"]["IngestEndpoints"][1]["Url"], "SecondaryUsername": response["HlsIngest"]["IngestEndpoints"][1]["Username"], "SecondaryPassword": response["HlsIngest"]["IngestEndpoints"][1]["Password"] } print(attributes) result = { 'Status': 'SUCCESS', 'Data': attributes, 'ResourceId': channel_id } except Exception as ex: print(ex) result = { 'Status': 'FAILED', 'Data': {"Exception": str(ex)}, 'ResourceId': channel_id } return result def update_channel(mediapackage, event, context): """ Update a MediaPackage channel Return the channel URL, username and password generated by MediaPackage """ channel_id = event["PhysicalResourceId"] try: result = delete_channel(mediapackage, event, context) if result['Status'] == 'SUCCESS': result = create_channel(mediapackage, event, context, False) except Exception as ex: print(ex) result = { 'Status': 'FAILED', 'Data': {"Exception": str(ex)}, 'ResourceId': channel_id } return result def delete_channel(mediapackage, event, context): """ Delete a MediaPackage channel Return success/failure """ channel_id = event["PhysicalResourceId"] try: response = mediapackage.delete_channel(Id=channel_id) result = { 'Status': 'SUCCESS', 'Data': response, 'ResourceId': channel_id } except Exception as ex: print(ex) result = { 'Status': 'FAILED', 'Data': {"Exception": str(ex)}, 'ResourceId': channel_id } return result