Podcast about DevSecOps

#58 - AWS CDK with Igor Soroka

TypeScript constructs put Lambda infrastructure in developers' hands, with CloudFormation handling deployment.

18 min read

You know our fondness for Terraform, but we are also open to exploring other tools. This episode is no different. We are joined by Igor Soroka, an expert in AWS serverless technology whose tool of choice is AWS CDK, but at the same time, he is no stranger to Terraform. We ask him practical questions about the tool and get answers based on his experience applying it to real-life projects. If you have been curious about CDK, how it functions, and if it’s appropriate for you, then tune in to learn more.

Discuss the episode or ask us anything on LinkedIn


Your team writes TypeScript. Your infrastructure tool wants HCL

You have an AWS account with a dozen Lambda functions, a few queues and buckets, and a team of backend developers who are fluent in TypeScript and have never written a line of HCL. Someone still has to define that infrastructure, and the person who knows Terraform is busy. The usual answer on this show is Terraform anyway, and for most estates we would still say it. This estate is a different shape: everything in it is AWS, nothing in it is a VPC design, and the developers already know an object-oriented language well enough to write classes in it.

Late 2023 added a second reason to look around, and it has nothing to do with the code. HashiCorp moved Terraform to the Business Source License on 10 August 2023, which restricts competing commercial products and leaves everyone else’s commercial use alone. In October, Serverless Inc announced that Framework V4 would charge organizations with more than $2M in annual revenue. One is a licence restriction, the other is a bill. Neither touches AWS CDK, which is reason enough to ask what CDK costs you in other ways.

We put the practical questions to Igor Soroka, a Barcelona-based cloud and serverless consultant who has built on AWS since 2017 and works as an independent contractor and team lead on a UK e-commerce project. He is an AWS Community Builder and a user group organizer, and his last several years have been Lambda-shaped, sitting between infrastructure and application code. He is no Terraform sceptic, which is what makes the comparisons useful.

CloudFormation, SAM and CDK sit on one engine

Start with what you are choosing between: three names that are layers on the same engine, stacked in the order they arrived.

What does AWS CDK actually deploy?

A CloudFormation template. CDK turns your code into JSON, hands the JSON to CloudFormation, and CloudFormation makes the API calls that create your resources.

The Cloud Development Kit is a framework for declaring AWS resources as objects in a general-purpose language: TypeScript, JavaScript, Python, Java, C# and Go. You install it from npm, run cdk init, write classes, and run cdk deploy. The step in the middle is called synthesis, and its output is an ordinary CloudFormation template.

The CLI is busy all the same. It creates and executes the change set, uploads your Lambda bundles and container images to the bootstrap bucket and registry, and reads the account during synthesis when your code looks up an existing VPC. Bootstrapping creates a separate role for each of those jobs, so the CLI carries permissions of its own on top of whatever CloudFormation uses to create your resources.

That inheritance explains the rest of the family. CloudFormation launched on 25 February 2011. Its templates are durable and verbose: two Lambda functions triggered by an S3 event run to 300 or 400 lines easily, and linting them is tedious. Serverless Framework appeared because packaging Lambda code through raw CloudFormation was painful, and wrapped the job in readable YAML plus a deploy command. AWS answered with the Serverless Application Model: a CLI plus a transform, so a SAM template is a CloudFormation template that CloudFormation expands on the way in. CDK came after Terraform and Pulumi had shown what a real programming language buys you, and v2 reached general availability in December 2021.

Classic CDK synthesizes CloudFormation, so AWS is what it provisions. Two routes reach past that. Kubernetes is the paved one: the EKS construct library has deployed manifests and Helm charts since 2020, and cdk8s writes manifests for any cluster. For other clouds, CloudFormation registry extensions and custom resource providers cover non-AWS resources one type at a time, each needing an extension somebody wrote. CDK for Terraform, CDKTF, is the broader answer: it keeps the construct model and swaps the engine for Terraform providers.

Where CDK belongs in your pipeline

Hearing “infrastructure in TypeScript” suggests an appealing idea: import the library into your service, and let the service provision a bigger database when load climbs, or open a port when the code opens one.

Can an application change its own infrastructure while it runs?

Not by itself. CDK doesn’t watch your application and reshape resources to match, so the deploy stays a command someone runs, and CI/CD is where to run it.

