我可以在Terraform中自动生成的资源上选择名称吗?

时间:2018-09-26 09:16:06

标签: terraform terraform-provider-azure

我正在使用Terraform生成Azure Kubernetes服务群集(以及其他东西),当我运行Terraform时,它会自动生成AKS群集使用的不同资源。我可以在AKS群集上选择一个自定义名称,但是自动生成的资源的名称接近随机名称。

是否可以在Terraform的这些自动生成的资源中选择自己的自定义名称?

示例:

resource "azurerm_kubernetes_cluster" "compute" {
    name                = "MyCluster"
    location            = "westeurope"
    resource_group_name = "my-rg"
    dns_prefix          = "something"

    linux_profile {
         admin_username = "azureuser"

    agent_pool_profile {
        name            = "default"
        count           = "1"
        vm_size         = "Standard_NC6"
        os_type         = "Linux"
   }
}

这将生成:

myCluster-Kubernetes服务

aks-agentpool-74438003-nsg-网络安全组

aks-agentpool-74438003-routetable-路线表

aks-default-74438003-0-虚拟机

aks-default-74438003-0_OsDisk_1_5d379bc3205545e1bcd3f88ec9605-磁盘

aks-default-74438003-nic-0-网络接口

aks-vnet-74438003-虚拟网络

default-availabilitySet-74438003-可用性设置

例如,我可以选择是否要 aks 前缀吗?那那个重复的数字是多少?我可以基本自定义这些吗?

1 个答案:

答案 0 :(得分:2)

您可以自定义其他资源的前缀或名称。
但是,仅当您创建这些资源并自己明确引用它们时。

请参见this Azure文档页面

基于Github的this AKS模块的示例代码

variable "my-prefix" {
  default = "myCluster"
  description = "The prefix name to give to all my resources"
}

variable "location" {
  default     = "West Europe"
  description = "The Azure Region in which all resources in this example should be provisioned"
}

variable "kubernetes_client_id" {
  description = "The Client ID for the Service Principal to use for this Managed Kubernetes Cluster"
}

variable "kubernetes_client_secret" {
  description = "The Client Secret for the Service Principal to use for this Managed Kubernetes Cluster"
}

variable "public_ssh_key_path" {
  description = "The Path at which your Public SSH Key is located. Defaults to ~/.ssh/id_rsa.pub"
  default     = "~/.ssh/id_rsa.pub"
}

resource "azurerm_resource_group" "test" {
  name     = "${var.my-prefix}-anw-resources"
  location = "${var.location}"
}

resource "azurerm_route_table" "test" {
  name                = "${var.my-prefix}-routetable"
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"

  route {
    name                   = "default"
    address_prefix         = "10.100.0.0/14"
    next_hop_type          = "VirtualAppliance"
    next_hop_in_ip_address = "10.10.1.1"
  }
}

resource "azurerm_virtual_network" "test" {
  name                = "${var.my-prefix}-network"
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"
  address_space       = ["10.1.0.0/16"]
}

resource "azurerm_subnet" "test" {
  name                 = "${var.my-prefix}-subnet"
  resource_group_name  = "${azurerm_resource_group.test.name}"
  address_prefix       = "10.1.0.0/24"
  virtual_network_name = "${azurerm_virtual_network.test.name}"

  # this field is deprecated and will be removed in 2.0 - but is required until then
  route_table_id = "${azurerm_route_table.test.id}"
}

resource "azurerm_network_security_group" "test" {
  name                = "${var.my-prefix}-nsg"
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_virtual_network.test.name}"

  security_rule {
    name                       = "port_80"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "80"
    source_address_prefixes    = "0.0.0.0/0"
    destination_address_prefix = "*"
  }
}

resource "azurerm_subnet_route_table_association" "test" {
  subnet_id      = "${azurerm_subnet.test.id}"
  route_table_id = "${azurerm_route_table.test.id}"
}

resource "azurerm_subnet_network_security_group_association" "test" {
  subnet_id                 = "${azurerm_subnet.test.id}"
  network_security_group_id = "${azurerm_network_security_group.test.id}"
}

resource "azurerm_kubernetes_cluster" "test" {
  name                = "${var.my-prefix}-anw"
  location            = "${azurerm_resource_group.test.location}"
  dns_prefix          = "${var.my-prefix}-anw"
  resource_group_name = "${azurerm_resource_group.test.name}"

  linux_profile {
    admin_username = "acctestuser1"

    ssh_key {
      key_data = "${file(var.public_ssh_key_path)}"
    }
  }

  agent_pool_profile {
    name            = "agentpool"
    count           = "2"
    vm_size         = "Standard_DS2_v2"
    os_type         = "Linux"
    os_disk_size_gb = 30

    # Required for advanced networking
    vnet_subnet_id = "${azurerm_subnet.test.id}"
  }

  service_principal {
    client_id     = "${var.kubernetes_client_id}"
    client_secret = "${var.kubernetes_client_secret}"
  }

  network_profile {
    network_plugin = "azure"
  }
}