Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invalidate objects in with CloudFront using boto and python3?

I found code similar to what I need at the bottom of this page.

#!/usr/bin/env python3
from boto.cloudfront import CloudFrontConnection

aws_access_key         = 'BJDJLSMQRWDSC4UPLS6S' # Not real
aws_secret_access_key  = '8xRnWKxRR/93TeQv3pwzMNR222nwe5kjhYweCAij' # Not real
aws_cf_distribution_id = 'foobar35'

objects = [ '/index.html' ]
conn = CloudFrontConnection(aws_access_key, aws_secret_access_key)
print(conn.create_invalidation_request(aws_cf_distribution_id, objects))

When I run it, I get the following error:

$ ./invalidate.py
Traceback (most recent call last):
  File "./invalidate.py", line 14, in <module>
    print(conn.create_invalidation_request(aws_cf_distribution_id, objects))
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/boto/cloudfront/__init__.py", line 263, in create_invalidation_request
    raise CloudFrontServerError(response.status, response.reason, body)
boto.cloudfront.exception.CloudFrontServerError: CloudFrontServerError: 404 Not Found
<?xml version="1.0"?>
<ErrorResponse xmlns="http://cloudfront.amazonaws.com/doc/2010-11-01/">
  <Error>
    <Type>Sender</Type>
    <Code>NoSuchDistribution</Code>
    <Message>The specified distribution does not exist.</Message>
  </Error>
  <RequestId>343d3d5b-e269-11e5-bc96-eb9a228cf3e7</RequestId>
</ErrorResponse>

I think the problem is that I have not identified the S3 bucket where /index.html resides. I have about 20 buckets, each named after the domain part of the URL. I tried various permutations but to no avail.

  • s3://www.example.com.s3.amazonaws.com/index.html
  • s3://www.example.com/index.html
  • /www.example.com/index.html
  • www.example.com/index.html
  • /index.html

Can someone tell me how to get this to work?

like image 662
rcrews Avatar asked Mar 05 '16 19:03

rcrews


2 Answers

The actual error is below:

<Message>The specified distribution does not exist.</Message>

According to your code, you have specified 'foobar35' as your distribution id - it's not correct.

Before trying to invalidate an object, you need to create a distribution. After you create it, you will receive a distribution ID that should be passed as a parameter to your create_invalidation_request method.

For more information, see: Creating or Updating a Web Distribution Using the CloudFront Console.

like image 93
Vladimir Mukhin Avatar answered Nov 15 '22 09:11

Vladimir Mukhin


The answer from Vladimir Mukhin was helpful: He was correct that my distribution ID was incorrect and that I needed to create a distribution. However, I didn't know how to get my distribution ID. I found it by looking through the awscli docs. The answer was not simply RTFM however, since the information I needed was not easy to find. Here's the answer that finally helped me

aws cloudfront list-distributions

With that info, I was able to find the parallels in the Python, Ruby, and Perl (Paws) APIs to get the correct distribution ID without shelling out to the command line. Hope this helps someone.

like image 29
rcrews Avatar answered Nov 15 '22 10:11

rcrews