Difference between revisions of "Terraform multiple providers"

From wikieduonline
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 4: Line 4:
  
  
  # The default provider configuration; resources that begin with `aws_` will use
+
  # 1st provider configuration; resources that begin with `aws_` will use it as the default, and it can be referenced as `aws`.
# it as the default, and it can be referenced as `aws`.
 
 
  provider "aws" {
 
  provider "aws" {
 
   region = "us-east-1"
 
   region = "us-east-1"
 
  }
 
  }
 
   
 
   
  # Additional provider configuration for west coast region; resources can
+
  # 2nd provider configuration for other region ; resources can reference this as `aws.mywestregion`.
# reference this as `aws.west`.
 
 
  provider "aws" {
 
  provider "aws" {
   alias  = "west"
+
   alias  = "mywestregion"
 
   region = "us-west-2"
 
   region = "us-west-2"
 
  }
 
  }
Line 32: Line 30:
 
   # ...
 
   # ...
 
  }
 
  }
 +
 +
== Errors ==
 +
* [[Error: Duplicate provider configuration]]
  
 
== Related ==
 
== Related ==
  
* [[Terraform providers]]
+
* [[Terraform providers: provider|Terraform providers]]
 
* <code>[[alias]]</code>
 
* <code>[[alias]]</code>
 
* [[Terraform Associate]]
 
* [[Terraform Associate]]
 +
* [[AWS Roles]]: <code>[[OrganizationAccountAccessRole]]</code>
  
 
== See also ==
 
== See also ==

Latest revision as of 20:13, 5 April 2023

You can optionally define multiple configurations for the same provider, and select which one to use on a per-resource or per-module basis. [1]

Example[edit]

# 1st provider configuration; resources that begin with `aws_` will use it as the default, and it can be referenced as `aws`.
provider "aws" {
  region = "us-east-1"
}

# 2nd provider configuration for other region ; resources can reference this as `aws.mywestregion`.
provider "aws" {
  alias  = "mywestregion"
  region = "us-west-2"
}

terraform {
  required_providers {
    mycloud = {
      source  = "mycorp/mycloud"
      version = "~> 1.0"
      configuration_aliases = [ mycloud.alternate ]
    }
  }
}

resource "aws_instance" "foo" {
  provider = aws.west

  # ...
}

Errors[edit]

Related[edit]

See also[edit]

  • https://www.terraform.io/language/providers/configuration
  • Advertising: