DEV Community

K
K

Posted on

The AWS Cloud Development Kit

A short while ago AWS released a developer preview of the Cloud Development Kit (CDK). A tool to manage your AWS infrastructure... with JavaScript! Guess JavaScript really eats the world :D

What

The CDK is an imperative alterantive to CloudFormation (CF).

While CF uses JSON or YAML to define your services, the CDK now allows you to do this with JavaScript, TypeScript and Java.

The version available over NPM is implemented in TypeScript.

Concepts

The CDK uses a concept called construct to define infrastructure. A construct can has child-constructs, so they form a tree.

Constructs are either low-level CF resources or high-level AWS Construct Libraries.

The CloudFormation Resources are used as a fall-back for advanced configuration the AWS Construct Libraries don't provide yet.

The AWS Construct Libraries are NPM packages written in TypeScript. They are basically pre-configured CF resources.

Why

The idea seems to be tighter integration with the tools some devs already use to implement their systems. If you already know JavaScript, you don't have to learn the custom YAML/JSON-dialect of CF.

Also, the methods of resource provisioning used in the CDK are a bit higher-level than CF. AWS added some pre-configuration so the definition can be more concise.

That said, it doesn't save us from learning how CF works.

How

Let's take this simple DynamoDB example:

const { Stack, App } = require("@aws-cdk/cdk");
const dynamodb = require("@aws-cdk/aws-dynamodb");

class MyStack extends Stack {
  constructor(parent, name, props) {
    super(parent, name, props);

    const table = new dynamodb.Table(this, "Table", {
      tableName: "MyAppTable",
      readCapacity: 5,
      writeCapacity: 5
    });

    table.addPartitionKey("Alias", dynamodb.KeyAttributeType.String);
    table.addSortKey("Timestamp", dynamodb.KeyAttributeType.String);
  }
}

const app = new App(process.argv);

new MyStack(app, "MyStack");

process.stdout.write(app.run());
Enter fullscreen mode Exit fullscreen mode

As we can see, CDK libraries can be included like every other node package.

There is the core package, that defines basic constructs.

  • The App construct is the root of our application, with Stacks as its direct children and every other construct is a descendant.
  • The Stack construct is a direct child of an App and holds all the resources as children.

Since resourcs are packages too, we can simply include them too.

A Stack has to defines its resources in its constructor.

The resource definition is done by creating objects from the resources classes.

The dynamodb package defines a Table class, it takes a reference to MyStack, a name and a config object that should feel familiar to a DynamoDB user.

The Table object, which is a construct like Stack and App, also has methods to add optional configurations.

After the Stack is defined, an object of the App construct and the Stack construct is created. The object of the App construct is also passed as parent into the Stack object.

Finally the App construct can be executed to create the defined infrastructure.

Conclusion

The AWS Cloud Development Kit brings a new way to create your infrastructure with JavaScript and TypeScript.

I think it's a nice addition to the AWS tooling landscape.

It's in development so I shouldn't ask for too much, but to me it feels a bit like a C# dev created a JavaScript library. The whole passing around of this, extending classes and overriding constructors just feels clunky and non-idiomatic. Nested functions, like Reacts stateless components would probably been a cleaner idea.

Anyway, try it out and tell me what you think of it in the comments!

Top comments (8)

Collapse
 
iansavchenko profile image
Ian Savchenko

Thanks for the article! Cool stuff! We actually do a similar thing generating CloudFormation in JSON with JS, so having also TS (and IntelliSense in VS Code, I guess) would speed up development and reduce the need of checking docs all the time.

Collapse
 
thisisjoelc profile image
Joel Clemence

Looks interesting similar to Troposphere (a python library for creating cf stacks) which until you create a lot of cloudformation templates you realise that it is easier to read and write native cloudformation templates (more of a gripe with troposphere). This does look like it improves on troposphere by using classes which is nicer. And allows you to harness the power of a programming language to construct complicated templates (for example a template with several of the same resources but slightly different). Thanks for sharing.

Collapse
 
kayis profile image
K

CloudFormation Modules also uses NPM for distributing pre-defined templates, but sticks to YAML/JSON.

I don't know what is the better approach.

CDK is easier for JS developers to use, cfn-modules are easier for CF developers to use.

Collapse
 
rhymes profile image
rhymes • Edited

A lot of AWS SDKs (don't know if this is the case as well) are autogenerated code based on common JSON specs of the different services, so I'm not surprised if this one feels weird :D

Collapse
 
kayis profile image
K

They say the TypeScript code is handwritten somewhere in the docs xD

Collapse
 
rhymes profile image
rhymes • Edited

Nice!

It seems also they are setting it up to be interoperable from other languages:

Well, it's an interesting project indeed!

Collapse
 
kylegalbraith profile image
Kyle Galbraith

Great post K! The CDK looks very cool and shows me that AWS is seeing the changes happening in this space. This looks a lot like Pulumi initially.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.