通过Terraform购买并管理DDoS高防实例

您可以通过Terraform购买并管理DDoS高防实例。本文以购买DDoS高防实例为例进行介绍。

说明

当前示例代码支持一键运行,您可以直接运行代码。一键运行

前提条件

  • 由于阿里云账号(主账号)具有资源的所有权限,一旦发生泄露将面临重大风险。建议您使用RAM用户,并为该RAM用户创建AccessKey,具体操作方式请参见创建RAM用户创建AccessKey

  • 为运行Terraform命令的RAM用户绑定以下最小权限策略,以获取管理本示例所涉及资源的权限。更多信息,请参见RAM用户授权

    该权限策略允许RAM用户创建、查看和删除RAM角色,并支持对RAM角色权限策略的管理。

    {
      "Statement": [
        {
          "Action": [
            "ddosprotection:CreateInstance",
            "ddosprotection:DeleteInstance"
          ],
          "Effect": "Allow",
          "Resource": "*"
        }
      ],
      "Version": "1"
    }
  • 准备Terraform运行环境,您可以选择以下任一方式来使用Terraform。

    • Terraform Explorer中使用Terraform:阿里云提供了Terraform的在线运行环境,您无需安装Terraform,登录后即可在线使用和体验Terraform。适用于零成本、快速、便捷地体验和调试Terraform的场景。

    • Cloud Shell:阿里云Cloud Shell中预装了Terraform的组件,并已配置好身份凭证,您可直接在Cloud Shell中运行Terraform的命令。适用于低成本、快速、便捷地访问和使用Terraform的场景。

    • 在本地安装和配置Terraform:适用于网络连接较差或需要自定义开发环境的场景。

使用的资源

操作步骤

  1. 创建一个工作目录,并且在工作目录中创建以下名为main.tf的配置文件。

    main.tf:Terraform主文件,定义了将要部署的资源。以购买DDoS高防(非中国内地)实例为例介绍。

    重要

    使用Terraform定义和部署DDoS高防(非中国内地)实例时,请注意,创建的实例不支持通过Terraform进行销毁。请谨慎管理实例的生命周期,避免不必要的费用。

    # 区域
    variable "region_id" {
      type    = string
      default = "ap-southeast-1"  # 修改为 新加坡
    }
    
    # DDoS CoO 实例名称
    variable "ddoscoo_instance_name" {
      description = "The name of the DDoS CoO instance"
      type        = string
      default     = "Ddoscoo-spm-fofo"  # 默认值
    }
    # 端口数量(必需)实例的端口重传规则数量。至少为50。每次增加5,例如55、60、65。仅支持升级。
    variable "port_count" {
      description = "Number of ports for the DDoS CoO instance"
      type        = string
      default     = "50"  # 默认值
    }
    #0:保险防护    #1:无限防护   #2:中国大陆加速线路。 #3:安全中国大陆加速(Sec-CMA)缓解计划。
    variable "product_plan" {
      description = "Product plan of the DDoS CoO instance"
      type        = string
      default     = "0"
    }
    
    # 域名数量(必需)实例的域名重传规则数量。至少为50。每次增加5,例如55、60、65。仅支持升级。
    variable "domain_count" {
      description = "Number of domains for the DDoS CoO instance"
      type        = string
      default     = "50"  # 默认值
    }
    
    # 购买周期
    variable "period" {
      description = "Purchase period of the DDoS CoO instance"
      type        = string
      default     = "1"  # 默认值
    }
    
    # 产品类型
    variable "product_type" {
      description = "Product type of the DDoS CoO instance"
      type        = string
      default     = "ddosDip"  #  国际版 ddoscoo_intl
    }
    
    # 计费模式
    variable "pricing_mode" {
      description = "Pricing mode of the DDoS CoO instance (Prepaid or Postpaid)"
      type        = string
      default     = "Postpaid"  # 默认值
    }
    # 清洗带宽 实例提供的清洗带宽
    variable "normal_bandwidth" {
      description = "Clean bandwidth provided by the instance, valid only when product_type is ddosDip"
      type        = number
      default     = 100
    }
    # 每秒查询数 实例提供的清洗QPS
    variable "normal_qps" {
      description = "Normal QPS provided by the instance, valid only for security_acceleration"
      type        = number
      default     = 500
    }
    # 功能版本 标准功能计划
    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 DDoS CoO instance"
      value       = alicloud_ddoscoo_instance.newInstance.id
    }
    
    output "instance_name" {
      description = "The name of the DDoS CoO instance"
      value       = var.ddoscoo_instance_name
    }
  2. 执行terraform init命令初始化Terraform。

  3. 预期输出:

    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.
  4. 执行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"
  1. 执行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"
  1. 操作验证。

执行terraform show命令

您可以使用以下命令查询Terraform已创建的资源详细信息:

terraform show

image

DDoS高防(中国内地)控制台

登录DDoS高防控制台,查看创建实例。

image

完整代码示例

说明

当前示例代码支持一键运行,您可以直接运行代码。一键运行

# 区域
variable "region_id" {
  type    = string
  default = "ap-southeast-1"  # 修改为 新加坡
}

# DDoS CoO 实例名称
variable "ddoscoo_instance_name" {
  description = "The name of the DDoS CoO instance"
  type        = string
  default     = "Ddoscoo-spm-fofo"  # 默认值
}
# 端口数量(必需)实例的端口重传规则数量。至少为50。每次增加5,例如55、60、65。仅支持升级。
variable "port_count" {
  description = "Number of ports for the DDoS CoO instance"
  type        = string
  default     = "50"  # 默认值
}
#0:保险防护    #1:无限防护   #2:中国大陆加速线路。 #3:安全中国大陆加速(Sec-CMA)缓解计划。
variable "product_plan" {
  description = "Product plan of the DDoS CoO instance"
  type        = string
  default     = "0"
}

# 域名数量(必需)实例的域名重传规则数量。至少为50。每次增加5,例如55、60、65。仅支持升级。
variable "domain_count" {
  description = "Number of domains for the DDoS CoO instance"
  type        = string
  default     = "50"  # 默认值
}

# 购买周期
variable "period" {
  description = "Purchase period of the DDoS CoO instance"
  type        = string
  default     = "1"  # 默认值
}

# 产品类型
variable "product_type" {
  description = "Product type of the DDoS CoO instance"
  type        = string
  default     = "ddosDip"  #  国际版 ddoscoo_intl
}

# 计费模式
variable "pricing_mode" {
  description = "Pricing mode of the DDoS CoO instance (Prepaid or Postpaid)"
  type        = string
  default     = "Postpaid"  # 默认值
}
# 清洗带宽 实例提供的清洗带宽
variable "normal_bandwidth" {
  description = "Clean bandwidth provided by the instance, valid only when product_type is ddosDip"
  type        = number
  default     = 100
}
# 每秒查询数 实例提供的清洗QPS
variable "normal_qps" {
  description = "Normal QPS provided by the instance, valid only for security_acceleration"
  type        = number
  default     = 500
}
# 功能版本 标准功能计划
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 DDoS CoO instance"
  value       = alicloud_ddoscoo_instance.newInstance.id
}

output "instance_name" {
  description = "The name of the DDoS CoO instance"
  value       = var.ddoscoo_instance_name
}

相关文档