Skip to content

Clean API Architecture with IaC

The Clean API Archectecture approach is a design pattern that is intended to help developers build scalable, maintainable, and testable APIs. The approach emphasizes the use of layers, separation of concerns, and the use of design patterns to help developers build better APIs. Dependancy Injection is a key part of the approach and is implemented in the CDKTF Infrastructura as Code Files.

Clean Architecture

Each stack is a seperate project within the repo and is designed to be deployed independantly. They will mirror a folder structure that represents the Clean API Archetecture layers if they are needed. The exhaustivegeneric folder structure of a stack is as follows:

  • Directorystacks
    • DirectorystackName
      • stackNameStack.ts // root IaC file that contains the code to deploy the stack
      • Directoryapp
        • Directoryservice-genericServiceName1
          • service.ts //IaC file that contains the code to deploy the specificservice
          • DirectoryspecificServiceName1 //contains the files required for the specific service outlined in service.ts
            • specificServiceFile1.ts
            • specificServiceFile2.ts
          • DirectoryspecificServiceSharedResourcesFolder // optional folder that contains files/classes/snippets that are shared across the services in the folders above
            • serviceSharedResource1.ts
            • serviceSharedResource2.ts
          • tests //optional folder that contains the test files for the specific service
        • Directoryservice-genericServiceName2
        • Directorytests //optional folder that contains the test files for the vartious services
          • specificServiceFile1.test.ts
          • specificServiceFile2.test.ts
      • Directoryentities
        • DirectoryspecificEntityName
          • specificEntityCode.ts //this code can be referred to by applicatin services
        • Directorytests
          • specificEntityCode.test.ts //independant test code here
      • Directoryinfrastructure // this folder contains all the Infrastructure code for use in the stackNameStack.ts root Iac file. eg any AWS, Stripe, Google Cloud, Azure, etc., resources are defined here.
        • Directoryaws
          • specificResource1.ts //example of a specific resource
          • specificResource2.ts //example of a specific resource
        • Directorystripe
          • specificResource1.ts //example of a specific resource
          • specificResource2.ts //example of a specific resource
        • googleCloud
      • Directoryinterface // this folder contains the code for bridging the infrastructure resources to the application layer. It’s fine to break out the controllers and the presenters in their own folder but ir’s joined here as it’s more like one set if files defining both.
        • DirectorycontrollerPresenter-specificName1
          • specificController1.ts
          • specificController2.ts
          • Directorytests
            • specificController1.test.ts
            • specificController2.test.ts
        • Directorygateway-specificName1
          • specificGateway1.ts
          • specificGateway2.ts
          • Directorytests
            • specificGateway1.test.ts
      • Directorytests // optional folder that contains the test files for the interface adapters
        • specificInterfaceFile1.test.ts

Although extensive, most stack folders and files will contain only what folders and files are necessary for the stack. Some stacks (eg: the blueGreenToggleStack) will only have Infrastructure as Code files and no Interface or Application logic folders/files at all.

  • Directorystacks
    • DirectoryblueGreenToggle
      • blueGreenToggleStack.ts