hi my name is noah gift and today i'm going to talk to you about aws cdk with python
we're going to build a project that will deploy something in just a few minutes using the aws
cloud 9 environment and infrastructure s code let's go ahead and get started
all right so here we're inside of aws cloud9 a perfect environment for working with cdk
first step what i'm going to do is i'm going to run this official documentation command
just make sure that i've got the right version of cdk okay good that's the latest version
next up i'm going to look at this initialization structure inside of a official aws workshop
i'll go through here and copy this i'll put this inside this goes through and it makes this
workshop directory and notice now i'm inside of it the next thing i'll do is i'll create a sample
app so we'll go through here run this command now i've got this sample app i can scroll up here and
and make sure that i do what it tells me to do so first step here i create the virtual environment
and i've sourced it great and now the next thing i'm going to do is make sure that i install what's
inside this requirements file let's take a look real quick what's inside of here this is all of
the python code that lets me use cdk so these are critical for steps once i've got this going now
i'm ready to actually go through and run other commands when the installation finishes perfect
and if i go through here and i type in for example cdkls this should make me
confident that cdk is working and it looks like it is working it's thinking about the stacks there we
go perfect next up now i'm going to go back to this documentation here i'm going to go to the
hello cdk and go to hello lambda it asks me to create a directory called lambda inside of the
cdk workshop and then create this file inside so let's go ahead and copy this go inside of here
let's make a directory called lambda perfect and then i'll touch lambda let's see the name it wants
us to do is hello.py that looks great hello dot p y and if i go inside of here i can just paste this
in very simple lambda lambda functions are python functions in the case of the python language that
accept an event very very straightforward and they're run inside of aws next up what i can do is
install the lambda construct library so how do we do this this is an interesting question well one
way to do it would be to take this construx code here and let's go over to our project
and go into uh the section that actually has all of our infrastructure built in
and where would that be well we would just need to look inside for that particular stack and in
this case we have it right here cdk workstop stack let's go ahead and double click this
and let's copy the whole thing delete it and replace it so this is going to deploy
our code here from cdk perfect now that i've got that inside the stack i can scroll down here
and go to cdk diff and this will show me what is the local versus remote version since we've never
deployed anything there should be a pretty big diff let's go ahead and run this diff
and we can see that it's going to go through and look at aws look at what we've got locally and see
if there's changes in fact there is right because we know that it's going to need to deploy this
change to aws so now that i've got that running i just type in cdk deploy
let's go here next cdk deploy and we can actually push this into aws this will take just a second
perfect do we want to deploy these changes yes we do and now in this case we can say please run
cdk bootstrap inside we've got a problem so these are these are uh pretty common things that happen
inside of cdk deployment let's go ahead and do that let's type in cdk a bootstrap perfect
and this will make sure that the bootstrapping is created so that we can do the changes on aws
one one nice thing about doing this in the cloud9 environment is it really is
ideally suited for working with cdk because it gives you such great feedback
and you know that it has the role level privileges in order to make these changes
okay now we go we see this is actually making changes and i can watch this and and make it
make sure that it deploys but i'm going to go back to the documentation here and double
check uh that everything is working and really the final step here is to go to aws lambda cons
console and make this change so i'm while this thing is going here i'm going to also open up a
console to lambda and we go through here we should see a last modified filter and this will populate
once we've got this thing complete so i can just wait for this thing to go
okay looks like it's deployed it took about 77 seconds not too bad considering how much
work it's doing if we go back to this lambda console there we go 20 seconds ago we created
it let's go to this console we know that it's very straightforward to test this out because
it's a very simple lambda function i'm going to click on test i'm going to go through here and say
hello and then for here let's just make something simple just say like events and then say hi there
we go let's go ahead and format the json create the events good through a test and uh oh it says
we have a stack trace here it wasn't looking for a payload like that it's actually looking for
something else and so we could go through and test that out let's go ahead and configure
another test event in fact we can just go to look at the documentation
and see what they want us to send so you could go through and configure a more complex test
event but the main the main idea here is that in fact if we look at our code here we can see that
it doesn't it looks like it's looking for the word path inside the event and so we could
just put that word path inside let's go ahead and do that let's just say path path is hello
and let's go ahead and save this format save test it there we go now we got rid of the stack tray
so pretty straightforward to go through and use the cdk system to deploy your changes and
then later you can actually update the changes by looking at the diff and then deploying them again