1. 3
    Build and deploy a sample AWS Cloud Development Kit stack to AWS
    3m 11s
⚠️ This lesson is retired and might contain outdated information.

Build and deploy a sample AWS Cloud Development Kit stack to AWS

Tomasz Łakomy
InstructorTomasz Łakomy
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated a year 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 construct here )
  • 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:00] Let's take a quick look at our CDK project. As we can see there are lots of things that were created and configured for us. There are two important files here.

[0:07] First up, there's been to-do app JS which is the entry point of our AWS CDK application. This is going to import the to-do app stack from [inaudible] to-do app stack. This is where we're going to spend the most of our time in because this is where our stack is defined.

[0:21] Basically, CDK apps are effectively a definition of your infrastructure using code. What kind of infrastructure? This is up to us. The other thing that we actually have to import in each CDK stack is this line. Import CDK from CDK core.

[0:35] This contains core CDK, the parenthesis such as CDK stack, CDK constructs and so on. The rest of them depends on what kind of things that we want to ship into AWS. For instance, this sample app is using SQS. SQS stands for Simple Queue Service and also an SNS topic. SNS stands for Simple Notification Service.

[0:54] For the record, you don't need to necessarily know what those are because we are not going to use either SQS and SNS in this course.

[1:01] What is important to highlight here is that both SQS queue and SNS topics are instances of something that is called a construct. According to the official documentation, constructs are the basic building blocks of AWS CDK apps. A contract represents a cloud component and encapsulates everything AWS CloudFormation needs to create the component.

[1:20] What that means is that a contract can represent a single resource such as Amazon Simple Storage Service bucket. As an example, we can see that over here, this SNS topic is a construct, because it's going to represent a single resource in AWS which in this case is a topic.

[1:34] Also, a construct can represent a higher level component consisting of multiple CDK resources. For instance, we can define an entire infrastructure of an app as a single construct. and you can both publish those constructs, for instance, to NPM.

[1:47] Also, use constructs that were created by somebody else in the community to make your work easier and avoid reinventing the wheel. With that being said, let us build and deploy our stock to AWS. To do that around NPM, around wat in order to start the TypeScript compiler next up in the second terminal window, we are going to run CDK diff.

[2:04] CDK diff is a command that shows us the difference between the stack that we already have deployed to AWS and the stack that we are about to deploy.

[2:11] Since we don't have anything deployed to AWS right now, this is going to show everything that we are about to deploy. As you can see, there are quite a few things highlighted here in this output. The most important ones are those resources.

[2:23] As you can see over here that we are about to deploy the SQS queue, which is defined over here, and also a SNS topic, which is also defined over here in our code. This is also kind of important to highlight that there's so many things that are already configured for us in a diverse code and we don't necessarily have to care about those.

[2:40] What we do care about is defining can infrastructure that we want to ship into AWS and CDK is going to do the heavy lifting for us in order to shift this infrastructure to AWS around CDK deploy.

[2:51] Basically, the time that we are about deploy potentially is sensitive changes to AWS, it's going to ask us for a confirmation. Right now, CDK is asking us whether we are OK adding the substitution to an SNS topic. We are more than OK with that.

[3:02] I'm going to press yes and continue deploying those changes. After about 20 seconds, we see a confirmation over her that our to-do apps has been successfully deployed to AWS cloud.

Todor
Todor
~ 4 years ago

Running cdk diff error with:

"Unable to resolve AWS account to use. It must be either configured when you define your CDK or through the environment"

I assume we need to configure an account as a prerequisite?

Todor
Todor
~ 4 years ago

Yes, I followed this AWS tutorial:

https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html

  1. Install AWS locally (from your terminal)
  2. Register and create a new administrator IAM user with access keys and then run.
  3. Run "aws configure" (from your terminal)
  4. Done, now "cdk diff/deploy" should work
Quang Le
Quang Le
~ 4 years ago

Hi Tomasz,

It would be great if you can break down some major steps in the CDK deployment? For example, it compiles the cdk code, generates the cloudformation template, packages it in a zip file, creates S3 bucket and loads code there, runs the cloudformation command to create cloudformation stack, deploys the lambda function (if it is for aws lambda project) etc.

I would also want to learn what are the minimum permissions (or policies) needed to do the cdk deployment? Like write access to S3 for cloudformation stack, create stack permission etc. The cdk bootstrap seems providing too broad power, it goes against the best practice of AWS least privilege.

Thank you

Markdown supported.
Become a member to join the discussionEnroll Today