If you want to build a cloud-native web service, consider reaching for the AWS Cloud Development Kit. CDK is a new generation of infrastructure-as-code (IaC) tools designed to make packaging your code and infrastructure together as seamless and powerful as possible. It’s great for any application running on AWS, and it’s especially well-suited to serverless applications.
The CDK consists of a set of libraries containing resource definitions and higher-level constructs, and a command line interface (CLI) that synthesizes CloudFormation from your resource definitions and manages deployments. You can imperatively define your cloud resources like Lambda functions, S3 buckets, APIs, DNS records, alerts, DynamoDB tables, and everything else in AWS using TypeScript, Python, .NET, or Java. You can then connect these resources together and into more abstract groupings of resources and finally into stacks. Typically one entire service would be one stack.
class HelloCdkStack extends Stack {
constructor(scope: App, id: string, props?: StackProps) {
super(scope, id, props);
new s3.Bucket(this, 'MyFirstBucket', {
versioned: true
});
}
}
CDK doesn’t exactly replace CloudFormation because it generates CloudFormation markup from your resource and stack definitions. But it does mean that if you use CDK you don’t really ever have to manually write CloudFormation ever again. CloudFormation is a declarative language, which makes it challenging and cumbersome to do simple things like conditionals, for example changing a parameter value or not including a resource when your app is being deployed to production. When using a typed language you get the benefit of writing IaC with type checking and code completion, and the ability to connect resources together with a very natural syntax. One of the real time-saving benefits of CDK is that you can group logical collections of resources into reusable classes, defining higher level constructs like CloudWatch canary scripts, NodeJS functions, S3-based websites with CloudFront, and your own custom constructs of whatever you find yourself using repeatedly.
The CLI for CDK gives you a set of tools mostly useful for deploying your application. A simple cdk deploy
parses your stacks and resources, synthesizes CloudFormation, and deploys it to AWS. The CLI is basic and relatively new, so don’t expect a ton of mature features just yet. I am still using the Serverless framework for serious applications because it has a wealth of built-in functionality and useful plugins for things like testing applications locally and tailing CloudWatch logs. AWS’s Serverless Application Model (SAM) is sort of equivalent to Serverless, but feels very Amazon-y and more like a proof-of-concept than a tool with any user empathy. The names of all of these tools are somewhat uninspired and can understandably cause confusion, so don’t feel bad if you feel a little lost.
Sample CDK Application
I built a small web service to put the CDK through its paces. My application has a React frontend that fetches a list of really shitty websites from a Lambda function and saves them in the browser’s IndexedDB, a sort of browser SQL database. The user can view the different shitty websites with previous and next buttons and submit a suggestion of a terrible site to add to the webring. You can view the entire source here and the finished product at cursed.lol.
To kick off a CDK project, run the init
command: cdk init app --language typescript
.
This generates an application scaffold we can fill in, beginning with the bin/cdk.ts
script if using TypeScript. Here you can optionally configure environments and import your stacks.
#!/usr/bin/env node
import "source-map-support/register";
import * as cdk from "@aws-cdk/core";
import { CursedStack } from "../lib/stack";
const envProd: cdk.Environment = {
account: "1234567890",
region: "eu-west-1",
};
const app = new cdk.App();
new CursedStack(app, "CursedStack", { env: envProd });
The environment config isn’t required; by default your application can be deployed into any region and AWS account, making it easy to share and create development environments. However if you want to pre-define some environments for dev/staging/prod you can do that explicitly here. The documentation suggests using environment variables to select the desired AWS account and region at deploy-time and then writing a small shell script to set those variables when deploying. This is a very flexible and customizable way to manage your deployments, but it lacks the simplicity of Serverless which has a simple command-line option to select which stage you want. CDK is great for customizing to your specific needs, but doesn’t quite have that out-of-the-box user friendliness.
DynamoDB
Let’s take a look at a construct that defines a DynamoDB table for storing user submissions:
import * as core from "@aws-cdk/core";
import * as dynamodb from "@aws-cdk/aws-dynamodb";
export class CursedDB extends core.Construct {
submissionsTable: dynamodb.Table;
constructor(scope: core.Construct, id: string) {
super(scope, id);
this.submissionsTable = new dynamodb.Table(this, "SubmissionsTable", {
partitionKey: {
name: "id",
type: dynamodb.AttributeType.STRING,
},
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
});
}
}
Here we create a table that has a string id
primary key. In this example we save the table as a public property (this.submissionsTable
) on the instance of our Construct
because we will want to reference the table in our Lambda function in order to grant write access and provide the name of the table to the function so that it can write to the table. This concept of using a class property to keep track of resources you want to pass to other constructs isn’t anything particular to CDK – it’s just something I decided to do on my own to make it easy to connect different pieces of my service together.
Lambda Functions
Here I declare a construct which defines two Lambda functions. One function fetches a list of websites for the user to browse, and the other handles posting submissions which saved into our DynamoDB submissionsTable
as well as Slacked to me. I am extremely lazy and manage most of my applications this way. We use the convenient NodejsFunction
high-level construct to make our lives easier. This is the most complex construct of our stack. It:
- Loads a secret containing our Slack webhook URL
- Defines a custom property
submissionsTable
that it expects to receive - Defines an API Gateway with CORS enabled
- Creates an API resource (
/sites/
) to hold our function endpoints - Defines two Lambda NodeJS functions (note that our source files are TypeScript – compilation happens automatically)
- Connects the Lambda functions to the API resource as
GET
andPOST
endpoints - Grants write access to the
submissionsTable
to thesubmitSiteHandler
function
import * as core from "@aws-cdk/core";
import * as apigateway from "@aws-cdk/aws-apigateway";
import * as sm from "@aws-cdk/aws-secretsmanager";
import { NodejsFunction } from "@aws-cdk/aws-lambda-nodejs";
import { LambdaIntegration, RestApi } from "@aws-cdk/aws-apigateway";
import { Table } from "@aws-cdk/aws-dynamodb";
// ARN of a secret containing the slack webhook URL
const slackWebhookSecret =
"arn:aws:secretsmanager:eu-west-1:178183757879:secret:cursed/slack_webhook_url-MwQ0dY";
// required properties to instantiate our construct
// here we pass in a reference to our DynamoDB table
interface CursedSitesServiceProps {
submissionsTable: Table;
}
export class CursedSitesService extends core.Construct {
constructor(
scope: core.Construct,
id: string,
props: CursedSitesServiceProps
) {
super(scope, id);
// load our webhook secret at deploy-time
const secret = sm.Secret.fromSecretCompleteArn(
this,
"SlackWebhookSecret",
slackWebhookSecret
);
// our API Gateway with CORS enabled
const api = new RestApi(this, "cursed-api", {
restApiName: "Cursed Service",
defaultCorsPreflightOptions: {
allowOrigins: apigateway.Cors.ALL_ORIGINS,
},
});
// defines the /sites/ resource in our API
const sitesResource = api.root.addResource("sites");
// get all sites handler, GET /sites/
const getAllSitesHandler = new NodejsFunction(
this,
"GetCursedSitesHandler",
{
entry: "resources/cursedSites.ts",
handler: "getAllHandler",
}
);
sitesResource.addMethod("GET", new LambdaIntegration(getAllSitesHandler));
// submit, POST /sites/
const submitSiteHandler = new NodejsFunction(
this,
"SubmitCursedSiteHandler",
{
entry: "resources/cursedSites.ts",
handler: "submitHandler",
environment: {
// let our function access the webhook and dynamoDB table
SLACK_WEBHOOK_URL: secret.secretValue.toString(),
CURSED_SITE_SUBMISSIONS_TABLE_NAME: props.submissionsTable.tableName,
},
}
);
// allow submit function to write to our dynamoDB table
props.submissionsTable.grantWriteData(submitSiteHandler);
sitesResource.addMethod("POST", new LambdaIntegration(submitSiteHandler));
}
}
While there’s a lot going on here it is very readable if taken line-by-line. I think this showcases some of the real expressibility of CDK. That props.submissionsTable.grantWriteData(submitSiteHandler)
stanza is really . It grants that one function permission to write to the DynamoDB table that we defined in our first construct. We didn’t have to write any IAM policy statements, reference CloudFormation resources, or even look up exactly which actions this statement needs to consists of. This gives you a bit of the flavor of CDK’s simplicity compared to writing CloudFormation by hand.
If you’d like to look at the source code of these Lambdas you can find it here. Fetching the list of sites is accomplished by loading a Google Sheet as a CSV (did I mention I’m really lazy?) and the submission handler does a simple DynamoDB Put
call and hits the Slack webhook with the submission. I love this kind of web service setup because once it’s deployed it runs forever and I never have to worry about managing it again, and it costs roughly $0 per month. If a website is submitted I can evaluate it and decide if it’s shitty enough to be included, and if so I can just add it to the Google Sheet. And I have a record of all submissions in case I forget or one gets lost in Slack or something.
CloudFront CDN
Let’s take a look at one last construct I put together for this application, a CloudFront CDN distribution in front of a S3 static website bucket. I realized the need to mirror many of these lame websites because due to their inherent crappiness they were slow, didn’t support HTTPS (needed when iFraming), and might not stay up forever. A little curl --mirror
magic fixed that right up.
Typically defining a CloudFront distribution with HTTPS support is a bit of a headache. Again the high-level constructs you get included with CDK really shine here and I made use of the CloudFrontWebDistribution
construct to define just what I needed:
import {
CloudFrontWebDistribution,
OriginProtocolPolicy,
} from "@aws-cdk/aws-cloudfront";
import * as core from "@aws-cdk/core";
// cursed.llolo.lol ACM cert
const certificateArn =
"arn:aws:acm:us-east-1:1234567890:certificate/79e60ba9-5517-4ce3-8ced-2d9d1ddb1d5c";
export class CursedMirror extends core.Construct {
constructor(scope: core.Construct, id: string) {
super(scope, id);
new CloudFrontWebDistribution(this, "cursed-mirrors", {
originConfigs: [
{
customOriginSource: {
domainName: "cursed.llolo.lol.s3-website-eu-west-1.amazonaws.com",
httpPort: 80,
originProtocolPolicy: OriginProtocolPolicy.HTTP_ONLY,
},
behaviors: [{ isDefaultBehavior: true }],
},
],
aliasConfiguration: {
acmCertRef: certificateArn,
names: ["cursed.llolo.lol"],
},
});
}
}
This creates a HTTPS-enabled CDN in front of my existing S3 bucket with static website hosting. I could have created the bucket with CDK as well but, since there can only be one bucket with this particular domain that seemed a bit overkill. If I wanted to make this more reusable these values could be stack parameters.
The Stack
Finally the top-level Stack
contains all of our constructs. Here you can see how we pass the DynamoDB table provided by the CursedDB
construct to the CursedSitesService
containing our Lambdas.
import * as cdk from "@aws-cdk/core";
import { CursedMirror } from "./cursedMirror";
import { CursedSitesService } from "./cursedSitesService";
import { CursedDB } from "./db";
export class CursedStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const db = new CursedDB(this, "CursedDB");
new CursedSitesService(this, "CursedSiteServices", {
submissionsTable: db.submissionsTable,
});
new CursedMirror(this, "CursedSiteMirrorCDN");
}
}
Putting it all together, all that’s left to do is run cdk deploy
to summon our cloud resources into existence and write our frontend.
Is This Better?
Going through this exercize of creating a real service using nothing but CDK was a great way for me to get more comfortable with the tools and concepts behind it. Once I wrapped my head around the way the constructs fit together and started discovering all of the high-level constructs already provided by the libraries I really started to dig it. Need to load some secrets? Need to define Lambda functions integrated to API Gateway? Need a CloudFront S3 bucket website distribution? Need CloudWatch canaries? It’s already there and ready to go along with strict compile-time checking of your syntax and properties. I pretty much never encountered a situation where my code compiled but the deployment was invalid, a vastly improved state of affairs from trying to write CloudFormation manually.
And what about Terraform? In my humble opinion if you’re going to build cloud-native software it’s a waste of effort to abstract out your cloud provider and their resources. Better to embrace the tooling and particulars of one provider and specialize instead of pursuing some idealistic cloud-agnostic setup at a great price of efficiency. Multi-cloud is the worst practice.
The one thing that I missed most from the Serverless framework was tailing my CloudWatch logs. When I had issues in my Lambda logic (not something the CDK can fix for you) I had to go into the CloudWatch console to look at the logs instead of simply being able to tail them from the command line. The upshot though is that CDK is simply code, and writing your own tooling around it using the AWS API should be straightforward enough. I expect SAM and the CDK CLI to only get more mature and user-friendly over time, so I imagine I’ll be building projects of increasing seriousness with them as time progresses.
If you want to learn more, start with the CDK docs. And if you know of any cursed websites please feel free to mash that submit button.
The post Web Services with AWS CDK appeared first on spiegelmock.com.
Top comments (0)