main.tf 中定义要创建的资源,比如resourcegroup,vnet,role assignment,k8s cluster 等,为了复用性,对于资源组名称,vnet 地址池,k8s name 等放在 variables.tf 中定义
[root@centos8 aks-private]# cat main.tfresource"azurerm_resource_group""rg"{location=var.resource_group_locationname=var.resource_group_nametags={CostCenter=var.resource_group_costcenterowner=var.resource_group_ownersource="terraform" }}resource"azurerm_virtual_network""vnet1"{name=var.vnet_nameaddress_space=var.vnet_rangelocation=azurerm_resource_group.rg.locationresource_group_name=azurerm_resource_group.rg.name}resource"azurerm_subnet""sub1"{name=var.subnet_nameresource_group_name=azurerm_resource_group.rg.namevirtual_network_name=azurerm_virtual_network.vnet1.nameaddress_prefixes=var.subnet_range}# Create NAT Gateway with a public IP. Associates NAT Gateway with sub1.resource"azurerm_public_ip""pubip1"{name="nat-gateway-publicIP"location=azurerm_resource_group.rg.locationresource_group_name=azurerm_resource_group.rg.nameallocation_method="Static"sku="Standard"zones= ["1"]}#resource "azurerm_public_ip_prefix" "example" {# name = "nat-gateway-publicIPPrefix"# location = azurerm_resource_group.example.location# resource_group_name = azurerm_resource_group.example.name# prefix_length = 30# zones = ["1"]#}resource"azurerm_nat_gateway""gw1"{name="nat-Gateway"location=azurerm_resource_group.rg.locationresource_group_name=azurerm_resource_group.rg.namesku_name="Standard"idle_timeout_in_minutes=10zones= ["1"]}resource"azurerm_nat_gateway_public_ip_association""gw1_pubip1"{nat_gateway_id=azurerm_nat_gateway.gw1.idpublic_ip_address_id=azurerm_public_ip.pubip1.id}resource"azurerm_subnet_nat_gateway_association""gw1_sub1"{subnet_id=azurerm_subnet.sub1.idnat_gateway_id=azurerm_nat_gateway.gw1.id}# Create route tableresource"azurerm_route_table""rt1"{name="rt${var.subnet_name}"location=azurerm_resource_group.rg.locationresource_group_name=azurerm_resource_group.rg.namedisable_bgp_route_propagation=false}resource"azurerm_subnet_route_table_association""example"{subnet_id=azurerm_subnet.sub1.idroute_table_id=azurerm_route_table.rt1.id}# Create user assigned managed identity for AKS master and noderesource"azurerm_user_assigned_identity""uai_master"{resource_group_name=azurerm_resource_group.rg.namelocation=azurerm_resource_group.rg.locationname="${var.cluster_name}-aks-master-identity"}#resource "azurerm_user_assigned_identity" "uai_node" {# resource_group_name = azurerm_resource_group.rg.name# location = azurerm_resource_group.rg.location# name = "${var.cluster_name}-aks-node-identity"#}# Assign the "Network Contributor" role on route table to the AKS managed identity.resource"azurerm_role_assignment""route_table_network_contributor"{scope=azurerm_route_table.rt1.idrole_definition_name="Network Contributor"principal_id=azurerm_user_assigned_identity.uai_master.principal_id}resource"azurerm_kubernetes_cluster""k8s"{ depends_on = [azurerm_role_assignment.route_table_network_contributor, azurerm_nat_gateway_public_ip_association.gw1_pubip1, azurerm_subnet_nat_gateway_association.gw1_sub1]
location=azurerm_resource_group.rg.locationname=var.cluster_nameresource_group_name=azurerm_resource_group.rg.namedns_prefix=var.dns_prefixprivate_cluster_enabled=trueidentity{type="UserAssigned"identity_ids= [azurerm_user_assigned_identity.uai_master.id] }# kubelet_identity {# client_id = azurerm_user_assigned_identity.uai_node.client_id# object_id = azurerm_user_assigned_identity.uai_node.principal_id# user_assigned_identity_id = azurerm_user_assigned_identity.uai_node.id# }default_node_pool{name="agentpool"vm_size=var.agent_vm_sizenode_count=var.agent_countvnet_subnet_id=azurerm_subnet.sub1.idtype="VirtualMachineScaleSets" }linux_profile{admin_username="ubuntu"ssh_key{key_data=file(var.ssh_public_key) } }network_profile{network_plugin="kubenet"network_policy="calico"service_cidr="172.28.0.0/16"dns_service_ip="172.28.0.10"pod_cidr="172.29.0.0/16"docker_bridge_cidr="172.16.0.0/16"outbound_type="userDefinedRouting"load_balancer_sku="standard" }}
[root@centos8 aks-private]# cat variables.tfvariable"resource_group_name"{default="rg_ea_mobile_sit01"description="ResourceGroup_Location_Application_EnvironmentNumber."}variable"resource_group_location"{default="eastasia"description="Location of the resource group."}variable"resource_group_costcenter"{default="mobile T31"description="CostCenter of the resource group."}variable"resource_group_owner"{default="caifeng"description="Owner of the resource group."}variable"vnet_name"{default="vneamobilesit01"}variable"vnet_range"{type=list(string)default= ["10.2.0.0/16"]description="Address range for deployment VNet"}variable"subnet_name"{default="sneamobilesit01"}variable"subnet_range"{type=list(string)default= ["10.2.0.0/24"]description="Address range for session host subnet"}variable"cluster_name"{default="akseamobilesit02"}variable"dns_prefix"{default="akseamobilesit02"}variable"agent_vm_size"{default="Standard_B2s"}variable"agent_count"{default=1}variable"ssh_public_key"{default="~/.ssh/id_rsa.pub"}
执行 terraform fmt 格式化, terraform plan 模拟运行,一些明显的错误,比如括号不完整,变量格式不对,变量名等问题会被发现。