如何使用像计数这样的 Terraform 条件?
How to make use of Terraform conditons like count?
我想在使用该特定条件时使用计数或任何可帮助我创建资源的条件。但我还不理解这个概念。任何人都可以 post 有什么帮助 link 或通过示例进行解释吗?
Count 是 Terraform 元参数。它使您可以通过简单地增加一个数字来创建多个资源副本。要在 Terraform 中完成同样的事情,您可以使用 count.index
来获取循环中每个实例的索引。
通常,您可以使用 Count
和 Conditional Expression (condition ? true_val : false_val
) 来动态创建您的资源。
这里有一些有用的链接和示例
- https://azurecitadel.com/automation/terraform/lab4/
- https://linuxacademy.com/blog/linux-academy/using-count-for-cleaner-terraform/
- https://blog.gruntwork.io/terraform-tips-tricks-loops-if-statements-and-gotchas-f739bbae55f9#0223
更新
这是 enable the private Azure Kubernetes Service cluster 的工作示例。这将创建 2 个 AKS 群集,其中一个应该是私有的。其余配置相同
variable "prefix" {
default = "nancyaks"
}
variable "custom_aks" {
description = "Custom tags to set on the Instances in the ASG"
type = map(string)
default = {
"aks1" = true
"aks2" = false
}
}
resource "azurerm_resource_group" "example" {
name = "${var.prefix}-resources"
location = "West Europe"
}
# add advanced networking information
# resource "azurerm_virtual_network" "example" {
# name = "${var.prefix}-network"
# location = azurerm_resource_group.example.location
# resource_group_name = azurerm_resource_group.example.name
# address_space = ["10.1.0.0/16"]
# }
# resource "azurerm_subnet" "example" {
# name = "internal"
# resource_group_name = azurerm_resource_group.example.name
# address_prefixes = ["10.1.0.0/22"]
# virtual_network_name = azurerm_virtual_network.example.name
# }
resource "azurerm_kubernetes_cluster" "example" {
for_each = var.custom_aks
name = "${var.prefix}-${each.key}"
location = azurerm_resource_group.example.location
dns_prefix = "${var.prefix}-${each.key}"
resource_group_name = azurerm_resource_group.example.name
linux_profile {
admin_username = "acctestuser1"
ssh_key {
key_data = file(var.public_ssh_key_path)
}
}
default_node_pool {
name = "agentpool"
node_count = 2
vm_size = "Standard_DS1_v2"
//vnet_subnet_id = azurerm_subnet.example.id
}
private_cluster_enabled = each.value
identity {
type = "SystemAssigned"
}
network_profile {
network_plugin = "azure"
}
}
结果
我想在使用该特定条件时使用计数或任何可帮助我创建资源的条件。但我还不理解这个概念。任何人都可以 post 有什么帮助 link 或通过示例进行解释吗?
Count 是 Terraform 元参数。它使您可以通过简单地增加一个数字来创建多个资源副本。要在 Terraform 中完成同样的事情,您可以使用 count.index
来获取循环中每个实例的索引。
通常,您可以使用 Count
和 Conditional Expression (condition ? true_val : false_val
) 来动态创建您的资源。
这里有一些有用的链接和示例
- https://azurecitadel.com/automation/terraform/lab4/
- https://linuxacademy.com/blog/linux-academy/using-count-for-cleaner-terraform/
- https://blog.gruntwork.io/terraform-tips-tricks-loops-if-statements-and-gotchas-f739bbae55f9#0223
更新
这是 enable the private Azure Kubernetes Service cluster 的工作示例。这将创建 2 个 AKS 群集,其中一个应该是私有的。其余配置相同
variable "prefix" {
default = "nancyaks"
}
variable "custom_aks" {
description = "Custom tags to set on the Instances in the ASG"
type = map(string)
default = {
"aks1" = true
"aks2" = false
}
}
resource "azurerm_resource_group" "example" {
name = "${var.prefix}-resources"
location = "West Europe"
}
# add advanced networking information
# resource "azurerm_virtual_network" "example" {
# name = "${var.prefix}-network"
# location = azurerm_resource_group.example.location
# resource_group_name = azurerm_resource_group.example.name
# address_space = ["10.1.0.0/16"]
# }
# resource "azurerm_subnet" "example" {
# name = "internal"
# resource_group_name = azurerm_resource_group.example.name
# address_prefixes = ["10.1.0.0/22"]
# virtual_network_name = azurerm_virtual_network.example.name
# }
resource "azurerm_kubernetes_cluster" "example" {
for_each = var.custom_aks
name = "${var.prefix}-${each.key}"
location = azurerm_resource_group.example.location
dns_prefix = "${var.prefix}-${each.key}"
resource_group_name = azurerm_resource_group.example.name
linux_profile {
admin_username = "acctestuser1"
ssh_key {
key_data = file(var.public_ssh_key_path)
}
}
default_node_pool {
name = "agentpool"
node_count = 2
vm_size = "Standard_DS1_v2"
//vnet_subnet_id = azurerm_subnet.example.id
}
private_cluster_enabled = each.value
identity {
type = "SystemAssigned"
}
network_profile {
network_plugin = "azure"
}
}
结果