in source/cfn-add-edge-lambda/cfn-add-edge-lambda.py [0:0]
def delete(event, context):
logger.info("Delete")
# Delete never returns anything. Should not fail if the underlying resources are already deleted.
# Desired state.
# 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
}
# delete any associated L@E functions with our Lambda ARN
try:
# get the distribution
response = client.get_distribution_config(
Id=event["ResourceProperties"]["Id"])
config = response["DistributionConfig"]
# handle the DefaultCacheBehavior key first
remove_cache_behavior(config["DefaultCacheBehavior"],
lambda_association)
# handle cache behaviors
for behavior in config["CacheBehaviors"].get("Items", []):
remove_cache_behavior(behavior, lambda_association)
# update the distribution with changes
client.update_distribution(DistributionConfig=config,
Id=event["ResourceProperties"]["Id"],
IfMatch=response["ETag"])
except Exception as e:
raise e