Use Terraform to manage DCDN resources
Terraform is an open source tool for provisioning and managing cloud infrastructure. You can use Terraform to manage DCDN resources, improve website performance, and speed up content delivery.
Procedure
Activate the DCDN service
Before you use Terraform to manage DCDN resources, you must activate the DCDN service in the Alibaba Cloud console. If you have not activated the DCDN service, a 403 DcdnServiceNotFound error is returned when you run terraform apply.
Grant required permissions to a RAM user
To execute a Terraform template, create a RAM user, obtain an AccessKey pair, and attach a permission policy to the RAM user. The AccessKey pair is used as the Terraform environment variable. Attach the following policy to the RAM user:
-
AliyunDCDNFullAccess: grants a RAM user the permissions to manage DCDN resources.
An Alibaba Cloud account has permissions on all API operations. Using an Alibaba Cloud account to call API operations poses security risks. We recommend that you use a RAM user for API calls and routine O&M. Grant the RAM user the required permissions based on your business requirements. The RAM user must have permissions to manage DCDN resources. For more information, see System policies for DCDN.
Before you add a domain name for acceleration, make sure that you have completed domain ownership verification in the Alibaba Cloud console. If domain ownership is not verified, a DomainOwnerVerifyFail error is returned when you run terraform apply.
Install Terraform
-
For information about how to install and configure Terraform on your local PC, see Install and configure Terraform in the local PC.
After Terraform is installed, open the CLI and run
terraform version. If version information is returned, Terraform is installed. -
If you do not want to install Terraform, use Cloud Shell provided by Alibaba Cloud. Cloud Shell includes a built-in Terraform runtime environment.
Create a template
Create a AddDomain.tf file with the following content to configure and manage domain name settings.
variable "domain_name" {
default = ""
}
resource "random_integer" "default" {
min = 10000
max = 99999
}
resource "alicloud_dcdn_domain" "domain" {
domain_name = "${var.domain_name}-${random_integer.default.result}"
scope = "overseas"
sources {
content = "1.1.1.1"
port = "80"
priority = "20"
type = "ipaddr"
weight = "10"
}
}
Replace the empty string with a real domain name for which you have completed domain ownership verification in the Alibaba Cloud console. If you use an unverified domain name such as tf-example.com, a DomainOwnerVerifyFail error is returned.
For more information, see DCDN resources and data sources on Terraform.
Execute the template
In this example, Windows is used. The commands may vary depending on your operating system.
-
Initialize and load required modules, such as the Provider module.
terraform init
-
Check whether the template syntax is valid.
terraform validate
-
Create an execution plan.
terraform plan
-
Deploy the template.
terraform apply
-
Enter yes as prompted. After the domain name is added, the system automatically assigns a CNAME. You can then use it to configure a CNAME record for the domain name at your DNS provider.
