You can use Terraform to purchase and manage Anti-DDoS Proxy instances. This topic provides an example of how to purchase an Anti-DDoS Proxy instance.
You can run the sample code in this topic in one click. Run with one click
Prerequisites
An Alibaba Cloud account has full permissions on all resources. If the account is compromised, it poses significant security risks. We recommend that you use a RAM user and create an AccessKey for the RAM user. For more information, see Create a RAM user and Create an AccessKey.
Attach the following minimum required policy to the RAM user that runs Terraform commands to obtain the permissions required to manage the resources in this example. For more information, see Manage RAM user permissions.
This policy allows the RAM user to create and delete Anti-DDoS Proxy instances.
{ "Statement": [ { "Action": [ "ddosprotection:CreateInstance", "ddosprotection:DeleteInstance" ], "Effect": "Allow", "Resource": "*" } ], "Version": "1" }Prepare a Terraform runtime environment. You can use one of the following methods:
Use Terraform in Terraform Explorer: Alibaba Cloud provides a free online environment to run Terraform without installation, which is ideal for quick trials and debugging.
Quickly create resources by using Terraform: Alibaba Cloud Cloud Shell comes with Terraform pre-installed and your identity credentials pre-configured, making it a fast, low-cost way to get started.
Install and configure Terraform in a local environment: This method is ideal if you have poor network connectivity or need a custom development environment.
Resources
alicloud_ddoscoo_instance: Creates an Anti-DDoS Proxy instance.
Procedure
Create a working directory and a configuration file named
main.tfinside it.main.tf: The main Terraform file that defines the resources to deploy. The example shows how to purchase an Anti-DDoS Proxy (Outside Chinese Mainland) instance.
ImportantNote that Anti-DDoS Proxy (Outside Chinese Mainland) instances created with Terraform cannot be destroyed by Terraform. To avoid unnecessary costs, carefully manage the instance lifecycle.
# The region where the instance is deployed. The default value "ap-southeast-1" corresponds to Singapore. variable "region_id" { type = string default = "ap-southeast-1" } # The name of the Anti-DDoS Proxy instance. variable "ddoscoo_instance_name" { description = "The name of the Anti-DDoS Proxy instance" type = string default = "Ddoscoo-spm-fofo" # The default value. } # (Required) The number of port forwarding rules for the instance. The value must be at least 50. You can increase the value in increments of 5, such as 55, 60, and 65. Only upgrades are supported. variable "port_count" { description = "Number of ports for the Anti-DDoS Proxy instance" type = string default = "50" # The default value. } # 0: Insurance Plan, 1: Unlimited Plan, 2: Chinese Mainland Acceleration Plan, 3: Secure Chinese Mainland Acceleration (Sec-CMA) Plan. variable "product_plan" { description = "Product plan of the Anti-DDoS Proxy instance" type = string default = "0" } # (Required) The number of domain forwarding rules for the instance. The value must be at least 50. You can increase the value in increments of 5, such as 55, 60, and 65. Only upgrades are supported. variable "domain_count" { description = "Number of domains for the Anti-DDoS Proxy instance" type = string default = "50" # The default value. } # The subscription duration. variable "period" { description = "Purchase period of the Anti-DDoS Proxy instance" type = string default = "1" # The default value. } # The product type. variable "product_type" { description = "Product type of the Anti-DDoS Proxy instance" type = string default = "ddoscoo_intl" # Use "ddoscoo_intl" for instances outside Chinese mainland. } # The billing method. variable "pricing_mode" { description = "Pricing mode of the Anti-DDoS Proxy instance (Prepaid or Postpaid)" type = string default = "Postpaid" # The default value. } # The clean bandwidth provided by the instance. variable "bandwidth" { description = "Clean bandwidth provided by the instance" type = number default = 100 } # The number of queries per second (QPS) provided by the instance. variable "normal_qps" { description = "Normal QPS provided by the instance, valid only for security_acceleration" type = number default = 500 } # The function version. Valid value: 0. Indicates the Standard Function Plan. variable "function_version" { description = "Function version of the instance, valid only for security_acceleration" type = number default = 0 } provider "alicloud" { region = var.region_id } resource "alicloud_ddoscoo_instance" "newInstance" { name = var.ddoscoo_instance_name port_count = var.port_count domain_count = var.domain_count period = var.pricing_mode == "Prepaid" ? var.period : null product_type = var.product_type product_plan = var.product_plan function_version = var.function_version bandwidth = var.bandwidth } output "instance_id" { description = "The ID of the Anti-DDoS Proxy instance" value = alicloud_ddoscoo_instance.newInstance.id } output "instance_name" { description = "The name of the Anti-DDoS Proxy instance" value = var.ddoscoo_instance_name }Run the
terraform initcommand to initialize Terraform.Expected output:
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.Run the
terraform plancommand to generate an execution plan.
Expected output:
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# alicloud_ddoscoo_instance.newInstance will be created
+ resource "alicloud_ddoscoo_instance" "newInstance" {
+ id = (known after apply)
+ name = "Ddoscoo-spm-fofo"
+ product_type = "ddoscoo_intl"
# ...
}
Plan: 1 to add, 0 to change, 0 to destroy.
Run the
terraform applycommand. When prompted, enteryesand press Enter. The operation is successful if the following output is displayed.
Expected output:
alicloud_ddoscoo_instance.newInstance: Creating...
alicloud_ddoscoo_instance.newInstance: Creation complete after 1s [id=ddoscoo-ap-southeast-1-xxxxxxxxx]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
instance_id = "ddoscoo-ap-southeast-1-xxxxxxxxx"
instance_name = "Ddoscoo-spm-fofo"Verify the results.
CLI
You can run the following command to query the details of the resources created by Terraform:
terraform show# alicloud_ddoscoo_instance.newInstance:
resource "alicloud_ddoscoo_instance" "newInstance" {
bandwidth = "100"
base_bandwidth = "100"
domain_count = "50"
id = "ddoscoo-ap-southeast-1-xxxxxxxxx"
name = "Ddoscoo-spm-fofo"
period = 1
port_count = "50"
product_type = "ddoscoo_intl"
service_bandwidth = "100"
}
Outputs:
instance_id = "ddoscoo-ap-southeast-1-xxxxxxxxx"
instance_name = "Ddoscoo-spm-fofo"Anti-DDoS Proxy (Outside Chinese Mainland) Console
Log on to the Anti-DDoS Proxy console, and select the Outside Chinese Mainland region at the top of the console. In the left-side navigation pane, click Assets > Instances to view the specifications and status of the created instance (including the protection plan version, normal business bandwidth, burstable bandwidth, QPS, and maximum connections).
Complete code example
You can run the sample code in this topic in one click. Run with one click
# The region where the instance is deployed. The default value "ap-southeast-1" corresponds to Singapore.
variable "region_id" {
type = string
default = "ap-southeast-1"
}
# The name of the Anti-DDoS Proxy instance.
variable "ddoscoo_instance_name" {
description = "The name of the Anti-DDoS Proxy instance"
type = string
default = "Ddoscoo-spm-fofo" # The default value.
}
# (Required) The number of port forwarding rules for the instance. The value must be at least 50. You can increase the value in increments of 5, such as 55, 60, and 65. Only upgrades are supported.
variable "port_count" {
description = "Number of ports for the Anti-DDoS Proxy instance"
type = string
default = "50" # The default value.
}
# 0: Insurance Plan, 1: Unlimited Plan, 2: Chinese Mainland Acceleration Plan, 3: Secure Chinese Mainland Acceleration (Sec-CMA) Plan.
variable "product_plan" {
description = "Product plan of the Anti-DDoS Proxy instance"
type = string
default = "0"
}
# (Required) The number of domain forwarding rules for the instance. The value must be at least 50. You can increase the value in increments of 5, such as 55, 60, and 65. Only upgrades are supported.
variable "domain_count" {
description = "Number of domains for the Anti-DDoS Proxy instance"
type = string
default = "50" # The default value.
}
# The subscription duration.
variable "period" {
description = "Purchase period of the Anti-DDoS Proxy instance"
type = string
default = "1" # The default value.
}
# The product type.
variable "product_type" {
description = "Product type of the Anti-DDoS Proxy instance"
type = string
default = "ddoscoo_intl" # Use "ddoscoo_intl" for instances outside Chinese mainland.
}
# The billing method.
variable "pricing_mode" {
description = "Pricing mode of the Anti-DDoS Proxy instance (Prepaid or Postpaid)"
type = string
default = "Postpaid" # The default value.
}
# The clean bandwidth provided by the instance.
variable "bandwidth" {
description = "Clean bandwidth provided by the instance"
type = number
default = 100
}
# The number of queries per second (QPS) provided by the instance.
variable "normal_qps" {
description = "Normal QPS provided by the instance, valid only for security_acceleration"
type = number
default = 500
}
# The function version. Valid value: 0. Indicates the Standard Function Plan.
variable "function_version" {
description = "Function version of the instance, valid only for security_acceleration"
type = number
default = 0
}
provider "alicloud" {
region = var.region_id
}
resource "alicloud_ddoscoo_instance" "newInstance" {
name = var.ddoscoo_instance_name
port_count = var.port_count
domain_count = var.domain_count
period = var.pricing_mode == "Prepaid" ? var.period : null
product_type = var.product_type
product_plan = var.product_plan
function_version = var.function_version
bandwidth = var.bandwidth
}
output "instance_id" {
description = "The ID of the Anti-DDoS Proxy instance"
value = alicloud_ddoscoo_instance.newInstance.id
}
output "instance_name" {
description = "The name of the Anti-DDoS Proxy instance"
value = var.ddoscoo_instance_name
}Related documentation
For more information about Terraform, see What is Alibaba Cloud Terraform?.