levon
levon

Reputation: 939

Correct invalidation path for CloudFront object

I am trying to figure out the correct path for an object to invalidate on CloudFront distribution.

CloudFront is configured with an alternate domain name of *.example.com

The tricky part is that I've setup a custom origin on EC2 that uses HAProxy to do some path rewriting.

So that the request to

mysubdomain.example.com/icon.png

is rewritten to

s3.amazonaws.com/examplebucket/somedirectory/mysubdomain/icon.png

and the result is then returned to CloudFront. (So, both the Path and the Host are being rewritten)

Now, I have trouble figuring out the correct path for this object when sending an invalidation request. (I don't want to use versioning, because I need the filename to remain the same)

I've tried with the following configuration, but it doesn't seem to be working. The invalidation is created and processed, but with no effect.

const invalidationParams = {
  DistributionId: 'MY_DISTRIBUTION_ID',
  InvalidationBatch: {
    CallerReference: 'SOME_RANDOM_STRING',
    Paths: {
      Quantity: 1,
      Items: [
        '/somedirectory/mysubdomain/icon.png'
      ]
    }
  }
}

Since only PATH is specified, which is relative to the distribution, and no way to specify the full URL in the invalidation configuration, does it make it impossible to invalidate the object in this configuration?

Upvotes: 0

Views: 4321

Answers (1)

Michael - sqlbot
Michael - sqlbot

Reputation: 179462

CloudFront invalidations consider every object matching the path specification, as requested by the browser. To invalidate http://example.com/cat.jpg you specify one of the following:

cat.jpg
/cat.jpg

The leading slash is optional, but implied if absent.

Paths are the only values accepted for invalidation requests.

For each edge location, every copy of an object matching that path -- regardless of the alternate domain name or other attributes associated with in it -- will be evicted.

Note that "every copy of an object matching that path" may be confusing to some, since the assumption might be that only one copy would match a given path, but this is not correct. CloudFront caches different copies of the "same" object, depending on which request parameters are forwarded to the origin. If the query string, a cookie, whitelisted headers, etc., are forwarded, then many copies of the "same" object will be cached, because caching requires that the cache assume the response will vary if any if the forwarded request parameters vary. This is why so little forwarded by default -- it helps your hit rate because it reduces the likelihood of any given request seeming "unique" to the cache logic.

Upvotes: 2

Related Questions