What's the way to overcome the circular dependency problem in AWS CDK: Let's imagine I have a stack for ECS cluster and a stack for ECS Service (several of them):
export class EcsClusterStack extends cdk.Stack {
public readonly cluster: ecs.Cluster
...
}
and
export class EcsServiceStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, cluster: ecs.ICluster) { }
}
Now, I can compose my app:
const app = new cdk.App();
const vpc = new VpcStack(app, 'vpc');
const cluster = new ClusterStack(app, 'ecs', vpc.vpc);
const service = new EcsServiceStack(app, 'ecs-service', cluster.cluster);
Let's assume, after that, I want to migrate my ECS service from one cluster to another. I would create another ECS Cluster stack and pass it to ECS Service, but here the thing: AWS CDK automatically generates Outputs (in Cluster stack there are outputs like cluster name, etc), and then, when I want to migrate my ECS Service to another cluster and if I pass another ICluster object down to ECS Service stack constructor AWS CDK tries to remove Outputs/Exports from my previous cluster definition and that's obviously going to fail upon deploy since it cannot remove exports from Cluster stack until there is the service that relies on it. Finally, I see an error like:
0 | 7:15:19 PM | UPDATE_IN_PROGRESS | AWS::CloudFormation::Stack | ecs User Initiated
0 | 7:15:26 PM | UPDATE_ROLLBACK_IN_P | AWS::CloudFormation::Stack | ecs Export ecs:ExportsOutputFnGetAttdefaultasgspotInstanceSecurityGroup2D2AFE98GroupId1084B7B2 cannot be deleted as it is in use by ecs-service
If there was a way to force ECS service stack to deploy first that would solve the problem, but it seems AWS CDK always deploys dependency first (ECS cluster in my case) and that fails deployment. So is there a way to overcome this?