I want to make two ECS Services
(admin
and ai
) and let each service has each capacityprovider
.
Now I made two codes for admin stack
and ai stack
This is my admin stack
, it works well.
//make cluster
const cluster = new ecs.Cluster(this, `sm-${targetEnv}-Cluster`, {
vpc:vpc,
});
//make autosacaling group
const adminAutoScalingGroup = new autoscaling.AutoScalingGroup(this, admin-asg , {
vpc,
keyName:"ssh-test",
instanceType: new ec2.InstanceType( t2.medium ),
machineImage: ecs.EcsOptimizedImage.amazonLinux(),
securityGroup:instanceSg,
minCapacity:1,
maxCapacity:3,
});
// make capacityprovider
const adminCapacityProvider = new ecs.AsgCapacityProvider(this, admin-asg-capacity-provider , {
autoScalingGroup:adminAutoScalingGroup,
});
// add capacityprovider to cluster
cluster.addAsgCapacityProvider(adminCapacityProvider);
//make service with capacity provider
const ecsAdminService = new ecs.Ec2Service(this, AdminService , {
cluster,
taskDefinition:adminTaskDefinition,
capacityProviderStrategies: [{
capacityProvider: adminCapacityProvider.capacityProviderName,
base: 1,
weight: 1,
}]
})
Then, I try to do the same thing in ai stack
//at first get cluster from admin stack
export interface SmAiStackProps extends StackProps {
readonly cluster: admin.Cluster;
}
then
//make autoscaling group
const aiAutoScalingGroup = new autoscaling.AutoScalingGroup(this, ai-asg , {
vpc,
instanceType: new ec2.InstanceType( g4dn.xlarge ),
machineImage: ec2.MachineImage.genericLinux({
"ap-northeast-1":"ami-023ec1c000b5029be"
}),
minCapacity:0,
maxCapacity:1,
});
// make capacity provider
const aiCapacityProvider = new ecs.AsgCapacityProvider(this, ai-asg-capacity-provider , {
autoScalingGroup:aiAutoScalingGroup,
});
// add capacity provider.
props!.cluster.addAsgCapacityProvider(aiCapacityProvider);
// add service
const ecsAiService = new ecs.Ec2Service(this, AiService , {
cluster: props!.cluster,
taskDefinition:aiTaskDefinition,
capacityProviderStrategies: [{
capacityProvider: aiCapacityProvider.capacityProviderName,
base: 1,
weight: 1,
}]
})
However now it shows the error when deploying ai stack
throw new Error(` ${target.node.path} depends on ${this.node.path} (${cycleDescription}). Adding this dependency (${reason.description}) would create a cyclic reference.`);
^
Error: sm-dev-ecs depends on ol-dev-ai (sm-dev-ecs -> sm-dev-ai/ai-asg-capacity-provider/ai-asg-capacity-provider.Ref). Adding this dependency (sm-dev-ai -> sm-dev-ecs/sm-dev-Cluster/Resource.Arn) would create a cyclic reference.
I guess this error happens here in ai stack code
// add capacity provider.
props!.cluster.addAsgCapacityProvider(aiCapacityProvider);
The difference is that in admin stack
, cluster is made there, but in ai stack
, cluster is passed from admin stack.
However I have no idea why this makes cyclic reference.
Why this happens?