Answers: <p>The tools that we have include in our Terraform pipelines to check code quality and best practises are;</p>
  <ol>
  <li><a href="https://github.com/terraform-linters/tflint" rel="noreferrer">Tflint</a></li>
  <li><a href="https://github.com/tfsec/tfsec" rel="noreferrer">TFSec</a></li>
  <li><a href="https://www.checkov.io/" rel="noreferrer">Checkov</a></li>
  </ol>
  <p>This way any issues get highlighted early at the MR stage.</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-07-13T10:39:48.450Z
Answers: this is the correct answer
Answers: krystan honour
Answers: CC BY-SA 4.0
Answers: 2021-07-13T10:41:31.723Z
Answers: you can also hook all these up with pre-commit, which is a really nice way to check a commit each time :)
Answers: krystan honour
Answers: CC BY-SA 4.0
Answers: 2021-05-13T10:22:10.310Z
Answers: 2021-05-13T10:22:10.310Z
Answers: Alastair Montgomery
Answers: <p><a href="https://www.terraform.io/docs/commands/fmt.html" rel="noreferrer"><code>terraform fmt</code></a></p>
  <blockquote>
  <p>The <code>terraform fmt</code> command is used to rewrite Terraform configuration files to a canonical format and style.</p>
  </blockquote>
  
Answers: CC BY-SA 4.0
Answers: 2020-12-06T21:34:16.460Z
Answers: I already use that.
Answers: doug
Answers: CC BY-SA 4.0
Answers: 2020-12-06T21:46:09.587Z
Answers: Thanks anyway though.
  
  I'm after something to spell check for me.
  
  I use terraform fmt to formate
  run terraform plan to check it should run.
  
  But for spelling, I currently do git compare with branch then look through each bit I changed and copy all my comments and variable names I'm not 100% sure about to word and change any seportors for spaces then see if anything gets highlighted.
  It's just really not a time-efficient process for that part of it.
Answers: doug
Answers: CC BY-SA 4.0
Answers: 2020-12-09T21:00:07.627Z
Answers: @doug it might be easier using an IDE that has spell check (or a plugin thereof).
Answers: mikequentel
Answers: CC BY-SA 4.0
Answers: 2020-12-11T08:06:32.767Z
Answers: The issue I had when I last tried that in VS code was the spell check plugin I used thought all the resources were misspelled as it couldn't pick out words separated by underscores.
  
  So I need it to treat   -   .  and _ as if they were spaces so it can tell if the words separated by them are spelled correctly.
Answers: doug
Answers: CC BY-SA 4.0
Answers: 2021-07-13T10:39:33.377Z
Answers: Sorry but this is not linting, this is putting something in canonical format as stated but it is not a linter
Answers: krystan honour
Answers: CC BY-SA 4.0
Answers: 2020-12-02T19:58:35.500Z
Answers: 2020-12-02T19:58:35.500Z
Answers: mikequentel
Answers: <p>VSCode has a Terraform Linter &quot;l2fprod.terraform-fork&quot;
  Have NOT tried it.  Did a simple search terra + lint.
  HTH
  DrewBai</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-12-01T18:56:35.883Z
Answers: 2020-12-01T18:56:35.883Z
Answers: DrewBai
Answers: <p>You can use tflint project. It has lot of plugins for cloud providers.
  <a href="https://github.com/terraform-linters/tflint" rel="nofollow noreferrer">https://github.com/terraform-linters/tflint</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2021-04-02T08:04:39.020Z
Answers: 2021-04-02T08:04:39.020Z
Answers: Oleksii Molchanov
Answers: <p>To go further and actually show what the change will do, after using linting as an initial gate (as other answers have mentioned how to do), I found what worked well as a quality gate was to perform a terraform plan (on a particular workspace that had the latest resources) without locking the state, e.g, using <code>-lock=false</code>. This tested that what an actual pull request would actually change and would not fail if there is a syntax error but also show to myself and whoever is reviewing the PR what the change would result in.</p>
  <p>I also found if you want to lint a whole group of terraform workspaces a helpful tool is <code>terragrunt</code>. Running <code>terragrunt run-all validate</code> could validate all the workspaces that had a <code>terragrunt.hcl</code> file in it in 1 command.</p>
  <p>I used this even before I passed my pull request onto others, creating a pull request early, pushing the code and let the pipeline validate things in the background while I'm also checking I have the best I could do at the same time.</p>
  
Answers: CC BY-SA 4.0
Answers: 2024-07-23T21:47:35.047Z
Answers: 2024-07-23T21:47:35.047Z
Answers: Samuel Garratt
Body: <p>I'm hoping there is something to pick up when I have misspelled variables and comments to save me some time when I'm checking my code and reduce the potential for any errors.</p>
  <p>I normally use IntelliJ IDE for Terraform on a MAC but also have VS code set up that I can use.</p>
  <p>Is there a plugin for either I could use or even a different tool?
  I imaging variable might be difficult to lint due as I use underscores to separate words in my variable names so the tool would need to know to treat underscores as white space for linting purposes.</p>
  
