5

Infrastructure as JS with AWS CDK

 3 years ago
source link: https://dev.to/fllstck/infrastructure-as-js-with-aws-cdk-46i2
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.
Infrastructure as JS with AWS CDK

When you looked into backend development, you probably found out quickly that it isn't just about "coding an API." The API has to be hosted somewhere; you need data-stores, compute, load balancer, API gateways, and whatnot.

After you installed MongoDB from the command line for the umpteenth time, you start asking yourself: Isn't there a better way?!

And you're right! There is, and this way is called "Infrastructure as Code" or IoC for short. You can define your whole infra with code and version it. IoC makes it much simpler to reason about deployments.

Then you search the web for IoC and learn about Terraform, CloudFormation, the Serverless Framework, AWS SAM, and more. They all promise taking care of your infrastructure needs, but using what as their language? YAML?!

You're right!

They're all nicely declarative IoC frameworks based on a markup language that doesn't want to be a markup language. This all sounds a bit sub-optimal, after all, you got mad JavaScript skills and had the impression JavaScript can do anything.

Enter the CDK

In 2018, AWS released a new tool to manage infrastructure called AWS CDK. It enables you to write IaC in a general-purpose language, namely JavaScript, TypeScript, Python, Java, and .NET.

So yes, you read that right, you can manage a whole computing cluster with JavaScript. I'm not going to argue if this is a good idea or not, but at least it's possible now with an IoC framework created and maintained by the most prominent cloud provider out there.

The CDK abstracts CloudFormation resources as construct classes that try to bake in best practices, so a basic CDK construct should be easier AND safer to use than a CFN resource.

Creating an API

Let's look at a simple example for an HTTP API based on Amazon API-Gateway, and AWS Lambda implemented in under 20 LoC.

const cdk = require("@aws-cdk/core");
const apigateway = require("@aws-cdk/apigateway");
const lambda = require("@aws-cdk/lambda");
class ApiStack extends cdk.Stack {
  constructor(scope, id, props) {
    super(scope, id, props);
    const handler = new lambda.Function(this, "apiHandler", {
      runtime: lambda.Runtime.NODEJS_12_X,
      handler: "index.handler",
      code: lambda.Code.fromInline(`
        exports.handler = async () => ({
          statusCode: 200,
          body: '{ "message": "Hello!" }',
        })
      `)
    })
    new apigateway.LambdaRestApi(this, "api", { handler });
  }
}
Enter fullscreen modeExit fullscreen mode

This is a Node.js application that will synthesize a CloudFormation YAML file with all the resources needed to create an API-Gateway and hook in a Lambda function.

CDK constructs for AWS services come as NPM packages nested in the @aws-cdk namespace, so you don't have to install all constructs that exist.

CDK Tutorials on Dev.to

There is already an excellent selection of tutorials on dev.to:

Guides

And the rest of the web is also filled with guides, workshops, and tutorials for that matter:

Bonus: CDK8s & CDK8s+

If you're not a big YAML and AWS fan either, there is still something for you. Namely CDK8s, a CDK that lets you write JavaScript and generates Kubernetes YAML instead of AWS CloudFormation YAML. Also, with CDK8s+, there is even the next taken to make K8s easier to use, with an even more straightforward interface.

CDK8s lets you define a K8s cluster with JavaScript that you can deploy on your favorite cloud.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK