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

如何将Terraform输出作为CircleCI工作流中AWS Orb的输入参数?

问题:如何将Terraform输出值作为CircleCI Orb的输入参数?

搭建Terraform/AWS CI部署的常规流程是用Terraform部署基础设施,CircleCI处理AWS部署。多数示例会手动提取terraform apply的输出并存储到CircleCI环境变量中,但我想直接通过terraform output获取值作为CircleCI Orb的输入参数。

现有配置

我查找了现有Terraform CircleCI Orb,Hashicorp官方Orb不支持output命令,于是自行创建了retrieve-terraform-params Job:

retrieve-terraform-params:
  parameters:
    dir:
      type: string
      default: "."
    service:
      type: string
      default: ""
  executor: python
  resource_class: small
  environment:
    DIR: << parameters.dir >>
  steps:
    - checkout
    - run:
        name: Parse Parameter and write to environment variable 
        command: |
          cd ${DIR}
          sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
          wget -O- https://apt.releases.hashicorp.com/gpg | \
              gpg --dearmor | \
              sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
          gpg --no-default-keyring \
              --keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
              --fingerprint
          echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
              https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
              sudo tee /etc/apt/sources.list.d/hashicorp.list
          sudo apt update
          sudo apt-get install terraform
          terraform init
          terraform output > tf.outputs

我有一个用于AWS部署的CircleCI Orb,需要传入以下参数:

- retrieve-terraform-params:
    dir: ./service/terraform/
- ecs/ecs-deploy:
    name: deploy-stuff
    launch-type: FARGATE
    listener-arn: "arn:..."
    blue-target-group: "arn:..."
    green-target-group: "arn:..."
    requires:
      - retrieve-terraform-params

这些ARN已经在retrieve-terraform-params Job生成的tf.outputs文件中,但我尝试了两种方法都无法将这些值作为第三方Orb的输入参数:

尝试方法1:解析为环境变量(失败)

我尝试将tf.outputs解析为环境变量,然后在工作流中引用,但VAR1、VAR2、VAR3始终为空,推测是CircleCI无法识别Job实例生成的环境变量。

Job命令补充:

...
cat tf.outputs | while read p; 
do IN=$p; 
arrIN=(${IN//=/ }); 
nvar="${arrIN[0]^^}";
export ${nvar}=${arrIN[1]};
echo "export ${nvar}='${arrIN[1]}'";
done

工作流引用方式:

- ecs/ecs-deploy:
    name: deploy-stuff
    launch-type: FARGATE
    listener-arn: ${VAR1}
    blue-target-group: ${VAR2}
    green-target-group: ${VAR3}
    requires:
      - retrieve-terraform-params

尝试方法2:写入流水线参数(失败)

我尝试将tf.outputs解析为CircleCI流水线参数,但发现无法在Job中写入流水线参数,只能读取。

配置示例:

parameters:
  VAR1:
    type: string
    default: ""
...
- ecs/ecs-deploy:
    name: deploy-stuff
    launch-type: FARGATE
    listener-arn: << pipeline.parameters.VAR1 >>
    blue-target-group: << pipeline.parameters.VAR2 >>
    green-target-group: << pipeline.parameters.VAR3 >>
    requires:
      - retrieve-terraform-params

目前我陷入困境,不确定是否需要模仿现有Orb创建新Job,寻求技术帮助。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 17:33:14