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

如何使用Terraform为单个Apigee API代理创建两个BasePath

解决方案

1. 修正变量定义,用列表存储多BasePath

把重复的单值basepath改成列表类型,解决变量冲突问题:

terraform.tfvars

app_version = "1.0.0"
approval_type = "manual"
basepaths = ["/v1/products", "/v2/products"]

variables.tf(新增/更新变量声明)

variable "basepaths" {
  type        = list(string)
  description = Apigee代理端点的多BasePath列表
}

2. 用for_each循环生成多端点配置

通过Terraform的for_each遍历BasePath列表,为每个路径生成独立的代理端点配置文件:

apigee.tf

# 生成每个BasePath对应的ProxyEndpoint文件
resource "local_file" "proxy_endpoints" {
  for_each = toset(var.basepaths)

  # 根据BasePath生成唯一文件名,比如/v1/products对应default-v1.xml
  filename = "./apiproxy/proxies/default-${split("/", each.value)[1]}.xml"
  content  = templatefile("./templates/proxy_endpoint.tmpl", {
    basepath       = each.value
    endpoint_name  = "default-${split("/", each.value)[1]}"
  })
}

# 生成API代理主配置文件,自动引用所有ProxyEndpoint
resource "local_file" "proxy_main_config" {
  filename = "./apiproxy/proxy.xml"
  content  = templatefile("./templates/proxy_main.tmpl", {
    proxy_name     = "ProductProxy"
    app_version    = var.app_version
    endpoint_names = [for bp in var.basepaths : "default-${split("/", bp)[1]}"]
  })
}

3. 创建配置模板文件

准备两个模板文件,分别生成ProxyEndpoint和代理主配置:

templates/proxy_endpoint.tmpl

<ProxyEndpoint name="${endpoint_name}">
  <PreFlow name="PreFlow">
    <Request/>
    <Response/>
  </PreFlow>
  <Flows/>
  <PostFlow name="PostFlow">
    <Request/>
    <Response/>
  </PostFlow>
  <HTTPProxyConnection>
    <BasePath>${basepath}</BasePath>
    <VirtualHost>secure</VirtualHost>
  </HTTPProxyConnection>
</ProxyEndpoint>

templates/proxy_main.tmpl

<APIProxy name="${proxy_name}" revision="${app_version}">
  <ProxyEndpoints>
    %{ for name in endpoint_names ~}
    <ProxyEndpoint>${name}</ProxyEndpoint>
    %{ endfor ~}
  </ProxyEndpoints>
  <TargetEndpoints>
    <TargetEndpoint>default</TargetEndpoint>
  </TargetEndpoints>
</APIProxy>

4. 效果说明

执行Terraform后,会在./apiproxy/proxies/目录下生成default-v1.xml和default-v2.xml两个端点文件,分别对应/v1/products和/v2/products路径;代理主配置文件proxy.xml会自动引用这两个端点,部署到Apigee后即可同时提供两个BasePath的访问入口。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 08:54:55