全部產品
Search
文件中心

Cloud Firewall:通過 Terraform 建立VPC邊界防火牆來防護通過Express Connect串連的兩個VPC之間的流量

更新時間:Apr 24, 2025

本文介紹如何使用Terraform建立VPC邊界防火牆(防護通過Express Connect串連的兩個VPC之間的流量)。

說明

當前範例程式碼支援一鍵運行,您可以直接運行代碼。

前提條件

  • 由於阿里雲帳號(主帳號)具有資源的所有許可權,一旦發生泄露將面臨重大風險。建議您使用RAM使用者,並為該RAM使用者建立AccessKey,具體操作方式請參見建立RAM使用者建立AccessKey

  • 使用以下樣本為RAM使用者授權,具體操作方式請參見為RAM使用者授權

    {
        "Version": "1",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "yundun-cloudfirewall:*",
                    "yundun-ndr:*",
                    "vpc:CreateVpc",
                    "vpc:DeleteVpc",
                    "vpc:DescribeVpcs",
                    "vpc:CreateVSwitch",
                    "vpc:DeleteVSwitch",
                    "vpc:DescribeVSwitches",
                    "vpc:CreateRouteEntry",
                    "vpc:DeleteRouteEntry",
                    "vpc:DescribeRouteEntries",
                    "vpc:CreateVpcPeerConnection",
                    "vpc:DeleteVpcPeerConnection",
                    "vpc:DescribeVpcPeerConnections",
                    "cloudfirewall:CreateVpcFirewall",
                    "cloudfirewall:DeleteVpcFirewall",
                    "cloudfirewall:DescribeVpcFirewalls"
                ],
                "Resource": "*"
            }
        ]
    }
  • 準備Terraform運行環境,您可以選擇以下任一方式來使用Terraform。

    Explorer中使用Terraform:阿里雲提供了Terraform的線上運行環境,您無需安裝Terraform,登入後即可線上使用和體驗Terraform。適用於零成本、快速、便捷地體驗和調試Terraform的情境。

    Cloud Shell:阿里雲Cloud Shell中預裝了Terraform的組件,並已配置好身份憑證,您可直接在Cloud Shell中運行Terraform的命令。適用於低成本、快速、便捷地訪問和使用Terraform的情境。

    在本地安裝和配置Terraform:適用於網路連接較差或需要自訂開發環境的情境。

    重要

    請確保Terraform版本不低於v0.12.28。如需檢查現有版本,請運行terraform --version命令。

使用的資源

alicloud_cloud_firewall_vpc_firewall:VPC邊界防火牆。

建立VPC邊界防火牆

