-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to only build one stack in a multistack project? #11625
Comments
Hey @adam-nielson, Sorry for the delayed response! I just tried to synth a single stack and was able to generate the template for each stack individually using In regard to the concern about deploying to different accounts, I highly recommend you bake the account data into the stack declaration or at least require it as an input parameter on synth and deploy. You can do this with environments. This will mean that when you do a generic Eg. const app = new cdk.App();
new StackOne(app, 'StackOne', {
env: {
account: '111111111',
region: 'us-west-1'
}
});
new StackTwo(app, 'StackTwo', {
env: {
account: '22222222',
region: 'eu-east-1'
}
}); Let me know if this helps! If not, please leave a little more code here as outlined above. 😸 😷 |
This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled. |
@NGL321 Sorry for the delay responding, and thanks for your reply! The example code you have matches my top level structure. When you say you were able to generate each stack individually this is correct, this is what happens, but the problem is that both stacks are processed before one is discarded and the final template is produced. In your example code, if you place a time consuming task in one stack, then that is executed even if you have told I guess as an example you could put a Hopefully this makes sense! |
Yep, I experience the same behavior. When I synth/deploy other stacks that have no dependencies, it bundles up lambda functions in other stacks that take awhile to run. |
Same issue here. |
Not experienced this with CDK, but I've had similar issues previously with Terraform (deploying simple Lambda change required full infra comparison). My conclusion was that tightly coupling your infrastructure deploy to you app code bundling, and even deployment, isn't a good idea - they are different concerns. |
That's a very good point, it does make sense to split the build and the deploy. It makes rollbacks easier too because you can just run another deploy with the previous version, as opposed to rolling back the code and then rebuilding the previous version from scratch in order to deploy it. All the CDK examples have the deploy as the main focus with the actual code you're running tacked on almost as an afterthought so I guess it's not necessarily the best architecture for a real application. I suppose the drawback is that if you want to split the build and deploy you have to figure out where you're uploading your code to, how to manage the versions, and how to control which version should get deployed. All of which requires more work outside of CDK so I guess that's why it's not really seen in the examples. |
For anyone who may be interested in doing something similar, here's what I have done: const stackBuildTargetAcct = process.env.STACK_BUILD_TARGET_ACCT || "dev";
if (stackBuildTargetAcct === "dev") {
// define dev account stacks
} else if (stackBuildTargetAcct === "prod") {
// define prod account stacks
} Then you can just set the value of the |
Very similar as above but used the context structure. Tying the build and deploy process together thru nuke with a wrapper around the cdk synth and deploy calls. makes it pretty easy to do json file updates both for appsettings and for cdk.context.json. Have a flag in the cdk.context.json file for "currentBuild", then just wrap the stack creations in if statements same as above. But fundamentally the cdk process feels broken in c#. No parameters are passed via arguments, --context flag values don't over write existing values in the cdk.context.json or cdk.json files, have to hack away around a process that should be able to synth a single stack at a time...very much does not feel like it is ready for release and is still a beta project....just my .02 |
Is it possible to do this without adding an additional env variable or context value? We are already specifying the stacks we want to deploy when calling Ideally, something like this should work:
And then call it like this:
Anyone had success figuring this out? |
If you only want to decide based on the account, you can use If you're trying to do this so you can name resources differently across stages (e.g. different S3 bucket names for test and prod) then you're better off passing the account into the stack as a prop, and in there define a stage (e.g. if account = 1234 then stage = 'prod' else stage = 'dev'). Then when you name your resources you just use that string, e.g. |
Thank you, this was the solution I was looking for! |
Still having this same issue. |
Can you please share code snippets of how you achieved that, your approach seems the closest thing to a solution to this issue. |
Definitely the only workaround I see for this issue... totally agree: when specifying a stack we shouldn't have to build other stacks, and this should be the default cdk behavior. |
Related: #6743 |
I know this is old, but as far as I can see it is still not resolved and someone might fight it useful. I tried using the const app = new App();
const bundlingStacks = app.node.tryGetContext("aws:cdk:bundling-stacks") as Array<string>;
const buildAllStacks = bundlingStacks.includes("**");
if (buildAllStacks || bundlingStacks.includes("MyStackId")) {
new Stack(app, "MyStackId", {});
}
if (buildAllStacks || bundlingStacks.includes("MyOtherStackId")) {
new Stack(app, "MyOtherStackId", {});
} |
@j5nb4l your solution worked beautifully! For clarity for others its important to note you should still use the flag
|
❓ General Issue
In the documentation you can create multiple
Stack
instances to have a multi-stack deployment. You can then runcdk synth <stackname>
to prepare only one stack.However when you do this, it seems to still build all stacks. This means if I have one stack building Lambda functions in custom Docker containers, and a second stack that does virtually nothing, I have to sit through the whole build process for the stack I am not using. It also does things like VPC lookups for resources that are not in the final stack that you want to deploy.
This is a problem because I want to deploy each stack into different AWS accounts. One stack should go into the test/staging/production accounts, and the other should go into the build account where all the CodeCommit repos are. This means when I run the main stack in the prod account, it complains that the repo stack can't find the repo permission groups (because they only exist in the build account, not in the prod/staging/test accounts). Alternatively when I run the repo stack in the build account, it complains that the main stack can't find its VPC IDs (which doesn't exist in the build account, only in prod/staging/test).
The Question
How can I synth/deploy only one stack in a multi-stack project, with the other stacks completely ignored and not even built, so that I can completely independently deploy each stack in a multi-stack project to a different AWS account?
Environment
The text was updated successfully, but these errors were encountered: