You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何借助env_region.tfvars将指定Terraform模块部署至Azure东部区域

实现指定Terraform模块跨双区域部署的方案

1. 调整变量文件(env_region.tfvars)

定义基础部署区域与目标模块的额外部署区域,明确区分范围:

# 原有基础部署区域(美国中部)
base_regions = ["centralus"]
# config_graph_service_module需额外部署的区域(美国东部)
graph_service_extra_regions = ["eastus"]

2. 补充变量声明(variables.tf)

如果当前配置未声明相关变量,添加以下内容:

variable "base_regions" {
  type        = list(string)
  description = "仅部署核心模块的区域列表"
}

variable "graph_service_extra_regions" {
  type        = list(string)
  description = "config_graph_service_module需额外部署的区域"
}

3. 重构main.tf的模块部署逻辑

3.1 核心模块(仅基础区域部署)

对原有的3个非目标模块,通过for_each绑定基础区域列表,限制其仅在美国中部部署:

# 示例:原有模块(如auth_service_module)仅在基础区域部署
module "auth_service_module" {
  for_each = toset(var.base_regions)
  source   = "./modules/auth_service_module"

  region = each.key
  # 其他模块参数(保留原有配置)
}

# 同理处理另外2个非目标模块

3.2 目标模块(跨双区域部署)

合并基础区域与额外区域生成完整部署列表,通过for_each让config_graph_service_module同时部署到两个区域:

# 合并部署区域:基础区域 + 额外区域
locals {
  graph_service_deploy_regions = concat(var.base_regions, var.graph_service_extra_regions)
}

# 部署config_graph_service_module到合并后的所有区域
module "config_graph_service_module" {
  for_each = toset(local.graph_service_deploy_regions)
  source   = "./modules/config_graph_service_module"

  region = each.key
  # 其他模块参数(保留原有配置)
}

4. 执行部署

使用指定变量文件执行Terraform命令:

terraform init
terraform plan -var-file=env_region.tfvars
terraform apply -var-file=env_region.tfvars

内容的提问来源于stack exchange,提问作者Vikash Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 16:25:03