2 min read

Terraform multi provider - sample -

Terraform multi provider - sample -
Photo by Carrie Borden / Unsplash

$ ~/Developer/learn-terraform-multi-provider
$ tree
.
├── main.tf
└── modules
    └── vpc
        └── main.tf

2 directories, 2 files
terminal
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  required_version = ">= 1.2.0"
}

provider "aws" {
  region = "us-west-2"
}

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

module "multi_provider_vpc" {
  source = "./modules/vpc"
  providers = {
    aws.tokyo = aws.tokyo
  }
}
main.tf
terraform {
  required_providers {
    aws = {
      source                = "hashicorp/aws"
      version               = "~> 4.16"
      configuration_aliases = [aws.tokyo]
    }
  }
}

resource "aws_vpc" "oregon" {
  cidr_block = "10.0.0.0/16"
  tags = {
    Region = "us-west-2"
  }
}

resource "aws_vpc" "tokyo" {
  provider   = aws.tokyo
  cidr_block = "10.0.0.0/16"
  tags = {
    Region = "ap-northeast-1"
  }
}
modules/main.tf

参考

Provider Configuration - Configuration Language | Terraform | HashiCorp Developer
Learn how to set up providers, including how to use the alias meta-argument to specify multiple configurations for a single provider.
The Module providers Meta-Argument - Configuration Language | Terraform | HashiCorp Developer
The providers meta-argument specifies which provider configurations from a parent module are available in a child module.
The Resource provider Meta-Argument - Configuration Language | Terraform | HashiCorp Developer
The provider meta-argument specifies the provider configuration Terraform should use for a resource, overriding Terraform’s default behavior.