I've faced a Circular dependency error. I'm about to create an aws tag, but I don't want to add each time the class instance; instead I've created a function, which iterates over a given object and applies them to tags.
Iterator:
....
ResourceTagger = (this, id, props: cdk.CfnTag[]) => {
props.forEach(tag => {
cdk.Tags.of(this).add(tag.key, tag.value)
})
}
Function Call
....
ResourceTagger(this.vpc, id, [
{
key: "Stack",
value: id,
},
{
key: "Cidr IPv4",
value: this.vpc.vpcCidrBlock,
}
])
Error Message:
❌ Deployment failed: Error: Stack Deployments Failed: ValidationError: Circular dependency between resources:
Is there an efficient solution for this code, as well maybe there are a way without using cdk.CfnTag[]
interface?
CodePudding user response:
The problem is this.vpc.vpcCidrBlock
which you're trying to use within your ResourceTagger
. By default ec2.Vpc construct
, creates many resources, which are not managed unless, if they are explicitly defined.
So in your case, when you adding a Tag some aws resources which are symbol sensitive, throws an error: ValidationError
. If you want to add vpcCidrBlock
try to change to string, and that should help.
As for cdk.CfnTag[]
:
You can try something like this:
ResourceTagger = (this, props: Record<string, string>) => {
Object.entries(props).forEach(([key, value]) => {
cdk.Tags.of(this).add(key, value)
});
ResourceTagger(this, {
"Foo1": "Bar1",
"Foo2": "Bar2",
})