Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»General Articles»Python Script to Create CloudFront Invalidations

    Python Script to Create CloudFront Invalidations

    RahulBy RahulMarch 16, 20212 Mins Read

    Amazon CloudFront invalidation feature allows you to remove an object from the CloudFront cache before it expires. It allows you to remove a specific object from cache or use (*) wildcard character to remove multiple objects. You can also invalidate all the objects by using “/*” parameters to invalidation requests.

    Python Script to Create CloudFront Invalidation

    Boto3 is the AWS SDK for the Python programming language. It allows Python developers to write programs that makes use of services like CloudFront, S3 and Ec2 etc.

    First, you need to install Boto3 Python library based on the Python version installed on your system. We recommened to use Python 3 to run below scripts.

    pip install boto3       ##For Python 2 or default 
    pip3 install boto3      ##For Python 3 
    

    Next, create a Python script with the following content.

    Python
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    import boto3
    import time
     
    # Create CloudFront client
    cf = boto3.client('cloudfront')
     
    # Enter Original name
     
    DISTRIBUTION_ID = "UJS7S8D8FD8FDF"
     
    # Create CloudFront invalidation
    def create_invalidation():
        res = cf.create_invalidation(
            DistributionId=DISTRIBUTION_ID,
            InvalidationBatch={
                'Paths': {
                    'Quantity': 1,
                    'Items': [
                        '/*'
                    ]
                },
                'CallerReference': str(time.time()).replace(".", "")
            }
        )
        invalidation_id = res['Invalidation']['Id']
        return invalidation_id
     
    # Create CloudFront Invalidation
    id = create_invalidation()
    print("Invalidation created successfully with Id: " + id)

    You must have to change DISTRIBUTION_ID value to the actual CloudFront distribution name. To find distribution name visit CloudFront web interface. There you can find Distribution id under the ID column.

    Now, execute Python script from a terminal to create invalidation request.

    python3 create_invalidation.py
    

    On successful execution, you will see a message on screen like:

    Invalidation created successfully with Id: I3HHNHJ0AF0ILQ
    

    The Invalidation request may take some time based on cached data. You can see the invalidation request status on CloudFront web interface.

    To view invalidation status, Go to CloudFront web interface. Open CloudFront distribution, then navigate to Invalidations tab. See the status of Invalidation request Id shown in above output.

    Create Invalidation for All CloudFront Distribution’s

    You can also use the following Python script to create Invalidation request for all CloudFront distributions available in selected region of your AWS account.

    Python
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    import boto3
    import time
     
    # Create CloudFront client
    cf = boto3.client('cloudfront')
     
     
    # Create CloudFront invalidation
    def create_invalidation(DISTRIBUTION_ID):
        res = cf.create_invalidation(
            DistributionId=DISTRIBUTION_ID,
            InvalidationBatch={
                'Paths': {
                    'Quantity': 1,
                    'Items': [
                        '/*'
                    ]
                },
                'CallerReference': str(time.time()).replace(".", "")
            }
        )
        invalidation_id = res['Invalidation']['Id']
        return invalidation_id
     
     
    # Loop through all distributions
    distributions=cf.list_distributions()
     
    if distributions['DistributionList']['Quantity'] > 0:
      for distribution in distributions['DistributionList']['Items']:
        id = create_invalidation(distribution['Id'])
        print("Invalidation created successfully for - " + distribution['Id'])
    else:
      print("No CloudFront distributions found.")

    Conclusion

    This tutorial helped you with a Python script to create CloudFront invalidation request.

    aws Boto3 Cloudfront Python
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow To Install and Configure VNC Server on Debian 10
    Next Article How to Install Tomcat 10 on Ubuntu 20.04

    Related Posts

    How to Find Django Install Location in Linux

    Updated:April 27, 20221 Min Read

    (Resolved) – ReactJS 404 Error on Page Reload

    2 Mins Read

    Adding a New SSH Key in GitHub

    Updated:April 1, 20223 Mins Read

    13 Best Linux Terminal Emulators and Bash Editors

    8 Mins Read

    How To Install Oracle VirtualBox on Debian 11

    2 Mins Read

    How To Install Python 3.10 on Debian 11/10

    2 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.