1 00:00:00,020 --> 00:00:01,910 ‫So here is something I promised you before. 2 00:00:01,910 --> 00:00:08,300 ‫We are going to see how code deploy is integrated with the framework and Sam will be using code deploy 3 00:00:08,300 --> 00:00:13,910 ‫to update our lambda functions and that will leverage the traffic shifting feature we've seen before. 4 00:00:13,910 --> 00:00:19,280 ‫Using the aliases, we can also define pre and post traffic hooks, lambda functions to validate the 5 00:00:19,280 --> 00:00:23,210 ‫deployments and we can do an automated rollbacks with cloudwatch alarms. 6 00:00:23,210 --> 00:00:29,030 ‫So in our diagram to remember to remind you, we had the lambda alias that had V1 lambda and we're going 7 00:00:29,030 --> 00:00:34,460 ‫to trigger a code deploy deployment with or with a CI CD or with a SAM framework. 8 00:00:34,460 --> 00:00:38,500 ‫And then we want to update our alias into a V2. 9 00:00:38,510 --> 00:00:45,290 ‫So code deploy will be running some pre traffic hook test using another lambda function and that's optional. 10 00:00:45,320 --> 00:00:49,070 ‫Then it will do a traffic shifting with the alias. 11 00:00:49,070 --> 00:00:52,850 ‫So according to a strategy and then it will monitor a cloudwatch alarm. 12 00:00:52,850 --> 00:00:57,500 ‫And that's also optional to ensure everything goes well during the deployment. 13 00:00:57,500 --> 00:01:02,580 ‫And then once the deployment is done and the traffic shifting is completely done, we can run a post 14 00:01:02,580 --> 00:01:04,110 ‫traffic hook lambda function. 15 00:01:04,110 --> 00:01:10,380 ‫This is again optional to run some tests onto your alias and if everything goes well, then your V1 16 00:01:10,380 --> 00:01:14,410 ‫function in your alias goes away and we're just ending up with the V2. 17 00:01:14,430 --> 00:01:19,440 ‫So here is an example of the YAML file that you will have within Sam. 18 00:01:19,440 --> 00:01:22,130 ‫So I want to just show you a couple of things. 19 00:01:22,140 --> 00:01:24,630 ‫The first one is auto publish Alias. 20 00:01:24,630 --> 00:01:28,200 ‫So this is helping Sam detect when new code is being deployed. 21 00:01:28,200 --> 00:01:34,770 ‫In that case, a new lambda function will be deployed as a new version with the latest code and then 22 00:01:34,770 --> 00:01:41,520 ‫automatically the alias will be updated to point to the updated version of the lambda function. 23 00:01:41,520 --> 00:01:46,350 ‫And in terms of how fast that happens, well, this is controlled by the deployment preference. 24 00:01:46,350 --> 00:01:47,580 ‫And so we have different types. 25 00:01:47,580 --> 00:01:53,670 ‫We have the canary type, the linear type and the all at once type, and they're basically code deploy 26 00:01:53,670 --> 00:01:54,120 ‫settings. 27 00:01:54,120 --> 00:01:59,160 ‫So in this example, we have canary 10%, ten minutes, then we have alarms. 28 00:01:59,160 --> 00:02:03,240 ‫What are the alarms that can trigger a rollback in your deployment? 29 00:02:03,240 --> 00:02:06,060 ‫So it's a list of alarms we want to monitor just in case. 30 00:02:06,060 --> 00:02:12,210 ‫For example, we have too many errors on the newer version and hooks which are lambda functions that 31 00:02:12,210 --> 00:02:16,860 ‫are going to be running pre traffic shifting or post traffic shifting. 32 00:02:16,860 --> 00:02:22,860 ‫And they can be helpful to test your deployment and to run some customized code just in case you need 33 00:02:22,860 --> 00:02:23,400 ‫to. 34 00:02:23,580 --> 00:02:25,800 ‫So let's go ahead and practice this. 35 00:02:25,800 --> 00:02:27,840 ‫So let's go ahead with Sam. 36 00:02:27,840 --> 00:02:29,280 ‫We'll create a new application. 37 00:02:29,280 --> 00:02:34,770 ‫So I've created a SAM code deploy directory and I'm going to go into this directory with my terminal. 38 00:02:34,770 --> 00:02:39,030 ‫So I create a new terminal and then I go into, So where are we? 39 00:02:39,060 --> 00:02:40,650 ‫I'm going into the right directory. 40 00:02:40,650 --> 00:02:44,520 ‫So I will go into the SAM code deploy directory. 41 00:02:44,520 --> 00:02:45,300 ‫Here I am. 42 00:02:45,300 --> 00:02:48,450 ‫Then I'm going to initialize a new SAM application. 43 00:02:48,450 --> 00:02:52,110 ‫So I will use Python 3.7, which is installed on my system. 44 00:02:52,110 --> 00:02:56,130 ‫So I will use the Quickstart templates, name the project Sam app. 45 00:02:56,130 --> 00:03:02,520 ‫And then in terms of the Quickstart, I want to use the Hello World example press enter and here we 46 00:03:02,520 --> 00:03:03,480 ‫go, We're done. 47 00:03:03,480 --> 00:03:10,260 ‫Next I go into the SAM app directory and I'm going to run a SAM build command, which is right here 48 00:03:10,260 --> 00:03:12,600 ‫to build my SAM application. 49 00:03:12,600 --> 00:03:15,390 ‫So I will build it right now. 50 00:03:15,390 --> 00:03:19,830 ‫And while we're at it, let's have a look at what this app does. 51 00:03:19,830 --> 00:03:25,920 ‫So there is a Hello world function and there's our app dot pi and it returns. 52 00:03:25,920 --> 00:03:30,750 ‫Let's see what returns it returns just a body saying Hello world. 53 00:03:30,750 --> 00:03:37,110 ‫So this is a very simple application and what I want to see more importantly is the SAM template dot 54 00:03:37,110 --> 00:03:37,650 ‫YAML. 55 00:03:37,650 --> 00:03:44,850 ‫So if I scroll down on this SAM template, I can see it create a serverless function for me and currently 56 00:03:44,850 --> 00:03:51,120 ‫it outputs just this function as well some and then many of the API that it uses and so on. 57 00:03:51,120 --> 00:03:52,230 ‫So this is great. 58 00:03:52,230 --> 00:03:58,050 ‫But what I want to do is add a little bit of information around how to deploy this application with 59 00:03:58,050 --> 00:03:58,860 ‫code deploy. 60 00:03:58,860 --> 00:04:05,400 ‫So to do so I need to add a bit of YAML to this file and so I can find this YAML into a code deploy 61 00:04:05,400 --> 00:04:11,070 ‫that YAML file that I've created which contains the necessary code for code deploy to be integrated 62 00:04:11,070 --> 00:04:12,510 ‫with my SAM framework. 63 00:04:12,510 --> 00:04:18,000 ‫So I'm going to paste this and you want to make sure that the auto publish Alias live is in line with 64 00:04:18,000 --> 00:04:18,840 ‫your events. 65 00:04:18,840 --> 00:04:20,230 ‫So it is. 66 00:04:20,250 --> 00:04:26,610 ‫So what this is going to do is say, okay, you're going to auto publish an alias and then you're going 67 00:04:26,610 --> 00:04:29,640 ‫to do a canary 10%, ten minutes deployments. 68 00:04:29,640 --> 00:04:34,320 ‫So that means that every time we deploy into our alias, we're going to have two versions. 69 00:04:34,320 --> 00:04:36,660 ‫One of them will have 10% of the traffic for ten minutes. 70 00:04:36,660 --> 00:04:40,470 ‫And if everything goes well after ten minutes, it will switch to 100%. 71 00:04:40,470 --> 00:04:43,170 ‫And this will be using code deploy behind the scenes. 72 00:04:43,170 --> 00:04:45,900 ‫So I'm going to rerun my SAM build. 73 00:04:45,900 --> 00:04:51,480 ‫So I'm going to go into my terminal and redo my SAM build. 74 00:04:51,480 --> 00:04:52,890 ‫So I'll do some build. 75 00:04:54,560 --> 00:04:56,840 ‫And then I will do something a bit better. 76 00:04:56,840 --> 00:05:03,020 ‫I will just do a SAM deploy guided to deploy my function, and this will be even easier for me. 77 00:05:03,020 --> 00:05:08,690 ‫So I'm going to do a SAM deploy minus minus guided, and this should make the steps even more simple. 78 00:05:08,690 --> 00:05:09,830 ‫So here we go. 79 00:05:10,040 --> 00:05:12,020 ‫It says Sam config not found. 80 00:05:12,020 --> 00:05:16,730 ‫So I'll just say okay, the region I want to deploy to is EU west two. 81 00:05:16,940 --> 00:05:19,490 ‫Do you want to show the changes before deploying? 82 00:05:19,490 --> 00:05:21,050 ‫I'll say yes, I want to. 83 00:05:21,200 --> 00:05:24,260 ‫Do you want to allow a SAM crawler credentials. 84 00:05:24,260 --> 00:05:24,920 ‫Yes. 85 00:05:24,950 --> 00:05:26,420 ‫Do you want to save these? 86 00:05:26,420 --> 00:05:27,230 ‫Yes. 87 00:05:27,230 --> 00:05:28,460 ‫And here we go. 88 00:05:28,460 --> 00:05:33,860 ‫So it is creating the required resources and it should prompt me to say yes or no before these resources 89 00:05:33,860 --> 00:05:34,790 ‫are deployed. 90 00:05:35,060 --> 00:05:37,070 ‫So I'm waiting a little bit here. 91 00:05:38,150 --> 00:05:44,480 ‫So now my function is being deployed, the deployment is being initialized, and a change set is created 92 00:05:44,480 --> 00:05:45,650 ‫in CloudFormation. 93 00:05:45,740 --> 00:05:47,450 ‫So I'm just waiting for it to happen. 94 00:05:47,450 --> 00:05:52,730 ‫So here I can look at my change sets and it shows everything that is going to be created and I'm happy 95 00:05:52,730 --> 00:05:52,910 ‫to. 96 00:05:52,910 --> 00:05:56,870 ‫And there is a code deploy service role that is going to be created for me as well. 97 00:05:56,870 --> 00:05:58,340 ‫So this is exactly what I need. 98 00:05:58,340 --> 00:06:01,640 ‫So I'll say, yes, please go ahead and create all these things. 99 00:06:01,670 --> 00:06:07,100 ‫Now we need to wait for the CloudFormation template to be fully created and I see all the changes streamed 100 00:06:07,100 --> 00:06:08,240 ‫into my terminal. 101 00:06:08,830 --> 00:06:10,940 ‫So my stack has been fully created. 102 00:06:10,960 --> 00:06:18,070 ‫Now I can go into my management console and I can go to Lambda and see if my lambda function has been 103 00:06:18,070 --> 00:06:18,750 ‫created. 104 00:06:18,760 --> 00:06:24,130 ‫So if I go to applications on the left hand side, I can see my app right now that has been created. 105 00:06:24,250 --> 00:06:26,500 ‫And in here we can look at deployments. 106 00:06:26,500 --> 00:06:31,090 ‫And right now there has been a lambda application that has been deployed with an alias. 107 00:06:31,090 --> 00:06:33,280 ‫So let's have a look at this lambda function. 108 00:06:33,280 --> 00:06:37,300 ‫So for this, I'm going to go into my functions and try to find it. 109 00:06:37,300 --> 00:06:39,040 ‫So it's called Hello World Something. 110 00:06:39,040 --> 00:06:41,950 ‫So I will try to find it a second ago. 111 00:06:41,950 --> 00:06:45,070 ‫So if we look by last modified, here we go, we can find it. 112 00:06:45,100 --> 00:06:46,540 ‫Here is my function. 113 00:06:46,540 --> 00:06:48,610 ‫And so it's a function. 114 00:06:48,610 --> 00:06:51,610 ‫And if I go to actions I'm sorry to qualifiers. 115 00:06:51,610 --> 00:06:54,760 ‫I can see I have a live alias which is at version one. 116 00:06:54,760 --> 00:06:59,020 ‫So this represents the latest version that has been deployed by Melinda function. 117 00:06:59,020 --> 00:07:07,330 ‫So if I test this and I create a fake test event and create it and test it, now I can see that this 118 00:07:07,330 --> 00:07:09,740 ‫has succeeded and just says Hello world. 119 00:07:09,740 --> 00:07:13,730 ‫So this is my version one function on my alias live one. 120 00:07:13,730 --> 00:07:15,590 ‫But now we want to use code deploy. 121 00:07:15,590 --> 00:07:17,870 ‫So let's go and edit our function. 122 00:07:17,870 --> 00:07:22,760 ‫So we'll go into Hello World App.py and I'll say Hello World V two. 123 00:07:22,760 --> 00:07:31,070 ‫So this is an update of my function and I'm going to repackage my function so I will do some build to 124 00:07:31,070 --> 00:07:35,150 ‫build my function yet again, which will replace the application code. 125 00:07:35,150 --> 00:07:40,490 ‫And then I can do Sam deploy guided, which should deploy my function. 126 00:07:40,490 --> 00:07:42,410 ‫So I say yes, this is a stack name. 127 00:07:42,410 --> 00:07:43,400 ‫This is a region. 128 00:07:43,400 --> 00:07:43,880 ‫Yes. 129 00:07:43,880 --> 00:07:49,910 ‫I want to see the changes before deploying and yes again and yes again. 130 00:07:49,910 --> 00:07:52,520 ‫So everything is a yes now. 131 00:07:52,520 --> 00:07:57,770 ‫The deployment is initiating and everything is being uploaded into CloudFormation. 132 00:07:57,800 --> 00:08:02,570 ‫The change that is yet again created and I'll be able to see what will change before. 133 00:08:02,570 --> 00:08:08,840 ‫And so as we can see here, there will be a modification of the alias, There will be a function being 134 00:08:08,840 --> 00:08:11,450 ‫modified as well as a version being created. 135 00:08:11,450 --> 00:08:12,680 ‫So everything looks good. 136 00:08:12,680 --> 00:08:15,860 ‫And this should involve cloud code deploy. 137 00:08:15,860 --> 00:08:17,060 ‫So I'll say yes. 138 00:08:17,060 --> 00:08:21,200 ‫And now this is being applied into my CloudFormation. 139 00:08:21,200 --> 00:08:23,480 ‫And so this is where the cool thing happened. 140 00:08:23,510 --> 00:08:28,370 ‫I should be able to go into code, deploy and see the thing happening live. 141 00:08:28,370 --> 00:08:35,090 ‫So if I look at this alias, refresh this page from my alias, now I should be seeing at the very bottom 142 00:08:35,090 --> 00:08:39,560 ‫that there is a version one and version two with weight 90 and ten. 143 00:08:39,560 --> 00:08:41,120 ‫So this was done by my Sam CLI. 144 00:08:41,450 --> 00:08:47,360 ‫But more importantly, let's go to code, deploy and see if code deploy is functioning. 145 00:08:47,780 --> 00:08:53,300 ‫So we're looking at deployment and there is a blue green deployment happening for lambda right now. 146 00:08:53,300 --> 00:08:58,130 ‫I click on this deployment ID and I can see that the pre-deployment validation has succeeded because 147 00:08:58,130 --> 00:08:58,880 ‫there was none. 148 00:08:58,910 --> 00:09:02,510 ‫Then we can look at the code, deploy traffic shift in progress. 149 00:09:02,510 --> 00:09:10,400 ‫So 90% on the original and 10% on the replacement which represents this 90 and ten from my alias. 150 00:09:10,400 --> 00:09:15,800 ‫So this is running the traffic shift and this will take about ten minutes to do because we have deployed 151 00:09:15,800 --> 00:09:20,000 ‫a lambda canary, 10%, ten minutes deployment configuration. 152 00:09:20,000 --> 00:09:26,180 ‫And so at the end of this, it's going to do a post deployment validation and then the entire traffic 153 00:09:26,210 --> 00:09:28,970 ‫should be 100% on my version two. 154 00:09:28,970 --> 00:09:31,520 ‫So let me pause here and wait a little bit. 155 00:09:31,760 --> 00:09:32,420 ‫Okay. 156 00:09:32,420 --> 00:09:34,430 ‫So now everything has completed. 157 00:09:34,430 --> 00:09:36,290 ‫The traffic shifting was successful. 158 00:09:36,290 --> 00:09:40,070 ‫The post deployment validation did not exist, so it was successful. 159 00:09:40,070 --> 00:09:45,050 ‫And if we scroll down, we can see a breakdown of all the events that happened. 160 00:09:45,050 --> 00:09:51,650 ‫And so if I go back to my alias and now refresh this page, what I should be seeing is that the entire 161 00:09:51,650 --> 00:09:56,510 ‫alias being updated is yes, it's just Alias pointing to version number two. 162 00:09:56,510 --> 00:10:02,090 ‫And so as such, we've done a canary deployment of our lambda function using code deploy and the SAM 163 00:10:02,090 --> 00:10:02,750 ‫framework. 164 00:10:02,780 --> 00:10:03,450 ‫That's it. 165 00:10:03,470 --> 00:10:05,900 ‫Hope you liked it and I will see you in the next lecture.