All Products
Search
Document Center

Dynamic Content Delivery Network:Use Terraform to manage DCDN resources

Last Updated:Aug 30, 2024

Terraform is an open source tool that allows you to safely and efficiently provision and manage your cloud infrastructure. You can use Terraform to manage DCDN resources. This topic describes how to use Terraform to improve website performance and speed up content delivery.

Procedure

Grant required permissions to a RAM user

You must grant a Resource Access Management (RAM) user permissions required to execute a Terraform template. You must create a RAM user, obtain an AccessKey pair, and then attach a permission policy to the RAM user. The AccessKey pair is configured as the environment variable of Terraform. 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. Security risks may arise if you use an Alibaba Cloud account to call API operations. We recommend that you call API operations or perform routine O&M as a RAM user. Before you call API operations as a RAM user, grant the required permissions to the RAM user based on your business requirements. The RAM user must have the permissions to manage DCDN resources. For more information, see System policies for DCDN.

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, you can open the CLI and then 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 provides a built-in runtime environment for Terraform.

Create a template

Create a AddDomain.tf file and enter the following content in the file. The file is used to configure and manage domain name settings.

variable "domain_name" {
  default = "tf-example.com"
}
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"
  }
}

For more information, see DCDN resources and data sources on Terraform.

Execute the template

In this example, Windows is used to execute the template. The commands vary with the operating system that you use.

  1. Initialize and load required modules, such as the Provider module.

    terraform init

    image

  2. Check whether the template syntax is valid.

    terraform validate

    image

  3. Create an execution plan.

    terraform plan

    image

  4. Deploy the template.

    terraform apply

    image

  5. 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.

    image