AWS: Cloudfront Viewer Policy



--- What is AWS Cloudfront Viewer Policy?

The CloudFront Access Viewer is a tool provided by Amazon Web Services (AWS) that allows you to monitor and analyze the usage of your Amazon CloudFront distributions. CloudFront is a content delivery network (CDN) that helps distribute your web content globally with low latency. The Access Viewer provides insights into viewer requests for your CloudFront distributions, showing details such as request methods, response status codes, geographic locations, and more. This tool aids in troubleshooting and optimizing content delivery performance by helping you identify patterns and potential issues. It's particularly useful for understanding how users interact with your content, which can guide improvements to your distribution setup and content delivery strategies. By leveraging the CloudFront Access Viewer, you can enhanc
e the overall user experience and ensure efficient distribution of your web content across the globe.




--- Python code that uses the `boto3` library to update the viewer policy of a CloudFront distribution to redirect HTTP requests to HTTPS:





import boto3


def update_cloudfront_distribution(distribution_id):

    client = boto3.client('cloudfront')

    

    response = client.get_distribution_config(Id=distribution_id)

    distribution_config = response['DistributionConfig']

    distribution_config['DefaultCacheBehavior']['ViewerProtocolPolicy'] = 'redirect-to-https'

 response = client.update_distribution(DistributionConfig=distribution_config, Id=distribution_id, IfMatch=response['ETag'])

    

    print("Distribution updated with HTTPS redirection")

update_cloudfront_distribution(<<Distribution ID>>)


Make sure to replace `'<<Distribution ID>>` with the actual ID of the CloudFront distribution you want to update.  ViewerProtocolPolicy 'redirect-to-https', and after the modification update the distribution with the modified configuration.


Cloudfront WebAcl ID short note and how to change WebAcl ID of a particular Cloudfront Distribution using Boto3 module will be discussed in my next week's blog.


Thank you 🙂

Comments

Popular posts from this blog

Data analysis with R

Machine learning in Python

AWS: Config Rule & Compliance Check