本樣本將建立VPC邊界防火牆。

  1. 建立一個工作目錄,並且在工作目錄中建立以下名為main.tf的設定檔。main.tfTerraform主檔案,定義了將要部署的資源。

    variable "region" {
      default = "cn-heyuan"
    }
    provider "alicloud" {
      region = var.region
    }
    # 擷取當前阿里雲uid
    data "alicloud_account" "current" {
    }
    # 建立VPC 1
    resource "alicloud_vpc" "vpc" {
      vpc_name   = "dd-tf-vpc-01"
      cidr_block = "192.168.0.0/16"
    }
    # 建立VPC 2
    resource "alicloud_vpc" "vpc1" {
      vpc_name   = "dd-tf-vpc-02"
      cidr_block = "172.16.0.0/12"
    }
    # 建立一個Vswitch CIDR 塊為 192.168.10.0/24
    resource "alicloud_vswitch" "vsw" {
      vpc_id       = alicloud_vpc.vpc.id
      cidr_block   = "192.168.10.0/24"
      zone_id      = "cn-heyuan-a"
      vswitch_name = "dd-tf-vpc-01-example-1"
    }
    # 建立另一個Vswitch CIDR 塊為 192.168.20.0/24
    resource "alicloud_vswitch" "vsw1" {
      vpc_id       = alicloud_vpc.vpc.id
      cidr_block   = "192.168.20.0/24"
      zone_id      = "cn-heyuan-b"
      vswitch_name = "dd-tf-vpc-01-example-2"
    }
    # 建立一個Vswitch CIDR 塊為 172.16.10.0/24
    resource "alicloud_vswitch" "vsw2" {
      vpc_id       = alicloud_vpc.vpc1.id
      cidr_block   = "172.16.10.0/24"
      zone_id      = "cn-heyuan-a"
      vswitch_name = "dd-tf-vpc-02-example-11"
    }
    # 建立另一個Vswitch CIDR 塊為 172.16.20.0/24
    resource "alicloud_vswitch" "vsw3" {
      vpc_id       = alicloud_vpc.vpc1.id
      cidr_block   = "172.16.20.0/24"
      zone_id      = "cn-heyuan-b"
      vswitch_name = "dd-tf-vpc-02-example-22"
    }
    # 建立VPC對等串連
    resource "alicloud_vpc_peer_connection" "default" {
      # 對等串連名稱
      peer_connection_name = "terraform-example-vpc-peer-connection"
      # 發起方VPC_ID
      vpc_id = alicloud_vpc.vpc.id
      # 接收方 VPC 對等串連的 Alibaba Cloud 帳號 ID
      accepting_ali_uid = data.alicloud_account.current.id
      # 接收方 VPC 對等串連的地區 ID。同地區建立時,輸入與發起方相同的地區 ID;跨地區建立時,輸入不同的地區 ID。
      accepting_region_id = "cn-heyuan"
      # 接收端VPC_ID
      accepting_vpc_id = alicloud_vpc.vpc1.id
      # 描述
      description = "terraform-example"
      # 是否強制移除
      force_delete = true
    }
    # 接收端
    resource "alicloud_vpc_peer_connection_accepter" "default" {
      instance_id = alicloud_vpc_peer_connection.default.id
    }
    # 配置路由條目-vpc-A
    resource "alicloud_route_entry" "foo" {
      # VPC-A 路由表ID
      route_table_id = alicloud_vpc.vpc.route_table_id
      # 目標網段,自訂
      destination_cidrblock = "1.2.3.4/32"
      # 下一跳類型
      nexthop_type = "VpcPeer"
      # 下一跳id
      nexthop_id = alicloud_vpc_peer_connection.default.id
    }
    # 配置路由條目2 -vpc-B
    resource "alicloud_route_entry" "foo1" {
      # VPC-A 路由表id
      route_table_id = alicloud_vpc.vpc1.route_table_id
      # 目標網段,自訂
      destination_cidrblock = "4.3.X.X/32"
      # 下一跳類型
      nexthop_type = "VpcPeer"
      # 下一跳id
      nexthop_id = alicloud_vpc_peer_connection.default.id
    }
    # 先建立其他前置資源
    resource "time_sleep" "wait_before_firewall" {
      # 確保雲企業網執行個體,網路連接執行個體建立好後
      depends_on = [
        alicloud_route_entry.foo,
        alicloud_route_entry.foo1
      ]
      create_duration = "720s" # 根據需要設定時間
    }
    # 延遲
    resource "null_resource" "wait_for_firewall" {
      provisioner "local-exec" {
        command = "echo waiting for firewall to be ready"
      }
      # 確保雲企業網執行個體建立
      depends_on = [time_sleep.wait_before_firewall]
    }
    # VPC對等串連Express Connect防火牆執行個體
    resource "alicloud_cloud_firewall_vpc_firewall" "default" {
      # 前置依賴
      depends_on = [
        null_resource.wait_for_firewall
      ]
      timeouts {
        create = "30m" # 給建立加上逾時時間
      }
      # 執行個體名稱
      vpc_firewall_name = "tf-test"
      # 使用者uid
      member_uid        = data.alicloud_account.current.id
      local_vpc {
        # 發起端vpc id
        vpc_id = alicloud_vpc.vpc.id
        # 地區
        region_no = "cn-heyuan"
        # 路由條目
        local_vpc_cidr_table_list {
          # 路由表id
          local_route_table_id = alicloud_vpc.vpc.route_table_id
          local_route_entry_list {
            # 下一跳
            local_next_hop_instance_id = alicloud_vpc_peer_connection.default.id
            # 目標網塊
            local_destination_cidr = alicloud_route_entry.foo.destination_cidrblock
          }
        }
      }
      peer_vpc {
        # 接收端vpc id
        vpc_id = alicloud_vpc.vpc1.id
        # 地區
        region_no = "cn-heyuan"
        # 路由條目
        peer_vpc_cidr_table_list {
          # 路由表id
          peer_route_table_id = alicloud_vpc.vpc1.route_table_id
          peer_route_entry_list {
            # 目標網塊
            peer_destination_cidr = alicloud_route_entry.foo1.destination_cidrblock
            # 下一跳
            peer_next_hop_instance_id = alicloud_vpc_peer_connection.default.id
          }
        }
      }
      # 資源的狀態。有效值:
      # open: 建立 VPC 邊界防火牆後,保護機制自動啟用。
      # close: 建立 VPC 邊界防火牆後,不自動啟用保護。
      status = "open"
    }
    output "vpc_id" {
      value = alicloud_vpc.vpc.id
    }
    output "vpc1_id" {
      value = alicloud_vpc.vpc1.id
    }
    output "route_table_id_vpc" {
      value = alicloud_vpc.vpc.route_table_id
    }
    output "route_table_id_vpc1" {
      value = alicloud_vpc.vpc1.route_table_id
    }
    output "foo_nexthop_id" {
      value = alicloud_vpc_peer_connection.default.id
    }
    output "foo1_nexthop_id" {
      value = alicloud_vpc_peer_connection.default.id
    }
    output "cidrblock" {
      value = alicloud_route_entry.foo.destination_cidrblock
    }
    output "cidrblock1" {
      value = alicloud_route_entry.foo1.destination_cidrblock
    }
  2. 執行以下命令,初始化Terraform運行環境。

    terraform init

    返回如下資訊,表示Terraform初始化成功。

    Initializing the backend...
    
    Initializing provider plugins...
    - Checking for available provider plugins...
    - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.203.0...
    
    
    Warning: registry.terraform.io: For users on Terraform 0.13 or greater, this provider has moved to aliyun/alicloud. Please update your source in required_providers.
    
    
    Terraform has been successfully initialized!
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. 建立執行計畫,並預覽變更。

    terraform plan
  4. 執行以下命令,建立VPC邊界防火牆來防護通過Express Connect串連的兩個VPC之間的流量。

    terraform apply

    在執行過程中,根據提示輸入yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示建立VPC邊界防火牆成功。

    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    
    alicloud_vpc.vpc: Creating...
    alicloud_vpc.vpc1: Creating...
    alicloud_vpc.vpc1: Creation complete after 6s [id=vpc-f8z3bgpc9436064a***]
    alicloud_vswitch.vsw2: Creating...
    alicloud_vswitch.vsw3: Creating...
    alicloud_vpc.vpc: Creation complete after 6s [id=vpc-f8zbmuyrti2q3t3exi***]
    alicloud_vpc_peer_connection.default: Creating...
    alicloud_vswitch.vsw1: Creating...
    alicloud_vswitch.vsw: Creating...
    alicloud_vswitch.vsw3: Creation complete after 4s [id=vsw-f8zxfkuawt6h3zorst***]
    alicloud_vswitch.vsw: Creation complete after 4s [id=vsw-f8zbfkhc4odb6fv3y4***]
    alicloud_vpc_peer_connection.default: Creation complete after 6s [id=pcc-rwz8io7yddag5y***]
    alicloud_vpc_peer_connection_accepter.default: Creating...
    alicloud_vswitch.vsw1: Creation complete after 7s [id=vsw-f8z88qcqyfbb5x2nj9***]
    alicloud_vswitch.vsw2: Creation complete after 7s [id=vsw-f8zqv2i961em95c7bv***]
    alicloud_vpc_peer_connection_accepter.default: Creation complete after 6s [id=pcc-rwz8io7yddag5ya***]
    alicloud_route_entry.foo: Creating...
    alicloud_route_entry.foo1: Creating...
    alicloud_route_entry.foo1: Creation complete after 6s [id=vtb-f8zbaphqlvdnb7njt1***:vrt-f8ze0dot16bcip5o8d***:4.3.X.X/32:VpcPeer:pcc-rwz8io7yddag5y***]
    alicloud_route_entry.foo: Creation complete after 6s [id=vtb-f8zukaban4cfna8f8k***:vrt-f8z23psp6f1ecy44z7***:1.2.3.4/32:VpcPeer:pcc-rwz8io7yddag5ya***]
    time_sleep.wait_before_firewall: Creating...
    time_sleep.wait_before_firewall: Still creating... [10s elapsed]
    time_sleep.wait_before_firewall: Still creating... [20s elapsed]
    time_sleep.wait_before_firewall: Still creating... [30s elapsed]
    time_sleep.wait_before_firewall: Still creating... [40s elapsed]
    time_sleep.wait_before_firewall: Still creating... [50s elapsed]
    time_sleep.wait_before_firewall: Still creating... [1m0s elapsed]
    time_sleep.wait_before_firewall: Still creating... [1m10s elapsed]
    time_sleep.wait_before_firewall: Still creating... [1m20s elapsed]
    time_sleep.wait_before_firewall: Still creating... [1m30s elapsed]
    time_sleep.wait_before_firewall: Still creating... [1m40s elapsed]
    time_sleep.wait_before_firewall: Still creating... [1m50s elapsed]
    time_sleep.wait_before_firewall: Still creating... [2m0s elapsed]
    time_sleep.wait_before_firewall: Still creating... [2m10s elapsed]
    time_sleep.wait_before_firewall: Still creating... [2m20s elapsed]
    time_sleep.wait_before_firewall: Still creating... [2m30s elapsed]
    time_sleep.wait_before_firewall: Still creating... [2m40s elapsed]
    time_sleep.wait_before_firewall: Still creating... [2m50s elapsed]
    time_sleep.wait_before_firewall: Still creating... [3m0s elapsed]
    time_sleep.wait_before_firewall: Still creating... [3m10s elapsed]
    time_sleep.wait_before_firewall: Still creating... [3m20s elapsed]
    time_sleep.wait_before_firewall: Still creating... [3m30s elapsed]
    time_sleep.wait_before_firewall: Still creating... [3m40s elapsed]
    time_sleep.wait_before_firewall: Still creating... [3m50s elapsed]
    time_sleep.wait_before_firewall: Still creating... [4m0s elapsed]
    time_sleep.wait_before_firewall: Still creating... [4m10s elapsed]
    time_sleep.wait_before_firewall: Still creating... [4m20s elapsed]
    time_sleep.wait_before_firewall: Still creating... [4m30s elapsed]
    time_sleep.wait_before_firewall: Still creating... [4m40s elapsed]
    time_sleep.wait_before_firewall: Still creating... [4m50s elapsed]
    time_sleep.wait_before_firewall: Still creating... [5m0s elapsed]
    time_sleep.wait_before_firewall: Still creating... [5m10s elapsed]
    time_sleep.wait_before_firewall: Still creating... [5m20s elapsed]
    time_sleep.wait_before_firewall: Still creating... [5m30s elapsed]
    time_sleep.wait_before_firewall: Still creating... [5m40s elapsed]
    time_sleep.wait_before_firewall: Creation complete after 12m0s [id=2024-11-05T01:44:57Z]
    null_resource.wait_for_firewall: Creating...
    null_resource.wait_for_firewall: Provisioning with 'local-exec'...
    null_resource.wait_for_firewall (local-exec): Executing: ["/bin/sh" "-c" "echo waiting for firewall to be ready"]
    null_resource.wait_for_firewall (local-exec): waiting for firewall to be ready
    null_resource.wait_for_firewall: Creation complete after 0s [id=5344790266853010843]
    alicloud_cloud_firewall_vpc_firewall.default: Creating...
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [10s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [20s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [30s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [40s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [50s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [1m0s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [1m10s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [1m20s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [1m30s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [1m40s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [1m50s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [2m0s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [2m10s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [2m20s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [2m30s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [2m40s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [2m50s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [3m0s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [3m10s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [3m20s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [3m30s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [3m40s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [3m50s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [4m0s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [4m10s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [4m20s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [4m30s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [4m40s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [4m50s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [5m0s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [5m10s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [5m20s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [5m30s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [5m40s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [5m50s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [6m0s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [6m10s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [6m20s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [6m30s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [6m40s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [6m50s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [7m0s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [7m10s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [7m20s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [7m30s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [7m40s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [7m50s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [8m0s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [8m10s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [8m20s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [8m30s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [8m40s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [8m50s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [9m0s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [9m10s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [9m20s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [9m30s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [9m40s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [9m50s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [10m0s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [10m10s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [10m20s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [10m30s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [10m40s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [10m50s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [11m0s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [11m10s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [11m20s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [11m30s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [11m40s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [11m50s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [12m0s elapsed]
    alicloud_cloud_firewall_vpc_firewall.default: Creation complete after 12m1s [id=vfw-782be77253a0462e8***]
    
    Apply complete! Resources: 13 added, 0 changed, 0 destroyed.
  5. 驗證結果

    執行terraform show命令

    您可以使用以下命令查詢Terraform已建立的資來源詳細資料。

    terraform show

    image

    登入Cloud Firewall控制台

    登入Cloud Firewall控制台,在防火牆開關>VPC邊界防火牆頁面,搜尋VPC邊界防火牆執行個體ID查看詳細資料。

清理資源

當您不再需要上述通過Terraform建立或管理的資源時,請運行以下命令以釋放資源。關於terraform destroy的更多資訊,請參見常用命令

terraform destroy

完整樣本

說明

當前範例程式碼支援一鍵運行,您可以直接運行代碼。

範例程式碼

variable "region" {
  default = "cn-heyuan"
}
provider "alicloud" {
  region = var.region
}
# 擷取當前阿里雲uid
data "alicloud_account" "current" {
}
# 建立VPC 1
resource "alicloud_vpc" "vpc" {
  vpc_name   = "dd-tf-vpc-01"
  cidr_block = "192.168.0.0/16"
}
# 建立VPC 2
resource "alicloud_vpc" "vpc1" {
  vpc_name   = "dd-tf-vpc-02"
  cidr_block = "172.16.0.0/12"
}
# 建立一個Vswitch CIDR 塊為 192.168.10.0/24
resource "alicloud_vswitch" "vsw" {
  vpc_id       = alicloud_vpc.vpc.id
  cidr_block   = "192.168.10.0/24"
  zone_id      = "cn-heyuan-a"
  vswitch_name = "dd-tf-vpc-01-example-1"
}
# 建立另一個Vswitch CIDR 塊為 192.168.20.0/24
resource "alicloud_vswitch" "vsw1" {
  vpc_id       = alicloud_vpc.vpc.id
  cidr_block   = "192.168.20.0/24"
  zone_id      = "cn-heyuan-b"
  vswitch_name = "dd-tf-vpc-01-example-2"
}
# 建立一個Vswitch CIDR 塊為 172.16.10.0/24
resource "alicloud_vswitch" "vsw2" {
  vpc_id       = alicloud_vpc.vpc1.id
  cidr_block   = "172.16.10.0/24"
  zone_id      = "cn-heyuan-a"
  vswitch_name = "dd-tf-vpc-02-example-11"
}
# 建立另一個Vswitch CIDR 塊為 172.16.20.0/24
resource "alicloud_vswitch" "vsw3" {
  vpc_id       = alicloud_vpc.vpc1.id
  cidr_block   = "172.16.20.0/24"
  zone_id      = "cn-heyuan-b"
  vswitch_name = "dd-tf-vpc-02-example-22"
}
# 建立VPC對等串連
resource "alicloud_vpc_peer_connection" "default" {
  # 對等串連名稱
  peer_connection_name = "terraform-example-vpc-peer-connection"
  # 發起方VPC_ID
  vpc_id = alicloud_vpc.vpc.id
  # 接收方 VPC 對等串連的 Alibaba Cloud 帳號 ID
  accepting_ali_uid = data.alicloud_account.current.id
  # 接收方 VPC 對等串連的地區 ID。同地區建立時,輸入與發起方相同的地區 ID;跨地區建立時,輸入不同的地區 ID。
  accepting_region_id = "cn-heyuan"
  # 接收端VPC_ID
  accepting_vpc_id = alicloud_vpc.vpc1.id
  # 描述
  description = "terraform-example"
  # 是否強制移除
  force_delete = true
}
# 接收端
resource "alicloud_vpc_peer_connection_accepter" "default" {
  instance_id = alicloud_vpc_peer_connection.default.id
  # 是否強制移除
  force_delete = true
}
# 配置路由條目-vpc-A
resource "alicloud_route_entry" "foo" {
  # VPC-A 路由表ID
  route_table_id = alicloud_vpc.vpc.route_table_id
  # 目標網段,自訂
  destination_cidrblock = "1.2.3.4/32"
  # 下一跳類型
  nexthop_type = "VpcPeer"
  # 下一跳id
  nexthop_id = alicloud_vpc_peer_connection.default.id
}
# 配置路由條目2 -vpc-B
resource "alicloud_route_entry" "foo1" {
  # VPC-A 路由表id
  route_table_id = alicloud_vpc.vpc1.route_table_id
  # 目標網段,自訂
  destination_cidrblock = "4.3.X.X/32"
  # 下一跳類型
  nexthop_type = "VpcPeer"
  # 下一跳id
  nexthop_id = alicloud_vpc_peer_connection.default.id
}
# 先建立其他前置資源
resource "time_sleep" "wait_before_firewall" {
  # 確保雲企業網執行個體,網路連接執行個體建立好後
  depends_on = [
    alicloud_route_entry.foo,
    alicloud_route_entry.foo1
  ]
  create_duration = "720s" # 根據需要設定時間
}
# 延遲
resource "null_resource" "wait_for_firewall" {
  provisioner "local-exec" {
    command = "echo waiting for firewall to be ready"
  }
  # 確保前置資源建立
  depends_on = [time_sleep.wait_before_firewall]
}
# VPC對等串連Express Connect防火牆執行個體
resource "alicloud_cloud_firewall_vpc_firewall" "default" {
  # 前置依賴
  depends_on = [
    null_resource.wait_for_firewall
  ]
  timeouts {
    create = "30m" # 給建立加上逾時時間
  }
  # 執行個體名稱
  vpc_firewall_name = "tf-test"
  member_uid        = data.alicloud_account.current.id
  local_vpc {
    # 發起端vpc id
    vpc_id = alicloud_vpc.vpc.id
    # 地區
    region_no = "cn-heyuan"
    # 路由條目
    local_vpc_cidr_table_list {
      # 路由表id
      local_route_table_id = alicloud_vpc.vpc.route_table_id
      local_route_entry_list {
        # 下一跳
        local_next_hop_instance_id = alicloud_vpc_peer_connection.default.id
        # 目標網塊
        local_destination_cidr = alicloud_route_entry.foo.destination_cidrblock
      }
    }
  }
  peer_vpc {
    # 接收端vpc id
    vpc_id = alicloud_vpc.vpc1.id
    # 地區
    region_no = "cn-heyuan"
    # 路由條目
    peer_vpc_cidr_table_list {
      # 路由表id
      peer_route_table_id = alicloud_vpc.vpc1.route_table_id
      peer_route_entry_list {
        # 目標網塊
        peer_destination_cidr = alicloud_route_entry.foo1.destination_cidrblock
        # 下一跳
        peer_next_hop_instance_id = alicloud_vpc_peer_connection.default.id
      }
    }
  }
  # 資源的狀態。有效值:
  # open: 建立 VPC 邊界防火牆後,保護機制自動啟用。
  # close: 建立 VPC 邊界防火牆後,不自動啟用保護。
  status = "open"
}
output "vpc_id" {
  value = alicloud_vpc.vpc.id
}
output "vpc1_id" {
  value = alicloud_vpc.vpc1.id
}
output "route_table_id_vpc" {
  value = alicloud_vpc.vpc.route_table_id
}
output "route_table_id_vpc1" {
  value = alicloud_vpc.vpc1.route_table_id
}
output "foo_nexthop_id" {
  value = alicloud_vpc_peer_connection.default.id
}
output "foo1_nexthop_id" {
  value = alicloud_vpc_peer_connection.default.id
}
output "cidrblock" {
  value = alicloud_route_entry.foo.destination_cidrblock
}
output "cidrblock1" {
  value = alicloud_route_entry.foo1.destination_cidrblock
}