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): | |
LOGGER.info(f"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"]) | |
LOGGER.info("Got Rules From S3") | |
edge_config_secret_arn = resource_arguments["SecretId"] | |
akamai_client = build_akamai_client(edge_config_secret_arn) | |
LOGGER.info("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 | |
) | |
LOGGER.info("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) | |
akamai_client.activate_config_version( | |
target_property_id, | |
new_version_id, | |
network_name=resource_arguments["ActivationNetwork"], | |
revision=resource_arguments["Revision"], | |
activation_emails=resource_arguments.get("EmailsOnActivation", []), | |
) | |
LOGGER.info("Activated A New Config Version") | |
def handler(event, context): | |
LOGGER.info(event) | |
custom_resource = CfnResource( | |
json_logging=True, log_level="DEBUG", boto_level="CRITICAL" | |
) | |
@custom_resource.create | |
@custom_resource.update | |
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