首页 > 其他分享 >使用-Terraform-创建一个新的Azure-资源组-虚拟网络-子网

使用-Terraform-创建一个新的Azure-资源组-虚拟网络-子网

时间:2023-08-20 11:44:46浏览次数:45  
标签:subnet 子网 azurerm terraform Terraform Azure my network name

本文主要讲解如何使用Terraform-在Azure Cloud 中创建一个新的资源组

然后在这个资源组中,创建一个虚拟网络-192.168.10.0/24, 以及8个/27位子网

环境说明:一般都是资源与资源组在一个Location位置中,写法也相对简单一些

因此本文,使用了,资源组,与虚拟网络资源不在同一个Location位置的情况进行演示,因此也会多一些细节点注意事项

首先还是先准备如下3个Terraform模板文件,以后各个文件的内容

li [ ~/qq-5201351 ]$ ls
main.tf  providers.tf  variables.tf

1、变量文件-variables.tf 内容

variable "resource_group_location" {
  type        = string
  default     = "East Asia"
  description = "Location of the resource group."
}

variable "resource_group_name" {
  type        = string
  default     = "RG001"
}

variable "vnet_name" {
  type        = string
  default     = "vnet-192.168.10.0"
}

2、providers文件-providers.tf-内容

terraform {
  required_version = ">=0.12"

  required_providers {
    azapi = {
      source  = "azure/azapi"
      version = "~>1.5"
    }
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~>2.0"
    }
    random = {
      source  = "hashicorp/random"
      version = "~>3.0"
    }
  }
}

provider "azurerm" {
  features {}
}

3、主文件-main.tf-内容

# Create Resource Group RG001
resource "azurerm_resource_group" "rg" {
  location = var.resource_group_location
  name     = var.resource_group_name
}

# Create virtual network - 192.168.10.0/24
resource "azurerm_virtual_network" "my_terraform_network" {
  name                = "vnet-192.168.10.0"
  address_space       = ["192.168.10.0/24"]
  location            = "Japan East"
  resource_group_name = azurerm_resource_group.rg.name
}

# Create subnet 01
resource "azurerm_subnet" "my_terraform_subnet_01" {
  name                 = "subnet-192.168.10.0"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.my_terraform_network.name
  address_prefixes     = ["192.168.10.0/27"]
  enforce_private_link_endpoint_network_policies = true
}

# Create subnet 02
resource "azurerm_subnet" "my_terraform_subnet_02" {
  name                 = "subnet-192.168.10.32"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.my_terraform_network.name
  address_prefixes     = ["192.168.10.32/27"]
  enforce_private_link_endpoint_network_policies = true
}

# Create subnet 03
resource "azurerm_subnet" "my_terraform_subnet_03" {
  name                 = "subnet-192.168.10.64"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.my_terraform_network.name
  address_prefixes     = ["192.168.10.64/27"]
  enforce_private_link_endpoint_network_policies = true
}

# Create subnet 04
resource "azurerm_subnet" "my_terraform_subnet_04" {
  name                 = "subnet-192.168.10.96"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.my_terraform_network.name
  address_prefixes     = ["192.168.10.96/27"]
  enforce_private_link_endpoint_network_policies = true
}

# Create subnet 05
resource "azurerm_subnet" "my_terraform_subnet_05" {
  name                 = "subnet-192.168.10.128"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.my_terraform_network.name
  address_prefixes     = ["192.168.10.128/27"]
  enforce_private_link_endpoint_network_policies = true
}

# Create subnet 06
resource "azurerm_subnet" "my_terraform_subnet_06" {
  name                 = "subnet-192.168.10.160"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.my_terraform_network.name
  address_prefixes     = ["192.168.10.160/27"]
  enforce_private_link_endpoint_network_policies = true
}

# Create subnet 07
resource "azurerm_subnet" "my_terraform_subnet_07" {
  name                 = "subnet-192.168.10.192"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.my_terraform_network.name
  address_prefixes     = ["192.168.10.192/27"]
  enforce_private_link_endpoint_network_policies = true
}

