Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Provider aliases #331

Closed
suzuki-shunsuke opened this issue Apr 4, 2022 · 3 comments
Closed

Support Provider aliases #331

suzuki-shunsuke opened this issue Apr 4, 2022 · 3 comments

Comments

@suzuki-shunsuke
Copy link
Contributor

suzuki-shunsuke commented Apr 4, 2022

Problem

Terraform supports defining multiple configurations for the same provider, and select which one to use on a per-resource or per-module basis.

https://www.terraform.io/language/providers/configuration#alias-multiple-provider-configurations

But tflint doesn't support this, so even if multiple configurations are defined only one configuration is used.
This raises a problem in Deep Checking.

How to reproduce

$ tflint -v
TFLint version 0.35.0
+ ruleset.aws (0.13.2)

.tflint.hcl

plugin "aws" {
  enabled = true
  version = "0.13.2"
  source  = "github.com/terraform-linters/tflint-ruleset-aws"

  deep_check = true
}

main.tf

terraform {
  required_version = ">= 0.15"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "4.0.0"
    }
  }
}

provider "aws" {
  region = "ap-northeast-1"
}

provider "aws" {
  region = "us-east-1"
  alias  = "us-east-1"
}

resource "aws_instance" "reverse_proxy_a01" {
  ami           = "ami-088da9557aae42f39"
  instance_type = "t3.micro"
  key_name      = "bootstrap"
}
$ tflint
2 issue(s) found:

Error: "ami-088da9557aae42f39" is invalid AMI ID. (aws_instance_invalid_ami)

  on main.tf line 21:
  21:   ami           = "ami-088da9557aae42f39"

Error: "bootstrap" is invalid key name. (aws_instance_invalid_key_name)

  on main.tf line 23:
  23:   key_name      = "bootstrap"

This error occurs even if Key Pair and AMI exist in the region ap-northeast-1, because tflint gets resources from us-east-1 using the alias us-east-1.

Solution

  • Change Runner.AwsClient to map map[string]*Client and creates clients per Provider Configuration
  • Check resource's provider attribute and get the appropriate Client from map of clients

I tried to implement this, but it doesn't work well yet.

#332

I'll appreciate if you help me.
It is difficult to debug because I don't know how to output the plugin log.

Reference

@wata727
Copy link
Member

wata727 commented Apr 4, 2022

It is difficult to debug because I don't know how to output the plugin log.

You can use a custom logger for debugging.
https://pkg.go.dev/github.com/terraform-linters/tflint-plugin-sdk@v0.10.1/logger

Logs via this logger are forwarded to TFLint. Depending on the log level, you can filter by TFLINT_LOG.

@suzuki-shunsuke
Copy link
Contributor Author

Oh, I see. Thank you. I can see the log.

@suzuki-shunsuke
Copy link
Contributor Author

Solved by #342

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants