‫So let's go ahead to practice stages and stage variables.
‫So the first thing I want to do
‫is to just change my Lambda functions,
‫so I'm just going to change my first Lambda function,
‫so the "root get", to make this very simple.
‫And I'm going to say right here
‫that this is "Hello from Lamda v1!"
‫And so I'm going to save this
‫and I'm going to publish this version.
‫So, I've published this new version,
‫this is my version one.
‫And then I'm going to go back
‫to my latest, I'm going to create
‫a new version so I go to my Lambda function,
‫I say, "Hello From Lambda v2!"
‫I will save this and I will also publish
‫this new version, so now this is my version two.
‫And now that we have two versions,
‫we can create multiple aliases.
‫So I can create an alias, and Action, Create alias.
‫And this one is going to be called "DEV."
‫And it's going to point to the latest version
‫of our Lambda function.
‫Then there's going to be an alias named "TEST."
‫so I'll create an alias, I'll name is "TEST".
‫And it's going to point to version two
‫of our Lambda function.
‫And then finally, there will be an alias "PROD."
‫So I'll call this one "PROD" and it's going
‫to point to version one of our Lambda function.
‫So this a set-up we've done from before,
‫so this is why I'm going a bit faster.
‫So we have three Lambda versions,
‫actually one latest and two Lambda versions.
‫And then three aliases, okay?
‫So, extremely simple, if we test
‫the PROD alias, and we test it.
‫It says, "Hello from Lambda v1!"
‫And if we test version two, it should say.
‫"Hello from Lambda v2!"
‫Excellent.
‫So, back into our API Gateway,
‫now we want to be able to point to the right alias.
‫So, for Resources, under my GET in here,
‫I'm going to change the API that gets invoked.
‫So I'll try to make this even more simple.
‫I'll create a resource and I'll call
‫this resource "dynamic" or something like this.
‫Or "stagevariables," even better.
‫Stagevariables, and this is going
‫to be demo-ing the stage variables feature.
‫So, I'll create this resource.
‫And underneath, I'm going to create
‫a GET so I will create a Method.
‫And this is a GET and it's going
‫to be invoking our Lambda function.
‫So, a Lambda function in proxy mode,
‫and the Lambda function, you get the name,
‫so get this entire name right here.
‫So I copy and paste it.
‫But I'm going to add something,
‫so I'd add a column.
‫Then dollar sign, I open brackets
‫and I write "stageVariables" with an S, dot, "lambdaAlias."
‫And I close this.
‫So here we are defining the fact
‫that the Lambda function we want to invoke,
‫right now, we don't really know it.
‫But this will be defined by the value
‫of the stage variable called "lambdaAlias."
‫So hopefully this works.
‫I'm going to save this and now it says,
‫"you defined your Lambda function as a stage variable.
‫Please ensure that you have the appropriate
‫Function Policy on all functions you will use."
‫And so for this, you can run
‫the AWS CLI command to do it correctly.
‫So I'm going to execute this, so,
‫if we look at the current permissions
‫of our Lambda functions.
‫So if we look at these permissions,
‫I'm going to refresh this page.
‫So if we look at the current Resource-based policy,
‫as we can see, it's empty.
‫And so we are going to add a resource-based policy
‫for this Lambda function.
‫So, let's go ahead and actually
‫it's going to be at the alias levels,
‫so I will go for example to the DEV alias,
‫and there is no Resource-based policy.
‫So let's go ahead and add one.
‫So I'm going go in my CLI, and copy this command
‫and I have to change something.
‫So I have to change and instead of the entire
‫stage variable in here, I'm going to remove
‫everything and I'm just going to replace this with DEV.
‫So once I'm done, I'll just write DEV.
‫Press Enter, and this has not functioned
‫because I'm in the wrong region.
‫So, region, region.
‫So, "--region eu-west-2"
‫And this should work now.
‫Okay, so this works.
‫Then, I'll do the exact same thing,
‫but this time I'll do it not with DEV, but with TEST.
‫So I replace the text on the right hand side by TEST.
‫And this has worked.
‫And then finally, I'll do one last time
‫this, but I will replace the text
‫with PROD so, colon PROD.
‫And we're good to go.
‫So this is something that the council
‫currently doesn't do for us,
‫we have to do it ourselves.
‫So the reason is now if I refresh this page,
‫we can see, and we'll see it very very soon,
‫so if I go to versions, alias and then
‫I'll go to PROD, we can see the Resource-based policy
‫for PROD, does allow my API gateway to invoke
‫my PROD function.
‫My PROD alias, and this is going
‫to be the same if we go into DEV,
‫and this is going to be the same if we go into TEST.
‫So this is something that's necessary
‫to do when you're using stage variables,
‫which is what we're trying to demonstrate right here.
‫So this is what the error message
‫was saying, now I click on "OK."
‫So now I can test my stage variables,
‫so I'm going to click on Test, I scroll down
‫and here there's a stage variable I have to fill.
‫So if I write "DEV" for example,
‫and test it, it says, "Hello from Lambda v2!"
‫Which is the exact output we would get
‫if we go to the alias DEV and run our functions.
‫So we would get, "Hello from Lambda v2!"
‫Now, let's test it out.
‫So we'll do TEST and see what the outcome
‫is so it again will say, "Hello from Lamda v2!"
‫which is the same we would have
‫if we had the TEST in here.
‫So we'll just test it and it says,
‫"Hello from Lamda v2!"
‫But then if we actually use the PROD,
‫so, I'll write PROD in here,
‫and click on Test, I get, "Hello from Lamda v1!"
‫So that means that my stage variables are indeed working.
‫And this is the same output we would get
‫if we went into the alias PROD,
‫and then test our function, which has,
‫"Hello from Lamda v1!"
‫So this is excellent.
‫That means our stage variables are working.
‫But now we need to deploy this API
‫and use it accordingly so I'm going
‫to deploy my API into my existing
‫deployment stage called DEV and click on Deploy.
‫And in DEV, I go into stage variables,
‫I will add a stage variable
‫and I'll call it "lambdaAlias."
‫And the value is going to be DEV.
‫And this is something I have to do once
‫for each stage I have my API into.
‫So this is my first API, then I go
‫to my second API that I'm going to deploy,
‫so I'll deploy a new API, and I'll call this one TEST.
‫Deploy it, and in here, in my stage variables,
‫I have to create a new one, so,
‫"lambdaAlias" and I'll name it TEST.
‫And then finally, I will create a third
‫stage, so, I'll create my PROD stage,
‫and I can use a deployment that we used from before,
‫another way of doing things.
‫I'll create it and I'll define a stage variable,
‫and this one I will say, "lambdaAlias", PROD.
‫Here we go.
‫So we have deployed three different stages,
‫so now let's go and test it out
‫from the URL, so, I will click on slash PROD,
‫and then what was the name of my resource?
‫I remember, Stage variable,
‫so I go to slash "stagevariables," press on Enter,
‫it says, "Hello from Lamda v1!"
‫But if I switch my stage and go
‫to DEV, it says, "Hello from Lamda v2!"
‫And likewise, for TEST, it should go
‫to "Hello from Lamda v2!"
‫So this is excellent.
‫This was a demonstration of how stages
‫and stage variables work, which I think is really
‫really full, a bit complicated,
‫I would agree, with the resource permissions,
‫but something that's necessary to see
‫before going to the exam.
‫And so now, we have our different stages
‫and we'll be able to look at the stage
‫configuration later on in more details.
‫So, that's it for this lecture.
‫I hope you liked it.
‫And I will see you in the next lecture.