在 Terraform 0.12 中,如果资源名称已经存在,如何跳过资源的创建? [英] In Terraform 0.12, how to skip creation of resource, if resource name already exists?

查看:31
本文介绍了在 Terraform 0.12 中,如果资源名称已经存在,如何跳过资源的创建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Terraform 0.12 版.如果已经存在同名资源,我需要跳过资源创建.

我为此做了以下事情:

读取自定义图片列表,

数据ibm_is_images"custom_images"{}

检查图像是否已经存在,

本地人{custom_vsi_image = contains([for x in data.ibm_is_images.custom_images.images: "true" if x.visibility == "private" && x.name == var.vnf_vpc_image_name], "true")}输出abc"{value="${local.custom_vsi_image}"}

仅当图像存在为假时才创建.

资源ibm_is_image"custom_image"{计数 = "${local.custom_vsi_image == true ? 0 : 1}"depends_on = ["data.ibm_is_images.custom_images"]href = "${local.image_url}"名称 = "${var.vnf_vpc_image_name}"操作系统 = "centos-7-amd64"超时{创建=30m"删除 = "10m"}}

第一次使用terraform apply"可以正常工作.它发现图像不存在,因此它创建图像.

当我第二次运行terraform apply"时.它正在删除上面创建的资源custom_image".知道为什么它会在第二次运行时删除资源吗?

另外,如何根据某些条件(比如不存在时)创建资源?

解决方案

在 Terraform 中,您需要明确决定哪个系统负责管理特定对象,反之,哪些系统只是消费 一个现有的对象.没有办法动态地做出该决定,因为这会使结果不确定,并且 - 对于 Terraform 管理的对象 - 不清楚哪个配置的 terraform destroy 会破坏该对象.p>

确实,这种不确定性是为什么您在尝试创建资源和尝试删除资源之间看到 Terraform 失败的原因:您告诉 Terraform 仅在该对象不存在时才管理该对象,因此,在 Terraform 存在后第一次运行 Terraform 时,Terraform 将看到该对象不再被管理,因此它会计划销毁它.

<小时>

如果您的目标是使用 Terraform 管理所有内容,那么一项重要的设计任务是决定对象依赖关系如何在 Terraform 配置内部和之间流动.在您的情况下,管理图像的系统(可能是也可能不是 Terraform 配置)与一个或多个使用现有图像的 Terraform 配置之间似乎存在生产者/消费者关系.

如果图像由 Terraform 管理,那么这表明您的主要 Terraform 配置应该假设图像确实存在并无条件地创建它 - 如果您的决定是图像由与使用它的系统相同 - 或者它应该假设图像 确实 已经存在并使用 data 块检索有关它的信息.

这里一个可能的解决方案是编写一个单独的 Terraform 配置来管理图像,然后仅在该对象预计不存在的情况下应用该配置.然后,使用现有图像的配置可以假设它存在,而不关心它是否是由其他 Terraform 配置创建的.

Terraform 文档部分模块组合对这种情况有更详细的概述,特别是小节 有条件地创建对象.该指南侧重于单一配置中模块之间的交互,但同样的基本原则也适用于配置之间配置(通过数据源)的依赖关系.

I am using Terraform version 0.12. I have a requirement to skip resource creation if resource with the same name already exists.

I did the following for this :

Read the list of custom images,

data "ibm_is_images" "custom_images" {
}

Check if image already exists,

locals {
 custom_vsi_image = contains([for x in data.ibm_is_images.custom_images.images: "true" if x.visibility == "private" && x.name == var.vnf_vpc_image_name], "true")
}

output "abc" {
value="${local.custom_vsi_image}"
}

Create only if image exists is false.

resource "ibm_is_image" "custom_image" {
  count            = "${local.custom_vsi_image == true ? 0 : 1}"
  depends_on       = ["data.ibm_is_images.custom_images"]
  href             = "${local.image_url}"
  name             = "${var.vnf_vpc_image_name}"
  operating_system = "centos-7-amd64"

  timeouts {
    create = "30m"
    delete = "10m"
  }
}

This works fine for the first time with "terraform apply". It finds that the image did not exists, so it creates image.

When I run "terraform apply" for the second time. It is deleting the resource "custom_image" that is created above. Any idea why it is deleting the resource, when it is run for the 2nd time ?

Also, how to create a resource based on some condition(like only when it does not exists) ?

解决方案

In Terraform, you're required to decide explicitly what system is responsible for the management of a particular object, and conversely which systems are just consuming an existing object. There is no way to make that decision dynamically, because that would make the result non-deterministic and -- for objects managed by Terraform -- make it unclear which configuration's terraform destroy would destroy the object.

Indeed, that non-determinism is why you're seeing Terraform in your situation flop between trying to create and then trying to delete the resource: you've told Terraform to only manage that object if it doesn't already exist, and so the first time you run Terraform after it exists Terraform will see that the object is no longer managed and so it will plan to destroy it.


If you goal is to manage everything with Terraform, an important design task is to decide how object dependencies flow within and between Terraform configurations. In your case, it seems like there is a producer/consumer relationship between a system that manages images (which may or may not be a Terraform configuration) and one or more Terraform configurations that consume existing images.

If the images are managed by Terraform then that suggests either that your main Terraform configuration should assume the image does not exist and unconditionally create it -- if your decision is that the image is owned by the same system as what consumes it -- or it should assume that the image does already exist and retrieve the information about it using a data block.

A possible solution here is to write a separate Terraform configuration that manages the image and then only apply that configuration in situations where that object isn't expected to already exist. Then your configuration that consumes the existing image can just assume it exists without caring about whether it was created by the other Terraform configuration or not.

There's a longer overview of this situation in the Terraform documentation section Module Composition, and in particular the sub-section Conditional Creation of Objects. That guide is focused on interactions between modules in a single configuration, but the same underlying principles apply to dependencies between configurations (via data sources) too.

这篇关于在 Terraform 0.12 中,如果资源名称已经存在,如何跳过资源的创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