Terraform を使用して、Anti-DDoS Pro または Anti-DDoS Premium インスタンスを購入および管理します。このトピックでは、Anti-DDoS Pro または Anti-DDoS Premium インスタンスの購入を例として説明します。
このトピックのサンプルコードは、ワンクリック実行をサポートしています。コードを直接実行してください。ワンクリック実行
前提条件
Alibaba Cloud アカウント (ルートアカウント) は、すべてのリソースに対する完全な権限を持っています。侵害された場合、重大なセキュリティリスクをもたらします。代わりに Resource Access Management (RAM) ユーザーを使用してください。RAM ユーザーの AccessKey を作成します。詳細については、「RAM ユーザーの作成」および「AccessKey の作成」をご参照ください。
Terraform コマンドを実行する RAM ユーザーに、以下の最小権限ポリシーをアタッチします。これにより、この例で使用されるリソースを管理する権限が付与されます。詳細については、「RAM ユーザーの権限の管理」をご参照ください。
このポリシーにより、RAM ユーザーは RAM ロールを作成、表示、削除できます。また、RAM ロールのアクセス権限ポリシーを管理することもできます。
{ "Statement": [ { "Action": [ "ddosprotection:CreateInstance", "ddosprotection:DeleteInstance" ], "Effect": "Allow", "Resource": "*" } ], "Version": "1" }Terraform 実行環境を準備します。以下のいずれかのオプションを選択してください。
Terraform Explorer で Terraform を使用: Alibaba Cloud はオンライン Terraform 環境を提供しています。Terraform をインストールする必要はありません。ログインしてオンラインで Terraform を使用してください。このオプションは、低コスト、高速、便利な Terraform の評価とデバッグに最適です。
Terraform を使用したリソースの迅速な作成: Alibaba Cloud Cloud Shell には Terraform がプリインストールされており、認証情報が事前設定されています。Cloud Shell で Terraform コマンドを直接実行してください。このオプションは、低コスト、高速、便利な Terraform のアクセスと使用に最適です。
Terraform をローカルにインストールして構成: ネットワーク接続が不安定な場合や、カスタム開発環境が必要な場合は、このオプションを使用してください。
使用リソース
alicloud_ddoscoo_instance: Anti-DDoS インスタンスを作成します。
操作手順
作業ディレクトリを作成します。そのディレクトリ内に、
main.tfという名前の構成ファイルを作成します。main.tf: メイン Terraform ファイル。デプロイするリソースを定義します。この例では、Anti-DDoS Proxy (中国本土以外) インスタンスの購入方法を示します。
重要Terraform を使用して Anti-DDoS Proxy (中国本土以外) インスタンスを定義およびデプロイする場合、Terraform を使用してインスタンスを破棄できないことに注意してください。不要な料金を避けるため、インスタンスのライフサイクルを慎重に管理してください。
# Region variable "region_id" { type = string default = "ap-southeast-1" # Change to Singapore } # Anti-DDoS CoO instance name variable "ddoscoo_instance_name" { description = "The name of the Anti-DDoS CoO instance" type = string default = "Ddoscoo-spm-fofo" # Default value } # Number of ports (required): Number of port retransmission rules for the instance. Minimum is 50. Increase in increments of 5, such as 55, 60, or 65. Upgrades only. variable "port_count" { description = "Number of ports for the Anti-DDoS CoO instance" type = string default = "50" # Default value } # 0: Insurance mitigation plan. 1: Unlimited mitigation plan. 2: Chinese Mainland Acceleration (CMA). 3: Security CMA mitigation plan. variable "product_plan" { description = "Product plan of the Anti-DDoS CoO instance" type = string default = "0" } # Number of domain names (required): Number of domain name retransmission rules for the instance. Minimum is 50. Increase in increments of 5, such as 55, 60, or 65. Upgrades only. variable "domain_count" { description = "Number of domains for the Anti-DDoS CoO instance" type = string default = "50" # Default value } # Purchase period variable "period" { description = "Purchase period of the Anti-DDoS CoO instance" type = string default = "1" # Default value } # Product type variable "product_type" { description = "Product type of the Anti-DDoS CoO instance" type = string default = "ddosDip" # International version: ddoscoo_intl } # Billing method variable "pricing_mode" { description = "Pricing mode of the Anti-DDoS CoO instance (Prepaid or Postpaid)" type = string default = "Postpaid" # Default value } # Scrubbing bandwidth: Clean bandwidth provided by the instance variable "normal_bandwidth" { description = "Clean bandwidth provided by the instance, valid only when product_type is ddosDip" type = number default = 100 } # Queries per second (QPS): Normal QPS provided by the instance variable "normal_qps" { description = "Normal QPS provided by the instance, valid only for security_acceleration" type = number default = 500 } # Function version: 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 normal_bandwidth = var.normal_bandwidth } output "instance_id" { description = "The ID of the Anti-DDoS CoO instance" value = alicloud_ddoscoo_instance.newInstance.id } output "instance_name" { description = "The name of the Anti-DDoS CoO instance" value = var.ddoscoo_instance_name }terraform initコマンドを実行して Terraform を初期化します。期待される出力:
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コマンドを実行してリソースプランを生成します。
期待される結果:
alicloud_ddoscoo_instance.newInstance: Refreshing state... [id=ddoscoo-cn-20s3zrc4k001]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
~ update in-place
Terraform will perform the following actions:
# alicloud_ddoscoo_instance.newInstance will be updated in-place
~ resource "alicloud_ddoscoo_instance" "newInstance" {
id = "ddoscoo-cn-20**********""
~ name = "yourDdoscooInstanceName" -> "Ddoscoo"
# (7 unchanged attributes hidden)
}
Plan: 0 to add, 1 to change, 0 to destroy.
Changes to Outputs:
+ instance_id = "ddoscoo-cn-**********"
+ instance_name = "Ddoscoo"
terraform applyコマンドを実行します。プロンプトが表示されたら、yesと入力し、Enter を押します。コマンドが完了するまで待ちます。以下のメッセージが表示された場合、権限付与が完了しています。
期待される結果:
alicloud_ddoscoo_instance.newInstance: Modifying... [id=ddoscoo-cn-*********]
alicloud_ddoscoo_instance.newInstance: Modifications complete after 1s [id=ddoscoo-cn-*********]
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
Outputs:
instance_id = "ddoscoo-cn-*********"
instance_name = "Ddoscoo"操作を確認します。
terraform show コマンドの実行
Terraform によって作成されたリソースの詳細を照会するには、以下のコマンドを実行します。
terraform show
Anti-DDoS Proxy (中国本土) コンソール
Anti-DDoS Proxy コンソールにログインして、作成されたインスタンスを表示します。

