From the course: AWS Certified Advanced Networking - Specialty (ANS-C01) Cert Prep

Infrastructure as code demo

- [Instructor] Another important component of a project is the ability to implement infrastructure as code on the AWS platform. Amazon CDK is one of the ways that you can do this. Why would you want to use CDK? Well, one of the things to talk about is that you can build with high-level constructs, like the ability to provide sensible secure defaults for your resources. The second is you can use programming idioms, like maybe a parameter or a condition or a loop and build through those solutions, and then finally, you can connect your AWS Resources all together so that they have the permissions and are built in an API-friendly way. So, if you go through the CDK system, you'll come up with maybe a stack like this, and if you look at it, you can see that this stack will have these constructs, which could include things like Amazon SQS or Lambda, and also the ability to talk to core services like Amazon S3 or DynamoDB. If we go through and look at a demo here, this is a great place for us to dive into the code. I'm going to take a look at this cdkworkshop.com here, where I can take a look at an example Hello, World Lambda project. And you can see here this is a very simple Lambda project, and inside of here, if I wanted to deploy it, all I would need to do is scroll down here and follow the instructions. For example, build a stack, and then finally when I'm done, I would just do a cdk diff, and then finally do a cdk deploy, so let's go ahead and do that. So, if I go here, you can see I have this stack here set up, and inside I actually have defined a Lambda function. If we look at this Lambda function, you can see that it's a Marco Polo function. I pass in a name, you look for the word Marco, I return back polo. If I don't find that, I return back no. So, it's a very straightforward function, and I can deploy this with the CDK command. First up, I would just go to the terminal and type in cdk --version, and you can see here that it's 2.8. If I wanted to do a cdk deploy, I would just go through here and do cdk deploy. It would then go through and make sure that that is actually deployed if there were any new changes. Now, one of the things I can do is also go over to this tab here, AWS, and find that Lambda function. If I go into Cloud9 and find this cdk-workshop function, right-click on it, invoke this on AWS, and this will give me the ability to put a sample payload, and in fact, we know that this sample payload could include a name, and then I could include the phrase Marco, and let's make sure that this deployment was successful. If it was, then this invocation will be successful. And there we go, we can see that the return back was successful. So, really a very straightforward way to deploy your code is to take a look at your changes and control a stack here. Make sure that you deploy those changes using the CDK command, and then also inspect or invoke your function or whatever resource you created inside of AWS.

Contents