簡體   English   中英

使用提供商在多個區域部署 Terraform 資源

[英]Deploy Terraform resource in multiple regions using providers

我正在嘗試使用提供商跨多個區域部署相同的實例。 文件夾的樹是:

.
|-- main.tf
`-- aws_instance
|   `-- main.tf
`-- versions.tf

main.tf 看起來像:

module "aws_instances" {
  source  = "./aws_instance"
  providers = {
    aws.east1 = aws.east1
    aws.east2 = aws.east2
  }
}

aws_instance/main.tf 看起來像:

resource "aws_instance" "webserver" {
  ami = "webserver-image"
  instance_type = "t2.micro"
  key_name = "EC2-keyPair-Name"
  associate_public_ip_address = true
  root_block_device {
    volume_type = "gp2"
    volume_size = "30"
    delete_on_termination = false
}

versions.tf 看起來像:

terraform {
  required_version = ">= 1.0.4"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 3.23.0"
      configuration_aliases = [aws.east1, aws.east2]
    }
  }
}

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

provider "aws" {
  region  = "us-east-2"
  alias   = "east2"
}

使用當前代碼,我只在一個區域部署了實例。 這里有什么可能出錯的提示嗎?

指定

  providers = {
    aws.east1 = aws.east1
    aws.east2 = aws.east2
  }

不會創建兩個實例。 它只是將兩個別名傳遞給您的模塊。 但是模塊仍然只被調用一次。 由於您無法遍歷提供者,因此您必須明確選擇要為每個實例使用的提供者。 這意味着,代碼重復。 所以你要么必須在模塊中有兩個實例定義:

aws_instance/main.tf

resource "aws_instance" "webserver1" {
  ami = "webserver-image"
  instance_type = "t2.micro"
  key_name = "EC2-keyPair-Name"
  associate_public_ip_address = true
  root_block_device {
    volume_type = "gp2"
    volume_size = "30"
    delete_on_termination = false

  provider = aws.east1
}

resource "aws_instance" "webserver2" {
  ami = "webserver-image"
  instance_type = "t2.micro"
  key_name = "EC2-keyPair-Name"
  associate_public_ip_address = true
  root_block_device {
    volume_type = "gp2"
    volume_size = "30"
    delete_on_termination = false

  provider = aws.east2
}

或者,調用模塊兩次:

主程序

module "aws_instance1" {
  source  = "./aws_instance"
  providers = {
    aws = aws.east1
  }
}

module "aws_instance2" {
  source  = "./aws_instance"
  providers = {
    aws = aws.east2
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM