DEV Community

Dakota Lewallen for AWS Community Builders

Posted on • Originally published at

Organize Your CDK lib Folder by Function Not Service

Don’t: Organize Your CDK lib Folder by Service

When working with IaC tools, it's standard to organize your work based on the service. You specify a service and then describe how you would like it configured. Carrying that pattern into CDK might look something like so.

An example of organizing the lib folder by service (lambda, sqs, s3, etc)

At first, this will feel great! Things are going great until you start to work on functionality across many services. At this point, changes will span many directories resulting in many CloudFormation stacks needing to be deployed.

Do: Organize Your CDK lib Folder by Function

If we follow the principle of working backward, we’ll end up with a structure that more aligns with how we’re going to work in the future.

Starting with our business use case. Let’s say you are going to receive flat files through an SFTP server from clients. You will need to perform some basic ETL processes to house it within your system. Instead of having a lambda folder, a step function folder, and an event bridge folder, you build a single FlatFileHandler folder. With a single stack, that deploys the resources necessary to perform this function.

An example of organizing by function. Creating only a stack for this end goal and not per service.

Now whenever work is needed on the flat file handling system, you can find everything you need in a singular place.


One of the greatest strengths of CDK, is the ability to perform more complex organizational patterns for your resources. This is one example of a pattern that can be useful. Like anything that's in code, you're now free to organize as you see fit! If you find a more useful pattern, let the world know (me in particular 😉)!

Find me on LinkedIn | Github

Top comments (0)