This topic describes how to create a Virtual Private Cloud (VPC), a VSwitch, and a NAT gateway by using Terraform.

Prerequisites

Before you begin, ensure that you have completed the following operations:

Procedure

  1. Create a VPC and a VSwitch.
    1. Create the terraform.tf file and define VPC and VSwitch resources in it as follows:
      provider "alicloud" {
        configuration_source = "terraform-provider-alicloud/examples/vpc"
      }
      
      resource "alicloud_vpc" "main" {
        # VPC name
        name       = "alicloud"
        # CIDR block of the VPC
        cidr_block = "10.1.0.0/21"
      }
      
      resource "alicloud_vswitch" "main" {
        # VPC ID
        vpc_id            = alicloud_vpc.main.id
        # CIDR block of the VSwitch
        cidr_block        = "10.1.0.0/24"
        # Zone
        availability_zone = "cn-hangzhou-b"
        # Dependent resource (this dependent resource will be created first)
        depends_on = [alicloud_vpc.main]
      }
    2. Run the terraform apply command to create resources. If log entries similar to the following ones are displayed, the resources are created.
      alicloud_vpc.main: Creating...
      alicloud_vpc.main: Creation complete after 6s [id=vpc-bp1ureoqifi1v1l87l2qt]
      alicloud_vswitch.main: Creating...
      alicloud_vswitch.main: Creation complete after 6s [id=vsw-bp17yxb3jsofbkaop7kxq]
      
      Apply complete! Resources: 2 added, 0 changed, 0 destroyed
    3. Run the terraform show command to view the created VPC and VSwitch.
  2. Create a NAT gateway.
    1. In terraform.tf, add the following content:
      resource "alicloud_nat_gateway" "main" {
        vpc_id        = alicloud_vpc.main.id
        specification = "Small"
        name          = "from-tf-example"
      }
      
      resource "alicloud_eip" "foo" {
      }
      
      resource "alicloud_eip_association" "foo" {
        allocation_id = alicloud_eip.foo.id
        instance_id   = alicloud_nat_gateway.main.id
      }
    2. Run the terraform apply command to create the NAT gateway. If log entries similar to the following ones are displayed, the NAT gateway is created.
      alicloud_eip.foo: Creating...
        bandwidth:            "" => "5"
        instance:             "" => "<computed>"
        instance_charge_type: "" => "PostPaid"
        internet_charge_type: "" => "PayByTraffic"
        ip_address:           "" => "<computed>"
        status:               "" => "<computed>"
      alicloud_nat_gateway.main: Creating...
        bandwidth_package_ids: "" => "<computed>"
        forward_table_ids:     "" => "<computed>"
        name:                  "" => "from-tf-example"
        snat_table_ids:        "" => "<computed>"
        specification:         "" => "Small"
        vpc_id:                "" => "vpc-bp1xhaa5nw4ad43m7wdai"
      alicloud_eip.foo: Creation complete after 2s (ID: eip-bp1y9pynihhurist6r1hk)
      alicloud_nat_gateway.main: Creation complete after 2s (ID: ngw-bp1wiycnrxk04vqnorzuo)
      alicloud_eip_association.foo: Creating...
        allocation_id: "" => "eip-bp1y9pynihhurist6r1hk"
        instance_id:   "" => "ngw-bp1wiycnrxk04vqnorzuo"
      alicloud_eip_association.foo: Creation complete after 6s (ID: eip-bp1y9pynihhurist6r1hk:ngw-bp1wiycnrxk04vqnorzuo)
      
      Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
    3. Run the terraform show command to view the created NAT gateway.