agent: |
Qpr6s68rWVbFffbaeARTAttach Compression Policy to AWS CloudFront Distributions
Attach Compression Policy to AWS CloudFront Distributions
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
This task attaches a compression policy on specific AWS CloudFront distributions to reduce the size of files being delivered. This optimizes the transfer speed, enhancing user experience and potentially decreasing data transfer costs.
inputs
outputs
import boto3
creds = _get_creds(cred_label)['creds']
access_key = creds['username']
secret_key = creds['password']
'''
# List of distributions needing compression received from the previous task
distributions_needing_compression = ['E2W06YV1I68DDO', 'E16ONX1GUDSAFS']
'''
# Initialize the CloudFront client
client = boto3.client('cloudfront',aws_access_key_id=access_key,aws_secret_access_key=secret_key)
# Check if there are any distributions passed to the task
if not distributions_needing_compression:
print("No CloudFront distributions provided to the task for enabling compression.")
else:
try:
# Counter to keep track of the number of distributions updated
updated_count = 0
# Loop through each distribution ID
for dist_id in distributions_needing_compression:
# Fetch the distribution's configuration
dist_config_response = client.get_distribution_config(Id=dist_id)
dist_config = dist_config_response['DistributionConfig']
# Update the distribution to enable compression
dist_config['DefaultCacheBehavior']['Compress'] = True
client.update_distribution(
Id=dist_id,
IfMatch=dist_config_response['ETag'],
DistributionConfig=dist_config
)
print(f"Enabled compression for distribution {dist_id}")
updated_count += 1
# Print a summary of the results
print(f"\nSummary:\nUpdated {updated_count} out of {len(distributions_needing_compression)} distributions.")
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)}")
except client.exceptions.InvalidIfMatchVersion as e:
print("Error: The If-Match version is invalid or mismatched.")
print(f"Details: {str(e)}")
except Exception as e:
print("An unexpected error occurred while updating the CloudFront distributions.")
print(f"Details: {str(e)}")
copied