Build and Deploy a Sample AWS Cloud Development Kit Stack to AWS

Tomasz Łakomy
InstructorTomasz Łakomy

Share this video with your friends

Send Tweet
Published 4 months ago
Updated 3 months ago

Now that our initial AWS CDK project has been created, it's time to deploy it! In this quick lesson we're going to learn: where do we define an AWS CDK stack what is a CDK construct (you can read more about CDK constructs here) how to bootstrap a CDK environment with cdk bootstrap how to use cdk diff command in order to see the difference between our currently deployed stack and the stack we're about to deploy how to use cdk deploy in order to deploy a CDK stack to AWS

Instructor: [0:04] Let's take a quick look at our CDK project. As we can see, there are a lot of things that were created and configured for us. There are two important files here. First of all, there's bin/todoapp.ts which is the entry point of our CDK application. We can see that it is going to import a todoapp stack from lib/todoappstack.

[0:20] Let's go over here, and this is where we are going to spend most of our time in because this is where our CDK stack is defined. Basically, a CDK stack is a definition or a description of what we are going to deploy into AWS cloud.

[0:32] In this particular example, we can see here at the top that we are importing an SNS which stands for Simple Notification Service, and SQS which stands for Simple Queue Service.

[0:43] For the record, you don't need to necessarily know what those are because we are not going to use either SQS or SNS in this course. What matters here is that both SQS queue and SNS topic are an instance of something called a construct in CDK.

[1:01] According to official AWS docs, constructs are the basic building blocks of AWS CDK apps. A construct represents a cloud component and encapsulates everything AWS needs to create their component. In our case, this SNS topic contains everything that AWS needs to know in order to create a brand new simple notification service topic in our AWS account.

[1:24] In this case, a construct is going to create a single resource in an AWS account, but constructs can represent higher-level components consisting of multiple CDK resources. For instance, we can define an entire infrastructure of an app as a single construct and you can also publish those constructs, for instance, to npm.

[1:38] In addition to that, you can also use constructs that were created by other members of AWS community which are available online on various sites such as AWS Construct Hub. With all of that in place, let's give it a shot and deploy the CDK stack to AWS.

[2:03] First of all, let's go back to bin/todoapp.ts. Over here, we are going to define to which AWS region we are going to deploy our stack to. I'm going to do nth and I'm going to set the region to eu-west-1. You are free to use any region that you want. I just happen to live in Europe so I'm going to choose the region which is closest to me.

[2:13] With all of that in place, let me open up the terminal and I'm going to run cdk deploy. We are getting this very scary-looking error. What matters here is that we need to run cdk bootstrap in order to bootstrap our environment in AWS cloud. I'm going to do exactly that.

[2:33] I'm going to clear the terminal and run cdk bootstrap. This took a second, but now we can see that the environment has been bootstrapped in my AWS account and in the region that I have chosen over here.

[2:57] Let's navigate to AWS Console. Afterwards, I'm going to go to AWS CloudFormation. Here we can see that we have created a CDK Toolkit stack -- let me just close that over here -- which includes all the resources needed to deploy AWS CDK apps into this environment. You don't need to actually look into this stack, but this stack needs to exist in order to use CDK in your applications.

[3:12] Let's go back to the terminal. Before I'm going to deploy this CDK stack, I'm going to run cdk diff to see what exactly am I about to deploy. Here in the resources section, we can see that, for instance, those three lines, I'm going to create a brand new AWS SQS queue over here, and we're also going to create an SNS topic and a subscription.

[3:36] In that sense, we are translating the TypeScript code to actual AWS resources created in AWS account. With all of that in place, let me go ahead and run cdk deploy once more. Every time we are about to make potentially sensitive changes to our stack, CDK is going to ask us whether we want to deploy those changes.

[3:54] In this particular case, I want to deploy those changes so I'm going to hit Yes and hit Enter. After 20 seconds or so, we can see that our stack has been successfully deployed to AWS cloud. Let's go back to our AWS Console. I'm going to go back to CloudFormation. Here we can see that our Todo app stack has been successfully deployed.