Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
AWS Lambda Based Custom Resource Handler for Akamai Deployments.
"""AWS Lambda Based Custom Resource Handler for Akamai Deployments.
This module defines the handler function (or entry point) to an AWS lambda
function that orchestrates the activation of akamai configurations to a specified
property. This lambda function is setup to except input and be invoked by AWS
cloudformation as a custom resource.
import logging
from crhelper import CfnResource
from .akamai_client import AkamaiClient
from .aws import get_json_from_s3, get_json_secret
LOGGER = logging.getLogger(__name__)
def build_akamai_client(edge_config_secret_arn):"Building Akamai Client Based on secret: {edge_config_secret_arn}")
return AkamaiClient(**get_json_secret(edge_config_secret_arn))
def handle_config_publish(event, context):
# Retrieve the rules that we want to activate in a new version by pulling from s3
resource_arguments = event["ResourceProperties"]["ProvisioningParameters"]
location = resource_arguments["Location"]
new_rules = get_json_from_s3(bucket=location["Bucket"], key=location["Key"])"Got Rules From S3")
edge_config_secret_arn = resource_arguments["SecretId"]
akamai_client = build_akamai_client(edge_config_secret_arn)"Built Akamai Client")
# Create a new version based on the most current
target_property_id = resource_arguments["PropertyId"]
current_version_id = akamai_client.get_current_config_version(target_property_id)
new_version_id = akamai_client.create_new_config_version(
target_property_id, current_version_id
)"Created New Version Based On Latest")
# Update the rules from what we have in s3 and activate it on the target network.
akamai_client.update_config_rules(target_property_id, new_version_id, new_rules)
activation_emails=resource_arguments.get("EmailsOnActivation", []),
)"Activated A New Config Version")
def handler(event, context):
custom_resource = CfnResource(
json_logging=True, log_level="DEBUG", boto_level="CRITICAL"
def create_or_update(event, context):
handle_config_publish(event, context)
custom_resource(event, context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment