Skip to content

Commit 5f638ee

Browse files
authored
Migrate rules from TFLint core (#1)
1 parent 541469a commit 5f638ee

File tree

65 files changed

+10098
-636
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+10098
-636
lines changed

LICENSE

Lines changed: 373 additions & 0 deletions
Large diffs are not rendered by default.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ build:
88

99
install: build
1010
mkdir -p ~/.tflint.d/plugins
11-
mv ./tflint-ruleset-template ~/.tflint.d/plugins
11+
mv ./tflint-ruleset-terraform ~/.tflint.d/plugins

README.md

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,40 @@
1-
# TFLint Ruleset Template
2-
[![Build Status](https://github.com/terraform-linters/tflint-ruleset-template/workflows/build/badge.svg?branch=main)](https://github.com/terraform-linters/tflint-ruleset-template/actions)
1+
# TFLint Ruleset for Terraform Language
2+
[![Build Status](https://github.com/terraform-linters/tflint-ruleset-terraform/workflows/build/badge.svg?branch=main)](https://github.com/terraform-linters/tflint-ruleset-terraform/actions)
3+
[![GitHub release](https://img.shields.io/github/release/terraform-linters/tflint-ruleset-terraform.svg)](https://github.com/terraform-linters/tflint-ruleset-terraform/releases/latest)
4+
[![License: MPL 2.0](https://img.shields.io/badge/License-MPL%202.0-blue.svg)](LICENSE)
35

4-
This is a template repository for building a custom ruleset. You can create a plugin repository from "Use this template". See also [Writing Plugins](https://github.com/terraform-linters/tflint/blob/master/docs/developer-guide/plugins.md).
6+
TFLint ruleset plugin for Terraform Language
7+
8+
This ruleset focus on possible errors and best practices about Terraform Language.
59

610
## Requirements
711

8-
- TFLint v0.35+
12+
- TFLint v0.40+
913
- Go v1.18
1014

1115
## Installation
1216

13-
You can install the plugin with `tflint --init`. Declare a config in `.tflint.hcl` as follows:
17+
This ruleset is built into TFLint, so you usually don't need to worry about how to install it. You can check the built-in version with `tflint -v`:
18+
19+
```
20+
$ tflint -v
21+
TFLint version 0.40.0
22+
+ ruleset.terraform (0.1.0-bundled)
23+
```
24+
25+
If you want to use a version different from the built-in version, you can declare `plugin` in `.tflint.hcl` as follows and install it with `tflint --init`:
1426

1527
```hcl
16-
plugin "template" {
17-
enabled = true
18-
19-
version = "0.1.0"
20-
source = "github.com/terraform-linters/tflint-ruleset-template"
21-
22-
signing_key = <<-KEY
23-
-----BEGIN PGP PUBLIC KEY BLOCK-----
24-
mQINBGCqS2YBEADJ7gHktSV5NgUe08hD/uWWPwY07d5WZ1+F9I9SoiK/mtcNGz4P
25-
JLrYAIUTMBvrxk3I+kuwhp7MCk7CD/tRVkPRIklONgtKsp8jCke7FB3PuFlP/ptL
26-
SlbaXx53FCZSOzCJo9puZajVWydoGfnZi5apddd11Zw1FuJma3YElHZ1A1D2YvrF
27-
...
28-
KEY
28+
plugin "terraform" {
29+
enabled = true
30+
version = "0.1.0"
31+
source = "github.com/terraform-linters/tflint-ruleset-terraform"
2932
}
3033
```
3134

3235
## Rules
3336

34-
|Name|Description|Severity|Enabled|Link|
35-
| --- | --- | --- | --- | --- |
36-
|aws_instance_example_type|Example rule for accessing and evaluating top-level attributes|ERROR|||
37-
|aws_s3_bucket_example_lifecycle_rule|Example rule for accessing top-level/nested blocks and attributes under the blocks|ERROR|||
38-
|google_compute_ssl_policy|Example rule with a custom rule config|WARNING|||
39-
|terraform_backend_type|Example rule for accessing other than resources|ERROR|||
37+
See [Rules](docs/rules/README.md).
4038

4139
## Building the plugin
4240

@@ -51,3 +49,11 @@ You can easily install the built plugin with the following:
5149
```
5250
$ make install
5351
```
52+
53+
Note that if you install the plugin with `make install`, you must omit the `version` and `source` attributes in` .tflint.hcl`:
54+
55+
```hcl
56+
plugin "terraform" {
57+
enabled = true
58+
}
59+
```

docs/rules/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Rules
2+
3+
Terraform language rules implement recommendations from the [Terraform language documentation](https://www.terraform.io/language). If you want to enforce additional usage and style conventions in your configuration, you can author your own ruleset plugin.
4+
5+
Below is a list of available rules.
6+
7+
|Rule|Description|Enabled|
8+
| --- | --- | --- |
9+
|[terraform_comment_syntax](terraform_comment_syntax.md)|Disallow `//` comments in favor of `#`||
10+
|[terraform_deprecated_index](terraform_deprecated_index.md)|Disallow legacy dot index syntax||
11+
|[terraform_deprecated_interpolation](terraform_deprecated_interpolation.md)|Disallow deprecated (0.11-style) interpolation||
12+
|[terraform_documented_outputs](terraform_documented_outputs.md)|Disallow `output` declarations without description||
13+
|[terraform_documented_variables](terraform_documented_variables.md)|Disallow `variable` declarations without description||
14+
|[terraform_empty_list_equality](terraform_empty_list_equality.md)|Disallow comparisons with `[]` when checking if a collection is empty||
15+
|[terraform_module_pinned_source](terraform_module_pinned_source.md)|Disallow specifying a git or mercurial repository as a module source without pinning to a version||
16+
|[terraform_module_version](terraform_module_version.md)|Checks that Terraform modules sourced from a registry specify a version||
17+
|[terraform_naming_convention](terraform_naming_convention.md)|Enforces naming conventions for resources, data sources, etc||
18+
|[terraform_required_providers](terraform_required_providers.md)|Require that all providers have version constraints through required_providers||
19+
|[terraform_required_version](terraform_required_version.md)|Disallow `terraform` declarations without require_version||
20+
|[terraform_standard_module_structure](terraform_standard_module_structure.md)|Ensure that a module complies with the Terraform Standard Module Structure||
21+
|[terraform_typed_variables](terraform_typed_variables.md)|Disallow `variable` declarations without type||
22+
|[terraform_unused_declarations](terraform_unused_declarations.md)|Disallow variables, data sources, and locals that are declared but never used||
23+
|[terraform_unused_required_providers](terraform_unused_required_providers.md)|Check that all `required_providers` are used in the module||
24+
|[terraform_workspace_remote](terraform_workspace_remote.md)|`terraform.workspace` should not be used with a "remote" backend with remote execution||
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# terraform_comment_syntax
2+
3+
Disallow `//` comments in favor of `#`.
4+
5+
## Example
6+
7+
```hcl
8+
# Good
9+
// Bad
10+
```
11+
12+
```
13+
$ tflint
14+
1 issue(s) found:
15+
16+
Warning: Single line comments should begin with # (terraform_comment_syntax)
17+
18+
on main.tf line 2:
19+
2: // Bad
20+
21+
Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_comment_syntax.md
22+
```
23+
24+
## Why
25+
26+
The Terraform language supports two different syntaxes for single-line comments: `#` and `//`. However, `#` is the default comment style and should be used in most cases.
27+
28+
* [Configuration Syntax: Comments](https://www.terraform.io/docs/configuration/syntax.html#comments)
29+
30+
## How To Fix
31+
32+
Replace the leading double-slash (`//`) in your comment with the number sign (`#`).
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# terraform_deprecated_index
2+
3+
Disallow legacy dot index syntax.
4+
5+
## Example
6+
7+
```hcl
8+
locals {
9+
list = ["a", "b", "c"]
10+
value = list.0
11+
}
12+
```
13+
14+
```
15+
$ tflint
16+
1 issue(s) found:
17+
18+
Warning: List items should be accessed using square brackets (terraform_deprecated_index)
19+
20+
on example.tf line 3:
21+
3: value = list.0
22+
23+
Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_deprecated_index.md
24+
```
25+
26+
## Why
27+
28+
Terraform v0.12 supports traditional square brackets for accessing list items by index. However, for backward compatability with v0.11, Terraform continues to support accessing list items with the dot syntax normally used for attributes. While Terraform does not print warnings for this syntax, it is no longer documented and its use is discouraged.
29+
30+
## How To Fix
31+
32+
Switch to the square bracket syntax when accessing items in list, including resources that use `count`.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# terraform_deprecated_interpolation
2+
3+
Disallow deprecated (0.11-style) interpolation
4+
5+
## Example
6+
7+
```hcl
8+
resource "aws_instance" "deprecated" {
9+
instance_type = "${var.type}"
10+
}
11+
12+
resource "aws_instance" "new" {
13+
instance_type = var.type
14+
}
15+
```
16+
17+
```
18+
$ tflint
19+
1 issue(s) found:
20+
21+
Warning: Interpolation-only expressions are deprecated in Terraform v0.12.14 (terraform_deprecated_interpolation)
22+
23+
on example.tf line 2:
24+
2: instance_type = "${var.type}"
25+
26+
Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_deprecated_interpolation.md
27+
28+
```
29+
30+
## Why
31+
32+
Terraform v0.12 introduces a new interpolation syntax, but continues to support the old 0.11-style interpolation syntax for compatibility.
33+
34+
Terraform will currently print diagnostic warnings when deprecated interpolations are used. Consistent with its deprecation policy, they will raise errors in the next major release (v0.13). TFLint emits an issue instead of a warning with the same logic.
35+
36+
## How To Fix
37+
38+
Switch to the new interpolation syntax. See the release notes for Terraform 0.12.14 for details: https://github.com/hashicorp/terraform/releases/tag/v0.12.14
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# terraform_documented_outputs
2+
3+
Disallow `output` declarations without description.
4+
5+
## Example
6+
7+
```hcl
8+
output "no_description" {
9+
value = "value"
10+
}
11+
12+
output "empty_description" {
13+
value = "value"
14+
description = ""
15+
}
16+
17+
output "description" {
18+
value = "value"
19+
description = "This is description"
20+
}
21+
```
22+
23+
```
24+
$ tflint
25+
2 issue(s) found:
26+
27+
Notice: `no_description` output has no description (terraform_documented_outputs)
28+
29+
on template.tf line 1:
30+
1: output "no_description" {
31+
32+
Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_documented_outputs.md
33+
34+
Notice: `empty_description` output has no description (terraform_documented_outputs)
35+
36+
on template.tf line 5:
37+
5: output "empty_description" {
38+
39+
Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_documented_outputs.md
40+
41+
```
42+
43+
## Why
44+
45+
Since `description` is optional value, it is not always necessary to write it. But this rule is useful if you want to force the writing of description. Especially it is useful when combined with [terraform-docs](https://github.com/segmentio/terraform-docs).
46+
47+
## How To Fix
48+
49+
Write a description other than an empty string.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# terraform_documented_variables
2+
3+
Disallow `variable` declarations without description.
4+
5+
## Example
6+
7+
```hcl
8+
variable "no_description" {
9+
default = "value"
10+
}
11+
12+
variable "empty_description" {
13+
default = "value"
14+
description = ""
15+
}
16+
17+
variable "description" {
18+
default = "value"
19+
description = "This is description"
20+
}
21+
```
22+
23+
```
24+
$ tflint
25+
2 issue(s) found:
26+
27+
Notice: `no_description` variable has no description (terraform_documented_variables)
28+
29+
on template.tf line 1:
30+
1: variable "no_description" {
31+
32+
Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_documented_variables.md
33+
34+
Notice: `empty_description` variable has no description (terraform_documented_variables)
35+
36+
on template.tf line 5:
37+
5: variable "empty_description" {
38+
39+
Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_documented_variables.md
40+
41+
```
42+
43+
## Why
44+
45+
Since `description` is optional value, it is not always necessary to write it. But this rule is useful if you want to force the writing of description. Especially it is useful when combined with [terraform-docs](https://github.com/segmentio/terraform-docs).
46+
47+
## How To Fix
48+
49+
Write a description other than an empty string.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# terraform_empty_list_equality
2+
3+
Disallow comparisons with `[]` when checking if a collection is empty.
4+
5+
## Example
6+
7+
```hcl
8+
variable "my_list" {
9+
type = list(string)
10+
}
11+
resource "aws_db_instance" "mysql" {
12+
count = var.my_list == [] ? 0 : 1
13+
instance_class = "m4.2xlarge"
14+
}
15+
```
16+
17+
```
18+
$ tflint
19+
1 issue(s) found:
20+
21+
Warning: Comparing a collection with an empty list is invalid. To detect an empty collection, check its length. (terraform_empty_list_equality)
22+
23+
on test.tf line 5:
24+
5: count = var.my_list == [] ? 0 : 1
25+
26+
Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_empty_list_equality.md
27+
28+
```
29+
30+
## Why
31+
32+
The `==` operator can only return true when the two operands have identical types, and the type of `[]` alone (without any further type conversions) is an empty tuple rather than a list of objects, strings, numbers or any other type. Therefore, a comparison with a single `[]` with the goal of checking if a collection is empty, will always return false.
33+
34+
## How To Fix
35+
36+
Check if a collection is empty by checking its length instead. For example: `length(var.my_list) == 0`.

0 commit comments

Comments
 (0)