【问题标题】:Terraform : Specifying the working directory when running terraform apply/planTerraform : 运行 terraform apply/plan 时指定工作目录
【发布时间】:2018-02-24 01:30:40
【问题描述】:

在运行 terraform apply 或 terraform plan 时,是否有任何方法(或解决方法)来指定工作目录? 例如:

terraform apply working_dir/vpc
terraform apply working_dir/dns
terraform apply working_dir/postgres
terraform apply working_dir/service-a
terraform apply working_dir/service-b

我知道有一个目标选项,我们可以在其中指定要目标的资源。但是在这里我需要一个文件夹来进行更多的抽象。 最终目标是能够设置运行以下命令的基础架构:

make vpc
make dns
make postgres
make service-a
make service-b

【问题讨论】:

  • 你可以使用pushd $working_dirpopd
  • pushd 和 popd 在我的用例中不会很有用。即使cd 会更好。
  • 那么为什么不直接使用这个,而不是要求 terraform 中的新功能。如果你认为这是一个有用的功能,terraform 是开源的,它欢迎你贡献代码来添加这个功能。
  • 你为什么不使用一个make文件来实现你想要的功能?

标签: amazon-web-services terraform


【解决方案1】:

这些天你也可以使用全局选项-chdir

chdir 选项指示 Terraform 在运行给定子命令之前将其工作目录更改为给定目录。这意味着 Terraform 通常会在当前工作目录中读取或写入的任何文件将改为在给定目录中读取或写入。

例如:

terraform -chdir=environments/production apply

【讨论】:

    【解决方案2】:

    您可以在发出apply 命令时指定计划或目录。您的示例是有效的应用命令。

    用法:terraform apply [options] [dir-or-plan]

    【讨论】:

      【解决方案3】:

      从具有多个包含 terraform (*.tf) 文件的目录的父目录执行以下命令。

      执行 terraform:

      terraform init dir-name
      terraform validate dir-name
      terraform plan -detailed-exitcode -out=plan-name.out dir-name
      terraform apply plan-name.out
      

      销毁使用:

      terraform destroy dir-name
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-28
        • 2021-03-19
        • 2019-10-13
        • 1970-01-01
        • 2018-11-21
        • 2022-12-19
        • 2021-05-10
        • 2019-12-13
        相关资源
        最近更新 更多