app-staging-synthesizer: Deletion of DefaultStagingStack fails when it does not contains any s3 asset #27834
Labels
@aws-cdk/app-staging-synthesizer-alpha
Related to the @aws-cdk/app-staging-synthesizer-alpha package
bug
This issue is a bug.
effort/medium
Medium work item – several days of effort
p2
Describe the bug
When using
AppStagingSynthesizer.defaultResources
and we don't use any asset in stacks in the app, CFnDependsOn
properties are not rendered properly onDefaultStagingStack
.This at least makes deleting the staging stack fail because a bucket policy sometimes gets deleted before
autoDeleteObject
custom resource ignoring its dependency.Expected Behavior
DependsOn
properties are rendered.Current Behavior
DependsOn
properties are not rendered.Reproduction Steps
Use the following code for cdk app.
Run
cdk synth
and check the output template (StagingStack-my-app-id2-ACCOUNT-REGION.template.json
).You can find this resource:
Where it should be:
Possible Solution
Here's my observation:
The root cause for this issue is that we add resources to an CDK app AFTER cdk runs
prepareApp
function.aws-cdk/packages/aws-cdk-lib/core/lib/private/synthesis.ts
Line 45 in fce26b6
Bucket
resource can be added even afterprepareApp
here (it's added on the first timeaddFileAsset
is called) :aws-cdk/packages/@aws-cdk/app-staging-synthesizer-alpha/lib/app-staging-synthesizer.ts
Line 320 in fce26b6
When we use any (s3) assets in the app (e.g. Lambda code asset),
addFileAsset
is called beforeprepareApp
, so in that case it works as expected.In
prepareApp
function, we search for all the dependencies between nodes and register them as a CFn resource dependencies. That's why dependencies added afterprepareApp
are not rendered at all.aws-cdk/packages/aws-cdk-lib/core/lib/private/prepare-app.ts
Lines 17 to 28 in fce26b6
So the possible solution can be to always create a staging bucket first to avoid adding it after
prepareApp
. To do that, for example, we can addthis.getCreateBucket();
to the stack constructor here:aws-cdk/packages/@aws-cdk/app-staging-synthesizer-alpha/lib/default-staging-stack.ts
Lines 241 to 246 in fce26b6
I'm not quite sure if there is more thorough solution (given that there are possibly other resources other than a staging bucket that can be added after
prepareApp
). Afaik it seems only a staging bucket is causing this issue because the bucket is used to upload CFn templates in the app (that's why it can be added afterprepareApp
).Additional Information/Context
On the other hand,
DependsOn
ofCustomResourceProviderHandler
is rendered properly.This is because custom resource provider uses
CfnResource.addDependency
directly instead ofNode.addDependency
.aws-cdk/packages/aws-cdk-lib/core/lib/custom-resource-provider/custom-resource-provider.ts
Line 313 in 7c497ee
CDK CLI Version
2.104.0
Framework Version
2.104.0
Node.js Version
18
OS
macOS
Language
TypeScript
Language Version
No response
Other information
No response
The text was updated successfully, but these errors were encountered: