‫So here is something I promised you before.
‫We are going to see how code deploy is integrated with the framework and Sam will be using code deploy
‫to update our lambda functions and that will leverage the traffic shifting feature we've seen before.
‫Using the aliases, we can also define pre and post traffic hooks, lambda functions to validate the
‫deployments and we can do an automated rollbacks with cloudwatch alarms.
‫So in our diagram to remember to remind you, we had the lambda alias that had V1 lambda and we're going
‫to trigger a code deploy deployment with or with a CI CD or with a SAM framework.
‫And then we want to update our alias into a V2.
‫So code deploy will be running some pre traffic hook test using another lambda function and that's optional.
‫Then it will do a traffic shifting with the alias.
‫So according to a strategy and then it will monitor a cloudwatch alarm.
‫And that's also optional to ensure everything goes well during the deployment.
‫And then once the deployment is done and the traffic shifting is completely done, we can run a post
‫traffic hook lambda function.
‫This is again optional to run some tests onto your alias and if everything goes well, then your V1
‫function in your alias goes away and we're just ending up with the V2.
‫So here is an example of the YAML file that you will have within Sam.
‫So I want to just show you a couple of things.
‫The first one is auto publish Alias.
‫So this is helping Sam detect when new code is being deployed.
‫In that case, a new lambda function will be deployed as a new version with the latest code and then
‫automatically the alias will be updated to point to the updated version of the lambda function.
‫And in terms of how fast that happens, well, this is controlled by the deployment preference.
‫And so we have different types.
‫We have the canary type, the linear type and the all at once type, and they're basically code deploy
‫settings.
‫So in this example, we have canary 10%, ten minutes, then we have alarms.
‫What are the alarms that can trigger a rollback in your deployment?
‫So it's a list of alarms we want to monitor just in case.
‫For example, we have too many errors on the newer version and hooks which are lambda functions that
‫are going to be running pre traffic shifting or post traffic shifting.
‫And they can be helpful to test your deployment and to run some customized code just in case you need
‫to.
‫So let's go ahead and practice this.
‫So let's go ahead with Sam.
‫We'll create a new application.
‫So I've created a SAM code deploy directory and I'm going to go into this directory with my terminal.
‫So I create a new terminal and then I go into, So where are we?
‫I'm going into the right directory.
‫So I will go into the SAM code deploy directory.
‫Here I am.
‫Then I'm going to initialize a new SAM application.
‫So I will use Python 3.7, which is installed on my system.
‫So I will use the Quickstart templates, name the project Sam app.
‫And then in terms of the Quickstart, I want to use the Hello World example press enter and here we
‫go, We're done.
‫Next I go into the SAM app directory and I'm going to run a SAM build command, which is right here
‫to build my SAM application.
‫So I will build it right now.
‫And while we're at it, let's have a look at what this app does.
‫So there is a Hello world function and there's our app dot pi and it returns.
‫Let's see what returns it returns just a body saying Hello world.
‫So this is a very simple application and what I want to see more importantly is the SAM template dot
‫YAML.
‫So if I scroll down on this SAM template, I can see it create a serverless function for me and currently
‫it outputs just this function as well some and then many of the API that it uses and so on.
‫So this is great.
‫But what I want to do is add a little bit of information around how to deploy this application with
‫code deploy.
‫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
‫that YAML file that I've created which contains the necessary code for code deploy to be integrated
‫with my SAM framework.
‫So I'm going to paste this and you want to make sure that the auto publish Alias live is in line with
‫your events.
‫So it is.
‫So what this is going to do is say, okay, you're going to auto publish an alias and then you're going
‫to do a canary 10%, ten minutes deployments.
‫So that means that every time we deploy into our alias, we're going to have two versions.
‫One of them will have 10% of the traffic for ten minutes.
‫And if everything goes well after ten minutes, it will switch to 100%.
‫And this will be using code deploy behind the scenes.
‫So I'm going to rerun my SAM build.
‫So I'm going to go into my terminal and redo my SAM build.
‫So I'll do some build.
‫And then I will do something a bit better.
‫I will just do a SAM deploy guided to deploy my function, and this will be even easier for me.
‫So I'm going to do a SAM deploy minus minus guided, and this should make the steps even more simple.
‫So here we go.
‫It says Sam config not found.
‫So I'll just say okay, the region I want to deploy to is EU west two.
‫Do you want to show the changes before deploying?
‫I'll say yes, I want to.
‫Do you want to allow a SAM crawler credentials.
‫Yes.
‫Do you want to save these?
‫Yes.
‫And here we go.
‫So it is creating the required resources and it should prompt me to say yes or no before these resources
‫are deployed.
‫So I'm waiting a little bit here.
‫So now my function is being deployed, the deployment is being initialized, and a change set is created
‫in CloudFormation.
‫So I'm just waiting for it to happen.
‫So here I can look at my change sets and it shows everything that is going to be created and I'm happy
‫to.
‫And there is a code deploy service role that is going to be created for me as well.
‫So this is exactly what I need.
‫So I'll say, yes, please go ahead and create all these things.
‫Now we need to wait for the CloudFormation template to be fully created and I see all the changes streamed
‫into my terminal.
‫So my stack has been fully created.
‫Now I can go into my management console and I can go to Lambda and see if my lambda function has been
‫created.
‫So if I go to applications on the left hand side, I can see my app right now that has been created.
‫And in here we can look at deployments.
‫And right now there has been a lambda application that has been deployed with an alias.
‫So let's have a look at this lambda function.
‫So for this, I'm going to go into my functions and try to find it.
‫So it's called Hello World Something.
‫So I will try to find it a second ago.
‫So if we look by last modified, here we go, we can find it.
‫Here is my function.
‫And so it's a function.
‫And if I go to actions I'm sorry to qualifiers.
‫I can see I have a live alias which is at version one.
‫So this represents the latest version that has been deployed by Melinda function.
‫So if I test this and I create a fake test event and create it and test it, now I can see that this
‫has succeeded and just says Hello world.
‫So this is my version one function on my alias live one.
‫But now we want to use code deploy.
‫So let's go and edit our function.
‫So we'll go into Hello World App.py and I'll say Hello World V two.
‫So this is an update of my function and I'm going to repackage my function so I will do some build to
‫build my function yet again, which will replace the application code.
‫And then I can do Sam deploy guided, which should deploy my function.
‫So I say yes, this is a stack name.
‫This is a region.
‫Yes.
‫I want to see the changes before deploying and yes again and yes again.
‫So everything is a yes now.
‫The deployment is initiating and everything is being uploaded into CloudFormation.
‫The change that is yet again created and I'll be able to see what will change before.
‫And so as we can see here, there will be a modification of the alias, There will be a function being
‫modified as well as a version being created.
‫So everything looks good.
‫And this should involve cloud code deploy.
‫So I'll say yes.
‫And now this is being applied into my CloudFormation.
‫And so this is where the cool thing happened.
‫I should be able to go into code, deploy and see the thing happening live.
‫So if I look at this alias, refresh this page from my alias, now I should be seeing at the very bottom
‫that there is a version one and version two with weight 90 and ten.
‫So this was done by my Sam CLI.
‫But more importantly, let's go to code, deploy and see if code deploy is functioning.
‫So we're looking at deployment and there is a blue green deployment happening for lambda right now.
‫I click on this deployment ID and I can see that the pre-deployment validation has succeeded because
‫there was none.
‫Then we can look at the code, deploy traffic shift in progress.
‫So 90% on the original and 10% on the replacement which represents this 90 and ten from my alias.
‫So this is running the traffic shift and this will take about ten minutes to do because we have deployed
‫a lambda canary, 10%, ten minutes deployment configuration.
‫And so at the end of this, it's going to do a post deployment validation and then the entire traffic
‫should be 100% on my version two.
‫So let me pause here and wait a little bit.
‫Okay.
‫So now everything has completed.
‫The traffic shifting was successful.
‫The post deployment validation did not exist, so it was successful.
‫And if we scroll down, we can see a breakdown of all the events that happened.
‫And so if I go back to my alias and now refresh this page, what I should be seeing is that the entire
‫alias being updated is yes, it's just Alias pointing to version number two.
‫And so as such, we've done a canary deployment of our lambda function using code deploy and the SAM
‫framework.
‫That's it.
‫Hope you liked it and I will see you in the next lecture.