全部產品
Search
文件中心

Cloud Firewall:通過Terraform建立Cloud Firewall執行個體

更新時間:Apr 24, 2025

本文介紹如何使用Terraform建立互連網防火牆執行個體。

說明

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

前提條件

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

  • 使用以下樣本為RAM使用者授權,需要為該RAM使用者授予以下許可權:Cloud FirewallCloudFirewall許可權和AliyunBSSFullAccess(管理費用中心BSS)許可權。具體操作方式請參見為RAM使用者授權

    {
        "Version": "1",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "yundun-cloudfirewall:CreateFirewall",
                    "yundun-cloudfirewall:DescribeFirewalls",
                    "yundun-cloudfirewall:DeleteFirewall"
                ],
                "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_instance:建立Cloud Firewall執行個體。

建立防火牆執行個體

本樣本將建立Cloud Firewall執行個體。

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

    resource "alicloud_cloud_firewall_instance" "example" {
      # 資源的支付類型。有效值:Subscription(訂閱),PayAsYouGo(按需計費)。
      payment_type    = "Subscription"
      # 目前的版本。premium_version(進階版)、enterprise_version(企業版)、ultimate_version(終極版)。
      spec            = "premium_version"
      # 可保護的公網 IP 數量。有效值:20 到 4000。
      ip_number       = 20
      # 公用網路處理能力。有效值:10 到 15000。單位:Mbps。
      band_width      = 10
      # 是否使用日誌審計。有效值:true,false。
      cfw_log         = false
      # 日誌儲存容量。當 cfw_log = false 時將被忽略。
      cfw_log_storage = 1000
      # 屬性 cfw_service 不再支援更長的設定,已於 v1.209.1 版本中移除。
      # cfw_service     = false
      # 預付費期。有效值:1, 3, 6, 12, 24, 36。
      period          = 1
    }
  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. 執行以下命令,建立Cloud Firewall執行個體。

    terraform apply

    在執行過程中,根據提示輸入yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示建立Cloud Firewall執行個體成功。

    An execution plan has been generated and is shown below.
    Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # alicloud_cloud_firewall_instance.example will be created
      + resource "alicloud_cloud_firewall_instance" "example" {
          + band_width            = 10
          + cfw_log               = false
          + cfw_log_storage       = 1000
          + cfw_service           = false
          + create_time           = (known after apply)
          + end_time              = (known after apply)
          + id                    = (known after apply)
          + ip_number             = 20
          + payment_type          = "Subscription"
          + period                = 6
          + release_time          = (known after apply)
          + renewal_duration_unit = (known after apply)
          + renewal_status        = (known after apply)
          + spec                  = "premium_version"
          + status                = (known after apply)
        }
    
    Plan: 1 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_cloud_firewall_instance.example: Creating...
    alicloud_cloud_firewall_instance.example: Creation complete after 4s [id=vipcloudfw-cn-x0r36mo****]
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
  5. 驗證結果

    執行terraform show命令

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

    terraform show

    image

    登入Cloud Firewall控制台

    登入Cloud Firewall控制台,在總覽頁面,單擊升級查看Cloud Firewall執行個體的當前配置。

清理資源

重要

目前無法通過 Terraform 退訂預付費的防火牆執行個體,執行 terraform destroy 後會將資源從狀態檔案中移除,請及時到控制台上進行退訂!

完整樣本

說明

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

範例程式碼

resource "alicloud_cloud_firewall_instance" "example" {
  # 資源的支付類型。有效值:Subscription(訂閱),PayAsYouGo(按需計費)。
  payment_type    = "Subscription"
  # 目前的版本。premium_version(進階版)、enterprise_version(企業版)、ultimate_version(終極版)。
  spec            = "premium_version"
  # 可保護的公網 IP 數量。有效值:20 到 4000。
  ip_number       = 20
  # 公用網路處理能力。有效值:10 到 15000。單位:Mbps。
  band_width      = 10
  # 是否使用日誌審計。有效值:true,false。
  cfw_log         = false
  # 日誌儲存容量。當 cfw_log = false 時將被忽略。
  cfw_log_storage = 1000
  # 屬性 cfw_service 不再支援更長的設定,已於 v1.209.1 版本中移除。
  # cfw_service     = false
  # 預付費期。有效值:1, 3, 6, 12, 24, 36。
  period          = 1
}