本文介绍如何使用Terraform创建互联网防火墙实例。
注意事项
Terraform运行环境:阿里云Cloud Shell
Terraform版本:0.12
alicloud版本:1.203.0
resource定义:cloud_firewall_instance
前提条件
已创建阿里云账号和访问密钥(AccessKey)。具体操作,请参见创建AccessKey。
已经安装并配置了Terraform。
创建云防火墙实例
在Terraform执行目录下的
terraform.tf
文件中,配置如下内容。代码示例如下:
provider "alicloud" { version = "~> 1.203.0" } resource "alicloud_cloud_firewall_instance" "example" { payment_type = "Subscription" spec = "premium_version" ip_number = 20 band_width = 10 cfw_log = false cfw_log_storage = 1000 cfw_service = false period = 6 }
运行
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,开始创建。代码示例如下:
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.
创建成功后,可以返回云防火墙ID为vipcloudfw-cn-x0r36mo****。
查看结果。
运行
terraform show
查看云防火墙实例的详细信息。# alicloud_cloud_firewall_instance.example: resource "alicloud_cloud_firewall_instance" "example" { band_width = 10 cfw_log = false cfw_log_storage = 1000 cfw_service = false create_time = "2023-04-21T01:38:58Z" end_time = "2023-10-21T16:00:00Z" id = "vipcloudfw-cn-x0r36mo****" ip_number = 20 payment_type = "Subscription" period = 6 renewal_duration_unit = "Month" renewal_status = "ManualRenewal" spec = "premium_version" status = "Normal" }
登录云防火墙控制台,在总览页面,单击升级查看云防火墙实例的当前配置。
文档内容是否对您有帮助?