【问题标题】:I would like to run terraform only for a specific resource我只想为特定资源运行 terraform
【发布时间】:2018-03-27 11:40:48
【问题描述】:

运行 terraform 并等待需要很长时间。 所以我想运行它来排除执行时间最长的 rds 或者我只想运行 ec2 资源。 有没有办法在 terraform 中做这些事情?

【问题讨论】:

  • 请重命名问题:“我只想为特定资源运行 terraform”
  • 关于标题中的问题(接受的答案未解决),Terraform 问题跟踪器中有此功能请求:github.com/hashicorp/terraform/issues/2253 关于该问题的一个建议是:'采取"terraform plan list" 的输​​出,grep 出我想保留的所有资源,然后使用 shell 脚本从其余资源中创建一个 -target 参数列表。'

标签: terraform


【解决方案1】:

你可以像这样使用-target=resource

terraform plan -target=module.mymodule.aws_instance.myinstance
terraform apply -target=module.mymodule.aws_instance.myinstance

terraform plan -target=aws_instance.myinstance
terraform apply -target=aws_instance.myinstance

免责声明:在否决答案之前,请注意他实际上要求“排除”或“仅运行 ec2 资源”。在这之后,exclude feature request 仍然在 terraform 存储库中打开。

【讨论】:

  • 感谢您的评论。我使用你教我的 -target 选项解决了它
  • 没问题,如果对你有帮助,请采纳。
  • 但这并不是排除。这种包容。如果您只需要排除 1 个资源但包含其他 99 个资源....那么这很乏味
  • 如前所述 - 这与提问者所问的相反。如果要排除特定资源,一种方法是使用“terraform rm state resource.key”将其从状态中删除
  • 在否决答案之前,请注意他实际上要求“排除”或“仅运行 ec2 资源”。在那之后,排除功能请求仍然在 terraform repo github.com/hashicorp/terraform/issues/2253 中打开。
【解决方案2】:

添加到 Julio 的 answer,您可以通过以下方式定位多个资源:

terraform init
terraform plan -target=resource_type1.resource_name1 -target=resource_type2.resource_name1
terraform apply -target=resource_type1.resource_name1 -target=resource_type2.resource_name1

【讨论】:

    【解决方案3】:

    我想运行它以排除耗时最长的 rds

    Terraform 目前不支持排除资源(也称为反向定位)。

    Issue #2253: "feature request: inverse targeting / exclude"

    (感谢 Julio Daniel Reyes 的链接。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-09
      • 2021-01-08
      • 2022-01-12
      • 1970-01-01
      • 2020-06-28
      • 2019-07-22
      • 1970-01-01
      • 2019-11-30
      相关资源
      最近更新 更多