aws / aws/aws-cdk

(rds): instanceUpdateBehaviour is broken with `writers`/`readers` configuration

Open
#27,694 3 comments 2 reactions 0 assignees View on GitHub
@aws-cdk/aws-rds bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

According to [the documentation](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_rds-readme.html#updating-the-database-instances-in-a-cluster), I should be able to use the `instanceUpdateBehaviour` setting with the new `writers`/`readers` (e.g., not `instanceProps`) configuration:

![Screenshot 2023-10-26 at 08 41 00](https://github.com/aws/aws-cdk/assets/630449/f38c1c53-c620-4632-bbd3-a2f58c15abee)

However, my `cdk diff` shows that even though the setting is still set, the dependency is removed (effectively undoing the `ROLLING` update setting):

```
Stack Database
Resources
[~] AWS::RDS::DBInstance db/Instance2 dbInstance2648805E1
└─ [-] DependsOn
└─ ["dbInstance1FD14D62F"]
```

Looking at the source code, `instanceUpdateBehaviour` is only paid attention to in the `legacyCreateInstances` method, called when using `instanceProps` https://github.com/aws/aws-cdk/blob/22a3234d7accb9ec78b87bfc8ca5fe6537b769c6/packages/aws-cdk-lib/aws-rds/lib/cluster.ts#L1233 https://github.com/aws/aws-cdk/blob/22a3234d7accb9ec78b87bfc8ca5fe6537b769c6/packages/aws-cdk-lib/aws-rds/lib/cluster.ts#L1330-L1335

When using `writers`/`readers`, this parameter is ignored https://github.com/aws/aws-cdk/blob/22a3234d7accb9ec78b87bfc8ca5fe6537b769c6/packages/aws-cdk-lib/aws-rds/lib/cluster.ts#L634

### Expected Behavior

I expected the `instanceUpdateBehaviour` to work still as I transitioned from `instanceProps` to the new API. The property is not marked as `@deprecated` and the documentation explicitly shows an example of it working with `writers`/`readers`.

### Current Behavior

The CloudFormation dependency is marked for removal in the `cdk diff` when I transition from `instanceProps` to the new `writers`/`readers` API:

```
Stack Database
Resources
[~] AWS::RDS::DBInstance db/Instance2 dbInstance2648805E1
└─ [-] DependsOn
└─ ["dbInstance1FD14D62F"]
```

### Reproduction Steps

To show this issue, you can `cdk synth` with the `instanceBehaviour` specified and not specified. The CFN produced is exactly the same.

Given,

1. Create this stack in a new CDK repo:
```ts
import * as cdk from 'aws-cdk-lib';
import * as rds from 'aws-cdk-lib/aws-rds';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { Construct } from 'constructs';

export class DatabaseStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const vpc = new ec2.Vpc(this, 'VPC');
new rds.DatabaseCluster(this, 'Database', {
engine: rds.DatabaseClusterEngine.auroraMysql({ version: rds.AuroraMysqlEngineVersion.VER_3_01_0 }),
writer: rds.ClusterInstance.provisioned('Instance1' ),
readers: [rds.ClusterInstance.provisioned('Instance2')],
instanceUpdateBehaviour: rds.InstanceUpdateBehaviour.ROLLING,
vpc,
});
}
}
```
2. Produce a template and save it off

```shell
> yarn cdk synth > instance-behaviour-set.yml
```
3. Update the stack to not set `instanceUpdateBehaviour`:
```ts
import * as cdk from 'aws-cdk-lib';
import * as rds from 'aws-cdk-lib/aws-rds';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { Construct } from 'constructs';

export class DatabaseStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const vpc = new ec2.Vpc(this, 'VPC');
new rds.DatabaseCluster(this, 'Database', {
engine: rds.DatabaseClusterEngine.auroraMysql({ version: rds.AuroraMysqlEngineVersion.VER_3_01_0 }),
writer: rds.ClusterInstance.provisioned('Instance1' ),
readers: [rds.ClusterInstance.provisioned('Instance2')],
// instanceUpdateBehaviour: rds.InstanceUpdateBehaviour.ROLLING, <- CHANGED
vpc,
});
}
}
```
4. Produce another template

```shell
> yarn cdk synth > instance-behaviour-not-set.yml
```
5. Diff the two templates:

```shell
> diff instance-behaviour-set.yml instance-behaviour-not-set.yml
571c571
< Done in 2.75s.
---
> Done in 2.28s
```

Note that the `instanceUpdateBehaviour` property has no effect on the resulting template.

### Possible Solution

1. Add the instance update behavior logic to the new `_createInstances` method used by the `writers`/`readers` API.
2. If `instanceUpdateBehaviour` does not make sense with the new API, mark it as deprecated and remove the example (linked above) from the documentation.

### Additional Information/Context

_No response_

### CDK CLI Version

2.103.0 (build d0d7547)

### Framework Version

_No response_

### Node.js Version

18.18.2

### OS

MacOS

### Language

TypeScript

### Language Version

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start in packages/aws-cdk-lib/aws-rds/lib/cluster.ts, comparing the existing legacyCreateInstances logic with _createInstances for the writers/readers API. Reproduce the issue with the supplied TypeScript stack and run yarn cdk synth with and without instanceUpdateBehaviour; done means the ROLLING setting produces the intended CloudFormation dependency, or the property and documentation are explicitly deprecated if it is unsupported.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.