Skip to content

Support Provider aliases #331

Description

@suzuki-shunsuke

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions