def handler()

in lambda-functions/aemp-cloudfront-sync-function/function.py [0:0]


def handler(event, context):
    log.info(f'Event :{event}')
    #Sample event object
    # {
    #    "DistributionId": "E2UZBZ6X6T11VU",
    #    "PackagingGroups": "VODWorkflow-packaging-group,me0010-vodjitp8-f7fb0b21d693-vod-package",
    #    "OriginShieldRegion": "eu-west-1"
    # }

    response = cloudfront.get_distribution_config(Id=event["DistributionId"])

    # fetch the ETag which is used later while updating the distribution

    eTag = response["ETag"]
    log.debug(f'ETag :{eTag}')
    distributionConfig = response["DistributionConfig"]
    log.debug(f'DistributionConfig :{distributionConfig}')
    origins = distributionConfig["Origins"]
    log.debug(f'Origins {origins}')

    # Get the Asset Id from each Packaging Group
    assetIds = list_assets(event)

    # Get the playback URLs from those assets
    endpoints = get_playable_endpoints(assetIds)

    # Determine the unique list of origins and path patterns which need to be updated
    pathPatterns = get_origin_pathpatterns(endpoints)

    originShieldRegion = event["OriginShieldRegion"]

    update_distribution_config(pathPatterns,distributionConfig,event["DistributionId"],eTag,originShieldRegion)
    return {
        'statusCode': 200,
        'body': 'Configuration complete.Please wait for the updates on the CloudFront distribution to finish.You can check the status here https://console.aws.amazon.com/cloudfront/home?#distribution-settings:{}'.format(event["DistributionId"])
    }