完全なコード例
このトピックのサンプルコードは、ワンクリック実行をサポートしています。コードを直接実行してください。ワンクリック実行
# Region
variable "region_id" {
type = string
default = "ap-southeast-1" # Change to Singapore
}
# Anti-DDoS CoO instance name
variable "ddoscoo_instance_name" {
description = "The name of the Anti-DDoS CoO instance"
type = string
default = "Ddoscoo-spm-fofo" # Default value
}
# Number of ports (required): Number of port retransmission rules for the instance. Minimum is 50. Increase in increments of 5, such as 55, 60, or 65. Upgrades only.
variable "port_count" {
description = "Number of ports for the Anti-DDoS CoO instance"
type = string
default = "50" # Default value
}
# 0: Insurance mitigation plan. 1: Unlimited mitigation plan. 2: Chinese Mainland Acceleration (CMA). 3: Security CMA mitigation plan.
variable "product_plan" {
description = "Product plan of the Anti-DDoS CoO instance"
type = string
default = "0"
}
# Number of domain names (required): Number of domain name retransmission rules for the instance. Minimum is 50. Increase in increments of 5, such as 55, 60, or 65. Upgrades only.
variable "domain_count" {
description = "Number of domains for the Anti-DDoS CoO instance"
type = string
default = "50" # Default value
}
# Purchase period
variable "period" {
description = "Purchase period of the Anti-DDoS CoO instance"
type = string
default = "1" # Default value
}
# Product type
variable "product_type" {
description = "Product type of the Anti-DDoS CoO instance"
type = string
default = "ddosDip" # International version: ddoscoo_intl
}
# Billing method
variable "pricing_mode" {
description = "Pricing mode of the Anti-DDoS CoO instance (Prepaid or Postpaid)"
type = string
default = "Postpaid" # Default value
}
# Scrubbing bandwidth: Clean bandwidth provided by the instance
variable "normal_bandwidth" {
description = "Clean bandwidth provided by the instance, valid only when product_type is ddosDip"
type = number
default = 100
}
# Queries per second (QPS): Normal QPS provided by the instance
variable "normal_qps" {
description = "Normal QPS provided by the instance, valid only for security_acceleration"
type = number
default = 500
}
# Function version: 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
normal_bandwidth = var.normal_bandwidth
}
output "instance_id" {
description = "The ID of the Anti-DDoS CoO instance"
value = alicloud_ddoscoo_instance.newInstance.id
}
output "instance_name" {
description = "The name of the Anti-DDoS CoO instance"
value = var.ddoscoo_instance_name
}参考資料
Terraform の概要については、「Alibaba Cloud Terraform を理解する」をご参照ください。