aws / aws/aws-cdk

bug(bedrockagentcore): Memory L2 construct does not set defaultChild, apply_removal_policy fails

Open
#38,262 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

The `Memory` L2 construct in `aws_bedrockagentcore` does not set `defaultChild` on the construct node. This causes `apply_removal_policy()` to fail with:

```
CannotApplyRemovalPolicy: Cannot apply RemovalPolicy: no child or not a CfnResource. Apply the removal policy on the CfnResource directly.
```

### Expected Behavior

`memory.apply_removal_policy(RemovalPolicy.RETAIN)` should work, like it does on other L2 constructs (e.g., `s3.Bucket`, `dynamodb.Table`).

### Current Behavior

The `Memory` L2 creates a `CfnMemory` child named `"Memory"` and a `Role` child named `"ServiceRole"`, but does not assign the `CfnMemory` as the `default_child`. The inherited `Resource.apply_removal_policy()` then fails because it relies on `self.node.default_child` being set.

### Workaround

```python
from typing import cast
from aws_cdk import CfnResource

cfn_memory = cast(CfnResource, memory.node.find_child("Memory"))
cfn_memory.apply_removal_policy(RemovalPolicy.RETAIN)
```

### Reproduction Steps

```python
import aws_cdk as cdk
from aws_cdk import aws_bedrockagentcore as agentcore, RemovalPolicy

app = cdk.App()
stack = cdk.Stack(app, "Test")

memory = agentcore.Memory(stack, "M",
memory_name="test",
expiration_duration=cdk.Duration.days(30),
memory_strategies=[agentcore.MemoryStrategy.using_built_in_semantic()],
)

# This fails:
memory.apply_removal_policy(RemovalPolicy.RETAIN)
```

### Additional context

- `aws-cdk-lib` version: 2.261.0
- `node.default_child` is `None`
- The `CfnMemory` resource is at `node.find_child("Memory")`

### CDK CLI Version

2.261.0

### Framework Version

2.261.0

### Node.js Version

20.x

### OS

Amazon Linux 2023

### Language

Python

### Language Version

3.12

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start at the aws_bedrockagentcore Memory L2 construct and its CfnMemory child, then inspect how apply_removal_policy resolves node.default_child. Reproduce the provided RETAIN case and add coverage showing that the CfnMemory child is used as the default child.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.