After you add a site to ESA, you must add DNS records for the site to manage its domain name within ESA. This process ensures that the accelerated domain name can be resolved and accessed. This topic describes how to use Terraform to add DNS records.
Resources involved
Purchase a plan: alicloud_esa_rate_plan_instance
Add a site: alicloud_esa_site
Add a DNS record: alicloud_esa_record
You cannot use Terraform to create an Enterprise Edition plan. If you want to purchase an Enterprise Edition plan, please contact us.
Write configuration files
Create a working directory and the configuration files as described in the following sections. Then, modify the configurations in the code as needed.
Define the provider and Terraform version
First, create a configuration file named providers.tf and copy the following code to the file. This file is used to centrally manage all provider configurations and version constraints.
terraform {
required_providers {
alicloud = {
source = "aliyun/alicloud"
version = "1.245.0"
}
}
}Define resource variables
To improve code reusability, declare input variables and local variables. To do this, create a configuration file named variables.tf and copy the following code to the file.
# The connection type of the site. Valid values: CNAME and NS.
variable "site_type" {
default = "NS"
}
# The billing method. The value must be Subscription. Other values are not supported.
variable "payment_type" {
default = "Subscription"
}
# The area where the endpoint is deployed. Valid values:
# domestic (the Chinese mainland), overseas (global, excluding the Chinese mainland), and global (global).
variable "coverage_name" {
default = "overseas"
}
# Specifies whether to enable auto-renewal.
variable "auto_pay" {
default = "true"
}
# The valid values of the plan name are as follows:
# entranceplan_intl (Entrance Edition), basicplan_intl (Pro Edition), and vipplan_intl (Premium Edition).
variable "plan_name" {
default = "entranceplan_intl"
}
# The site name. You must replace the value with your domain name.
variable "site_name" {
default = "aliyundoc.com"
}
# The value of the origin server, which is your origin domain name.
variable "record_value" {
default = "www.example.com"
}
# The port for origin fetch, which is the port number of your source port.
variable "record_port" {
default = "80"
}
# The DNS record value, which is the accelerated domain name.
variable "record_name" {
default = "_udp._sip.aliyundoc.com"
}
# The DNS record type.
variable "record_type" {
default = "SRV"
}Define resources
Finally, create a configuration file named main.tf and copy the following code to the file. This file serves as the main entry point for all resource declarations.
# Create a plan.
resource "alicloud_esa_rate_plan_instance" "my_plan" {
type = var.site_type
auto_renew = "false"
period = "1"
payment_type = var.payment_type
coverage = var.coverage_name
auto_pay = var.auto_pay
plan_name = var.plan_name
}
# Create a site.
resource "alicloud_esa_site" "my_site" {
site_name = var.site_name
instance_id = alicloud_esa_rate_plan_instance.my_plan.id
coverage = var.coverage_name
access_type = var.site_type
}
# Add a DNS record.
resource "alicloud_esa_record" "my_record" {
data {
value = var.record_value
weight = "1"
priority = "1"
port = var.record_port
}
ttl = "100"
record_name = var.record_name
comment = "This is a remark"
site_id = alicloud_esa_site.my_site.id
record_type = var.record_type
}Create resources
Run the following command to initialize the Terraform environment.
terraform initRun the following command to validate the Terraform syntax and configuration.
terraform validateIf the output is the same as that shown in the following figure, the validation is successful.

Run the following command to preview the execution plan.
terraform plan
Run the following command to apply the configuration.
terraform apply
Verify the results
View the plan
Log on to the ESA console. In the navigation pane on the left, choose .
On the Plan Management page, you can view the new plan, as shown in the following figure.

View the site
In the ESA console, choose Websites. You can view the new site, as shown in the following figure.

View DNS records
In the ESA console, choose Websites, and click the name of the target site in the Website column.
In the navigation pane on the left, choose . You can view the new DNS record, as shown in the following figure.

(Optional) Clean up resources
If you no longer need the resources that are created or managed by Terraform, run the terraform destroy command to release the resources.
terraform destroy