AWS CDK vs. Nitric
Nitric and AWS Cloud Development Kit (CDK) both provide developer-friendly approaches to cloud resource management, but they operate on different philosophies. Nitric uses an "Infrastructure from Code" approach, focusing on deriving the necessary infrastructure from the application code. This stands in contrast to AWS CDK's "Infrastructure as Code" methodology, which requires explicit definition of cloud resources. These are the main differences between the two:
- Cloud Provider Support: Nitric is provider-agnostic, capable of deploying to multiple cloud providers such as AWS, Google Cloud, and Azure. AWS CDK is primarily designed for AWS services.
- Abstraction Level: Nitric provides a high level of abstraction, allowing developers to focus more on writing application code. AWS CDK, while also abstracting over raw CloudFormation, requires a more detailed understanding of AWS services and their interactions.
- Infrastructure Provisioning: Nitric uses Pulumi by default for provisioning cloud resources, but also allows the use of custom providers. AWS CDK uses AWS CloudFormation for provisioning.
- Local Simulation: Nitric provides a local cloud simulation tool, allowing you to test your applications locally before deploying them. This is a feature that AWS CDK does not offer, you are required to deploy your application to the cloud.
Code Comparison
To get a deeper understanding of the differences, let's see the same app built in both Nitric and AWS CDK. This simple cloud app uses a Function to retrieve a hello world message using a path parameter.
Nitric
import { api } from '@nitric/sdk'
const helloApi = api('main')
helloApi.get('/hello/:name', async (ctx) => {
const { name } = ctx.req.params
ctx.res.body = `Hello ${name}`
})
AWS CDK
exports.handler = async function (event = {}) {
const name = event.pathParameters.name
return {
statusCode: 200,
body: `Hello ${name}`,
}
}
AWS CDK
import * as cdk from 'aws-cdk-lib'
import { Construct } from 'constructs'
import * as apigateway from 'aws-cdk-lib/aws-apigateway'
import * as lambda from 'aws-cdk-lib/aws-lambda'
export class HelloWorldStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props)
const handler = new lambda.Function(this, 'HelloworldHandler', {
runtime: lambda.Runtime.NODEJS_18_X,
code: lambda.Code.fromAsset('src'),
handler: 'index.handler',
})
const api = new apigateway.RestApi(this, 'hello-world-api', {
restApiName: 'Hello World Service',
description: 'A hello world API.',
})
const resource = api.root.addResource('hello').addResource('{name}')
resource.addMethod('GET', new apigateway.LambdaIntegration(handler))
}
}
Nitric offers a simplified, user-friendly way to define APIs with the added benefit of being able to use custom providers for greater control and flexibility, while maintaining cloud neutrality. On the other hand, AWS CDK, with its verbose and explicit syntax, provides deep customization and control specifically within the AWS ecosystem, but might be more complex for beginners.
Differences
Nitric | AWS CDK | |
---|---|---|
Language | Your choice | Your choice |
Lines of code | 9 | 33 |
Cloud Infrastructure | Inferred | Explicit |
Extensibility | Custom providers can be created | Custom Constructs / Resource Providers |
Local simulation | Built-in local simulator with instant hot reloading | No |
Cloud providers | AWS, Azure, GCP and Custom providers | AWS |
Provisioning engine | Pulumi by default, other custom providers can be created | CloudFormation (CDKTF is used for Terraform, but you cannot compile the same code to different engines) |