部署HTTPS和缓存域名
应用场景
相关资源/数据源
资源
操作步骤
1. 脚本准备
2. 创建CDN域名资源
variable "domain_name" {
description = "The name of the CDN domain to be accelerated"
type = string
}
variable "domain_type" {
description = "The business type of the domain"
type = string
default = "web"
validation {
condition = contains(["web", "download", "video", "wholeSite"], var.domain_type)
error_message = "The domain_type must be one of: web, download, video, wholeSite."
}
}
variable "service_area" {
description = "The area covered by the acceleration service"
type = string
default = "mainland_china"
validation {
condition = contains(["mainland_china", "outside_mainland_china", "global"], var.service_area)
error_message = "The service_area must be one of: mainland_china, outside_mainland_china, global."
}
}
variable "origin_server" {
description = "The origin server address (IP address or domain name)"
type = string
}
variable "origin_type" {
description = "The origin server type"
type = string
default = "ipaddr"
validation {
condition = contains(["ipaddr", "domain", "obs_bucket"], var.origin_type)
error_message = "The origin_type must be one of: ipaddr, domain, obs_bucket."
}
}
variable "http_port" {
description = "The HTTP port of the origin server"
type = number
default = 80
}
variable "https_port" {
description = "The HTTPS port of the origin server"
type = number
default = 443
}
variable "origin_protocol" {
description = "The protocol used to retrieve data from the origin server"
type = string
default = "http"
validation {
condition = contains(["http", "https", "follow"], var.origin_protocol)
error_message = "The origin_protocol must be one of: http, https, follow."
}
}
variable "ipv6_enable" {
description = "Whether to enable IPv6"
type = bool
default = false
}
variable "range_based_retrieval_enabled" {
description = "Whether to enable range-based retrieval"
type = bool
default = false
}
variable "domain_description" {
description = "The description of the CDN domain"
type = string
default = ""
}
variable "https_enabled" {
description = "Whether to enable HTTPS"
type = bool
default = false
}
variable "certificate_name" {
description = "The name of the SSL certificate (required when https_enabled is true)"
type = string
default = ""
nullable = false
}
variable "certificate_source" {
description = "The source of the SSL certificate (required when https_enabled is true)"
type = string
default = "0"
nullable = false
validation {
condition = contains(["0", "2"], var.certificate_source)
error_message = "The certificate_source must be one of: 0, 2."
}
}
variable "certificate_body_path" {
description = "The file path to the SSL certificate (required when https_enabled is true and using custom certificate)"
type = string
default = ""
sensitive = false
nullable = false
}
variable "private_key_path" {
description = "The file path to the private key (required when https_enabled is true and using custom certificate)"
type = string
default = ""
sensitive = false
nullable = false
}
variable "http2_enabled" {
description = "Whether to enable HTTP/2 (only valid when https_enabled is true)"
type = bool
default = false
}
variable "ocsp_stapling_status" {
description = "The OCSP stapling status (only valid when https_enabled is true)"
type = string
default = "off"
validation {
condition = contains(["on", "off"], var.ocsp_stapling_status)
error_message = "The ocsp_stapling_status must be one of: on, off."
}
}
variable "cache_rules" {
description = "The cache rules configuration"
type = list(object({
rule_type = string
content = string
ttl = number
ttl_type = string
priority = number
url_parameter_type = optional(string)
url_parameter_value = optional(string)
}))
default = []
}
variable "domain_tags" {
description = "The tags of the CDN domain"
type = map(string)
default = {}
}
# 在指定region(region参数缺省时默认继承当前provider块中所指定的region)下创建CDN域名资源
resource "huaweicloud_cdn_domain" "test" {
name = var.domain_name
type = var.domain_type
service_area = var.service_area
sources {
origin = var.origin_server
origin_type = var.origin_type
active = 1
http_port = var.http_port
https_port = var.https_port
}
configs {
origin_protocol = var.origin_protocol
ipv6_enable = var.ipv6_enable
range_based_retrieval_enabled = var.range_based_retrieval_enabled
description = var.domain_description
dynamic "https_settings" {
for_each = var.https_enabled ? [1] : []
content {
certificate_name = var.https_enabled ? var.certificate_name : null
certificate_source = var.https_enabled ? var.certificate_source : null
certificate_body = var.https_enabled && var.certificate_body_path != "" ? file(var.certificate_body_path) : null
private_key = var.https_enabled && var.private_key_path != "" ? file(var.private_key_path) : null
https_enabled = var.https_enabled
http2_enabled = var.http2_enabled
ocsp_stapling_status = var.ocsp_stapling_status
}
}
}
dynamic "cache_settings" {
for_each = length(var.cache_rules) > 0 ? [var.cache_rules] : []
content {
dynamic "rules" {
for_each = cache_settings.value
content {
rule_type = rules.value.rule_type
ttl = rules.value.ttl
ttl_type = rules.value.ttl_type
priority = rules.value.priority
content = rules.value.content
url_parameter_type = lookup(rules.value, "url_parameter_type", null)
url_parameter_value = lookup(rules.value, "url_parameter_value", null)
}
}
}
}
tags = var.domain_tags
}3. 预设资源部署所需的入参(可选)
4. 初始化并应用Terraform配置
参考信息
Last updated