The order is the same as any other infrastructure as code: write the constructs, run cdk diff, run cdk deploy. CDK does more of the packaging than Terraform does. The NodejsFunction construct runs esbuild to transpile and bundle your handler, falling back to a Lambda-compatible Docker container when esbuild isn’t available locally, so one command produces both the artifact and the infrastructure it lands in. One CDK app can hold several stacks, and you deploy them individually or together.

There is one sanctioned shortcut past the engine, and it exists for the inner loop. cdk deploy --hotswap pushes a change straight to the service API, bypassing CloudFormation so a Lambda code change lands in seconds. It leaves the deployed application drifted from its template, and AWS says plainly that it is not for production.

Nothing stops you running the command from inside your own software, and some teams do. Vendia runs CDK commands inside a Lambda function to provision a customer’s infrastructure when that customer asks for it, instead of from a pipeline. That is a product whose feature is provisioning, and it pays for the privilege: anything that can reshape infrastructure needs IAM permissions to reshape infrastructure, and those permissions are the blast radius of every bug in it. For the case that prompts the question, capacity that has to move with load belongs in the template anyway, as autoscaling policies and on-demand capacity modes a reviewer can see.

Constructs are the module system, in three levels

The reuse story is where CDK stops being a syntax preference and starts being an architecture decision.

What is a construct?

A class representing one or more AWS resources. It’s the unit you reuse, and AWS sorts constructs into three levels.

Level 1 maps one-to-one onto a CloudFormation resource. These carry a Cfn prefix, CfnBucket for an S3 bucket, and are generated from the CloudFormation resource specification, so they expose the raw properties and none of the opinions. Level 2 is the curated layer: Bucket instead of CfnBucket, with an intent-based API, sensible defaults and typed values. That typing is the part developers feel first. A Lambda runtime is Runtime.NODEJS_18_X, a constant on the Runtime class, rather than a string you spell right or find out about at deploy time. The constants are not a closed set, because new Runtime('nodejs99.99') compiles too, so they catch your typos without promising that Lambda will accept what you send.

Level 3 is where the reuse pays off. A level 3 construct is a pattern: the queue, the function it triggers, the dead letter queue and the IAM policy that connects them, wrapped in one class someone else calls with three arguments. You write your own, or take them from the community. Construct Hub is the catalogue, and AWS Solutions Constructs is AWS’s own set of multi-service patterns with logging and encryption already switched on.

Renaming a construct is where the state model shows

Reuse is the upside. The cost arrives the first time you rename one of those constructs.

What happens when you rename a construct?

CloudFormation creates a resource under the new logical ID and deletes the one under the old ID.

CDK builds each resource’s logical ID from the construct path plus an 8-character hash, which is why a VPC route table lands in the template as VPCPrivateSubnet2RouteTable0A19E10E. Change a construct ID anywhere along that path and the logical ID changes with it. AWS states the consequence plainly: CloudFormation “creates a new resource with the new logical ID, then deletes the existing one”, which can mean service interruption, data loss, or both.

It’s the same problem as a renamed Terraform resource address, which terraform state mv or a moved block fixes. CloudFormation has no moved block. Igor’s verdict on the two engines goes the other way from ours: “It wants to destroy everything all the time from my point of view.” Both tools replace a resource when the identity they track it by changes.

The diff won’t call it a replacement, because in CloudFormation’s terms nothing is replaced. cdk diff writes [+] for an added resource, [-] for a removed one and [~] for a modified one, so a rename arrives as a [-] and a [+] for one physical thing.

Keeping the resource through the refactor

Spotting the replacement in the diff is only useful if you can stop it.

How do you rename a construct without losing the resource?

Retain it before you touch it, and prefer the refactor that leaves the logical ID where it is.

One RemovalPolicy.RETAIN writes both DeletionPolicy: Retain, which covers the rename, and UpdateReplacePolicy: Retain, which covers a property change that forces a replacement.

Surviving is all it does. The new logical ID usually comes up empty, and sometimes it doesn’t come up at all. CloudFormation requires custom resource names to be unique across your active stacks, so a construct that pins a physical name fails to create while the retained resource still holds that name. The bucket you kept has the name the new one wants.

Moving a resource rather than recreating it takes three deploys: retain it and deploy that alone; delete the construct and deploy again, leaving it outside the stack; then add it back under the new ID and cdk import it, where CloudFormation supports import for the type.

The cheaper move is to leave the logical ID alone, and AWS’s own refactoring example sets the conditions for that. A bucket at the path MyStack/DataBucket/Resource keeps its logical ID when you wrap it in a higher-level construct, as long as the wrapper sits in the same scope under the ID the bucket had, DataBucket, and the bucket inside it is named Default. CDK strips a Default component from the readable portion and from the hash. Give the wrapper a different ID or a different parent and the logical ID moves anyway. Only one child per scope can be Default.

