Infrastructure as JS with AWS CDK
Tags: devops, cloud, javascript
Published: 2020-08-13 15:09:38

---
title: "Infrastructure as JS with AWS CDK"
published: true
tags: devops, cloud, javascript
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/yx1m70xyra6q3hnxbhkg.jpg
---

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](https://dev.to/kayis/the-aws-cloud-development-kit-5c9n) 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](https://acloud.guru/series/serverlessconf-nyc-2019/view/yaml-better), 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.

```javascript
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 });
  }
}
```

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: 

1. [CDK Patterns at 20! Let's Walk Through all 20 Serverless Patterns for AWS ](https://dev.to/nideveloper/cdk-patterns-at-20-let-s-walk-through-all-20-serverless-patterns-for-aws-d1n)
2. [Getting Hands Dirty with AWS CDK in AWS Cloud9 ](https://dev.to/wingkwong/getting-hands-dirty-with-aws-cdk-in-aws-cloud9-4l1a)
3. [AWS CDK sample with Existing S3 bucket and existing SNS topic](https://dev.to/katryo/aws-cdk-sample-with-existing-s3-bucket-and-existing-sns-topic-4f1f)
4. [AWS CDK - API Gateway Service Integration with DynamoDB](https://dev.to/elthrasher/aws-cdk-api-gateway-service-integration-with-dynamodb-2ek0)
5. [How to build distributable serverless application module by AWS CDK](https://dev.to/mizutani/how-to-build-distributable-serverless-application-module-by-aws-cdk-2a4)
6. [Initializing a new CDK app](https://dev.to/aws-heroes/initializing-a-new-cdk-app-1a30)
7. [Fighting COVID-19 with Folding@Home & AWS CDK](https://dev.to/t04glovern/fighting-covid-19-with-folding-home-aws-cdk-39lf)
8. [Surviving Infrastructure As Real Code with AWS CDK - My Playbook ](https://dev.to/nideveloper/surviving-infrastructure-as-real-code-with-aws-cdk-my-playbook-3168)
9. [Imperative Infrastructure as Code using AWS CDK](https://dev.to/kylegalbraith/imperative-infrastructure-as-code-using-aws-cdk-1l8c)
10. [Exploring AWS CDK - Step Functions](https://dev.to/elthrasher/exploring-aws-cdk-step-functions-1d1e)

# Guides

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

1. [AWS CDK with TypeScript](https://cdkworkshop.com/20-typescript.html)
2. [CDK Intro on Egghead (paid)](https://egghead.io/lessons/aws-introduction-to-build-an-app-with-the-aws-cloud-development-kit)
3. [Re:Invent 2018](https://www.youtube.com/watch?v=Lh-kVC2r2AU)
4. [Open Source Guide for CDK](https://github.com/kevinslin/open-cdk)
5. [Official CDK Patterns](https://github.com/cdk-patterns/serverless)
6. [What is the AWS CDK?](https://docs.aws.amazon.com/cdk/latest/guide/home.html)
7. [AWS CDK Examples](https://github.com/aws-samples/aws-cdk-examples)

# Bonus: CDK8s & CDK8s+

If you're not a big YAML and AWS fan either, there is still something for you. Namely [CDK8s](https://cdk8s.io/), a CDK that lets you write JavaScript and generates Kubernetes YAML instead of AWS CloudFormation YAML. Also, with [CDK8s+](https://github.com/awslabs/cdk8s/tree/master/packages/cdk8s-plus), 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.