After connecting a site to ESA, you must add a DNS record to register the domain name with ESA. This ensures that the accelerated domain name can be resolved and accessed. This topic describes how to use Terraform to add a DNS record.
Resources
Resource for purchasing a plan: alicloud_esa_rate_plan_instance
Resource for adding a site: alicloud_esa_site
Resource for adding a DNS record: alicloud_esa_record
Notes
The available plan types differ for Alibaba Cloud China Website and Alibaba Cloud International Website accounts.
Plans available for Alibaba Cloud China Website accounts:
Free Edition:
entranceplanBasic Edition:
basicStandard Edition:
mediumPremium Edition:
high
Plans available for Alibaba Cloud International Website accounts:
Entrance:
entranceplan_intlPro:
basicplan_intlPremium:
vipplan_intl
You cannot currently create an Enterprise plan by using Terraform. To purchase an Enterprise plan, contact us.
When you use Terraform to create a site, you can associate it with the instance ID of an existing Enterprise Edition plan.
When you use Terraform to create a plan, you must configure the Terraform provider according to your account type: Alibaba Cloud China Website or Alibaba Cloud International Website.
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:
DomesticAlibaba Cloud International Website:
International
Write configuration files
Create a working directory. In the directory, create the configuration files as described in the following sections. Adjust the code settings as needed.
Define the provider and Terraform version
First, create a configuration file named providers.tf. Copy the following code into the file to manage all provider configurations and version constraints.
terraform {
required_providers {
alicloud = {
source = "aliyun/alicloud"
version = "1.266.0"
}
}
}
provider "alicloud" {
region = "ap-southeast-1"
}Define resource variables
To make your code more reusable, declare input and local variables. Create a configuration file named variables.tf and copy the following code into it.
# The access type for the site. Valid values: CNAME and NS.
variable "site_type" {
default = "NS"
}
# The billing method. The only valid value is Subscription.
variable "payment_type" {
default = "Subscription"
}
# The service coverage. Valid values:
# domestic (Chinese mainland), overseas (Global, excluding Chinese mainland), global (Global).
variable "coverage_name" {
default = "overseas"
}
# Enable auto-renewal.
variable "auto_pay" {
default = "true"
}
# The plan name. Valid values:
# entranceplan_intl (Entrance), basicplan_intl (Pro), vipplan_intl (Premium)
variable "plan_name" {
default = "entranceplan_intl"
}
# The site name. Replace this with your domain name.
variable "site_name" {
default = "aliyundoc.com"
}
# The domain name of your origin.
variable "record_value" {
default = "www.example.com"
}
# The origin port.
variable "record_port" {
default = "80"
}
# The DNS record name, 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 contains the main 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 the resources
Navigate to the directory that contains your configuration files and run the following command to initialize the Terraform environment.
terraform initRun the following command to validate the Terraform syntax and configuration.
terraform validateRun the following command to preview the execution plan.
terraform planRun the following command to apply the configuration.
terraform applyWhen prompted, enter
yesto confirm and apply the changes.
Verify the results
View the plan
Log on to the ESA console and go to the Plan Management page.
On the Plan Management page, the new plan appears, as shown in the following figure.

View the site
On the ESA console, choose Websites. The new site appears, as shown in the following figure.

View the DNS record
On the ESA console, choose Websites, and in the Website column, click the target site.
In the navigation pane, choose . The new DNS record appears, as shown in the following figure.

(Optional) Clean up resources
When you no longer need these resources, run the terraform destroy command.
terraform destroy