Skip to content

cdk cli: Deploy of empty non-toplevel stack does not get deleted #20822

@rabbittsoup

Description

@rabbittsoup

Describe the bug

After deploying a stack that uses a stage as it's scope (and not the App), so that it is not defined at the top level of the construct tree, a subsequent deploy of that same stack with all resources removed (including analytics_reporting=False) will recognize the stack as empty and existing, but does not delete it.

Expected Behavior

A previously existing stack deployed with no resources should delete the stack.

Current Behavior

The previously existing stack is not deleted.

Reproduction Steps

#app.py
from aws_cdk import App, Stage, Stack
app = App()
stage = Stage(app, 'Stage')
stack = Stack(stage, 'Stack')

cdk deploy '**'

#changed app.py
from aws_cdk import App, Stage, Stack
app = App()
stage = Stage(app, 'Stage')
stack = Stack(stage, 'Stack', analytics_reporting=False)

cdk deploy '**'

Possible Solution

I suspect the stack.stackName reference during the deploy of an existing empty stack should be stack.node.path instead.

selector: { patterns: [stack.stackName] },

>>> app = App()
>>> top_level_stack = Stack(app, 'TopLevelStack')
>>> stage = Stage(app, 'Stage')
>>> staged_level_stack = Stack(stage, 'StagedLevelStack')
>>> top_level_stack.stack_name; top_level_stack.node.path
'TopLevelStack'
'TopLevelStack'
>>> staged_level_stack.stack_name; staged_level_stack.node.path
'Stage-StagedLevelStack'
'Stage/StagedLevelStack'

Additional Information/Context

No response

CDK CLI Version

2.28.1 (build d035432)

Framework Version

2.28.1

Node.js Version

14.16.0

OS

AL2

Language

Python

Language Version

Python 3.8

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue is a bug.effort/smallSmall work item – less than a day of effortp1package/toolsRelated to AWS CDK Tools or CLI

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions