本文介紹如何使用Terraform建立雲企業網(企業版)的VPC邊界防火牆執行個體。
當前範例程式碼支援一鍵運行,您可以直接運行代碼。
前提條件
由於阿里雲帳號(主帳號)具有資源的所有許可權,一旦發生泄露將面臨重大風險。建議您使用RAM使用者,並為該RAM使用者建立AccessKey,具體操作方式請參見建立RAM使用者和建立AccessKey。
使用以下樣本為RAM使用者授權,需要為該RAM使用者授予以下許可權:
Cloud FirewallCloudFirewall許可權和AliyunBSSFullAccess(管理費用中心BSS)許可權。具體操作方式請參見為RAM使用者授權。
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "yundun-cloudfirewall:CreateVpcFirewallCenConfigure", "yundun-cloudfirewall:CreateVpcFirewallCenManualConfigure", "yundun-cloudfirewall:ModifyVpcFirewallCenConfigure", "yundun-cloudfirewall:ModifyVpcFirewallCenSwitchStatus", "yundun-cloudfirewall:DeleteVpcFirewallCenConfigure", "yundun-cloudfirewall:DescribeVpcFirewallCenDetail", "yundun-cloudfirewall:DescribeVpcFirewallCenList" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "bssapi:*", "bss:*" ], "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_cen_tr_firewall:VPC防護牆-雲企業網(企業版)。
建立VPC邊界防火牆
操作步驟
本樣本將建立一個雲企業網(企業版)的VPC邊界防火牆。
建立一個工作目錄,並且在工作目錄中建立名為
main.tf的設定檔。main.tfTerraform主檔案,定義了將要部署的資源。內容如下:variable "name" { default = "terraform-example" } provider "alicloud" { region = "cn-hangzhou" } variable "description" { default = "Created by Terraform" } variable "firewall_name" { default = "tf-example" } variable "tr_attachment_master_cidr" { default = "192.168.3.192/26" } variable "firewall_subnet_cidr" { default = "192.168.3.0/25" } variable "region" { default = "cn-hangzhou" } variable "tr_attachment_slave_cidr" { default = "192.168.3.128/26" } variable "firewall_vpc_cidr" { default = "192.168.3.0/24" } variable "zone1" { default = "cn-hangzhou-h" } variable "firewall_name_update" { default = "tf-example-1" } variable "zone2" { default = "cn-hangzhou-i" } data "alicloud_cen_transit_router_available_resources" "default" { } data "alicloud_zones" "default" { available_resource_creation = "VSwitch" } resource "alicloud_cen_instance" "cen" { description = "terraform example" cen_instance_name = var.name } resource "alicloud_cen_transit_router" "tr" { transit_router_name = var.name transit_router_description = "tr-created-by-terraform" cen_id = alicloud_cen_instance.cen.id } resource "alicloud_vpc" "vpc1" { description = "created by terraform" cidr_block = "192.168.1.0/24" vpc_name = var.name } resource "alicloud_vswitch" "vpc1vsw1" { cidr_block = "192.168.1.0/25" vswitch_name = var.name vpc_id = alicloud_vpc.vpc1.id zone_id = data.alicloud_cen_transit_router_available_resources.default.resources[0].master_zones[1] } resource "alicloud_vswitch" "vpc1vsw2" { vpc_id = alicloud_vpc.vpc1.id cidr_block = "192.168.1.128/26" vswitch_name = var.name zone_id = data.alicloud_cen_transit_router_available_resources.default.resources[0].master_zones[2] } resource "alicloud_route_table" "foo" { vpc_id = alicloud_vpc.vpc1.id route_table_name = var.name description = var.name } resource "alicloud_cen_transit_router_vpc_attachment" "tr-vpc1" { zone_mappings { vswitch_id = alicloud_vswitch.vpc1vsw1.id zone_id = data.alicloud_cen_transit_router_available_resources.default.resources[0].master_zones[1] } zone_mappings { zone_id = data.alicloud_cen_transit_router_available_resources.default.resources[0].master_zones[2] vswitch_id = alicloud_vswitch.vpc1vsw2.id } vpc_id = alicloud_vpc.vpc1.id cen_id = alicloud_cen_instance.cen.id transit_router_id = alicloud_cen_transit_router.tr.transit_router_id depends_on = [alicloud_route_table.foo] } resource "time_sleep" "wait_10_minutes" { depends_on = [alicloud_cen_transit_router_vpc_attachment.tr-vpc1] create_duration = "10m" } resource "alicloud_cloud_firewall_vpc_cen_tr_firewall" "default" { cen_id = alicloud_cen_transit_router_vpc_attachment.tr-vpc1.cen_id firewall_name = var.name firewall_subnet_cidr = var.firewall_subnet_cidr tr_attachment_slave_cidr = var.tr_attachment_slave_cidr firewall_description = "VpcCenTrFirewall created by terraform" region_no = var.region tr_attachment_master_cidr = var.tr_attachment_master_cidr firewall_vpc_cidr = var.firewall_vpc_cidr transit_router_id = alicloud_cen_transit_router.tr.transit_router_id route_mode = "managed" depends_on = [time_sleep.wait_10_minutes] }執行以下命令,初始化
Terraform運行環境。terraform init返回如下資訊,表示Terraform初始化成功。
Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "time" (hashicorp/time) 0.12.1... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.238.0... The following providers do not have any version constraints in configuration, so the latest version was installed. To prevent automatic upgrades to new major versions that may contain breaking changes, it is recommended to add version = "..." constraints to the corresponding provider blocks in configuration, with the constraint strings suggested below. * provider.alicloud: version = "~> 1.238" * provider.time: version = "~> 0.12" 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.建立執行計畫,並預覽變更。
terraform plan執行以下命令,建立VPC邊界防火牆 。
terraform apply在執行過程中,根據提示輸入
yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示VPC邊界防火牆建立成功。說明根據
main.tf中定義的資源,無執行個體會建立,有執行個體則變更,不同帳號下的資源不同,plan和apply時提示的數量也不同。請以使用帳號下的資源實際情況為準。alicloud_vpc.vpc1: Creating... alicloud_vpc.vpc1: Creation complete after 7s [id=vpc-bp1apj641aaonvpocqrn8] alicloud_route_table.foo: Creating... alicloud_vswitch.vpc1vsw1: Creating... alicloud_vswitch.vpc1vsw2: Creating... alicloud_route_table.foo: Creation complete after 3s [id=vtb-bp1owz6rahb5oqftbd4pt] alicloud_vswitch.vpc1vsw1: Creation complete after 7s [id=vsw-bp1kqr2wtnz9nyqzpqpvg] alicloud_vswitch.vpc1vsw2: Still creating... [10s elapsed] alicloud_vswitch.vpc1vsw2: Creation complete after 13s [id=vsw-bp1dpzmh55f6ezk3m1q02] alicloud_cen_transit_router_vpc_attachment.tr-vpc1: Creating... alicloud_cen_transit_router_vpc_attachment.tr-vpc1: Still creating... [10s elapsed] alicloud_cen_transit_router_vpc_attachment.tr-vpc1: Still creating... [20s elapsed] alicloud_cen_transit_router_vpc_attachment.tr-vpc1: Still creating... [30s elapsed] alicloud_cen_transit_router_vpc_attachment.tr-vpc1: Still creating... [40s elapsed] alicloud_cen_transit_router_vpc_attachment.tr-vpc1: Still creating... [50s elapsed] alicloud_cen_transit_router_vpc_attachment.tr-vpc1: Still creating... [1m0s elapsed] alicloud_cen_transit_router_vpc_attachment.tr-vpc1: Still creating... [1m10s elapsed] alicloud_cen_transit_router_vpc_attachment.tr-vpc1: Creation complete after 1m17s [id=cen-9sbqmk479yu5y1y66d:tr-attach-e9ji0mjibg9y10ets4] time_sleep.wait_10_minutes: Creating... time_sleep.wait_10_minutes: Still creating... [10s elapsed] time_sleep.wait_10_minutes: Still creating... [20s elapsed] time_sleep.wait_10_minutes: Still creating... [30s elapsed] time_sleep.wait_10_minutes: Still creating... [40s elapsed] time_sleep.wait_10_minutes: Still creating... [50s elapsed] time_sleep.wait_10_minutes: Still creating... [1m0s elapsed] time_sleep.wait_10_minutes: Still creating... [1m10s elapsed] time_sleep.wait_10_minutes: Still creating... [1m20s elapsed] time_sleep.wait_10_minutes: Still creating... [1m30s elapsed] time_sleep.wait_10_minutes: Still creating... [1m40s elapsed] time_sleep.wait_10_minutes: Still creating... [1m50s elapsed] time_sleep.wait_10_minutes: Still creating... [2m0s elapsed] time_sleep.wait_10_minutes: Still creating... [2m10s elapsed] time_sleep.wait_10_minutes: Still creating... [2m20s elapsed] time_sleep.wait_10_minutes: Still creating... [2m30s elapsed] time_sleep.wait_10_minutes: Still creating... [2m40s elapsed] time_sleep.wait_10_minutes: Still creating... [2m50s elapsed] time_sleep.wait_10_minutes: Still creating... [3m0s elapsed] time_sleep.wait_10_minutes: Still creating... [3m10s elapsed] time_sleep.wait_10_minutes: Still creating... [3m20s elapsed] time_sleep.wait_10_minutes: Still creating... [3m30s elapsed] time_sleep.wait_10_minutes: Still creating... [3m40s elapsed] time_sleep.wait_10_minutes: Still creating... [3m50s elapsed] time_sleep.wait_10_minutes: Still creating... [4m0s elapsed] time_sleep.wait_10_minutes: Still creating... [4m10s elapsed] time_sleep.wait_10_minutes: Still creating... [4m20s elapsed] time_sleep.wait_10_minutes: Still creating... [4m30s elapsed] time_sleep.wait_10_minutes: Still creating... [4m40s elapsed] time_sleep.wait_10_minutes: Still creating... [4m50s elapsed] time_sleep.wait_10_minutes: Still creating... [5m0s elapsed] time_sleep.wait_10_minutes: Still creating... [5m10s elapsed] time_sleep.wait_10_minutes: Still creating... [5m20s elapsed] time_sleep.wait_10_minutes: Still creating... [5m30s elapsed] time_sleep.wait_10_minutes: Still creating... [5m40s elapsed] time_sleep.wait_10_minutes: Still creating... [5m50s elapsed] time_sleep.wait_10_minutes: Still creating... [6m0s elapsed] time_sleep.wait_10_minutes: Still creating... [6m10s elapsed] time_sleep.wait_10_minutes: Still creating... [6m20s elapsed] time_sleep.wait_10_minutes: Still creating... [6m30s elapsed] time_sleep.wait_10_minutes: Still creating... [6m40s elapsed] time_sleep.wait_10_minutes: Still creating... [6m50s elapsed] time_sleep.wait_10_minutes: Still creating... [7m0s elapsed] time_sleep.wait_10_minutes: Still creating... [7m10s elapsed] time_sleep.wait_10_minutes: Still creating... [7m20s elapsed] time_sleep.wait_10_minutes: Still creating... [7m30s elapsed] time_sleep.wait_10_minutes: Still creating... [7m40s elapsed] time_sleep.wait_10_minutes: Still creating... [7m50s elapsed] time_sleep.wait_10_minutes: Still creating... [8m0s elapsed] time_sleep.wait_10_minutes: Still creating... [8m10s elapsed] time_sleep.wait_10_minutes: Still creating... [8m20s elapsed] time_sleep.wait_10_minutes: Still creating... [8m30s elapsed] time_sleep.wait_10_minutes: Still creating... [8m40s elapsed] time_sleep.wait_10_minutes: Still creating... [8m50s elapsed] time_sleep.wait_10_minutes: Still creating... [9m0s elapsed] time_sleep.wait_10_minutes: Still creating... [9m10s elapsed] time_sleep.wait_10_minutes: Still creating... [9m20s elapsed] time_sleep.wait_10_minutes: Still creating... [9m30s elapsed] time_sleep.wait_10_minutes: Still creating... [9m40s elapsed] time_sleep.wait_10_minutes: Still creating... [9m50s elapsed] time_sleep.wait_10_minutes: Still creating... [10m0s elapsed] time_sleep.wait_10_minutes: Creation complete after 10m0s [id=2024-12-17T03:55:32Z] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Creating... alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [10s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [20s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [30s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [40s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [50s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [1m0s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [1m10s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [1m20s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [1m30s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [1m40s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [1m50s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [2m0s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [2m10s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [2m20s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [2m30s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [2m40s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [2m50s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [3m0s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [3m10s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [3m20s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [3m30s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [3m40s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [3m50s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [4m0s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [4m10s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [4m20s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Still creating... [4m30s elapsed] alicloud_cloud_firewall_vpc_cen_tr_firewall.default: Creation complete after 4m39s [id=vfw-tr-f6524a0da36e44d285f8] Apply complete! Resources: 7 added, 0 changed, 0 destroyed.驗證結果
執行terraform show命令
您可以使用以下命令查詢Terraform已建立的資來源詳細資料。
terraform show登入Cloud Firewall控制台

清理資源
當您不再需要上述通過Terraform建立或管理的資源時,請運行以下命令以釋放資源。關於terraform destroy的更多資訊,請參見常用命令。
terraform destroy完整樣本
當前範例程式碼支援在OpenAPI門戶線上調試。