Skip to main content

AWS CDK Mixins let you reuse infrastructure rules

· Do Minh Tuan · Business Hackers 🚀

AWS CDK Mixins let you reuse infrastructure rules

AWS has introduced CDK Mixins, a way to package a reusable capability and apply it to existing cloud resources. The CDK is Amazon's infrastructure as code tool: you describe servers, databases and permissions in a programming language, and it generates the CloudFormation that creates them. Mixins let you write a rule once, like enabling encryption or attaching a standard logging policy, and apply it across many resource types without copying the same block into every stack. I think the idea is sound, and the risk sits in the details.

If you have maintained a large CDK codebase, you know the problem. The same security configuration gets pasted into a new bucket, a new queue and a new function. Someone updates two of the three. The third sits there for a year with a slightly different policy, and nobody notices until an audit. A mixin is an attempt to make the correct configuration the default one.

What a mixin is, plainly

A mixin is a small unit of behaviour that can be attached to a construct. It modifies or extends an existing resource, adding a trait like encryption, monitoring or a tag. The point is composition: instead of building a giant base class that does everything, you stack small behaviours and reuse each one where it applies.

That is a sensible design, and it matches how experienced platform teams already think. The interesting question is not whether reuse is good. It is whether the reuse is visible to the person reviewing the change.

The risk I would manage

Infrastructure code has a property that application code mostly does not: a small change can open a database to the internet or delete a volume. When behaviour is attached implicitly, the diff gets smaller and the blast radius does not. A reviewer looking at a pull request that adds one line may not see that the mixin also changed a retention policy or a public access setting.

So the first thing I would ask is what the tooling shows. Does the generated CloudFormation diff make the effect obvious? I would also want to list every resource a given mixin touched. If the tooling cannot answer that, the convenience is hiding risk, and the team will learn that the hard way during an incident review.

The second question is versioning. A mixin used by ten stacks is a shared dependency. When someone changes it, ten stacks change on their next deploy. That is the same problem as a shared library, with higher stakes. I would want a review process for mixin changes that treats them as production changes, and a way to roll one back without redeploying everything.

The third is testability. A mixin should come with a test that asserts the resource ends up with the property it claims to set. Without that, the mixin is a convention, and conventions drift. A small assertion in the CDK test framework is enough, and it is the difference between a rule and a hope.

Why it is still worth using

Given all that, I think the direction is right. The cloud incidents that scare me are rarely about clever architecture. They come from an inconsistent default: one bucket without encryption, one security group open to the world, one log group without retention. A reusable rule is the cheapest way to make the safe path the easy path.

It also helps with onboarding. A new developer on a platform team does not need to memorise every policy. They attach the standard mixins and get the organisation's baseline. That is how you scale a small platform team without hiring a reviewer for every pull request.

For a startup, the practical version is smaller. You do not need a library of mixins on day one. You need two or three: encryption on storage, logging on everything that can log, and a tagging standard so the bill is attributable. Write them once, test them, and apply them everywhere. That is a weekend of work that pays off for years, and it scales better than a wiki page of rules that everyone is supposed to remember.

For a team in Vietnam delivering infrastructure for an overseas client, mixins are also a good way to encode the client's compliance requirements in code instead of a document. The rule lives in the repository, and the audit becomes a test run rather than a conversation about whether everyone remembered.

What I would watch

I would want to see how mixins interact with the existing ways of composing CDK: constructs, aspects and the escape hatches people already use. Every layer of abstraction has a cost, and the cost is usually paid by the person debugging a failed deploy at the worst possible time. If the error messages point clearly at the mixin that caused the problem, the abstraction is earning its place. If they point at generated CloudFormation with no context, it is not. I would rather have a boring feature with clear errors than a clever one that hides them.

The documentation matters as much as the mechanics. A mixin is only useful if a developer who did not write it can understand what it does from the name and the test. That sounds obvious, and it is the difference between a library that spreads and one that becomes folklore.

My honest view is that this is a good feature for teams that already have discipline and a mild trap for teams that do not. It makes the right thing easier, and it also makes it easier to do a lot of things without looking. The difference is review culture. No tool fixes that, and a good tool makes it slightly cheaper to get right.

Written with AI assistance, reviewed and edited by Do Minh Tuan.