After you connect a site to ESA, you must add the domain name to be managed to ESA by adding DNS records. This ensures that the accelerated domain name can be parsed and accessed normally. This topic describes how to add DNS records using Terraform.
Involved resources
Purchase package resource: alicloud_esa_rate_plan_instance
Add site resource: alicloud_esa_site
Add DNS record resource: alicloud_esa_record
Notes
The types of packages that Alibaba Cloud China Website accounts and Alibaba Cloud International Website accounts can create differ.
Package names supported by Alibaba Cloud China Website:
Free Edition:
entranceplanBasic Edition:
basicStandard Edition:
mediumPremium Edition:
high
Package names supported by Alibaba Cloud International Website:
Entrance:
entranceplan_intlPro:
basicplan_intlPremium:
vipplan_intl
Terraform does not support creating Enterprise Edition packages. To purchase an Enterprise Edition package, contact us.
When you create a site using Terraform, you can associate an existing Enterprise Edition package instance ID.
When you create a package using Terraform, you must set the Terraform Provider information based on whether your account is an Alibaba Cloud China Website account or an Alibaba Cloud International Website account.
Method 1: Set the
regionparameter.Alibaba Cloud China Website:
cn-hangzhouAlibaba Cloud International Website:
ap-southeast-1
Method 2: Set the
account_typeparameter.Alibaba Cloud China Website:
DomesticInternational site:
International
Write configuration files
Create a working directory. Then, create the configuration files in the working directory as described in the following sections. Adjust the configuration information in the code as needed.
Define provider and Terraform version
First, create a configuration file named providers.tf. Then, copy the following code into the configuration file to centrally manage all provider configurations and version constraints.
terraform {
required_providers {
alicloud = {
source = "aliyun/alicloud"
version = "1.266.0"
region = "ap-southeast-1"
}
}
}Define Resource Variables
To improve code reusability, you can declare input variables and local variables. Create a configuration file named variables.tf, and copy the following code into the configuration file.
# The connection type of the site. Valid values are CNAME and NS.
variable "site_type" {
default = "NS"
}
# The billing method. The valid value is Subscription. Other values are not supported.
variable "payment_type" {
default = "Subscription"
}
# The area where the endpoint is deployed. Valid values are:
# domestic (the Chinese mainland), overseas (global excluding the Chinese mainland), global (global)
variable "coverage_name" {
default = "overseas"
}
# Specifies whether to enable auto-renewal.
variable "auto_pay" {
default = "true"
}
# The valid package names are:
# entranceplan_intl (Entrance Edition), basicplan_intl (Pro Edition), vipplan_intl (Premium Edition)
variable "plan_name" {
default = "entranceplan_intl"
}
# The site name. Replace it with your domain name.
variable "site_name" {
default = "aliyundoc.com"
}
# The origin value, which is your Origin Domain Name.
variable "record_value" {
default = "www.example.com"
}
# The origin port, which is the port number of your origin.
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 into it. This file serves as the main entry for all resource declarations.
# Create a package
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
Navigate to the directory that contains the configuration files. Then, run the following command to initialize the Terraform environment.
terraform init
Run the following command to validate the syntax and configuration of the Terraform files.
terraform validateIf the output is similar to the following figure, the validation is successful.

Run the following command to preview the changes that will be applied.
terraform planRun the following command to execute the Terraform script.
terraform applyWhen prompted, enter
yesto confirm the operation.
Verify results
View plans
Log on to the ESA console. In the navigation pane on the left, choose .
On the Package Management page, you can view the newly added package, as shown in the following figure.

View sites
In the ESA console, choose Site Management. You can view the newly added site, as shown in the following figure.

View DNS records
In the ESA console, choose Site Management. In the Website column, click the target site.
In the navigation pane on the left, choose . On the Records page, you can view the newly added DNS records, as shown in the following figure.

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