gpt4 book ai didi

azure - Terraform 如何定义变量以让用户选择现有资源或创建新资源

转载 作者:行者123 更新时间:2023-12-03 02:19:46 25 4
gpt4 key购买 nike

我是 Terraform 新手。我有这个问题,想在这里问一下。

因此,我正在使用所有这些输入变量创建一个 azurerm_storage_account

#ie: 

resource "azurerm_storage_account" "example" {
name = "storageaccountname"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
account_tier = "Standard"
account_replication_type = "GRS"

tags = {
environment = "staging"
}
}

如何定义变量可以让用户选择现有的存储帐户或创建新的存储帐户。

我正在考虑这样做:

variable "choice" {
description = "Allow user to choose the existing storage account or creating a new one"
type = string
default = "create"
}

然后我不知道在哪里使用这个变量来处理 azurerm_storage_account 资源。

任何指针将不胜感激!

谢谢。

最佳答案

您可以使用count为了那个原因。因此,如果您的变量是“create”,则将创建一个新资源。如果不是,则 azurerm_storage_account数据源将用于查询现有资源的详细信息。

resource "azurerm_storage_account" "example" {

count = var.choice == "create" ? 1 : 0

name = "storageaccountname"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
account_tier = "Standard"
account_replication_type = "GRS"

tags = {
environment = "staging"
}
}

data "azurerm_storage_account" "example" {

count = var.choice == "create" ? 0 : 1

#...

}

关于azure - Terraform 如何定义变量以让用户选择现有资源或创建新资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69545098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com