Sign in
agent:

Filter Out Non-Optimized AWS CloudFront Distributions

There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.

This task scans and isolates AWS CloudFront distributions that don't have compression/caching enabled. Addressing these configurations enhances content delivery performance and potentially reduces associated costs.

import boto3 creds = _get_creds(cred_label)['creds'] access_key = creds['username'] secret_key = creds['password'] ''' # List of all distributions received from the previous task all_distributions = ['E2W06YV1I68DDO', 'E16ONX1GUDSAFS'] ''' # Initialize the CloudFront client client = boto3.client('cloudfront',aws_access_key_id=access_key,aws_secret_access_key=secret_key) # List to store distributions that need compression enabled distributions_needing_compression = [] # Check if there are any distributions passed to the task if not all_distributions: print("No CloudFront distributions provided to the task.") else: try: # Loop through each distribution ID for dist_id in all_distributions: # Fetch the distribution's configuration dist_config_response = client.get_distribution_config(Id=dist_id) cache_behaviors = dist_config_response['DistributionConfig']['DefaultCacheBehavior'] # Check if compression is not enabled for the distribution if 'Compress' not in cache_behaviors or not cache_behaviors['Compress']: distributions_needing_compression.append(dist_id) # Print the results if distributions_needing_compression: print(f"{len(distributions_needing_compression)} distributions need compression enabled:") for dist_id in distributions_needing_compression: print(dist_id) else: print("All provided distributions already have compression enabled.") # Handle specific exceptions for service errors except client.exceptions.NoSuchDistribution as e: print("Error: One or more distributions not found.") print(f"Details: {str(e)}") except client.exceptions.AccessDenied as e: print("Error: Access denied. Ensure you have the appropriate permissions.") print(f"Details: {str(e)}") # General exception handling for unforeseen errors except Exception as e: print("An unexpected error occurred while processing the CloudFront distributions.") print(f"Details: {str(e)}") context.proceed = False
copied