How do you deal with "Cycle" errors?

For some context, we didn’t start with Terraform when our AWS infra was initially set up, but I’m working to get it into Terraform now.

I used Terracognita to do the import with.

At this point, I’m trying to create two new EC2 instances, and I’m running into “Cycle” errors upon doing a plan. (I realize I haven’t put any TF code in here, but I’m not sure what’s needed yet. I’m just trying to create two EC2 instances and specify an existing VPC and subnet to put them into. But on doing a “plan” I get the output below.)

My question is this: when I look at these records, initially, it looks as if things are legit. So I’m not sure what to fix. (or how to fix it)

Error: Cycle: aws_cloudfront_distribution.e1p20bgh67w7tx, aws_route53_record._hostedzone_z2qumuawxd7t3j_cdn_colorbar_mycompany_com__cname
Error: Cycle: aws_cloudfront_distribution.e2onprbybtqzch, aws_route53_record._hostedzone_z2qumuawxd7t3j_s_blog_mycompany_com__a
Error: Cycle: aws_cloudfront_distribution.e2ws917rxmof1l, aws_route53_record._hostedzone_z2qumuawxd7t3j_assets_mycompany_com__cname
Error: Cycle: aws_cloudfront_distribution.e8etcaygb3ucg, aws_route53_record._hostedzone_z2qumuawxd7t3j_s_mycompany_com__a
Error: Cycle: aws_cloudfront_distribution.el4wnnumb9seh, aws_route53_record._hostedzone_z2qumuawxd7t3j_skin_mycompany_com__cname
Error: Cycle: aws_cloudfront_distribution.evzlo4phfotmr, aws_route53_record._hostedzone_z2qumuawxd7t3j_staging_cdn_mycompany_com__cname
Error: Cycle: aws_route53_record._hostedzone_z2qumuawxd7t3j_cdn_blog_mycompany_com__cname, aws_cloudfront_distribution.e1vurobgn3zwit
Error: Cycle: aws_route53_record.hostedzone_z2qumuawxd7t3j_cdn_mycompany_com__cname, aws_cloudfront_distribution.e3vdx0sqhaatpu
Error: Cycle: aws_route53_record.hostedzone_z2qumuawxd7t3j_media_mycompany_com__cname, aws_cloudfront_distribution.e3qkmukc3009oh
Error: Cycle: aws_subnet.production_private_subnet__az2
, aws_db_subnet_group.rds_prod_dbsubnetgroup, aws_db_instance.xoffprd
Error: Cycle: aws_subnet.uat_private_subnet__az1
, aws_db_subnet_group.rds_uat_dbsubnetgroup, aws_db_instance.xoffuat

Terraform requires that it be able to compute a dependency graph without cycles so it can figure out in what order to manipulate resources.

Apparently, Terracognita has written configuration which has A depending on B which depends on A again.

At this point, you basically have two options…

  • You can try to understand and refactor the generated Terraform code to avoid the cycles - a difficult task for someone not already very comfortable with Terraform

  • Or you can decide to use the generated Terraform code only as a loose suggestion of the kinds of things you need, and write your own, manually crafted Terraform code to manage your actual infrastructure. It’ll be quite a bit of work, but should leave you in a far better end position for making future changes to it.

After all, humans do a lot better with meaningfully named concepts, than autogenerated random IDs.