Search code examples
iosamazon-web-servicesamazon-s3amazon-cloudfront

Force update Amazon CloudFront on iOS


I am using Amazon S3 in conjunction with Amazon CloudFront, basically in my app I have a method to update an S3 object, basically I get the S3 object using CloudFront, I make a change to the data and I reupload it under the same key -- basically replacing/updating the file/object.

However, CloudFront doesn't seem to update along with S3 (well it does, but my users don't have all day), is there a way to force a CloudFront content update? Apparently you can invalidate it, is there an iOS SDK way to do that?


Solution

  • I don't know that there is a way to make an CloudFront invalidation request via the iOS SDK. You would likely need to build your own method to formulate the request against AWS API.

    I would however suggest that you take another approach. Invalidation requests are expensive operations (relative to other Cloudfront costs). You probably do not want to leave it up to your user to be able to initiate an unlimited amount of invalidation requests against CloudFront via the application. You will also run up against limits to the number of concurrent invalidation requests you can have. Your best best is to actually implement a file name versioning scheme to where you can change the file name in a programmatic way for each revision. You would then reference the new URL in Cloudfront with each revision, eliminating the need to wait for a cache refresh or perform an invalidation. Also this will lead to more immediate response availability for the image, as invalidation requests may take a while to process.

    Please note the following from the CloudFront FAQ:

    Q. Is there a limit to the number of invalidation requests I can make? There are no limits on the total number of files you can invalidate; however, each invalidation request you make can have a maximum of 1,000 files. In addition, you can only have 3 invalidation requests in progress at any given time. If you exceed this limit, further invalidation requests will receive an error response until one of the earlier requests completes. You should use invalidation only in unexpected circumstances; if you know beforehand that your files will need to be removed from cache frequently, it is recommended that you either implement a versioning system for your files and/or set a short expiration period.