# Create subnet 08
resource "azurerm_subnet" "my_terraform_subnet_08" {
  name                 = "subnet-192.168.10.224"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.my_terraform_network.name
  address_prefixes     = ["192.168.10.224/27"]
  enforce_private_link_endpoint_network_policies = true
}

4、将 Terraform 部署进行初始化,下载管理 Azure 资源所需的 Azure 提供程序 : terraform init -upgrade

5、创建Terraform-执行计划:terraform plan -out main.tfplan

li [ ~/qq-5201351 ]$ terraform plan -out main.tfplan

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # azurerm_resource_group.rg will be created
  + resource "azurerm_resource_group" "rg" {
      + id       = (known after apply)
      + location = "eastasia"
      + name     = "RG001"
    }

  # azurerm_subnet.my_terraform_subnet_01 will be created
  + resource "azurerm_subnet" "my_terraform_subnet_01" {
      + address_prefix                                 = (known after apply)
      + address_prefixes                               = [
          + "192.168.10.0/27",
        ]
      + enforce_private_link_endpoint_network_policies = true
      + enforce_private_link_service_network_policies  = false
      + id                                             = (known after apply)
      + name                                           = "subnet-192.168.10.0"
      + resource_group_name                            = "RG001"
      + virtual_network_name                           = "vnet-192.168.10.0"
    }

  # azurerm_subnet.my_terraform_subnet_02 will be created
  + resource "azurerm_subnet" "my_terraform_subnet_02" {
      + address_prefix                                 = (known after apply)
      + address_prefixes                               = [
          + "192.168.10.32/27",
        ]
      + enforce_private_link_endpoint_network_policies = true
      + enforce_private_link_service_network_policies  = false
      + id                                             = (known after apply)
      + name                                           = "subnet-192.168.10.32"
      + resource_group_name                            = "RG001"
      + virtual_network_name                           = "vnet-192.168.10.0"
    }

  # azurerm_subnet.my_terraform_subnet_03 will be created
  + resource "azurerm_subnet" "my_terraform_subnet_03" {
      + address_prefix                                 = (known after apply)
      + address_prefixes                               = [
          + "192.168.10.64/27",
        ]
      + enforce_private_link_endpoint_network_policies = true
      + enforce_private_link_service_network_policies  = false
      + id                                             = (known after apply)
      + name                                           = "subnet-192.168.10.64"
      + resource_group_name                            = "RG001"
      + virtual_network_name                           = "vnet-192.168.10.0"
    }

  # azurerm_subnet.my_terraform_subnet_04 will be created
  + resource "azurerm_subnet" "my_terraform_subnet_04" {
      + address_prefix                                 = (known after apply)
      + address_prefixes                               = [
          + "192.168.10.96/27",
        ]
      + enforce_private_link_endpoint_network_policies = true
      + enforce_private_link_service_network_policies  = false
      + id                                             = (known after apply)
      + name                                           = "subnet-192.168.10.96"
      + resource_group_name                            = "RG001"
      + virtual_network_name                           = "vnet-192.168.10.0"
    }

  # azurerm_subnet.my_terraform_subnet_05 will be created
  + resource "azurerm_subnet" "my_terraform_subnet_05" {
      + address_prefix                                 = (known after apply)
      + address_prefixes                               = [
          + "192.168.10.128/27",
        ]
      + enforce_private_link_endpoint_network_policies = true
      + enforce_private_link_service_network_policies  = false
      + id                                             = (known after apply)
      + name                                           = "subnet-192.168.10.128"
      + resource_group_name                            = "RG001"
      + virtual_network_name                           = "vnet-192.168.10.0"
    }

  # azurerm_subnet.my_terraform_subnet_06 will be created
  + resource "azurerm_subnet" "my_terraform_subnet_06" {
      + address_prefix                                 = (known after apply)
      + address_prefixes                               = [
          + "192.168.10.160/27",
        ]
      + enforce_private_link_endpoint_network_policies = true
      + enforce_private_link_service_network_policies  = false
      + id                                             = (known after apply)
      + name                                           = "subnet-192.168.10.160"
      + resource_group_name                            = "RG001"
      + virtual_network_name                           = "vnet-192.168.10.0"
    }

  # azurerm_subnet.my_terraform_subnet_07 will be created
  + resource "azurerm_subnet" "my_terraform_subnet_07" {
      + address_prefix                                 = (known after apply)
      + address_prefixes                               = [
          + "192.168.10.192/27",
        ]
      + enforce_private_link_endpoint_network_policies = true
      + enforce_private_link_service_network_policies  = false
      + id                                             = (known after apply)
      + name                                           = "subnet-192.168.10.192"
      + resource_group_name                            = "RG001"
      + virtual_network_name                           = "vnet-192.168.10.0"
    }

  # azurerm_subnet.my_terraform_subnet_08 will be created
  + resource "azurerm_subnet" "my_terraform_subnet_08" {
      + address_prefix                                 = (known after apply)
      + address_prefixes                               = [
          + "192.168.10.224/27",
        ]
      + enforce_private_link_endpoint_network_policies = true
      + enforce_private_link_service_network_policies  = false
      + id                                             = (known after apply)
      + name                                           = "subnet-192.168.10.224"
      + resource_group_name                            = "RG001"
      + virtual_network_name                           = "vnet-192.168.10.0"
    }

  # azurerm_virtual_network.my_terraform_network will be created
  + resource "azurerm_virtual_network" "my_terraform_network" {
      + address_space         = [
          + "192.168.10.0/24",
        ]
      + dns_servers           = (known after apply)
      + guid                  = (known after apply)
      + id                    = (known after apply)
      + location              = "japaneast"
      + name                  = "vnet-192.168.10.0"
      + resource_group_name   = "RG001"
      + subnet                = (known after apply)
      + vm_protection_enabled = false
    }

