
AWS SDK vs. AWS CDK: A Comparison
TL;DR: AWS SDK integrates AWS services into your application code with manual lifecycle management, while AWS CDK deploys infrastructure as code through CloudFormation. Use SDK for runtime service integration; use CDK for infrastructure provisioning and management.
A common question for developers new to AWS is: What is the difference between AWS SDK and AWS CDK? Both are software libraries provided and maintained by AWS as open-source projects. Both help developers build applications for the cloud. But how do they compare, and when should one library be preferred over the other? Are they even comparable?
Let’s take a closer look!
AWS SDK: Integrating the AWS API into Your Code
The key feature of the AWS Software Development Kit (SDK) is providing ready-to-use software components (such as objects and interfaces) that enable developers to manage the lifecycle of AWS services. These components abstract the AWS Cloud’s Web API, so developers don’t need to manually build HTTP handlers from scratch. If your application needs to deploy an EC2 instance at some point, no problem—just use the respective SDK components that abstract the EC2 service and manage the lifecycle within your code.
The following example shows how to 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";
const client = new EC2Client({});
const input = {
// Configuration for the EC2 instance
};
const command = new RunInstancesCommand(input);
await client.send(command);
But when does the EC2 instance terminate? It doesn’t—the instance will continue running until you explicitly terminate or stop it through additional code or the AWS Console. You’re responsible for the entire lifecycle management.
The SDK is available for most popular programming languages, including Python, JavaScript, Go, and more. It’s relatively straightforward to use—just add the dependency to your app and go.
Now that we’ve covered AWS SDK, let’s move on to CDK.
AWS CDK: Deploying Infrastructure Applications
Unlike AWS SDK, the AWS Cloud Development Toolkit (CDK) abstracts not only the AWS service API but also the entire lifecycle management of most AWS services. Developers don’t have to implement provisioning and other lifecycle operations as much as with the SDK. So why not use CDK exclusively? That’s a good question!
The truth about AWS CDK
First, we must understand a fundamental truth about CDK: Its main function is managing Infrastructure as Code (IaC) or CDK Apps.
You heard right—you build CDK Apps with it! It provides its own CLI tool for this purpose, tightly integrated with AWS’s IaC service, CloudFormation. After building the app, you simply deploy it to AWS using the CLI:
cdk deploy # Deploy template via AWS CloudFormation
cdk destroy # Destroy the deployment
AWS CDK also provides libraries for building CDK Apps. Create code in one of the supported (popular) programming languages and synthesize it into a CloudFormation template using:
cdk synth # Compile template from code
But what does the code look like? Let’s examine a simple TypeScript-based example using AWS CDK v2, which could be synthesized with cdk synth into a CloudFormation template (typically a *.yaml or *.json document):
import { App, Stack, 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 - In this case, an EC2 instance
class MyInfrastructure extends Stack {
constructor(scope: Construct, id: string, props: StackProps) {
super(scope, id, props);
const vpc = ec2.Vpc.fromLookup(this, 'VPC', { isDefault: true });
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 }),
});
}
}
Quick Comparison
| Feature | AWS SDK | AWS CDK |
|---|---|---|
| Primary Purpose | Runtime service integration | Infrastructure provisioning |
| Lifecycle Management | Manual (your code) | Automated (CloudFormation) |
| Use Case | Application logic, API calls | Infrastructure as Code |
| Deployment | Part of your application | Separate infrastructure app |
| Best For | Lambda functions, API integrations, runtime operations | Multi-service architectures, repeatable environments |
| Learning Curve | Lower | Moderate |
| State Management | Your responsibility | CloudFormation handles it |
Key Takeaways
- AWS SDK is for integrating AWS services into your application at runtime—think API calls, data operations, and service interactions from within your code
- AWS CDK is for defining and deploying infrastructure—think provisioning EC2 instances, S3 buckets, and entire architectures as code
- SDK requires manual lifecycle management; CDK automates it through CloudFormation
- They’re not mutually exclusive—you can and often should use both together
- Choose SDK when your application needs to interact with AWS services dynamically
- Choose CDK when you need to provision and manage cloud infrastructure
Conclusion: When to Use AWS SDK or AWS CDK
Clearly, AWS SDK and AWS CDK have significant differences. These differences impact how you handle each tool and the responsibilities of the developer when using them.
AWS SDK is easy to integrate into application code, making it a good choice when you want to integrate your application with AWS services. However, you must manage the lifecycle of those services yourself.
AWS CDK abstracts much of the lifecycle management of AWS services, making it easier for developers to manage entire sets of services as stacks in AWS CloudFormation. The drawback is that you must maintain a separate infrastructure application and figure out how to integrate it with your application architecture.
Another important fact: You can actually use AWS SDK and AWS CDK together. Check my other blog post to learn how.
Frequently Asked Questions
Can I use AWS SDK and AWS CDK together?
Absolutely! In fact, this is a common pattern. Use CDK to provision your infrastructure and SDK within your application code to interact with AWS services at runtime.
Which is easier to learn for beginners?
AWS SDK is generally easier to start with since it’s just a library you add to your application. CDK requires understanding Infrastructure as Code concepts and CloudFormation.
Does AWS CDK replace CloudFormation?
No, CDK actually uses CloudFormation under the hood. It provides a higher-level abstraction that generates CloudFormation templates from your code.
Which programming languages support AWS CDK?
CDK supports TypeScript, JavaScript, Python, Java, C#, and Go. All CDK code compiles to CloudFormation templates regardless of the language used.
Can I use AWS SDK in Lambda functions?
Yes, this is one of the most common use cases. Your Lambda function code would use the SDK to interact with other AWS services like S3, DynamoDB, or SNS.