in source/cfn-add-edge-lambda/cfn-add-edge-lambda.py [0:0]
def create(event, context):
logger.info("Create or Update")
logger.info(json.dumps(event))
# Check that all the required properties are specified
if "Id" not in event["ResourceProperties"]:
raise ValueError("Missing property 'Id'")
if "LambdaFunctionARN" not in event["ResourceProperties"]:
raise ValueError("Missing property 'LambdaFunctionARN'")
if "EventType" not in event["ResourceProperties"]:
raise ValueError("Missing property 'EventType'")
lambda_association = {
"LambdaFunctionARN": event["ResourceProperties"]["LambdaFunctionARN"],
"EventType": event["ResourceProperties"]["EventType"],
"IncludeBody": False
}
try:
response = client.get_distribution_config(
Id=event["ResourceProperties"]["Id"])
config = copy.deepcopy(response["DistributionConfig"])
saved_config = copy.deepcopy(response["DistributionConfig"])
# handle the DefaultCacheBehavior key first
update_cache_behavior(config["DefaultCacheBehavior"],
lambda_association)
# handle cache behaviors
for behavior in config["CacheBehaviors"].get("Items", []):
update_cache_behavior(behavior, lambda_association)
# update CloudFormation and wait for the response
response = client.update_distribution(
DistributionConfig=config,
Id=event["ResourceProperties"]["Id"],
IfMatch=response["ETag"])
except Exception as e:
raise e