Alibaba Cloud CDN is integrated with Terraform. You can use Terraform to add and configure domain names that you want Alibaba Cloud CDN to accelerate. Terraform simplifies the configuration process. This topic describes how to use Terraform to add a domain name to Alibaba Cloud CDN and configure the domain name.

Prerequisites

  • Terraform is installed and configured. Terraform is supported by mainstream operating systems such as macOS, Windows, and Linux. For more information, see Install and configure Terraform in the local PC.
  • If Terraform is not installed on your on-premises machine, you can use Cloud Shell to install and configure Terraform. Cloud Shell is a free service that provides ready-to-use Terraform components and credentials. You can run Terraform commands in Cloud Shell. This improves O&M efficiency. For more information, see Use Terraform in Cloud Shell.

Procedure

Note The following procedure shows how to run Terraform in Cloud Shell to add a domain name to Alibaba Cloud CDN.
  1. Open your browser and enter https://shell.aliyun.com in the address bar to access Cloud Shell.

    For more information, see Use Cloud Shell.

  2. Log on to Cloud Shell.
  3. Create a Terraform template.

    Terraform templates can be used to initialize providers and add resource configurations. Use the Terraform syntax to create a Terraform template and save it as a TF file.

    For more information about the syntax for adding and configuring domain names by using Terraform, see alicloud_cdn_domain_config.

    Run the following vim command to create a template:
    shell@Alicloud:~$ vim provider.tf
    The following code block is an example of the provider.tf file:
    # Specify the Alibaba Cloud provider.
    provider "alicloud" {}
    
    # Add a domain name to Alibaba Cloud CDN.
    resource "alicloud_cdn_domain_new" "domain" {
      domain_name = "yourcdndomain.example.com"
      cdn_type    = "download"
      scope       = "overseas"
      sources {
        content  = "172.16.0.1"
        type     = "ipaddr"
        priority = "20"
        port     = 80
        weight   = "15"
      }
    }
    
    # Create an IP whitelist for the domain name.
    resource "alicloud_cdn_domain_config" "config" {
      domain_name   = alicloud_cdn_domain_new.domain.domain_name
      function_name = "ip_allow_list_set"
      function_args {
        arg_name  = "ip_list"
        arg_value = "192.168.0.1"
      }
    }
  4. Run the terraform init command to initialize the configurations.
    In this step, Terraform automatically identifies the provider field in the TF file and sends a request to the Terraform repository on GitHub to download the latest resource modules and plug-ins. The following output indicates that the configurations are initialized.
    * provider.alicloud: version = "~> 1.171"
    Terraform has been successfully initialized!
  5. Run the terraform plan command to preview the configurations.
    shell@Alicloud:~$ terraform plan
    Refreshing Terraform state in-memory prior to plan...
    The refreshed state will be used to calculate this plan, but will not be
    persisted to local or remote state storage.
    
    
    ------------------------------------------------------------------------
    
    An execution plan has been generated and is shown below.
    Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # alicloud_cdn_domain_config.config will be created
      + resource "alicloud_cdn_domain_config" "config" {
          + config_id     = (known after apply)
          + domain_name   = "yourcdndomain.example.com"
          + function_name = "ip_allow_list_set"
          + id            = (known after apply)
          + status        = (known after apply)
    
          + function_args {
              + arg_name  = "ip_list"
              + arg_value = "192.168.0.1"
            }
        }
    
      # alicloud_cdn_domain_new.domain will be created
      + resource "alicloud_cdn_domain_new" "domain" {
          + cdn_type          = "download"
          + cname             = (known after apply)
          + domain_name       = "yourcdndomain.example.cn"
          + id                = (known after apply)
          + resource_group_id = (known after apply)
          + scope             = "overseas"
    
          + certificate_config {
              + cert_name                 = (known after apply)
              + cert_type                 = (known after apply)
              + force_set                 = (known after apply)
              + private_key               = (sensitive value)
              + server_certificate        = (sensitive value)
              + server_certificate_status = (known after apply)
            }
    
          + sources {
              + content  = "172.16.0.1"
              + port     = 80
              + priority = 20
              + type     = "ipaddr"
            }
        }
    
    Plan: 2 to add, 0 to change, 0 to destroy.
  6. Run the terraform apply command to add the domain name and its configurations to Terraform.

    After you run the terraform apply command, you must preview the code to ensure that the code is correct and enter yes to confirm. Then, Terraform starts applying the configurations. It may take a few minutes to apply the configurations.

    Note The plus sign (+) before each parameter indicates newly added resources. It changes to a minus sign (-) when you delete resources. It changes to a minus sign and a plus sign (-/+) when you modify parameters to redeploy resources. A right arrow () is used to indicate that a parameter is set to a new value.
    shell@Alicloud:~$ terraform apply
    
    An execution plan has been generated and is shown below.
    Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # alicloud_cdn_domain_config.config will be created
      + resource "alicloud_cdn_domain_config" "config" {
          + config_id     = (known after apply)
          + domain_name   = "yourcdndomain.example.com"
          + function_name = "ip_allow_list_set"
          + id            = (known after apply)
          + status        = (known after apply)
    
          + function_args {
              + arg_name  = "ip_list"
              + arg_value = "192.168.0.1"
            }
        }
    
      # alicloud_cdn_domain_new.domain will be created
      + resource "alicloud_cdn_domain_new" "domain" {
          + cdn_type          = "download"
          + cname             = (known after apply)
          + domain_name       = "yourcdndomain.example.com"
          + id                = (known after apply)
          + resource_group_id = (known after apply)
          + scope             = "overseas"
    
          + certificate_config {
              + cert_name                 = (known after apply)
              + cert_type                 = (known after apply)
              + force_set                 = (known after apply)
              + private_key               = (sensitive value)
              + server_certificate        = (sensitive value)
              + server_certificate_status = (known after apply)
            }
    
          + sources {
              + content  = "172.16.0.1"
              + port     = 80
              + priority = 20
              + type     = "ipaddr"
            }
        }
    
    Plan: 2 to add, 0 to change, 0 to destroy.
    
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    
    alicloud_cdn_domain_new.domain: Creating...
    alicloud_cdn_domain_new.domain: Still creating... [10s elapsed]
    alicloud_cdn_domain_new.domain: Still creating... [20s elapsed]
    alicloud_cdn_domain_new.domain: Still creating... [30s elapsed]
    alicloud_cdn_domain_new.domain: Still creating... [40s elapsed]
  7. The following output indicates that the configurations are applied. You can also log on to the Alibaba Cloud CDN console to view the domain name and its configurations.
    alicloud_cdn_domain_new.domain: Still creating... [7m10s elapsed]
    alicloud_cdn_domain_new.domain: Still creating... [7m20s elapsed]
    alicloud_cdn_domain_new.domain: Creation complete after 7m24s [id=yourcdndomain.example.com]
    alicloud_cdn_domain_config.config: Creating...
    alicloud_cdn_domain_config.config: Still creating... [10s elapsed]
    alicloud_cdn_domain_config.config: Still creating... [20s elapsed]
    alicloud_cdn_domain_config.config: Still creating... [30s elapsed]
    alicloud_cdn_domain_config.config: Creation complete after 36s [id=yourcdndomain.example.com:ip_allow_list_set:238025248620544]
    
    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
    shell@Alicloud:~$