# Recommended approach for multi-env, multi-stack layout

**URL:** <https://discuss.hashicorp.com/t/recommended-approach-for-multi-env-multi-stack-layout/57034>\
**Category:** CDK for Terraform\
**Created:** [August 9, 2023, 11:30am UTC](https://discuss.hashicorp.com/t/recommended-approach-for-multi-env-multi-stack-layout/57034 "2023-08-09T11:30:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AutomationD](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/automationd/32/37319_2.png) [@AutomationD](https://discuss.hashicorp.com/u/AutomationD)\
**Post date:** [August 9, 2023, 11:30am UTC](https://discuss.hashicorp.com/t/recommended-approach-for-multi-env-multi-stack-layout/57034/1 "2023-08-09T11:30:05Z")

</div>

Hey all,

I’ve looked through the documentation and examples, but I can’t seem to find a pattern recommended for the infrastructure that contains the following layout:

**Environments:**

- dev
- dev-
- prod

And each of the environments has:

- vpc
- apps
  - api
  - web

- db
  - RDS
  - DynamoDB.

I have some assumptions, but I’m not sure if I’m thinking in a right direction. Can someone share what the most current recommendation is?

Thanks!

---

<div class="post-metadata">

**Author:** ![mark.decrane](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/mark.decrane/32/37400_2.png) [@mark.decrane](https://discuss.hashicorp.com/u/mark.decrane)\
**Post date:** [August 9, 2023, 9:26pm UTC](https://discuss.hashicorp.com/t/recommended-approach-for-multi-env-multi-stack-layout/57034/2 "2023-08-09T21:26:12Z")

</div>

We have example projects that sounds very similar to what your looking for, you can take a look at [the Typescript version here](https://github.com/cdktf/cdktf-integration-serverless-example). The same project has also been re-implemented in [Java](https://github.com/cdktf/cdktf-integration-serverless-java-example) and [Python](https://github.com/cdktf/cdktf-integration-serverless-python-example).

Even if you’re not using Java or Python I’d recommend taking a look at either one as they both have far more descriptive READMEs in regards to the design patterns of the project than the Typescript implementation.

---

<div class="post-metadata">

**Author:** ![AutomationD](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/automationd/32/37319_2.png) [@AutomationD](https://discuss.hashicorp.com/u/AutomationD)\
**Post date:** [August 9, 2023, 9:49pm UTC](https://discuss.hashicorp.com/t/recommended-approach-for-multi-env-multi-stack-layout/57034/3 "2023-08-09T21:49:33Z")

</div>

Thanks, Mark,

Yes, I saw those, and I can see the inclination is to have flat list of stacks:

- dev-vpc
- dev-apps
- dev-db
- prod-vpc
- prod-apps
- prod-db

It makes sense, but then it is unclear what’s the best way to differentiate between differences across environments.

For example, a bucket would have a different lifecycle policy or number of zones in a  
VPC would be different.

The only way I can think of is using conditionals, is that the recommended way?

In addition, the approach of user suffixes while having common stack names seems to be prone to unexpected resource deletions during a true CI/CD, when, for example, a non-user `dev` environment (`dev-posts` stack, in the python example) would be applied and `sls-posts-automationd` Dynamo table would be considered for deletion (as it would be missing in the `dev-posts` stack synthed).

If I’m understanding it correctly, one of the solutions would be to add user component into the stack id [here](https://github.com/cdktf/cdktf-integration-serverless-python-example/blob/main/main.py#L26-L40).

To re-iterate, I’m solely trying to understand the design concepts assumed & confirm my assumptions.

---

<div class="post-metadata">

**Author:** ![mark.decrane](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/mark.decrane/32/37400_2.png) [@mark.decrane](https://discuss.hashicorp.com/u/mark.decrane)\
**Post date:** [August 10, 2023, 8:10pm UTC](https://discuss.hashicorp.com/t/recommended-approach-for-multi-env-multi-stack-layout/57034/4 "2023-08-10T20:10:08Z")

</div>

Conditionals could be an option, though I can envision some flakiness if it’s done directly through the passed environment string. If going this route, I’d personally use an enum to add a level of strictness on whats allowable for the environment. Could potentially help to make future changes and any subsequent problems they might create more easily visible prior to an apply.

Another way it could be done is through the passing of pre-determined options (something like devOptions and prodOptions) object into the respective stacks in the `main` file, and then accessing the options object where it’s needed to account for differences in the environments. Though this would have it’s own downsides. Depending on the complexity of your project, or rather the complexity of the differences between environments, having configuration details that live outside of the space in which they are to configure could prove to be awkward and/or difficult to work with.

In total, it’s a trade off between added complexity of the applying based on conditionals and configuration existing separate to what its configuring.

In terms of the approach of using user suffix in CI/CD, I foresee the same potential issue as you. To my understanding, the inclusion of the user in to the stack id as you suggest would mitigate such problems.

---

<div class="post-metadata">

**Author:** ![AutomationD](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/automationd/32/37319_2.png) [@AutomationD](https://discuss.hashicorp.com/u/AutomationD)\
**Post date:** [August 11, 2023, 6:41am UTC](https://discuss.hashicorp.com/t/recommended-approach-for-multi-env-multi-stack-layout/57034/5 "2023-08-11T06:41:22Z")

</div>

Thanks for all your input. It makes sense, although I’ll probably need to build it and feel it out to get a full understanding of pros/cons.

I have another alternative in mind for env structuring, do you think it would make any sense to split cdktf directories/projects on the env level? Let’s say a directory for `dev` (with user-based envs in it) and directory for `prod`? Or would it be a severe antipattern?

Or maybe split the cdktd.json into two different directories/files with different context config section? Let’s say:

1. cdktf.dev.json contains information about the “dev” super environment with it’s own state backend
2. cdktf.prod.json contains information about the “prod” super environment with it’s own state backend  
I guess the feature and a downside here would be that it would loose continuity from dev to prod completely (let’s say provider/module upgrade)

---

<div class="post-metadata">

**Author:** ![mark.decrane](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/mark.decrane/32/37400_2.png) [@mark.decrane](https://discuss.hashicorp.com/u/mark.decrane)\
**Post date:** [August 14, 2023, 3:49pm UTC](https://discuss.hashicorp.com/t/recommended-approach-for-multi-env-multi-stack-layout/57034/6 "2023-08-14T15:49:53Z")

</div>

I can imagine you’d run into some issues having a split config for the same project, though with issues there are usually work-arounds to be found. Main thing I see you already pointed out– having a general discontinuity between environments. Additionally having naming that differs from `cdktf.json` would likely cause issues with the CLI and more generally the `cdktf` package. As it stands it’d be hard to recommend this approach as you will be working against the current intentions of our design– but I’ll leave it up to you to evaluate trade-offs between the approaches. Happy to be proven wrong!

If you have the chance, I’d be interested to hear about your experience building out a solution to your use case. Either in the cdk.dev slack, or a github issue if you have any ideas on improving support for your use case. Hands-on experience from practitioners is always appreciated 😁

---

<div class="post-metadata">

**Author:** ![vsuzdaltsev](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/vsuzdaltsev/32/43779_2.png) [@vsuzdaltsev](https://discuss.hashicorp.com/u/vsuzdaltsev)\
**Post date:** [November 12, 2024, 12:15pm UTC](https://discuss.hashicorp.com/t/recommended-approach-for-multi-env-multi-stack-layout/57034/7 "2024-11-12T12:15:15Z")

</div>

Example of structured setup can be found in [GitHub - vsuzdaltsev/arranger](https://github.com/vsuzdaltsev/arranger)
