Terraform を使用して Anti-DDoS Proxy インスタンスを購入および管理できます。このトピックでは、Anti-DDoS Proxy インスタンスを購入する方法の例を説明します。
このトピックのサンプルコードはワンクリックで実行できます。ワンクリックで実行
前提条件
-
Alibaba Cloud アカウント (root ユーザー) は、すべてのリソースに対する完全な権限を持っています。アカウントが侵害されると、重大なセキュリティリスクが生じます。RAM ユーザーを使用し、その RAM ユーザーの AccessKey を作成することを推奨します。詳細については、「RAM ユーザーの作成」および「AccessKey の作成」をご参照ください。
-
Terraform コマンドを実行する RAM ユーザーに、この例のリソースを管理するために必要な権限を付与するために、以下の最小限必要なポリシーをアタッチします。詳細については、「RAM ユーザー権限の管理」をご参照ください。
このポリシーにより、RAM ユーザーは Anti-DDoS Proxy インスタンスを作成および削除できます。
{ "Statement": [ { "Action": [ "ddosprotection:CreateInstance", "ddosprotection:DeleteInstance" ], "Effect": "Allow", "Resource": "*" } ], "Version": "1" } -
Terraform の実行環境を準備します。以下のいずれかの方法を使用できます。
-
Terraform Explorer で Terraform を使用する:Alibaba Cloud は、インストール不要で Terraform を実行できる無料のオンライン環境を提供しており、迅速な試用やデバッグに最適です。
-
Terraform を使用してリソースを迅速に作成する:Alibaba Cloud の Cloud Shell には Terraform がプリインストールされており、認証情報も事前設定されているため、迅速かつ低コストで開始できます。
-
ローカル環境で Terraform をインストールおよび設定する:この方法は、ネットワーク接続が不安定な場合や、カスタムの開発環境が必要な場合に最適です。
-
リソース
-
alicloud_ddoscoo_instance:Anti-DDoS Proxy インスタンスを作成します。
操作手順
-
作業ディレクトリを作成し、その中に
main.tfという名前の設定ファイルを作成します。main.tf:デプロイするリソースを定義する Terraform のメインファイルです。この例では、Anti-DDoS Proxy (中国本土以外) インスタンスを購入する方法を示します。
重要Terraform で作成された Anti-DDoS Proxy (中国本土以外) インスタンスは、Terraform では削除できないことにご注意ください。不要なコストを避けるため、インスタンスのライフサイクルを慎重に管理してください。
# インスタンスがデプロイされるリージョン。デフォルト値 "ap-southeast-1" はシンガポールに対応します。 variable "region_id" { type = string default = "ap-southeast-1" } # Anti-DDoS Proxy インスタンスの名前。 variable "ddoscoo_instance_name" { description = "The name of the Anti-DDoS Proxy instance" type = string default = "Ddoscoo-spm-fofo" # デフォルト値。 } # (必須) インスタンスのポート転送ルールの数。値は 50 以上である必要があります。55、60、65 のように 5 刻みで値を増やすことができます。スペックアップのみサポートされています。 variable "port_count" { description = "Number of ports for the Anti-DDoS Proxy instance" type = string default = "50" # デフォルト値。 } # 0: 保険版、1: 無制限版、2: 中国本土アクセラレーションプラン、3: セキュア中国本土アクセラレーション (Sec-CMA) プラン。 variable "product_plan" { description = "Product plan of the Anti-DDoS Proxy instance" type = string default = "0" } # (必須) インスタンスのドメイン転送ルールの数。値は 50 以上である必要があります。55、60、65 のように 5 刻みで値を増やすことができます。スペックアップのみサポートされています。 variable "domain_count" { description = "Number of domains for the Anti-DDoS Proxy instance" type = string default = "50" # デフォルト値。 } # サブスクリプション期間。 variable "period" { description = "Purchase period of the Anti-DDoS Proxy instance" type = string default = "1" # デフォルト値。 } # プロダクトタイプ。 variable "product_type" { description = "Product type of the Anti-DDoS Proxy instance" type = string default = "ddoscoo_intl" # 中国本土以外のインスタンスには "ddoscoo_intl" を使用します。 } # 課金方法。 variable "pricing_mode" { description = "Pricing mode of the Anti-DDoS Proxy instance (Prepaid or Postpaid)" type = string default = "Postpaid" # デフォルト値。 } # インスタンスが提供するクリーン帯域幅。 variable "bandwidth" { description = "Clean bandwidth provided by the instance" type = number default = 100 } # インスタンスが提供する秒間クエリ数 (QPS)。 variable "normal_qps" { description = "Normal QPS provided by the instance, valid only for security_acceleration" type = number default = 500 } # 関数バージョン。有効な値: 0。標準機能プランを示します。 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 } -
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コマンドを実行して実行計画を生成します。
期待される出力:
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.
-
terraform applyコマンドを実行します。プロンプトが表示されたら、yesと入力し、Enter キーを押します。以下の出力が表示されれば、操作は成功です。
期待される出力:
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"
-
結果の確認
CLI
次のコマンドを実行して、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 コンソールにログインして、作成されたインスタンスを表示します。
上部のナビゲーションバーで、[中国本土以外] リージョンを選択します。左側のナビゲーションウィンドウで、[資産管理] > [インスタンス管理] を選択します。表示されたページで、作成されたインスタンスの仕様とステータスを確認できます。仕様には、保護プラン、基本帯域幅、拡張帯域幅、秒間クエリ数 (QPS)、接続制限が含まれます。
完全なコード例
このトピックのサンプルコードはワンクリックで実行できます。ワンクリックで実行
# インスタンスがデプロイされるリージョン。デフォルト値 "ap-southeast-1" はシンガポールに対応します。
variable "region_id" {
type = string
default = "ap-southeast-1"
}
# Anti-DDoS Proxy インスタンスの名前。
variable "ddoscoo_instance_name" {
description = "The name of the Anti-DDoS Proxy instance"
type = string
default = "Ddoscoo-spm-fofo" # デフォルト値。
}
# (必須) インスタンスのポート転送ルールの数。値は 50 以上である必要があります。55、60、65 のように 5 刻みで値を増やすことができます。スペックアップのみサポートされています。
variable "port_count" {
description = "Number of ports for the Anti-DDoS Proxy instance"
type = string
default = "50" # デフォルト値。
}
# 0: 保険版、1: 無制限版、2: 中国本土アクセラレーションプラン、3: セキュア中国本土アクセラレーション (Sec-CMA) プラン。
variable "product_plan" {
description = "Product plan of the Anti-DDoS Proxy instance"
type = string
default = "0"
}
# (必須) インスタンスのドメイン転送ルールの数。値は 50 以上である必要があります。55、60、65 のように 5 刻みで値を増やすことができます。スペックアップのみサポートされています。
variable "domain_count" {
description = "Number of domains for the Anti-DDoS Proxy instance"
type = string
default = "50" # デフォルト値。
}
# サブスクリプション期間。
variable "period" {
description = "Purchase period of the Anti-DDoS Proxy instance"
type = string
default = "1" # デフォルト値。
}
# プロダクトタイプ。
variable "product_type" {
description = "Product type of the Anti-DDoS Proxy instance"
type = string
default = "ddoscoo_intl" # 中国本土以外のインスタンスには "ddoscoo_intl" を使用します。
}
# 課金方法。
variable "pricing_mode" {
description = "Pricing mode of the Anti-DDoS Proxy instance (Prepaid or Postpaid)"
type = string
default = "Postpaid" # デフォルト値。
}
# インスタンスが提供するクリーン帯域幅。
variable "bandwidth" {
description = "Clean bandwidth provided by the instance"
type = number
default = 100
}
# インスタンスが提供する秒間クエリ数 (QPS)。
variable "normal_qps" {
description = "Normal QPS provided by the instance, valid only for security_acceleration"
type = number
default = 500
}
# 関数バージョン。有効な値: 0。標準機能プランを示します。
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
}
関連ドキュメント
-
Terraform の詳細については、「Alibaba Cloud Terraform とは」をご参照ください。