Plan: 10 to add, 0 to change, 0 to destroy.

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Saved the plan to: main.tfplan

To perform exactly these actions, run the following command to apply:
    terraform apply "main.tfplan"

6、应用 Terraform 执行计划:terraform apply main.tfplan

li [ ~/qq-5201351 ]$ terraform apply main.tfplan
azurerm_resource_group.rg: Creating...
azurerm_resource_group.rg: Creation complete after 2s [id=/subscriptions/2027a5ea-db41-4d93-a2b3-caff8e2c1157/resourceGroups/RG001]
azurerm_virtual_network.my_terraform_network: Creating...
azurerm_virtual_network.my_terraform_network: Creation complete after 6s [id=/subscriptions/2027a5ea-db41-4d93-a2b3-caff8e2c1157/resourceGroups/RG001/providers/Microsoft.Network/virtualNetworks/vnet-192.168.10.0]
azurerm_subnet.my_terraform_subnet_06: Creating...
azurerm_subnet.my_terraform_subnet_03: Creating...
azurerm_subnet.my_terraform_subnet_04: Creating...
azurerm_subnet.my_terraform_subnet_01: Creating...
azurerm_subnet.my_terraform_subnet_05: Creating...
azurerm_subnet.my_terraform_subnet_02: Creating...
azurerm_subnet.my_terraform_subnet_07: Creating...
azurerm_subnet.my_terraform_subnet_08: Creating...
azurerm_subnet.my_terraform_subnet_06: Creation complete after 5s [id=/subscriptions/2027a5ea-db41-4d93-a2b3-caff8e2c1157/resourceGroups/RG001/providers/Microsoft.Network/virtualNetworks/vnet-192.168.10.0/subnets/subnet-192.168.10.160]
azurerm_subnet.my_terraform_subnet_04: Still creating... [10s elapsed]
azurerm_subnet.my_terraform_subnet_03: Still creating... [10s elapsed]
azurerm_subnet.my_terraform_subnet_01: Still creating... [10s elapsed]
azurerm_subnet.my_terraform_subnet_05: Still creating... [10s elapsed]
azurerm_subnet.my_terraform_subnet_07: Still creating... [10s elapsed]
azurerm_subnet.my_terraform_subnet_08: Still creating... [10s elapsed]
azurerm_subnet.my_terraform_subnet_02: Still creating... [10s elapsed]
azurerm_subnet.my_terraform_subnet_01: Creation complete after 10s [id=/subscriptions/2027a5ea-db41-4d93-a2b3-caff8e2c1157/resourceGroups/RG001/providers/Microsoft.Network/virtualNetworks/vnet-192.168.10.0/subnets/subnet-192.168.10.0]
azurerm_subnet.my_terraform_subnet_03: Creation complete after 15s [id=/subscriptions/2027a5ea-db41-4d93-a2b3-caff8e2c1157/resourceGroups/RG001/providers/Microsoft.Network/virtualNetworks/vnet-192.168.10.0/subnets/subnet-192.168.10.64]
azurerm_subnet.my_terraform_subnet_05: Creation complete after 20s [id=/subscriptions/2027a5ea-db41-4d93-a2b3-caff8e2c1157/resourceGroups/RG001/providers/Microsoft.Network/virtualNetworks/vnet-192.168.10.0/subnets/subnet-192.168.10.128]
azurerm_subnet.my_terraform_subnet_04: Still creating... [20s elapsed]
azurerm_subnet.my_terraform_subnet_07: Still creating... [20s elapsed]
azurerm_subnet.my_terraform_subnet_02: Still creating... [20s elapsed]
azurerm_subnet.my_terraform_subnet_08: Still creating... [20s elapsed]
azurerm_subnet.my_terraform_subnet_07: Creation complete after 24s [id=/subscriptions/2027a5ea-db41-4d93-a2b3-caff8e2c1157/resourceGroups/RG001/providers/Microsoft.Network/virtualNetworks/vnet-192.168.10.0/subnets/subnet-192.168.10.192]
azurerm_subnet.my_terraform_subnet_08: Creation complete after 29s [id=/subscriptions/2027a5ea-db41-4d93-a2b3-caff8e2c1157/resourceGroups/RG001/providers/Microsoft.Network/virtualNetworks/vnet-192.168.10.0/subnets/subnet-192.168.10.224]
azurerm_subnet.my_terraform_subnet_04: Still creating... [30s elapsed]
azurerm_subnet.my_terraform_subnet_02: Still creating... [30s elapsed]
azurerm_subnet.my_terraform_subnet_02: Creation complete after 34s [id=/subscriptions/2027a5ea-db41-4d93-a2b3-caff8e2c1157/resourceGroups/RG001/providers/Microsoft.Network/virtualNetworks/vnet-192.168.10.0/subnets/subnet-192.168.10.32]
azurerm_subnet.my_terraform_subnet_04: Creation complete after 39s [id=/subscriptions/2027a5ea-db41-4d93-a2b3-caff8e2c1157/resourceGroups/RG001/providers/Microsoft.Network/virtualNetworks/vnet-192.168.10.0/subnets/subnet-192.168.10.96]