The prerequisite nobody puts on the list

Getting started is genuinely short, as long as you know what the second day looks like.

What do you need to install?

Node.js and the CDK CLI, whatever language you then write in. cdk init sample-app --language typescript gives you a working stack to deploy and take apart, and --language is required unless you have set a default, so put python, java, csharp or go there for yours. The CDK Workshop walks the first project in several languages.

One step comes before the first deploy into an account or region: cdk bootstrap, the one-off that creates the asset bucket, the registry and the roles the next section is about.

The prerequisite that doesn’t appear in any quickstart is CloudFormation itself. Failures arrive in CloudFormation’s vocabulary, rollbacks follow CloudFormation’s rules, and per-stack resource limits are CloudFormation’s limits. You need to know a little more about CloudFormation than the tutorials imply, because that is the layer you debug in. Someone with no TypeScript background copes with CDK fine. Someone with no CloudFormation background is reading error messages in a language they don’t have.

Should developers write the infrastructure code?

That prerequisite is the real price of handing infrastructure to the people who write the application, which is the question underneath the whole tool choice.

Who should write the infrastructure for a serverless service?

The developers who own the service, when the infrastructure is Lambda-shaped and someone who has done it before is reviewing.

Andrey’s standing position on this show is the opposite: developers don’t have time to write infrastructure on top of shipping their own services. Igor’s claim is narrower than it sounds, and the scope is the argument. He’s talking about Lambda functions, queues, buckets and event wiring, with no VPC design, no security groups to reason about and no Kubernetes cluster. ECS with Fargate he calls borderline, and simpler than it used to be. Where there is real networking or a cluster involved, his answer flips: that needs guidance from someone experienced, hand in hand, not unattended.

The scope decides the review question too, and the two halves sit closer together than they sound. For a Kubernetes cluster, or a change to the networking around one, two developers who commit, approve each other and ship to production is not a review, and that work wants someone experienced beside it rather than a sign-off after the fact. For the resources a service needs for itself, the same two developers are enough as long as the boundaries hold: the output is a CloudFormation template the company’s pipeline deploys, and it lands inside the boxes the platform already drew. On those terms it is hard to tell apart from a self-service portal with pre-approved components. The interface is a class rather than a form.

That pattern was spreading beyond backend teams at the end of 2023. SST was building full-stack deployment on top of CDK, far enough up the ladder that front-end developers could ship a Next.js app without reading a construct, and Amplify was AWS’s own version of the idea behind a CLI.

Who creates the IAM role, you or CloudFormation?

Which leads to the question that decides whether handing any of this to developers is safe.

Can CloudFormation create an IAM role your own credentials can’t?

By default, no. In a bootstrapped CDK environment, yes, and that gap is the thing to check before you widen who can deploy.

Plain CloudFormation runs as you. The RoleARN parameter on the CreateStack API names a service role CloudFormation assumes instead; without one, it falls back to a temporary session generated from your credentials. So a template full of IAM roles submitted by a principal without IAM permissions fails, exactly as the API calls would have. CloudFormation adds a second gate on top: it scans templates for IAM resources and refuses to proceed unless the caller passes CAPABILITY_IAM or CAPABILITY_NAMED_IAM.

CDK changes the answer because bootstrapping sets up that service role for you. The role CloudFormation assumes to deploy your stacks gets its permissions from --cloudformation-execution-policies, and the CLI reference says stacks deploy with full administrator permissions by default. The same page carries the warning: the modern bootstrap template effectively grants those permissions to any account in the --trust list. So in an account bootstrapped with defaults, a developer who cannot create an IAM role at their own desk can create one through cdk deploy.

The useful consequence is for pipelines: the laptop and the pipeline shouldn’t carry the same permissions, and the interesting set belongs to the pipeline. The deployment role is the real security boundary, not the language the template was written in. Terraform works the same way, with its own twist. It deploys as whatever the AWS provider is configured with, which is your own credentials or a role the provider block assumes, and when it is the second one that role deserves the review you would give a bootstrap execution policy.

What the licensing argument is worth

Permissions decide what a team can deploy. Procurement decides what it is allowed to deploy with, and that question moved in 2023.

