loading...
Cover image for Infrastructure as JS with AWS CDK
Fullstack Frontend

Infrastructure as JS with AWS CDK

kayis profile image K ・4 min read

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

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
  2. Getting Hands Dirty with AWS CDK in AWS Cloud9
  3. AWS CDK sample with Existing S3 bucket and existing SNS topic
  4. AWS CDK - API Gateway Service Integration with DynamoDB
  5. How to build distributable serverless application module by AWS CDK
  6. Initializing a new CDK app
  7. Fighting COVID-19 with Folding@Home & AWS CDK
  8. Surviving Infrastructure As Real Code with AWS CDK - My Playbook
  9. Imperative Infrastructure as Code using AWS CDK
  10. Exploring AWS CDK - Step Functions

Guides

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

  1. AWS CDK with TypeScript
  2. CDK Intro on Egghead (paid)
  3. Re:Invent 2018
  4. Open Source Guide for CDK
  5. Official CDK Patterns
  6. What is the AWS CDK?
  7. 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, 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.

Posted on by:

kayis profile

K

@kayis

Taking care of developer relations at Moesif and creating educational content at fllstck.dev

Discussion

markdown guide