Comments: CC BY-SA 4.0
Comments: 2020-11-30T16:39:34.413Z
Comments: Have you tried the Hashicorp Terraform plugin for VSCode? https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform
Comments: Bruce Becker
Comments: CC BY-SA 4.0
Comments: 2024-06-02T02:28:42.107Z
Comments: Others have mentioned TFLint on Github and it's great, but if you want to get the lint output directly in your IDE, then you'll need to do some additional setup. This will help: https://blog.james-mullen.me.uk/2022-02-15-tflint-intellij/
Comments: shadowz1337
Tags: <terraform><linting>
Title: Is there a way to lint Terraform?
UserDisplayName: doug
ThreadText: # Is there a way to lint Terraform?
  
  (Asked by: username_0 on 2020-11-30)
  
  I'm hoping there is something to pick up when I have misspelled variables and comments to save me some time when I'm checking my code and reduce the potential for any errors.
  
  I normally use IntelliJ IDE for Terraform on a MAC but also have VS code set up that I can use.
  
  Is there a plugin for either I could use or even a different tool?
  I imaging variable might be difficult to lint due as I use underscores to separate words in my variable names so the tool would need to know to treat underscores as white space for linting purposes.
  
  **Tags:** terraform, linting
  
  > **Comments:**
  > **username_1**: Have you tried the Hashicorp Terraform plugin for VSCode? https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform  
  > **username_2**: Others have mentioned TFLint on Github and it's great, but if you want to get the lint output directly in your IDE, then you'll need to do some additional setup. This will help: https://blog.james-mullen.me.uk/2022-02-15-tflint-intellij/  
  
  ---
  
  ## 5 answers
  
  ### Answer by username_5 on 2020-12-02. Score: 13
  
  [`terraform fmt`](https://www.terraform.io/docs/commands/fmt.html)
  
  > The `terraform fmt` command is used to rewrite Terraform configuration files to a canonical format and style.
  
  > **Comments:**
  > **username_0**: I already use that.  
  > **username_0**: Thanks anyway though.  I'm after something to spell check for me.  I use terraform fmt to formate run terraform plan to check it should run.  But for spelling, I currently do git compare with branch then look through each bit I changed and copy all my comments and variable names I'm not 100% sure about to word and change any seportors for spaces then see if anything gets highlighted. It's just really not a time-efficient process for that part of it.  
  > **username_5**: @username_0 it might be easier using an IDE that has spell check (or a plugin thereof).  
  > **username_0**: The issue I had when I last tried that in VS code was the spell check plugin I used thought all the resources were misspelled as it couldn't pick out words separated by underscores.  So I need it to treat   -   .  and _ as if they were spaces so it can tell if the words separated by them are spelled correctly.  
  > **username_4**: Sorry but this is not linting, this is putting something in canonical format as stated but it is not a linter  
  
  ---
  
  ### Answer by username_3 on 2021-05-13. Score: 6
  
  The tools that we have include in our Terraform pipelines to check code quality and best practises are;
  
  1. [Tflint](https://github.com/terraform-linters/tflint)
  2. [TFSec](https://github.com/tfsec/tfsec)
  3. [Checkov](https://www.checkov.io/)
  
  This way any issues get highlighted early at the MR stage.
  
  > **Comments:**
  > **username_4**: this is the correct answer  
  > **username_4**: you can also hook all these up with pre-commit, which is a really nice way to check a commit each time :)  
  
  ---
  
  ### Answer by username_6 on 2020-12-01. Score: 1
  
  VSCode has a Terraform Linter "l2fprod.terraform-fork"
  Have NOT tried it. Did a simple search terra + lint.
  HTH
  username_6
  
  
  ---
  
  ### Answer by username_7 on 2021-04-02. Score: 1
  
  You can use tflint project. It has lot of plugins for cloud providers.
  <https://github.com/terraform-linters/tflint>
  
  
  ---
  
  ### Answer by username_8 on 2024-07-23. Score: 0
  
  To go further and actually show what the change will do, after using linting as an initial gate (as other answers have mentioned how to do), I found what worked well as a quality gate was to perform a terraform plan (on a particular workspace that had the latest resources) without locking the state, e.g, using `-lock=false`. This tested that what an actual pull request would actually change and would not fail if there is a syntax error but also show to myself and whoever is reviewing the PR what the change would result in.
  
  I also found if you want to lint a whole group of terraform workspaces a helpful tool is `terragrunt`. Running `terragrunt run-all validate` could validate all the workspaces that had a `terragrunt.hcl` file in it in 1 command.
  
  I used this even before I passed my pull request onto others, creating a pull request early, pushing the code and let the pipeline validate things in the background while I'm also checking I have the best I could do at the same time.
  
  
  ---
  
  