Skip to content

Commit

Permalink
Add support for count/each value
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Oct 15, 2022
1 parent fce5684 commit d71268d
Show file tree
Hide file tree
Showing 32 changed files with 1,654 additions and 258 deletions.
68 changes: 36 additions & 32 deletions docs/user-guide/compatibility.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Compatibility with Terraform

TFLint interprets the [Terraform language](https://www.terraform.io/language) with its own parser which is a fork of the Terraform's native one. This allows it to be parsed correctly even if Terraform is not installed at runtime.
TFLint interprets the [Terraform language](https://developer.hashicorp.com/terraform/language) with its own parser which is a fork of the Terraform's native one. This allows it to be parsed correctly even if Terraform is not installed at runtime.

The parser supports Terraform v1.x syntax and semantics. The language compatibility on Terraform v1.x is defined by [Compatibility Promises](https://www.terraform.io/language/v1-compatibility-promises). TFLint follows this promise. New features are only supported in newer TFLint versions, and bug and experimental features compatibility are not guaranteed.
The parser supports Terraform v1.x syntax and semantics. The language compatibility on Terraform v1.x is defined by [Compatibility Promises](https://developer.hashicorp.com/terraform/language/v1-compatibility-promises). TFLint follows this promise. New features are only supported in newer TFLint versions, and bug and experimental features compatibility are not guaranteed.

## Input Variables

Like Terraform, TFLint supports the `--var`,` --var-file` options, environment variables (`TF_VAR_*`), and automatically loading variable definitions (`terraform.tfvars` and `*.auto.tfvars`) files. See [Input Variables](https://www.terraform.io/language/values/variables).
Like Terraform, TFLint supports the `--var`,` --var-file` options, environment variables (`TF_VAR_*`), and automatically loading variable definitions (`terraform.tfvars` and `*.auto.tfvars`) files. See [Input Variables](https://developer.hashicorp.com/terraform/language/values/variables).

Input variables are evaluated just like in Terraform:

Expand Down Expand Up @@ -45,7 +45,7 @@ resource "aws_instance" "foo" {

## Local Values

TFLint supports [Local Values](https://www.terraform.io/language/values/locals).
TFLint supports [Local Values](https://developer.hashicorp.com/terraform/language/values/locals).

```hcl
variable "foo" {
Expand All @@ -65,40 +65,23 @@ local.local # => "static value"
local.resource # => ignored (unknown)
```

## Named Values
## The `count` and `for_each` Meta-Arguments

[Named values](https://www.terraform.io/language/expressions/references) are supported partially. The following named values are available:

- `var.<NAME>`
- `local.<NAME>`
- `path.module`
- `path.root`
- `path.cwd`
- `terraform.workspace`

Unsupported named values (e.g. `count.index`, `each.key`) are ignored:
TFLint supports the [`count`](https://developer.hashicorp.com/terraform/language/meta-arguments/count) and [`for_each`](https://developer.hashicorp.com/terraform/language/meta-arguments/for_each) meta-arguments.

```hcl
resource "aws_instance" "foo" {
count = 3
count = 0
subnet_id = var.subnet_ids[count.index] # => ignored
instance_type = "invalid" # => ignored because ths resource is not created
}
```

## Built-in Functions

[Built-in Functions](https://www.terraform.io/language/functions) are fully supported.

## Conditional Resources/Modules

Resources and modules with [`count = 0`](https://www.terraform.io/language/meta-arguments/count) or [`for_each = {}`](https://www.terraform.io/language/meta-arguments/for_each) are ignored:

```hcl
resource "aws_instance" "foo" {
count = 0
count = 2
instance_type = "invalid" # => ignored
instance_type = "t${count.index}.micro" # => "t0.micro" and "t1.micro"
}
```

Expand All @@ -116,10 +99,31 @@ resource "aws_instance" "foo" {
}
```

## The `path.*` and `terraform.workspace` Values

TFLint supports [filesystem and workspace info](https://developer.hashicorp.com/terraform/language/expressions/references#filesystem-and-workspace-info).

- `path.module`
- `path.root`
- `path.cwd`
- `terraform.workspace`.

## Unsupported Named Values

The values below are state-dependent and cannot be determined statically, so TFLint resolves them to unknown values.

- `<RESOURCE TYPE>.<NAME>`
- `module.<MODULE NAME>`
- `data.<DATA TYPE>.<NAME>`
- `self`

## Built-in Functions

[Built-in Functions](https://developer.hashicorp.com/terraform/language/functions) are fully supported.

## Dynamic Blocks

[Dynamic blocks](https://www.terraform.io/language/expressions/dynamic-blocks
) work just like normal blocks:
[Dynamic blocks](https://developer.hashicorp.com/terraform/language/expressions/dynamic-blocks) work just like normal blocks:

```hcl
resource "aws_instance" "static" {
Expand Down Expand Up @@ -173,6 +177,6 @@ module "aws_instance" {

The following environment variables are supported:

- [TF_VAR_name](https://www.terraform.io/cli/config/environment-variables#tf_var_name)
- [TF_DATA_DIR](https://www.terraform.io/cli/config/environment-variables#tf_data_dir)
- [TF_WORKSPACE](https://www.terraform.io/cli/config/environment-variables#tf_workspace)
- [TF_VAR_name](https://developer.hashicorp.com/terraform/cli/config/environment-variables#tf_var_name)
- [TF_DATA_DIR](https://developer.hashicorp.com/terraform/cli/config/environment-variables#tf_data_dir)
- [TF_WORKSPACE](https://developer.hashicorp.com/terraform/cli/config/environment-variables#tf_workspace)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/sourcegraph/go-lsp v0.0.0-20200429204803-219e11d77f5d
github.com/sourcegraph/jsonrpc2 v0.1.0
github.com/spf13/afero v1.9.2
github.com/terraform-linters/tflint-plugin-sdk v0.13.1-0.20221007143453-76cc99146499
github.com/terraform-linters/tflint-plugin-sdk v0.13.1-0.20221015184526-a26fa34f0348
github.com/terraform-linters/tflint-ruleset-terraform v0.1.1
github.com/xeipuuv/gojsonschema v1.2.0
github.com/zclconf/go-cty v1.11.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/terraform-linters/tflint-plugin-sdk v0.13.1-0.20221007143453-76cc99146499 h1:4abfVUQM2L3teOoJE7asKqzkG/NLvX+F2xuWpqaTeG8=
github.com/terraform-linters/tflint-plugin-sdk v0.13.1-0.20221007143453-76cc99146499/go.mod h1:oYEwDLOQrn3mIUQwiyXPBOnTqO1DcvRPnl/zOqHNu8A=
github.com/terraform-linters/tflint-plugin-sdk v0.13.1-0.20221015184526-a26fa34f0348 h1:jRCoStJUg2ueU/Rq1p3wXGaY0M5YbacY6X5HKG2nelY=
github.com/terraform-linters/tflint-plugin-sdk v0.13.1-0.20221015184526-a26fa34f0348/go.mod h1:oYEwDLOQrn3mIUQwiyXPBOnTqO1DcvRPnl/zOqHNu8A=
github.com/terraform-linters/tflint-ruleset-terraform v0.1.1 h1:dAi25kUMZ3+c1aiQZlP+ifxXnRv+WNpSVSMBroUxeOI=
github.com/terraform-linters/tflint-ruleset-terraform v0.1.1/go.mod h1:+iOphcKeOXXNPqjc3STxHvJ+4km5y8Zo+qqqPLgqFFw=
github.com/ulikunitz/xz v0.5.8 h1:ERv8V6GKqVi23rgu5cj9pVfVzJbOqAY2Ntl88O6c2nQ=
Expand Down
20 changes: 20 additions & 0 deletions integrationtest/inspection/conditional/result.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@
},
"callers": []
},
{
"rule": {
"name": "aws_instance_example_type",
"severity": "error",
"link": ""
},
"message": "instance type is t2.micro",
"range": {
"filename": "template.tf",
"start": {
"line": 61,
"column": 19
},
"end": {
"line": 61,
"column": 29
}
},
"callers": []
},
{
"rule": {
"name": "aws_iam_policy_example",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Modules":[{"Key":"","Source":"","Dir":"."},{"Key":"count","Source":"./module","Dir":"module"},{"Key":"for_each","Source":"./module","Dir":"module"}]}
7 changes: 7 additions & 0 deletions integrationtest/inspection/expand/.tflint.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugin "testing" {
enabled = true
}

plugin "terraform" {
enabled = false
}
41 changes: 41 additions & 0 deletions integrationtest/inspection/expand/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
resource "aws_instance" "count" {
count = 2

instance_type = "t${count.index}.micro"
}

resource "aws_instance" "for_each" {
for_each = {
v1 = "micro"
v2 = "medium"
}

instance_type = "${each.key}.${each.value}"
}

module "count" {
source = "./module"
count = 2

instance_type = "t${count.index}.micro"
}

module "for_each" {
source = "./module"
for_each = {
v1 = "micro"
v2 = "medium"
}

instance_type = "${each.key}.${each.value}"
}

variable "sensitive" {
sensitive = true
}

resource "aws_instance" "sensitive" {
count = 1

instance_type = "${count.index}.${var.sensitive}"
}
5 changes: 5 additions & 0 deletions integrationtest/inspection/expand/module/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "instance_type" {}

resource "aws_instance" "foo" {
instance_type = var.instance_type
}
Loading

0 comments on commit d71268d

Please sign in to comment.