本文介紹如何使用Terraform建立地址簿。
注意事項
Terraform運行環境:阿里雲Cloud Shell
Terraform版本:0.12
alicloud版本:1.203.0
resource定義:cloud_firewall_address_book
前提條件
已建立阿里雲帳號和存取金鑰(AccessKey)。具體操作,請參見建立AccessKey。
已經安裝並配置了Terraform。
建立地址簿
在Terraform執行目錄下的
terraform.tf
檔案中,配置如下內容。程式碼範例如下:
provider "alicloud" { version = "~> 1.203.0" } resource "alicloud_cloud_firewall_address_book" "example" { description = "Created_by_terraform" group_name = "IPListExample" group_type = "ip" address_list = ["192.0.2.1/32", "192.0.2.2/32"] }
運行
terraform init
,對環境進行初始化。程式碼範例如下:
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.
運行
terraform apply
。出現如下配置資訊後,確認配置資訊並輸入yes,開始建立。程式碼範例如下,建立成功後,可以返回地址簿ID為1d89edf3-5711-41e6-b8dd-0c1ac080****。
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_address_book.example will be created + resource "alicloud_cloud_firewall_address_book" "example" { + address_list = [ + "192.0.2.1/32", + "192.0.2.2/32", ] + description = "Created_by_terraform" + group_name = "IPListExample" + group_type = "ip" + id = (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_address_book.example: Creating... alicloud_cloud_firewall_address_book.example: Creation complete after 2s [id=1d89edf3-5711-41e6-b8dd-0c1ac080****]
查看結果。
運行
terraform show
查看地址簿詳細資料。# alicloud_cloud_firewall_address_book.example: resource "alicloud_cloud_firewall_address_book" "example" { address_list = [ "192.0.2.1/32", "192.0.2.2/32", ] auto_add_tag_ecs = 0 description = "Created_by_terraform" group_name = "IPListExample" group_type = "ip" id = "1d89edf3-5711-41e6-b8dd-0c1ac080****" }
登入Cloud Firewall控制台,在存取控制>地址簿管理頁面,搜尋地址簿名稱查看地址簿的詳細資料。