in lambda-functions/aemp-cloudfront-sync-function/function.py [0:0]
def update_distribution_config(pathPatterns,distributionConfig, distributionId, eTag, originShieldRegion):
# checking if Cache Behaviors exists
try:
distributionConfig["CacheBehaviors"]["Items"]
log.debug('Cache Behaviors exist')
except:
log.debug('No Cache Behaviors exist..adding')
distributionConfig["CacheBehaviors"] = {}
distributionConfig["CacheBehaviors"]["Items"] = []
# checking if Origins exists
try:
distributionConfig["Origins"]["Items"]
log.debug('Some Origins exist')
except:
log.debug('No Origins exist..preparing to add a new origin')
distributionConfig["Origins"] = {}
distributionConfig["Origins"]["Items"] = []
# check if Origin Shield is enabled, default to False
originShieldEnabled = False
try:
if originShieldRegion and originShieldRegion.strip():
originShieldEnabled = True
except:
log.debug('Origin Shield is not enabled')
origins = {}
log.info(f'Length of CacheBehavior before :{len(distributionConfig["CacheBehaviors"]["Items"])}')
log.info(f'Length of Origins before :{len(distributionConfig["Origins"]["Items"])}')
for origin in distributionConfig["Origins"]["Items"]:
log.debug(f"Origin = {origin}")
origins[origin["DomainName"]] = origin["Id"]
cacheBehaviors = []
for cb in distributionConfig["CacheBehaviors"]["Items"]:
cacheBehaviors.append(cb["PathPattern"])
cacheBehaviors = set(cacheBehaviors)
for path in pathPatterns.keys():
if path in cacheBehaviors:
log.info(f'Cache Behavior already defined :{path}')
else:
log.info(f'Adding new CacheBehavior :{path}:{pathPatterns[path]}')
originDetails = pathPatterns[path]
originDomain = originDetails["OriginDomain"]
# check to create if MediaPackage origin is defined, if not create a new origin
if originDomain in origins.keys():
log.info(f'Origin already defined :{originDomain}')
originId = origins[originDomain]
else:
originId = 'EMP-{}'.format(originDomain.split(".")[0])
newCacheBehavior = create_cache_behavior(path,originId,originDetails["isMSS"])
distributionConfig["CacheBehaviors"]["Items"].insert(0,newCacheBehavior)
# check to create if MediaPackage origin is defined, if not create a new origin
if originDomain in origins.keys():
log.debug(f'Origin already defined :{originDomain}')
else:
log.debug(f'Creating new origin for: {originDomain}')
newOrigin = create_new_origin(originDomain,originId,originShieldEnabled,originShieldRegion)
distributionConfig["Origins"]["Items"].append(newOrigin)
origins[originDomain] = originId
distributionConfig["CacheBehaviors"]["Quantity"] = len(distributionConfig["CacheBehaviors"]["Items"])
distributionConfig["Origins"]["Quantity"] = len(distributionConfig["Origins"]["Items"])
log.info(f'Length of CacheBehavior after :{len(distributionConfig["CacheBehaviors"]["Items"])}')
log.info(f'Length of Origins after :{len(distributionConfig["Origins"]["Items"])}')
log.debug(f'Origins :{origins}')
log.debug(f'Updated DistributionConfig :{distributionConfig}')
response = cloudfront.update_distribution(DistributionConfig=distributionConfig,Id=distributionId,IfMatch=eTag)
log.debug(f'Updated distributionConfig :{response}')