aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

Unexpected cyclic reference with Vpc and DatabaseCluster

Open shurmanidze opened this issue 7 years ago • 3 comments

If Vpc is created in one stack and a DatabaseCluster in this Vpc in another stack, then I get the cyclic reference error

Reproduction Steps

export class CommonStack extends Stack {
  private vpc: IVpc;
  private securityGroup: ISecurityGroup;
  constructor(parent: App, name: string) {
    super(parent, name);

    const vpc = new Vpc(this, 'CommonVpc', {
      natGateways: 0,
    });
    this.vpc = vpc;
    this.securityGroup = SecurityGroup.fromSecurityGroupId(this, 'VpcDefaultSecurityGroup', vpc.vpcDefaultSecurityGroup);
  }
  get Vpc(): IVpc {
    return this.vpc;
  }
  get SecurityGroup(): ISecurityGroup {
    return this.securityGroup;
  }
}
export class DbStack extends Stack {
  constructor(parent: App, name: string, options: DbStackOptions) {
    super(parent, name);

...
    const cluster = new DatabaseCluster(this, "DbCluster", {
      engine: options.clusterEngine,
      parameterGroup: ParameterGroup.fromParameterGroupName(this, 'ParameterGroup', 'default.aurora-mysql5.7'),
      defaultDatabaseName: defaultDbName,
      masterUser: {
          username: options.rootUserName,
      },
      instanceProps: {
          instanceType: InstanceType.of(options.instanceClass, options.instanceSize),
          vpcSubnets: {
              subnetType: options.subnetType,
          },
          vpc,
          securityGroup,
      }
    });
  }
}

Error Log

Error: 'DbStack' depends on 'CommonStack' (DbStack -> CommonStack/CommonVpc/PublicSubnet1/Subnet.Ref). Adding this dependency (CommonStack -> DbStack/DbCluster/Resource.Endpoint.Port) would create a cyclic reference.

Environment

  • CLI Version : 1.14.0
  • Framework Version: 1.14.0
  • OS : Windows 10
  • Language : TS

This is :bug: Bug Report

shurmanidze avatar Oct 30 '19 18:10 shurmanidze

@shurmanidze I would recommend creating a security group specifically for your database cluster in the database cluster stack rather than importing a security group from another stack. If you need to allow access to your database cluster you can always do that. For example if you wanted to allow access from the vpc security group you would do something like

...
   const dbSecurityGroup = new SecurityGroup(this, 'DbSg');
    const cluster = new DatabaseCluster(this, "DbCluster", {
      ...,
      instanceProps: {
          securityGroup: dbSecurityGroup,
      }
    });
  }
  cluster.connections.allowDefaultPortFrom(props.vpcSecurityGroup);
}

Also, I would recommend against using the vpc default security group. Most security controls recommend making sure the vpc default security group does not allow inbound or outbound traffic. https://docs.aws.amazon.com/securityhub/latest/userguide/securityhub-standards-fsbp-controls.html#fsbp-ec2-2

corymhall avatar Feb 16 '22 15:02 corymhall

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

github-actions[bot] avatar Feb 18 '22 16:02 github-actions[bot]

I am seeing the same issue, with a rds.DatabaseProxy in one stack and the VPC in another stack. How did you resolve this @shurmanidze ?

alechewitt avatar Aug 07 '24 22:08 alechewitt