1 00:00:00,180 --> 00:00:01,230 ‫So let's have a look 2 00:00:01,230 --> 00:00:04,200 ‫at the caching behavior of our platform distribution. 3 00:00:04,200 --> 00:00:06,270 ‫And this is the behavior I want to look at. 4 00:00:06,270 --> 00:00:08,760 ‫So as you can see, we have the default behavior. 5 00:00:08,760 --> 00:00:09,960 ‫It's a star. 6 00:00:09,960 --> 00:00:11,430 ‫We can edit this, 7 00:00:11,430 --> 00:00:13,500 ‫and we cannot edit the path pattern 8 00:00:13,500 --> 00:00:15,720 ‫because this is the default behavior. 9 00:00:15,720 --> 00:00:17,370 ‫Now if you scroll down, as you can see, 10 00:00:17,370 --> 00:00:20,100 ‫we have cache key and origin request that we can set. 11 00:00:20,100 --> 00:00:23,400 ‫And so we have a cache policy that we can define, 12 00:00:23,400 --> 00:00:26,670 ‫and then we have an origin request policy that's optional. 13 00:00:26,670 --> 00:00:30,120 ‫So let's go into creating a cache policy. 14 00:00:30,120 --> 00:00:31,870 ‫So I call this one DemoCachePolicy. 15 00:00:33,990 --> 00:00:35,070 ‫And as we can see here, 16 00:00:35,070 --> 00:00:37,350 ‫we have control over the time to live settings. 17 00:00:37,350 --> 00:00:38,910 ‫So we can set the minimum TTL, 18 00:00:38,910 --> 00:00:41,760 ‫the maximum TTL, and the default TTL. 19 00:00:41,760 --> 00:00:43,530 ‫And here we can have the cache key settings. 20 00:00:43,530 --> 00:00:45,810 ‫So which headers do we want to include? 21 00:00:45,810 --> 00:00:47,940 ‫And we can choose the headers from this list, 22 00:00:47,940 --> 00:00:50,790 ‫or even add a custom header if we wanted to. 23 00:00:50,790 --> 00:00:53,310 ‫Which query strings do we want to include? 24 00:00:53,310 --> 00:00:55,320 ‫So all, or just maybe again, 25 00:00:55,320 --> 00:00:58,230 ‫a list of query strings that we can add over here. 26 00:00:58,230 --> 00:01:00,630 ‫And which cookies do we want to include? 27 00:01:00,630 --> 00:01:02,790 ‫Again, all or a specified list. 28 00:01:02,790 --> 00:01:06,480 ‫So here we can really specify what goes into our cache key. 29 00:01:06,480 --> 00:01:09,420 ‫And then we know that data is going to be cached 30 00:01:09,420 --> 00:01:11,490 ‫based on these settings for headers, 31 00:01:11,490 --> 00:01:13,320 ‫query strings, and cookies. 32 00:01:13,320 --> 00:01:14,610 ‫And we know for a fact that 33 00:01:14,610 --> 00:01:18,480 ‫if we do select a cache key with some headers and so on, 34 00:01:18,480 --> 00:01:21,180 ‫they are going to be passed to our origin request. 35 00:01:21,180 --> 00:01:22,200 ‫But on top of it, 36 00:01:22,200 --> 00:01:25,500 ‫if we wanted to also have extra headers passed 37 00:01:25,500 --> 00:01:29,010 ‫or extra query strings passed to our origin request, 38 00:01:29,010 --> 00:01:31,620 ‫we could create an origin request policy. 39 00:01:31,620 --> 00:01:32,880 ‫So you will here you create 40 00:01:32,880 --> 00:01:37,880 ‫a DemoOriginPolicy request policy. 41 00:01:38,010 --> 00:01:40,170 ‫And here again we can specify 42 00:01:40,170 --> 00:01:43,680 ‫which headers do we want to add into the request. 43 00:01:43,680 --> 00:01:45,090 ‫So we have the headers, 44 00:01:45,090 --> 00:01:48,510 ‫we have the query string facilities, and we have cookies. 45 00:01:48,510 --> 00:01:52,920 ‫And this is to enhance our own little request in here. 46 00:01:52,920 --> 00:01:55,500 ‫So back into the platform configuration. 47 00:01:55,500 --> 00:01:57,540 ‫This is where we have the control 48 00:01:57,540 --> 00:02:00,480 ‫over the cache keys and the origin requests. 49 00:02:00,480 --> 00:02:02,490 ‫And hopefully, that makes sense to you now. 50 00:02:02,490 --> 00:02:04,050 ‫So let me cancel this. 51 00:02:04,050 --> 00:02:05,700 ‫The other thing I can do is to create 52 00:02:05,700 --> 00:02:08,430 ‫a new behavior to override this behavior. 53 00:02:08,430 --> 00:02:10,440 ‫So as you can see, I create a new behavior, 54 00:02:10,440 --> 00:02:13,380 ‫and here I can say for /images/*, 55 00:02:13,380 --> 00:02:15,780 ‫I want to send to a new origin. 56 00:02:15,780 --> 00:02:17,970 ‫And of course you could define another S3 bucket 57 00:02:17,970 --> 00:02:20,280 ‫as an origin or an EC2 instance, 58 00:02:20,280 --> 00:02:21,540 ‫or whatever you want really. 59 00:02:21,540 --> 00:02:23,580 ‫And again, you can have a specific cache key 60 00:02:23,580 --> 00:02:25,710 ‫and origin request policy for this. 61 00:02:25,710 --> 00:02:28,800 ‫And so your two cache behaviors will coexist. 62 00:02:28,800 --> 00:02:30,330 ‫And, of course, the most specific is going 63 00:02:30,330 --> 00:02:33,480 ‫to be the one that is going to be selected first. 64 00:02:33,480 --> 00:02:34,590 ‫So here we have seen 65 00:02:34,590 --> 00:02:37,740 ‫how to set up a behavior or multiple behaviors, 66 00:02:37,740 --> 00:02:40,410 ‫and next we're going to have a look at invalidations. 67 00:02:40,410 --> 00:02:43,410 ‫So first, let's observe the TTL setting 68 00:02:43,410 --> 00:02:45,780 ‫by having this index.html file. 69 00:02:45,780 --> 00:02:48,390 ‫And we're going to add some text to the title. 70 00:02:48,390 --> 00:02:51,120 ‫Maybe I will add it, so let's go into index.html 71 00:02:51,120 --> 00:02:52,920 ‫and it says instead of, "I really love coffee," 72 00:02:52,920 --> 00:02:56,460 ‫I say, "I really love coffee every morning." 73 00:02:56,460 --> 00:02:59,640 ‫Okay, so we have updated this index.html file. 74 00:02:59,640 --> 00:03:02,550 ‫What we're going to do now is to go into our S3 bucket, 75 00:03:02,550 --> 00:03:04,470 ‫and we're going to upload a new version of that file. 76 00:03:04,470 --> 00:03:06,840 ‫So because we haven't enabled versioning, 77 00:03:06,840 --> 00:03:08,610 ‫actually this is going to replace that file. 78 00:03:08,610 --> 00:03:13,610 ‫So we are uploading this index.html file here, upload. 79 00:03:14,310 --> 00:03:16,110 ‫Now the upload has been succeeded. 80 00:03:16,110 --> 00:03:19,620 ‫So if we click on this index.html and click on open, 81 00:03:19,620 --> 00:03:22,110 ‫this is a opening directly from Amazon S3. 82 00:03:22,110 --> 00:03:24,990 ‫So we get "I really love coffee every morning," 83 00:03:24,990 --> 00:03:29,520 ‫so we are getting the updated file directly into Amazon S3, 84 00:03:29,520 --> 00:03:32,910 ‫but if we go into CloudFront and refresh this page, 85 00:03:32,910 --> 00:03:35,160 ‫we're still getting just, "I really love coffee." 86 00:03:35,160 --> 00:03:36,960 ‫The reason is CloudFront knows 87 00:03:36,960 --> 00:03:39,780 ‫to cache the previous version for one day, 88 00:03:39,780 --> 00:03:41,820 ‫and therefore CloudFront is not going to request 89 00:03:41,820 --> 00:03:44,077 ‫from Amazon S3 this new file, 90 00:03:44,077 --> 00:03:46,260 ‫"I really love coffee every morning." 91 00:03:46,260 --> 00:03:47,490 ‫So how can we tell CloudFront 92 00:03:47,490 --> 00:03:50,010 ‫to actually force fetching this new file version. 93 00:03:50,010 --> 00:03:51,870 ‫Well, we go back into CloudFront, 94 00:03:51,870 --> 00:03:54,720 ‫and we are going to go into the invalidations tab 95 00:03:54,720 --> 00:03:56,700 ‫We're going to create an invalidation, 96 00:03:56,700 --> 00:03:59,610 ‫and we'll just have a wild card, so a star. 97 00:03:59,610 --> 00:04:01,350 ‫And this is going to say any object 98 00:04:01,350 --> 00:04:03,690 ‫within the CloudFront cache should be removed. 99 00:04:03,690 --> 00:04:05,820 ‫And then new objects are going 100 00:04:05,820 --> 00:04:08,160 ‫to be fetched directly from Amazon S3. 101 00:04:08,160 --> 00:04:10,050 ‫So let's go ahead and create this invalidation. 102 00:04:10,050 --> 00:04:14,550 ‫So we can just do slash and then the star. 103 00:04:14,550 --> 00:04:16,263 ‫Let's create this invalidation. 104 00:04:17,940 --> 00:04:20,670 ‫And now that the invalidation is completed, 105 00:04:20,670 --> 00:04:22,140 ‫what's going to happen is that if I go back 106 00:04:22,140 --> 00:04:25,770 ‫to my CloudFront URL and refresh this, 107 00:04:25,770 --> 00:04:27,840 ‫I'm gonna get, "I really love coffee every morning" 108 00:04:27,840 --> 00:04:29,340 ‫because now the file has been 109 00:04:29,340 --> 00:04:31,560 ‫correctly fetched from Amazon S3. 110 00:04:31,560 --> 00:04:32,640 ‫So this shows you a demonstration 111 00:04:32,640 --> 00:04:34,440 ‫of caching and invalidations. 112 00:04:34,440 --> 00:04:35,400 ‫I hope you like this lecture, 113 00:04:35,400 --> 00:04:37,350 ‫and I will see you in the next lecture.