Apply complete! Resources: 10 added, 0 changed, 0 destroyed.

这样,就可以在Azure Portal 上看到 创建了一个 RG001资源组,一个虚拟网络,8个子网,对于上面显示的10个资源

其他说明:但是实际到Azure上可以看到还多创建了如下2个

1、NetworkWatcherRG 这样一个资源组,位置与虚拟网络位置一样:Japan East

2、NetworkWatcher_japaneast 网络观察程序,位于NetworkWatcherRG资源组:Location位置也是:Japan East

 

 


尊重别人的劳动成果 转载请务必注明出处:https://www.cnblogs.com/5201351/p/17643791.html

 

标签:subnet,子网,azurerm,terraform,Terraform,Azure,my,network,name
From: https://www.cnblogs.com/5201351/p/17643791.html

相关文章

  • [Microsoft Azure] 在 Azure App Service 上创建和使用虚拟目录
    本文将详细介绍如何在MicrosoftAzure的AzureAppService上创建和使用虚拟目录。我们将通过实际操作步骤来帮助您理解虚拟目录的创建和使用方法,为您的应用程序提供灵活的资源管理方式。一、什么是虚拟目录?虚拟目录是一种将物理文件夹路径映射到Web应用程序的URL空间......
  • 【Azure App Service】为部署在App Service上的PHP应用开启JIT编译器
    问题描述在AppServiceforlinux上创建一个PHP应用,通过phpinfo()查看PHP的扩展设置,发现JIT没有被开启, jit_buffer_size大小为0.那么,在AppService的环境中,如何开启JIT呢? 问题解答PHP8在PHP的内核中添加了JIT编译器,可以极大地提高性能。首先,仅在启用opcache的情况下,JI......
  • 【Azure App Service】为部署在App Service上的PHP应用开启JIT编译器
    问题描述在AppServiceforlinux上创建一个PHP应用,通过phpinfo()查看PHP的扩展设置,发现JIT没有被开启, jit_buffer_size大小为0.那么,在AppService的环境中,如何开启JIT呢? 问题解答PHP8在PHP的内核中添加了JIT编译器,可以极大地提高性能。首先,仅在启用opcache的情况下,JIT才有效......
  • [Microsoft Azure] 快速上手创建Azure App Service服务
    本文将带您快速了解如何使用MicrosoftAzure创建AzureAppService服务,让您在短时间内掌握这一流行的云计算平台。 MicrosoftAzure是一个功能强大的云计算平台,提供了广泛的云服务,包括虚拟机、数据库、存储等。在本篇博客中,我们将重点介绍AzureAppService服务的创建过程。A......
  • 通过微软Azure调用GPT的接口API-兼容平替OpenAI官方的注意事项
    众所周知,我们是访问不通OpenAI官方服务的,但是我们可以自己通过代理或者使用第三方代理访问接口现在新出台的规定禁止使用境外的AI大模型接口对境内客户使用,所以我们需要使用国内的大模型接口国内的效果真的很差,现在如果想使用GPT大模型,可以使用微软Azure的OpenAI服务。 负责......
  • 关于使用Terraform为Azure创建一个资源组的简单案例
    使用Terraform创建AzureCloud平台的资源需要,得还有如下主要的环境及条件a、安装有Terraformb、解决身份认证及相关的权限++++++++++++++++++++++++++++++++++++++++++++++本文的目标,创建一个rg-开着的随机名称的资源组先得准备有4个文件,【providers.tf】、【main.tf】、【v......
  • 【Azure Service Fabric】关于Service Fabric的相关问题
    问题一:ServiceFabric是否支持PrivateLink?在AzurePrivateEndpoint文档中,罗列出了Azure上支持PrivateLink的服务。ServiceFabric不在其中。AzurePrivateLinkavailability:https://learn.microsoft.com/en-us/azure/private-link/availability 问题二:是否可以Disable......
  • 【Azure Service Fabric】关于Service Fabric的相关问题
    问题一:ServiceFabric是否支持PrivateLink?在AzurePrivateEndpoint文档中,罗列出了Azure上支持PrivateLink的服务。ServiceFabric不在其中。AzurePrivateLinkavailability:https://learn.microsoft.com/en-us/azure/private-link/availability 问题二:是否可以Dis......
  • [Microsoft Azure] 配置Azure App Service仅虚拟内网访问
    本文将介绍如何配置AzureAppService以仅允许虚拟内网访问,从而提高安全性和隐私性。我们将讨论配置过程的不同步骤以及注意事项,以便您可以轻松实现更安全的应用部署。 随着云计算的普及,越来越多的企业和开发者开始将其应用部署到云平台上。在这种情况下,如何确保应用安全性和......
  • 【Azure K8S | AKS】在中国区AKS上遇见ImagePullBackOff时的替代方案
    问题描述在AKS集群中部署calico时候,遇见ImagePullBackOff问题。在创建POD calico-typha-horizontal-autoscale时候遇见拉取镜像失败问题。错误消息为:Failedtopullimage"k8s.gcr.io/cluster-proportional-autoscaler-amd64:1.1.2-r2":rpcerror:code=Unknowndesc=E......