-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Amazon.CDK.AWS.RDS.DatabaseCluster: cyclical reference when using Credentials passed from another stack #30738
Comments
@NovaLogicDev Good morning. The issue is reproducible using the below TypeScript CDK code as well: import * as cdk from 'aws-cdk-lib';
import { Vpc } from 'aws-cdk-lib/aws-ec2';
import { AuroraPostgresEngineVersion, ClusterInstance, Credentials, DatabaseCluster, DatabaseClusterEngine } from 'aws-cdk-lib/aws-rds';
import { Secret } from 'aws-cdk-lib/aws-secretsmanager';
import { Construct } from 'constructs';
export class ReproBugStack extends cdk.Stack {
private auroraUsername: string;
private auroraSecret: Secret;
public clusterCredentials : Credentials;
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
this.auroraUsername = "clusteradmin";
this.auroraSecret = new Secret(this, 'DatabaseClusterSecret', {
description: 'Database cluster secret for Edfi',
secretName: 'EdfiDatabaseClusterSecret',
generateSecretString: {
excludeCharacters: "\"@/\\ '",
generateStringKey: 'password',
passwordLength: 128,
secretStringTemplate: '{"username": \"'+ this.auroraUsername+ '\"}'
}
});
this.clusterCredentials = Credentials.fromSecret(this.auroraSecret, this.auroraUsername);
}
}
export interface DBStackProps {
credentials: Credentials;
}
export class DBStack extends cdk.Stack {
constructor(scope: Construct, id: string, props: DBStackProps) {
super(scope, id);
const vpc = new Vpc(this, 'DatabaseVPC');
const auroraEngine = DatabaseClusterEngine.auroraPostgres({
version: AuroraPostgresEngineVersion.VER_16_2
});
const database = new DatabaseCluster(this, 'AuroraDatabaseCluster', {
engine: auroraEngine,
vpc: vpc,
writer: ClusterInstance.serverlessV2('Instance1', {
publiclyAccessible: false
}),
credentials: props.credentials
});
}
}
const app = new cdk.App();
const stack1 = new ReproBugStack(app, 'ReproBugStack', {});
new DBStack(app, 'DBStack', { credentials: stack1.clusterCredentials }); Running
The reason you are getting this error is that DBStack is depending on the resource Since you have the Modified code below: import * as cdk from 'aws-cdk-lib';
import { Vpc } from 'aws-cdk-lib/aws-ec2';
import { AuroraPostgresEngineVersion, ClusterInstance, Credentials, DatabaseCluster, DatabaseClusterEngine } from 'aws-cdk-lib/aws-rds';
import { Secret } from 'aws-cdk-lib/aws-secretsmanager';
import { Construct } from 'constructs';
export class ReproBugStack extends cdk.Stack {
private auroraUsername: string;
private auroraSecret: Secret;
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
this.auroraUsername = "clusteradmin";
this.auroraSecret = new Secret(this, 'DatabaseClusterSecret', {
description: 'Database cluster secret for Edfi',
secretName: 'EdfiDatabaseClusterSecret',
generateSecretString: {
excludeCharacters: "\"@/\\ '",
generateStringKey: 'password',
passwordLength: 128,
secretStringTemplate: '{"username": \"'+ this.auroraUsername+ '\"}'
}
});
}
}
export interface DBStackProps {
credentials: Credentials;
}
export class DBStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const vpc = new Vpc(this, 'DatabaseVPC');
const auroraEngine = DatabaseClusterEngine.auroraPostgres({
version: AuroraPostgresEngineVersion.VER_16_2
});
const auroraUsername = "clusteradmin";
const auroraSecret = Secret.fromSecretNameV2(this, 'DatabaseClusterSecret', 'EdfiDatabaseClusterSecret');
const creds = Credentials.fromSecret(auroraSecret, auroraUsername);
const database = new DatabaseCluster(this, 'AuroraDatabaseCluster', {
engine: auroraEngine,
vpc: vpc,
writer: ClusterInstance.serverlessV2('Instance1', {
publiclyAccessible: false
}),
credentials: creds
});
}
}
const app = new cdk.App();
const stack1 = new ReproBugStack(app, 'ReproBugStack', {});
new DBStack(app, 'DBStack').addDependency(stack1); Please let me know if it works for you. Thanks, |
Yup, that does work, thanks |
|
Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one. |
Describe the bug
When running CDK synth on my project:
Unhandled exception. System.Exception: Error: 'ReproBugStack' depends on 'DBStack' (ReproBugStack -> DBStack/AuroraDatabaseCluster/Resource.Ref). Adding this dependency (DBStack -> ReproBugStack/DatabaseClusterSecret/Resource.Ref) would create a cyclic reference.
Expected Behavior
No cyclical reference error, the database stack references the resource created by the ReproBugStack and there is no circular referencing.
Current Behavior
$cdk synth
ReproBugStack.cs
DatabaseStack.cs
Possible Solution
No response
Additional Information/Context
No response
CDK CLI Version
2.144.0 (build 5fb15bc)
Framework Version
No response
Node.js Version
v20.14.0
OS
Darwin, 14.5
Language
.NET
Language Version
net6.0
Other information
No response
The text was updated successfully, but these errors were encountered: