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:
- CDK Patterns at 20! Let's Walk Through all 20 Serverless Patterns for AWS
- Getting Hands Dirty with AWS CDK in AWS Cloud9
- AWS CDK sample with Existing S3 bucket and existing SNS topic
- AWS CDK - API Gateway Service Integration with DynamoDB
- How to build distributable serverless application module by AWS CDK
- Initializing a new CDK app
- Fighting COVID-19 with Folding@Home & AWS CDK
- Surviving Infrastructure As Real Code with AWS CDK - My Playbook
- Imperative Infrastructure as Code using AWS CDK
- Exploring AWS CDK - Step Functions
Guides
And the rest of the web is also filled with guides, workshops, and tutorials for that matter:
- AWS CDK with TypeScript
- CDK Intro on Egghead (paid)
- Re:Invent 2018
- Open Source Guide for CDK
- Official CDK Patterns
- What is the AWS CDK?
- 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.