aws / aws/aws-cdk

aws_cdk.Annotations: Annotations only work at Stack or lower level Constructs

Open
#36,514 1 comment 0 reactions 0 assignees View on GitHub
effort/medium feature-request p2 package/tools
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
1d 19h
Merged PRs (30d)
71

Description

### Describe the bug

When adding an `Annotation` to a Construct, if that construct is not a `Stack`, or in the scope of a `Stack`, the `Annotation` will not be output or respected. This has to be an error - since we should be able to add annotations at the app level (say, for synthesis of AWS CDK Pipelines, or other non-stack resources).

As a workaround, I've been looking for the "first found stack" by iterating through the `App` children and adding the `Annotation` at that level, but it should not be necessary.

### Regression Issue

- [ ] Select this option if this issue appears to be a regression.

### Last Known Working CDK Library Version

_No response_

### Expected Behavior

```python
from aws_cdk import App, Stack, Annotations

app = App()
stack = Stack(app, "test")
Annotations.of(app).add_error("test") # no failure
app.synth()
```

I expect this to generate an error message at synthesis.

### Current Behavior

Currently, it does not.

### Reproduction Steps

See above for reproduction steps. Adding the annotation to the "stack" instead of the "app" does work and generate an appropriate warning - but not all resources in CDK fall under a Stack!

### Possible Solution

There may be something in how Annotations are configured that require them to be added to a Stack and not an App.

### Additional Information/Context

_No response_

### AWS CDK Library version (aws-cdk-lib)

2.233.0

### AWS CDK CLI version

2.1031.0 (build 3d7b09b)

### Node.js Version

v20.18.1

### OS

MacOS

### Language

Python

### Language Version

Python (3.13)

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the Python example using App, Stack, Annotations.of(app), and app.synth(). Start by tracing the Annotations.of and synthesis entry points, then compare app-level behavior with stack-level behavior. Done means an app-level add_error produces an error message during synthesis without requiring a Stack scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, python, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.