AWS Cloud Development Kit: Simple Approach to Infrastructure-as-Code

Cloud Native App Dev

Learn about AWS Cloud Development Kit and a new breed of tools that allow you deploy architecture with familiar programming languages.

While Terraform is the de facto standard for writing infrastructure-as-code (IaC), a few years ago, a new breed of IaC tools that allow you to define infrastructure in various object oriented programming languages started gaining traction. It started with Pulumi, and now we have the CDKTF and the AWS Cloud Development Kit (CDK).

These tools allow you to define infrastructure using a language that you are likely familiar with if you are a developer. While they support multiple languages, there is generally one that has better support and in the case of most of these tools, that language is typescript.

There are some benefits in being able to define your infrastructure in an object oriented programming language as we will see. As with anything, there are plenty of cons as well and it really depends on what suits your tech stack and your organization.

Creating a lambda with SQS: Terraform vs AWS CDK

One common pattern in serverless architectures is having a lambda read from an SQS queue. This allows you to process as many messages as allowed concurrent executions in your AWS account. You can forget about having to scale your consumers, since for every message in the queue a lambda function will be triggered.

Let’s see how much code writing this requires in Terraform vs AWS CDK

Terraform

AWS CDK

So we already see some benefit in terms of lines of code written. Let’s add a Amazon DynamoDB table that our lambda function will write to.

Terraform

AWS CDK

One of the main benefits of using an object oriented programming language is that our resources are actually objects, thus you can call methods on them, something that is not possible in Terraform due to the nature of the Hashicorp Configuration Language (HCL), which behaves more like a Domain Specific Language (DSL) and not a real programming language. The ability of resources having both methods and properties makes code easier to understand, since you can use properties as means to just read data from your resources and use methods for anything that would change the state of that resource.

Your constructs can also implement interfaces that are part of the AWS CDK. In this case our lambda function implements the IGrantable interface, which allows you to pass the lambda as an argument to the “grantReadWrite” method. You can also implement this interface in your custom constructs to simplify granting permissions to any children resources of the construct. This saves a lot of boilerplate code and also makes the code’s intent much clearer with less lines.

Conclusion

Nobody is going to migrate a whole codebase from Terraform to AWS CDK just because you no longer have to memorize every IAM action. This is just a small example of how it makes sense to use an object oriented language to write infrastructure. In my experience, the AWS CDK proves particularly convenient when you want to give more autonomy to development teams. It is more likely that they will adopt a programming language that they already know and are familiar with than an HCL.

Cloud Native App Dev

Learn more about the services mentioned

Caylent Services

Cloud Native App Dev

Deliver high-quality, scalable, cloud native, and user-friendly applications that allow you to focus on your business needs and deliver value to your end users faster.

Accelerate your cloud native journey

Leveraging our deep experience and patterns

Get in touch

Related Blog Posts

Automated Testing with Jest on AWS

Learn how to automate testing and safeguard your JavaScript apps using Jest with AWS CodeBuild and CodePipeline.

Cloud Native App Dev
Infrastructure & DevOps Modernization
Cloud Technology

AWS Lambda Performance Boost with SnapStart

Supercharge AWS Lambda cold start times by up to 90% by leveraging AWS Lambda SnapStart and Firecracker, helping you minimize latency without any additional caching costs.

Cloud Technology
Cloud Native App Dev

Caylent Catalysts

Learn how we develop and implement Caylent Catalysts - a set of accelerators designed to fuel your AWS cloud adoption initiatives.

Migrations
Cloud Native App Dev
Security
Video