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

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

问题描述

我正在使用Terraform版本0.12.如果具有相同名称的资源已经存在,我需要跳过资源创建.

我为此做了以下事情:

阅读自定义图像列表,

 数据"ibm_is_images""custom_images" {} 

检查图像是否已经存在,

  locals {custom_vsi_image =包含([对于data.ibm_is_images.custom_images.images中的x,如果x.visibility =="private"&& x.name == var.vnf_vpc_image_name],则为"true"}输出"abc" {value ="$ {local.custom_vsi_image}"} 

仅当图像存在为false时创建.

 资源"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}"名称="$ {var.vnf_vpc_image_name}"operating_system ="centos-7-amd64"超时{创建="30m"delete ="10m"}} 

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

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

此外,如何根据某种条件(例如仅在不存在时)创建资源?

解决方案

在Terraform中,您需要明确决定哪个系统负责管理特定对象,反之,哪些系统仅消耗 现有对象.无法动态地做出决定,因为这将使结果不确定,并且-对于Terraform管理的对象-不清楚哪种配置的 terraform destroy 将破坏对象.

的确,这种不确定性是为什么您会在尝试创建然后删除资源之间看到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天全站免登陆