通过Terraform配置端口接入

本文将指导您如何通过Terraform配置端口接入,以新增端口为例进行详细说明。

说明

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

前提条件

  • 已购买DDoS高防实例。具体操作,请参见通过Terraform购买并管理DDoS高防实例

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

  • 使用以下示例为RAM用户授权,具体操作方式请参见RAM用户授权

    {
        "Version": "1",
        "Statement": [
            {
                "Action": [
                    "slb:CreateLoadBalancer",
                    "slb:CreateLoadBalancerHTTPListener",
                    "slb:CreateLoadBalancerHTTPSListener",
                    "slb:CreateLoadBalancerTCPListener",
                    "slb:CreateLoadBalancerUDPListener",
                    "slb:ModifyLoadBalancerInternetSpec",
                    "slb:AddBackendServers",
                    "slb:RemoveBackendServers",
                    "slb:SetLoadBalancerName",
                    "ecs:AuthorizeSecurityGroup",
                    "ecs:RevokeSecurityGroup",
                    "ecs:DescribeSecurityGroups",
                    "ecs:DescribeSecurityGroupAttribute"
                ],
                "Resource": "*",
                "Effect": "Allow"
            }
        ]
    }
  • 准备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主文件,定义了将要部署的资源。以在ddoscoo-cn-20s3zrc4k001实例中添加端口配置为例。

    variable "region_id" {
      type    = string
      default = "cn-hangzhou"
    }
    
    # DDoS CoO 实例名称
    variable "ddoscoo_instance_name" {
      description = "The name of the DDoS CoO instance"
      type        = string
      default     = "Ddoscootest"  # 默认值
    }
    
    # 基础带宽
    variable "base_bandwidth" {
      description = "Base bandwidth of the DDoS CoO instance"
      type        = string
      default     = "30"  # 默认值
    }
    
    # 带宽
    variable "bandwidth" {
      description = "Bandwidth of the DDoS CoO instance"
      type        = string
      default     = "40"  # 默认值
    }
    
    # 服务带宽
    variable "service_bandwidth" {
      description = "Service bandwidth of the DDoS CoO instance"
      type        = string
      default     = "100"  # 默认值
    }
    
    # 端口数量
    variable "port_count" {
      description = "Number of ports for the DDoS CoO instance"
      type        = string
      default     = "50"  # 默认值
    }
    
    # 域名数量
    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     = "ddoscoo"  # 默认值
    }
    
    # 计费模式
    variable "pricing_mode" {
      description = "Pricing mode of the DDoS CoO instance (Prepaid or Postpaid)"
      type        = string
      default     = "Postpaid"  # 默认值
    }
    
    # 前端端口
    variable "frontend_port" {
      description = "The frontend port for the DDoS CoO port"
      type        = string
      default     = "7001"
    }
    
    # 后端端口
    variable "backend_port" {
      description = "The backend port for the DDoS CoO port"
      type        = string
      default     = "7001"
    }
    
    # 前端协议
    variable "frontend_protocol" {
      description = "The frontend protocol for the DDoS CoO port"
      type        = string
      default     = "tcp"
    }
    
    # 实际服务器列表
    variable "real_servers" {
      description = "The list of real servers for the DDoS CoO port"
      type        = list(string)
      default     = ["196.128.10.21", "196.129.10.11"] #用户手动设置
    }
    
    provider "alicloud" {
      region = var.region_id
    }
    
    resource "alicloud_ddoscoo_instance" "newInstance" {
      name              = var.ddoscoo_instance_name
      base_bandwidth    = var.base_bandwidth
      bandwidth         = var.bandwidth
      service_bandwidth = var.service_bandwidth
      port_count        = var.port_count
      domain_count      = var.domain_count
      period            = var.pricing_mode == "Prepaid" ? var.period : null
      product_type      = var.product_type
    }
    
    resource "alicloud_ddoscoo_port" "default" {
      instance_id       = alicloud_ddoscoo_instance.newInstance.id
      frontend_port     = var.frontend_port
      backend_port      = var.backend_port
      frontend_protocol = var.frontend_protocol
      real_servers      = var.real_servers
    }
    
    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
    }
    
    output "port_id" {
      description = "The ID of the DDoS CoO port"
      value       = alicloud_ddoscoo_port.default.id
    }
    
  2. 执行terraform init命令初始化Terraform运行环境。

预期结果:

image

  1. 执行terraform apply命令。在执行过程中,根据提示输入yes并按下Enter键,等待命令执行完成,若出现以下信息,则表示授权完成。

预期结果:

image

  1. 操作验证。

执行terraform show命令

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

terraform show

image

DDoS高防(中国内地)控台截图

登录DDoS高防控制台,查看端口创建。

image

清理资源

当您不再需要上述通过Terraform创建或管理的资源时,请运行以下命令以释放资源。关于terraform destroy的更多信息,请参见Terraform常用命令

terraform destroy

完整代码示例

说明

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

variable "region_id" {
  type    = string
  default = "cn-hangzhou"
}

# DDoS CoO 实例名称
variable "ddoscoo_instance_name" {
  description = "The name of the DDoS CoO instance"
  type        = string
  default     = "Ddoscootest"  # 默认值
}

# 基础带宽
variable "base_bandwidth" {
  description = "Base bandwidth of the DDoS CoO instance"
  type        = string
  default     = "30"  # 默认值
}

# 带宽
variable "bandwidth" {
  description = "Bandwidth of the DDoS CoO instance"
  type        = string
  default     = "40"  # 默认值
}

# 服务带宽
variable "service_bandwidth" {
  description = "Service bandwidth of the DDoS CoO instance"
  type        = string
  default     = "100"  # 默认值
}

# 端口数量
variable "port_count" {
  description = "Number of ports for the DDoS CoO instance"
  type        = string
  default     = "50"  # 默认值
}

# 域名数量
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     = "ddoscoo"  # 默认值
}

# 计费模式
variable "pricing_mode" {
  description = "Pricing mode of the DDoS CoO instance (Prepaid or Postpaid)"
  type        = string
  default     = "Postpaid"  # 默认值
}

# 前端端口
variable "frontend_port" {
  description = "The frontend port for the DDoS CoO port"
  type        = string
  default     = "7001"
}

# 后端端口
variable "backend_port" {
  description = "The backend port for the DDoS CoO port"
  type        = string
  default     = "7001"
}

# 前端协议
variable "frontend_protocol" {
  description = "The frontend protocol for the DDoS CoO port"
  type        = string
  default     = "tcp"
}

# 实际服务器列表
variable "real_servers" {
  description = "The list of real servers for the DDoS CoO port"
  type        = list(string)
  default     = ["196.128.10.21", "196.129.10.11"]
}

provider "alicloud" {
  region = var.region_id
}

resource "alicloud_ddoscoo_instance" "newInstance" {
  name              = var.ddoscoo_instance_name
  base_bandwidth    = var.base_bandwidth
  bandwidth         = var.bandwidth
  service_bandwidth = var.service_bandwidth
  port_count        = var.port_count
  domain_count      = var.domain_count
  period            = var.pricing_mode == "Prepaid" ? var.period : null
  product_type      = var.product_type
}

resource "alicloud_ddoscoo_port" "default" {
  instance_id       = alicloud_ddoscoo_instance.newInstance.id
  frontend_port     = var.frontend_port
  backend_port      = var.backend_port
  frontend_protocol = var.frontend_protocol
  real_servers      = var.real_servers
}

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
}

output "port_id" {
  description = "The ID of the DDoS CoO port"
  value       = alicloud_ddoscoo_port.default.id
}

相关文档