🧱 AWS CDK Core Concepts: Constructs, Stacks, and Apps


πŸ‘€ This is not properly displayed? Read all of our issues online! πŸ’‘

Hi Reader πŸ‘‹πŸ½

We hope you're having an amazing week!

After writing about Terraform and the Serverless Framework let's tackle Sandro's favorite framework for IAC, which is the AWS Cloud Development Kit - the CDK.

What is the CDK?

The CDK was launched back in July 2019, which makes it one of the younger Infrastructure-as-Code tools out there.

It is a so-called componentized for provisioning your resources. Check out this article to learn more about the different stages of Infrastructure-as-Code frameworks.

The main difference between CDK and declarative tools like CloudFormation is the programming language you use.

In CloudFormation, you create configuration files in YAML or JSON. In CDK, you can use a proper programming language such as Python or TypeScript.

After writing your code, CDK will transform this code into CloudFormation templates. This has two amazing benefits:

  1. You can use all the benefits of your preferred programming language.
  2. You can use the powerful CloudFormation service (often this is a benefit, sometimes also a con πŸ˜‰)

For the examples, we will use TypeScript. You can also use the examples in another language of your choice.

Benefits of CDK

CDK allows us to use our own programming languages. This enables us to build abstractions. And it also allows us to use our most beloved tool, the IDE. The IDE is the Integrated Development Environment. For example, VS Code, Webstorm, or IntelliJ. What benefits does that actually bring us?

Developer πŸ’– IDE

Developers love their IDEs. We are used to them. We understand shortcuts, file managers, and actions like refactoring or debugging.

Developer Experience

It is amazing how fast developers can be with a programming language and syntax they know. The only issue in the past came with how to provision resources. These days are gone with the CDK.

CDK Wants You to Build Abstractions

The core building blocks of CDK are Constructs. Constructs can but don't have to be shared. This enables developers to create constructs and share them across the company.

For example, if you have a standardized way of creating Lambda functions with attached DLQs you can publish a construct for that.

CDK Is Developed by AWS

When choosing a framework to develop your cloud infrastructure in it is important to think about the stability and future of the tool. CDK is developed and supported by AWS. This brings a great benefit compared to other tools.

Concepts of CDK - Apps, Stacks, Constructs

The CDK follows the concept of:

  • Construct - this is the main building block, e.g. an S3 bucket
  • Stack - a deployable unit of one or more constructs
  • App - one or more stacks

Constructs Define the Actual Cloud Resource

Constructs are the main building blocks in CDK. You should model your application by creating constructs for the different cloud resources you need. Constructs can be reusable but don’t need to be. That means you can also create a construct that is only used once.

Constructs are differentiated into different levels.

Level 1: This is the exact CloudFormation Construct:

Level 2: Level 2 constructs have some abstractions built-in on top of Level 1 like additional APIs, and default settings. They are often much easier to work with.

Level 3: Level 3 constructs are often community-shared constructs and combine multiple services with each other. The CloudFrontToS3 construct, for example, connects a CloudFront distribution (CDN) with an S3 bucket. This is often used for hosting single-page applications.

πŸ‘†πŸ½ Tip: Try using as many Level 2 constructs as possible when developing your infrastructure with CDK. Don’t start over abstracting constructs right from the beginning. See your patterns emerging and build on top of that.

Stacks - Deployable Units

Stacks bundle multiple Constructs together and build them into one deployable unit. The concept is similar to stacks in CloudFormation. One stack contains one or more Constructs.

Here is an example stack in CDK:

Modeling stacks is an art in itself. There are two common approaches to modeling your stacks:

Stateless vs. Stateful

In this example, we can see that DynamoDB is in the stateful stack. Lambda and API Gateway are in the stateless stack.

Microservices

The second common approach is to model your stack by Microservices:

Whatever you decide, try to stick to one approach πŸ˜‰

Apps Bundle Multiple Stacks Together

This brings us to the last concept, the App. The App is the entry point of your CDK application (hence the name). The CDK app combines all stacks.

The app bundles all stacks together and has several phases: constructing, validating, synthesizing, and deploying your infrastructure.

The details here are not important to get started with the CDK!

Final Words

This is about the CDK introduction πŸ‘‹

Now get your hands dirty with your first hands-on project.πŸ‘©β€πŸ’» πŸ§‘β€πŸ’»

​

Have a great rest of the week!

Sandro & Tobi ✌️

​

P.S.: Thanks for making it that far. Maybe you didn't know, but we are working on a new project: The CloudWatch Book. This project will include deployable code πŸ‘Ύ, an e-book πŸ“•, infographics πŸ“Έ, and even a video course πŸ“½οΈ.

If you want to receive detailed info about this in the future, click on the following button ↓


πŸ’‘ As a subscriber, you can get our e-book with a 15% discount at all times.

Still hungry for AWS content? Have a look at our blog! πŸ“š ↓

AWS for the Real World

Join our community of over 8,800 readers delving into AWS. We highlight real-world best practices through easy-to-understand visualizations and one-pagers. Expect a fresh newsletter edition every two weeks.

Read more from AWS for the Real World

Hey Reader First things first: we apologize for not providing updates on The CloudWatch Book for a while! 😒 Sometimes, things don't go as planned and unexpected obstacles arise. But now, we're back in action, creating videos and putting the final touches on the book's content! πŸŽ₯ Don't just take our word for it! As an early subscriber, here's a free video from one of our favorite chapters: Anomaly Detection πŸ› In this deep-dive, you'll learn how to detect unusual patterns in metrics without...

βŒ› Reading time: 11.5 minutes πŸŽ“ Main Learning: Host Web Applications on AWS with the CloudFront Hosting Toolkit πŸ‘¨πŸ½πŸ’» GitHub Code πŸ“ Blog Post Hey Reader πŸ‘‹πŸ½ Happy New Week! I (Sandro) will attend the Serverless Days in Milano next week where Jeremy Daley will hold the keynote. I look forward to meeting many of you and the overall AWS community. This week's newsletter is all about hosting your frontend on AWS. AWS launched a new way of deploying your frontend to it: The CloudFront Hosting Toolkit....

βŒ› Reading time: 7 minutes πŸŽ“ Main Learning: Visual Regression Testing with AWS CloudWatch Synthetics πŸ‘¨πŸ½πŸ’» GitHub Code πŸ“ Blog Post Hey Reader Keeping your website running smoothly is important.It's also important to keep an eye on its visual appearance and that it doesn't change due to an unwanted change. AWS CloudWatch Synthetic Canaries is a tool that helps you check on your websites (and APIs!) to make sure it’s working well and look as expected. In this newsletter, we’re going to show you...