通过Terraform创建VPC

更新时间:

本文介绍如何使用Terraform创建专有网络,交换机以及NAT网关。

说明

本教程所含示例代码支持一键运行,您可以直接运行代码。一键运行

前提条件

在开始之前,请您确保完成以下操作:

操作步骤

  1. 创建专有网络和交换机。

    说明

    当前示例代码支持一键运行,您可以直接运行代码。一键运行

    1. 创建terraform.tf文件 ,定义专有网络和交换机资源,文件内容如下。

      provider "alicloud" {
        configuration_source = "terraform-provider-alicloud/examples/vpc"
      }
      
      resource "alicloud_vpc" "main" {
        # VPC名称
        vpc_name = "alicloud"
        # VPC地址块
        cidr_block = "10.1.0.0/21"
      }
      
      resource "alicloud_vswitch" "main" {
        # VPC ID
        vpc_id            = alicloud_vpc.main.id
        # 交换机地址块
        cidr_block        = "10.1.0.0/24"
        # 可用区
        zone_id = "cn-hangzhou-b"
        # 资源依赖,会优先创建该依赖资源
        depends_on = [alicloud_vpc.main]
      }
    2. 运行terraform apply开始创建。出现类似下面的日志,说明创建成功。

      alicloud_vpc.main: Creating...
      ......
      Apply complete! Resources: 2 added, 0 changed, 0 destroyed
    3. 运行terraform show查看已创建的专有和专有网络。

  2. 创建NAT网关并配置Snat和Dnat条目。

    1. terraform.tf文件中增加以下内容:

      variable "name" {
        default = "natGatewayExampleName"
      }
      
      resource "alicloud_vpc" "enhanced" {
        vpc_name   = var.name
        cidr_block = "10.0.0.0/8"
      }
      
      data "alicloud_enhanced_nat_available_zones" "enhanced"{}
      
      resource "alicloud_vswitch" "enhanced" {
        vswitch_name      = var.name
        zone_id           = data.alicloud_enhanced_nat_available_zones.enhanced.zones.0.zone_id
        cidr_block        = "10.10.0.0/20"
        vpc_id            = alicloud_vpc.enhanced.id
      }
      
      resource "alicloud_nat_gateway" "main" {
        vpc_id               = alicloud_vpc.enhanced.id
        nat_gateway_name     = var.name
        payment_type         = "PayAsYouGo"
        vswitch_id           = alicloud_vswitch.enhanced.id
        nat_type             = "Enhanced"
      }
      
      resource "alicloud_eip_address" "foo" {
        address_name = var.name
      }
      
      resource "alicloud_eip_association" "foo" {
        allocation_id = alicloud_eip_address.foo.id
        instance_id   = alicloud_nat_gateway.main.id
      }
      
      # adding dnat entry
      resource "alicloud_forward_entry" "default" {
        forward_table_id = alicloud_nat_gateway.main.forward_table_ids
        external_ip      = alicloud_eip_address.foo.ip_address
        external_port    = "80"
        ip_protocol      = "tcp"
        internal_ip      = "172.16.0.3"
        internal_port    = "8080"
      }
      
      # adding snat entry
      resource "alicloud_snat_entry" "default" {
        depends_on        = [alicloud_eip_association.foo]
        snat_table_id     = alicloud_nat_gateway.main.snat_table_ids
        source_vswitch_id = alicloud_vswitch.enhanced.id
        snat_ip           = join(",", alicloud_eip_address.foo.*.ip_address)
      }
      
    2. 运行terraform apply开始创建。出现下面的日志,说明创建成功。

      alicloud_vpc.enhanced: Creating...
      alicloud_eip_address.foo: Creating...
      ......
      Apply complete! Resources: 7 added, 0 changed, 0 destroyed.
                              
    3. 运行terraform show查看已经创建的NAT网关。