The two moves were different kinds of change. Terraform’s licence changed in August, restricting anyone who ships a competing commercial product built on it; ordinary use, commercial use included, carries on as before and nobody got a bill. Serverless Inc’s October announcement was about money: V4 and later would charge organizations above $2M in annual revenue, with earlier versions exempt and V3 kept on critical security and bug fixes through 2024. Independent developers, open-source projects and smaller organizations stay free either way, so for plenty of teams nothing moved.

For a large enterprise both land in the same place, which is procurement. We have worked in organizations with an approved vendor list, where the approved cloud providers are down to one or two and adding a tool outside the list means a conversation with legal. CDK and SAM come from AWS and cost nothing on top of the resources they create, so where the AWS relationship is already approved, they can be the shorter path. There is one line item underneath: CloudFormation charges nothing to create its native AWS::* resource types, while third-party types from the public registry are metered per handler operation, which is what the registry extension route to other clouds costs you. That is still a smaller claim than it sounds. It doesn’t make the legal review disappear, and what your own counsel makes of the terms each tool ships under is a question someone there has to answer.

The engineering question is lock-in. CDK’s output is a CloudFormation template, and cdk synth writes those templates to disk, so you can stop authoring in CDK and keep every deployed stack where it is. What you are left with is CloudFormation, by hand, with the constructs you wrote gone. Leaving CloudFormation, or AWS, is the much bigger job, and choosing CDK because the licence is quiet without wanting CloudFormation as your deployment engine is how teams end up migrating twice.

What this means for teams

If your AWS estate is serverless and your developers write TypeScript, CDK is a reasonable default rather than a contrarian pick. If your estate is multi-cloud, or mostly networking and clusters, the case is weaker and CDKTF is the compromise worth looking at first.

Four things worth doing this week:

  • Find out what your accounts were bootstrapped with. Read the execution policies on the CDKToolkit stack in each one. If the answer is AdministratorAccess and more than a handful of people can deploy, that’s the permission model you actually have.
  • Put cdk diff output in the pull request. Read the [-] and [+] resource lines as closely as the replacement warnings, because a rename hides in those.
  • Put a retain removal policy on every stateful construct you have. Buckets and tables, today, before the refactor rather than during it.
  • Give one small service to a backend developer with a named reviewer. One Lambda, one queue, one bucket, no VPC work. Two weeks is the claim, and it’s cheap to test.

Common questions, answered

Should we use CDK or Terraform if everything we run is on AWS?

Use CDK when your estate is AWS-only, your team already writes an object-oriented language, and the infrastructure is application-shaped: functions, queues, buckets, tables. The deciding factor is the reuse model, because a level 3 construct lets a platform team ship a reviewed pattern as a class. Stay with Terraform when you provision across more than one provider, when your existing modules and pipeline checks are Terraform-shaped, or when your work is mostly networking and clusters. Splitting the two works as well: CDK for the serverless applications, Terraform for the accounts and networks underneath them.

Can I use AWS CDK for Google Cloud or Kubernetes?

For Google Cloud, reach for CDK for Terraform, which keeps the construct model and the languages while generating Terraform configuration, so you get Terraform’s provider coverage. Classic CDK targets AWS because it synthesizes CloudFormation, and the registry extension route reaches other clouds one resource type at a time, only where somebody wrote the extension. Kubernetes is the easier answer: CDK’s own EKS construct library has deployed manifests and Helm charts since 2020, and cdk8s authors manifests on their own for any cluster.

Is AWS CDK free, and does using it lock us into AWS?

CDK is free, and CloudFormation adds no charge for native AWS resource types, so you pay for the resources the template creates, plus the per-operation metering on any third-party registry types you pull in. On lock-in, cdk synth writes CloudFormation templates you can keep deploying after you stop authoring in CDK, so dropping the tool costs you the constructs rather than the stacks. Dropping CloudFormation or AWS is the rewrite. If that matters, CDKTF offers the same authoring against a portable engine, without the AWS-maintained construct library.

Resources

Since we recorded

The question left open at the end of this conversation, how to rename without destroying, has an answer now: AWS shipped CloudFormation stack refactoring on 6 February 2025, which moves resources between stacks and renames logical IDs in one atomic operation, and CDK built cdk refactor on it in September 2025. The CDKTF hedge recommended above is gone: HashiCorp deprecated CDK for Terraform on 10 December 2025 and no longer maintains it. SST moved too, dropping CDK and CloudFormation for Pulumi and Terraform in v3, released in August 2024, where existing CDK constructs cannot run unchanged and must be rewritten or replaced. We came back to the wider tooling question in Episode 101, Infrastructure as code in 2026.