Thumbnail image

AWS SDK vs. AWS CDK: The Comparison

A common question especially for developers new to AWS is: What is the difference between AWS CDK and SDK? Both are software libraries provided and maintained by AWS as open source projects. Both help developers with building applications for the cloud. But how they compare and when one library should be preferred over the other? Are they even comparable? So let’s have a closer look!

AWS SDK: Integrate the AWS API into your code

The key feature of AWS Software Development Kit (SDK) basically is to provide ready-to-use software fragments (as objects, interfaces etc.) enabling developers to manage the lifecycle of AWS services. Those fragments are an abstraction of AWS Cloud’s Web API, therefore developers don’t have to care as much about building http handlers from scratch. Your application shall deploy an EC2 instance at some point? No problem. Just use the respective SDK fragments abstracting the EC2 service and manage the lifecycle within your code.

The following example shows how you would create and run an EC2 instance in AWS using TypeScript and the RunInstanceCommand in AWS SDK for JavaScript.

import { EC2Client, RunInstancesCommand } from "@aws-sdk/client-ec2";

input = {
    ...
}

const command = new RunInstancesCommand(input);
await client.send(command);

But when does the instance EC2 terminate? Right! Never (almost)! Unless you implemented follow up operations in your code (which is actually executed at runtime :)).

There are versions available for some of the most popular programming languages as Python, JavaScript, Go and more. So it’s relatively straight forward to use. Just add the dependency to your app and go on.

Since we now should have a rough understanding of AWS SDK it’s time to move on with CDK.

AWS CDK: Deploy infrastructure applications

Other than AWS SDK the AWS Cloud Development Toolkit (CDK) abstracts not only the AWS service API but the whole life-cycle-management of (most) AWS services. Developers don’t have to care about implementation of provisioning and other life-cycle-operations as much as with SDK. So why not using CDK only? Good question!

The truth about AWS CDK

First of all we have to understand a fundamental truth about CDK: It’s main function is managing Infrastructure as Applications or CDK Apps

You heard right, you build CDK Apps with it! It provides an own cli tool for that purpose which is tightly coupled with AWS’s own IaC service CloudFormation. After you built in you simply deploy it to AWS using the cli.

cdk deploy  | "deploy template via AWS CloudFormation" 
cdk destroy | "destroy deployment"

AWS CDK also provides libraries for building CDK Apps. Create a code with one of the supported (popular) programming languages and compile (or “synthesize”) it into a CloudFormation template using:

cdk synth   | "compile template from code"

But how does the code look like? Let’s have a look into a simple TypeScript-based example, which could be synthesized with cdk synth into CloudFormation template in a later step(typically a *.yaml or *.json document):

import { App, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as ec2 from 'aws-cdk-lib/aws_ec2';

// Create AWS CDK App (the "infrastructure app")
const app = new App();

// Add EC2 instance to the app
new myInfrastructure (app, 'Sample Infrastructure App', {})

// Define the infrastructure - This case EC2 instance 
class myInfrastructure extends Stack {
    constructor(scope: Construct, id: string, props: StackProps) {
        super(scope, id, props);
        declare const vpc: ec2.IVpc;

        const instance = new ec2.Instance(this, 'targetInstance', {
            vpc: vpc,
            instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
            machineImage: new ec2.AmazonLinuxImage({ generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2 }),
        });
    }
}

Conclusion: When to use AWS SDK or AWS CDK

Obviously AWS SDK and AWS CDK have significant differences. This has implications on the handling of both tools as well as the responsibilities of developer when using either of them.

AWS SDK is pretty easy to integrate into application code. Therefore it is a good pick when you want to integrate your application with AWS services. The drawback is, that you have to manage the lifecycle of those AWS services you want to use.

AWS CDK abstracts away a lot of life-cycle-management on AWS services which can make life easier for developers. You simply manage whole sets of services as stacks in AWS CloudFormation. The drawback is that you have to maintain a separate infrastructure application and figure out how to integrate it with your application architecture.

Another fact is: You can actually use AWS SDK & AWS CDK in conjunction. Check my other blog post if you want to know how.

Related Posts