28

The AWS Cloud Development Kit

 5 years ago
source link: https://www.tuicool.com/articles/hit/UvMFJfa
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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());

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 Stack s 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!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK