Exemple #1
0
def lambda_handler(event, context):
    """
    Lambda entry point. Print the event first.
    """
    print("Event Input: %s" % json.dumps(event))
    settings_table = event["ResourceProperties"]["SettingsTable"]
    result = {'Status': 'SUCCESS', "StackId": event["StackId"], "RequestId": event["RequestId"], "LogicalResourceId": event["LogicalResourceId"], 'Data': {}, 'ResourceId': settings_table}

    if event.get("PhysicalResourceId", False):
        result["PhysicalResourceId"] = event["PhysicalResourceId"]
    else:
        result["PhysicalResourceId"] = "{}-{}".format(resource_tools.stack_name(event), event["LogicalResourceId"])

    try:
        if event["RequestType"] == "Create" or event["RequestType"] == "Update":
            print(event["RequestType"])
            make_default_settings(settings_table)
    except ClientError as client_error:
        print("Exception: %s" % client_error)
        result = {
            'Status': 'FAILED',
            "StackId": event["StackId"],
            "RequestId": event["RequestId"],
            "LogicalResourceId": event["LogicalResourceId"],
            'Data': {
                "Exception": str(client_error)
            },
            'ResourceId': None
        }
    resource_tools.send(event, context, result['Status'], result['Data'], result["PhysicalResourceId"])
Exemple #2
0
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 lambda_handler(event, context):
    print("Event Input: %s" % json.dumps(event))
    emt_client = boto3.client('mediatailor') 

    # get environment variables
    distribution_id = os.environ["CloudFrontDistributionId"]
    video_source = os.environ["VideoSource"]
    ad_server = os.environ["ADS"]

    # make up the configuration name
    emt_config_name = "%s-%s" % (resource_tools.stack_name(event), event["LogicalResourceId"])

    response = {}
    result = {
        "Status": "SUCCESS",
        "Data": response,
        "ResourceId": emt_config_name
    }
    
    if event["RequestType"] == "Create" or event["RequestType"] == "Update":
        cf_client = boto3.client('cloudfront')
        session = boto3.session.Session()
        region = session.region_name

        # all the info we need to set up a proper distribution
        parsed_url = urlparse(video_source)
        source_domain_name = parsed_url.netloc
        # remove the asset name (eg. index.m3u8) from the path
        url_path_list = parsed_url.path.split('/')
        asset_name = url_path_list.pop(-1)
        source_path = ('/').join(url_path_list)

        # CloudFront distro info
        distribution_info = cf_client.get_distribution(Id = distribution_id)
        distribution_domain_name = distribution_info['Distribution']['DomainName']
        distribution_config = distribution_info['Distribution']['DistributionConfig']
        etag = distribution_info['ETag']
    
        cdn_prefix = "https://" + distribution_domain_name
        cdn_segment_prefix = cdn_prefix + source_path
        video_source_without_asset_name = "https://" + source_domain_name + source_path
        emt_data = {}
        try:
            emt_data = emt_client.put_playback_configuration(
                AdDecisionServerUrl=ad_server,
                Name=emt_config_name,
                VideoContentSourceUrl=video_source_without_asset_name,
                CdnConfiguration={
                    "AdSegmentUrlPrefix": cdn_prefix,
                    "ContentSegmentUrlPrefix": cdn_segment_prefix
                }
            )
            parsed_emt_url = urlparse(emt_data["HlsConfiguration"]["ManifestEndpointPrefix"])
            hls_playback_path = parsed_emt_url.path
            ads_domain_name = "ads.mediatailor." + region + ".amazonaws.com"
            meditailor_domain_name = parsed_emt_url.netloc
    
            result["Data"] = {
                "ConfigurationName": emt_config_name,
                "HLSPlaybackURL": emt_data["HlsConfiguration"]["ManifestEndpointPrefix"] + asset_name,
                "CloudFrontPlaybackURL": cdn_prefix + hls_playback_path + asset_name
            }  
            #update the origins of the CloudFront Distribution
            response = update_distribution_origins(cf_client, distribution_config, distribution_id, etag, 
                source_domain_name, meditailor_domain_name, ads_domain_name )
            print("CloudFront update origins response: %s" % response)
            #if successful, update the cache behaviors of the distribution
            if response['ResponseMetadata']['HTTPStatusCode'] == 200:
                #get the latest distribution information after origin update
                distribution_info = cf_client.get_distribution(Id = distribution_id)
                distribution_config = distribution_info['Distribution']['DistributionConfig']
                etag = distribution_info['ETag']
                response = update_distribution_cache_behaviors(cf_client, distribution_config, distribution_id, etag)
                if response['ResponseMetadata']['HTTPStatusCode'] == 200:
                    print("CloudFront update cache behaviors response: %s" % response)
                else:
                    result["Status"] = "FAILED",
        except Exception as exp:
            print("Exception: %s" % exp)
            result["Status"] = "FAILED"
            result["Data"] = {"Exception": str(exp)}

    elif event["RequestType"] == "Delete":
        try:
            result["Data"] = emt_client.delete_playback_configuration(Name=emt_config_name)
        except Exception as exp:
            print("Exception: %s" % exp)
            result["Status"] = "FAILED",
            result["Data"] = {"Exception": str(exp)}

    resource_tools.send(event, context, result["Status"],
         result["Data"], result["ResourceId"])
    return