本文介紹如何使用Terraform建立VPC邊界防火牆(防護雲企業網基礎版的網路執行個體和指定VPC之間的互訪流量)。
當前範例程式碼支援一鍵運行,您可以直接運行代碼。
前提條件
由於阿里雲帳號(主帳號)具有資源的所有許可權,一旦發生泄露將面臨重大風險。建議您使用RAM使用者,並為該RAM使用者建立AccessKey,具體操作方式請參見建立RAM使用者和建立AccessKey。
使用以下樣本為RAM使用者授權,需要為該RAM使用者授予以下許可權:Cloud FirewallCloudFirewall許可權。具體操作方式請參見為RAM使用者授權。
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "yundun-cloudfirewall:*", "yundun-ndr:*" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "vpc:*" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "vpc:CreateVSwitch", "vpc:DescribeVSwitches", "vpc:DeleteVSwitch" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "cen:*" ], "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_cen:VPC邊界防火牆。
建立VPC邊界防火牆
本樣本將建立一個VPC邊界防火牆。
建立一個工作目錄,並且在工作目錄中建立以下名為
main.tf的設定檔。main.tfTerraform主檔案,定義了將要部署的資源。variable "region" { default = "cn-qingdao" } provider "alicloud" { region = var.region } data "alicloud_account" "current" { } # 建立VPC 1 resource "alicloud_vpc" "vpc" { vpc_name = "cen-vpc-01" cidr_block = "172.16.0.0/12" } # 建立VPC 2 resource "alicloud_vpc" "vpc1" { vpc_name = "cen-vpc-02" cidr_block = "172.16.0.0/12" } # 建立一個Vswitch CIDR 塊為 172.16.1.0/24 resource "alicloud_vswitch" "vsw" { vpc_id = alicloud_vpc.vpc.id cidr_block = "172.16.1.0/24" zone_id = "cn-qingdao-b" vswitch_name = "terraform-example-1" } # 建立另一個Vswitch CIDR 塊為 172.16.0.0/24 resource "alicloud_vswitch" "vsw1" { vpc_id = alicloud_vpc.vpc.id cidr_block = "172.16.0.0/24" zone_id = "cn-qingdao-c" vswitch_name = "terraform-example-2" } # 建立一個Vswitch CIDR 塊為 172.16.4.0/24 resource "alicloud_vswitch" "vsw2" { vpc_id = alicloud_vpc.vpc1.id cidr_block = "172.16.4.0/24" zone_id = "cn-qingdao-b" vswitch_name = "terraform-example-11" } # 建立另一個Vswitch CIDR 塊為 172.16.5.0/24 resource "alicloud_vswitch" "vsw3" { vpc_id = alicloud_vpc.vpc1.id cidr_block = "172.16.5.0/24" zone_id = "cn-qingdao-c" vswitch_name = "terraform-example-22" } # 雲企業網執行個體 resource "alicloud_cen_instance" "example" { # CEN執行個體名稱 cen_instance_name = "tf_example" # 描述 description = "an example for cen" } # 轉寄路由器建立網路執行個體串連 1 resource "alicloud_cen_instance_attachment" "example" { instance_id = alicloud_cen_instance.example.id child_instance_id = alicloud_vpc.vpc.id child_instance_type = "VPC" child_instance_region_id = "cn-qingdao" } # 轉寄路由器建立網路執行個體串連 2 resource "alicloud_cen_instance_attachment" "example1" { instance_id = alicloud_cen_instance.example.id child_instance_id = alicloud_vpc.vpc1.id child_instance_type = "VPC" child_instance_region_id = "cn-qingdao" } # 先建立雲企業網執行個體和網路連接執行個體 resource "time_sleep" "wait_before_firewall" { # 確保雲企業網執行個體,網路連接執行個體建立好後 depends_on = [ alicloud_cen_instance_attachment.example, alicloud_cen_instance_attachment.example1 ] 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] } # 雲企業(基礎版)執行個體 resource "alicloud_cloud_firewall_vpc_firewall_cen" "default" { # 雲企業網執行個體,網路連接執行個體,延遲確保建立 depends_on = [ null_resource.wait_for_firewall ] timeouts { create = "30m" # 給建立加上逾時時間 } # CEN 執行個體的 ID cen_id = alicloud_cen_instance.example.id # VPC 的詳細資料 local_vpc { # 建立 VPC 防火牆的 VPC 執行個體 ID。 network_instance_id = alicloud_cen_instance_attachment.example.child_instance_id } # 防火牆開關狀態。 status = "open" # 當前阿里雲賬戶的成員賬戶 UID 可選 member_uid = data.alicloud_account.current.id # 建立 VPC 的地區 ID。 vpc_region = var.region # VPC 防火牆執行個體的名稱。 vpc_firewall_name = "tf-test" }執行以下命令,初始化
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... 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.建立執行計畫,並預覽變更。
terraform plan執行以下命令,建立VPC邊界防火牆(防護雲企業網基礎版的網路執行個體和指定VPC之間的互訪流量)。
terraform apply在執行過程中,根據提示輸入
yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示建立VPC邊界防火牆(防護雲企業網基礎版的網路執行個體和指定VPC之間的互訪流量)成功。Plan: 12 to add, 0 to change, 0 to destroy. 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.vpc1: Creating... alicloud_cen_instance.example: Creating... alicloud_vpc.vpc: Creating... alicloud_cen_instance.example: Creation complete after 6s [id=cen-igzi112vm9qo95y***] alicloud_vpc.vpc: Creation complete after 6s [id=vpc-m5e80rjklhxmpvjpc6***] alicloud_vswitch.vsw: Creating... alicloud_vpc.vpc1: Creation complete after 6s [id=vpc-m5e31tqo1qwevzb8xy***] alicloud_vswitch.vsw1: Creating... alicloud_cen_instance_attachment.example: Creating... alicloud_vswitch.vsw2: Creating... alicloud_cen_instance_attachment.example1: Creating... alicloud_vswitch.vsw3: Creating... alicloud_vswitch.vsw: Creation complete after 3s [id=vsw-m5e8ziezyqzqv0sc61***] alicloud_vswitch.vsw2: Creation complete after 3s [id=vsw-m5eefi2ucwn9v6cf9d***] alicloud_vswitch.vsw1: Creation complete after 7s [id=vsw-m5e60062s0hrie96wd***] alicloud_vswitch.vsw3: Creation complete after 7s [id=vsw-m5eqoruknlh503wmtf***] alicloud_cen_instance_attachment.example: Still creating... [10s elapsed] alicloud_cen_instance_attachment.example1: Still creating... [10s elapsed] alicloud_cen_instance_attachment.example: Still creating... [20s elapsed] alicloud_cen_instance_attachment.example1: Still creating... [20s elapsed] alicloud_cen_instance_attachment.example: Creation complete after 25s [id=cen-igzi112vm9qo95y***:vpc-m5e80rjklhxmpvjpc6***:VPC:cn-qingdao] alicloud_cen_instance_attachment.example1: Still creating... [30s elapsed] alicloud_cen_instance_attachment.example1: Creation complete after 35s [id=cen-igzi112vm9qo95y***:vpc-m5e31tqo1qwevzb8xy***:VPC:cn-qingdao] 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: Creation complete after 1m21s [id=2024-10-31T08:49:08Z] 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=8138826525954951***] alicloud_cloud_firewall_vpc_firewall_cen.default: Creating... alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [10s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [20s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [30s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [40s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [50s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [1m0s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [1m10s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [1m20s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [1m30s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [1m40s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [1m50s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [2m0s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [2m10s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [2m20s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [2m30s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [2m40s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [2m50s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [3m0s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [3m10s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [3m20s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [3m30s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [3m40s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [3m50s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [4m0s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [4m10s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [4m20s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [4m30s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [4m40s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Still creating... [4m50s elapsed] alicloud_cloud_firewall_vpc_firewall_cen.default: Creation complete after 4m55s [id=vfw-1bc3b436d49b46ac8***] Apply complete! Resources: 12 added, 0 changed, 0 destroyed.驗證結果
執行terraform show命令
您可以使用以下命令查詢Terraform已建立的資來源詳細資料。
terraform show
登入Cloud Firewall控制台
登入Cloud Firewall控制台,在防火牆開關>VPC邊界防火牆頁面,搜尋VPC邊界防火牆執行個體ID查看詳細資料。
清理資源
當您不再需要上述通過Terraform建立或管理的資源時,請運行以下命令以釋放資源。關於terraform destroy的更多資訊,請參見常用命令。
terraform destroy完整樣本
當前範例程式碼支援一鍵運行,您可以直接運行代碼。