‫So let's have a look
‫at the caching behavior of our platform distribution.
‫And this is the behavior I want to look at.
‫So as you can see, we have the default behavior.
‫It's a star.
‫We can edit this,
‫and we cannot edit the path pattern
‫because this is the default behavior.
‫Now if you scroll down, as you can see,
‫we have cache key and origin request that we can set.
‫And so we have a cache policy that we can define,
‫and then we have an origin request policy that's optional.
‫So let's go into creating a cache policy.
‫So I call this one DemoCachePolicy.
‫And as we can see here,
‫we have control over the time to live settings.
‫So we can set the minimum TTL,
‫the maximum TTL, and the default TTL.
‫And here we can have the cache key settings.
‫So which headers do we want to include?
‫And we can choose the headers from this list,
‫or even add a custom header if we wanted to.
‫Which query strings do we want to include?
‫So all, or just maybe again,
‫a list of query strings that we can add over here.
‫And which cookies do we want to include?
‫Again, all or a specified list.
‫So here we can really specify what goes into our cache key.
‫And then we know that data is going to be cached
‫based on these settings for headers,
‫query strings, and cookies.
‫And we know for a fact that
‫if we do select a cache key with some headers and so on,
‫they are going to be passed to our origin request.
‫But on top of it,
‫if we wanted to also have extra headers passed
‫or extra query strings passed to our origin request,
‫we could create an origin request policy.
‫So you will here you create
‫a DemoOriginPolicy request policy.
‫And here again we can specify
‫which headers do we want to add into the request.
‫So we have the headers,
‫we have the query string facilities, and we have cookies.
‫And this is to enhance our own little request in here.
‫So back into the platform configuration.
‫This is where we have the control
‫over the cache keys and the origin requests.
‫And hopefully, that makes sense to you now.
‫So let me cancel this.
‫The other thing I can do is to create
‫a new behavior to override this behavior.
‫So as you can see, I create a new behavior,
‫and here I can say for /images/*,
‫I want to send to a new origin.
‫And of course you could define another S3 bucket
‫as an origin or an EC2 instance,
‫or whatever you want really.
‫And again, you can have a specific cache key
‫and origin request policy for this.
‫And so your two cache behaviors will coexist.
‫And, of course, the most specific is going
‫to be the one that is going to be selected first.
‫So here we have seen
‫how to set up a behavior or multiple behaviors,
‫and next we're going to have a look at invalidations.
‫So first, let's observe the TTL setting
‫by having this index.html file.
‫And we're going to add some text to the title.
‫Maybe I will add it, so let's go into index.html
‫and it says instead of, "I really love coffee,"
‫I say, "I really love coffee every morning."
‫Okay, so we have updated this index.html file.
‫What we're going to do now is to go into our S3 bucket,
‫and we're going to upload a new version of that file.
‫So because we haven't enabled versioning,
‫actually this is going to replace that file.
‫So we are uploading this index.html file here, upload.
‫Now the upload has been succeeded.
‫So if we click on this index.html and click on open,
‫this is a opening directly from Amazon S3.
‫So we get "I really love coffee every morning,"
‫so we are getting the updated file directly into Amazon S3,
‫but if we go into CloudFront and refresh this page,
‫we're still getting just, "I really love coffee."
‫The reason is CloudFront knows
‫to cache the previous version for one day,
‫and therefore CloudFront is not going to request
‫from Amazon S3 this new file,
‫"I really love coffee every morning."
‫So how can we tell CloudFront
‫to actually force fetching this new file version.
‫Well, we go back into CloudFront,
‫and we are going to go into the invalidations tab
‫We're going to create an invalidation,
‫and we'll just have a wild card, so a star.
‫And this is going to say any object
‫within the CloudFront cache should be removed.
‫And then new objects are going
‫to be fetched directly from Amazon S3.
‫So let's go ahead and create this invalidation.
‫So we can just do slash and then the star.
‫Let's create this invalidation.
‫And now that the invalidation is completed,
‫what's going to happen is that if I go back
‫to my CloudFront URL and refresh this,
‫I'm gonna get, "I really love coffee every morning"
‫because now the file has been
‫correctly fetched from Amazon S3.
‫So this shows you a demonstration
‫of caching and invalidations.
‫I hope you like this lecture,
‫and I will see you in the next lecture.