We are a DevOps Team doing a lot with IaC Terraform with a heavy engineering background in java/python.
We currently evaluate Sonarqube (still Community Version, on EKS with Helm) and are quite impressed.
I used to see duplication metrics for code, but except for scala I dont see any for our TF Code.
I played with sonar.cpd.terraform.minimumtokens and … …Lines. But from the log perspective I dont see any ‘DEBUG: Detection of duplications for …’ for terraform files.
As example let me describe the context we work in to understand the problem.
We implement “datalakes” in AWS consisting of multiple data pipelines.
Each data pipeline
gets data from somewhere (S3/…)
mangle the data (Glue/Lambda/…)
places the results to somewhere (S3/RDS/…).
One can imagine when it comes to routine implementations one way to proceed is to copy and adapt (adjusting the terraform state, change a base name and implement the logic in a lambda). In an oversimplified example could it be the copy 10 files and change 3 lines and have another 300 lines duplicate code.
Hi,
I have just started evaluating the use of sonarqube for detecting duplicate terraform code and am disappointed to discover that this is currently not supported.
Please provide us some examples where you expect copy-paste detection, so we can better understand your case and think about it again.
I would like to add an example of where this would be useful in my organisation.
Its generally good practise to write terraform code that is agnostic of the environment that it will be applied against. One way this can be accomplished by having a variable:
and that variable can be referred to in terraform resource blocks.
Another way is by using for_each arguments.
Sadly in our organisation we have lots of terraform code and much of it doesn’t exhibit these simple good practises. Hence we have code like the following: