All Products
Search
Document Center

CDN:Use Terraform to add and configure a domain name

Last Updated:Mar 22, 2024

Alibaba Cloud CDN is integrated with Terraform. You can use Terraform to add and configure domain names that you want 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.

Background information

For more information about Terraform, see What is Terraform?

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.alibabacloud.com/ in the address bar to access Cloud Shell.

    For more information about how to use Cloud Shell, see Use Cloud Shell.

  2. Log on to Cloud Shell.

  3. Create a Terraform template.

    You can use Terraform templates to initialize providers and add resource configurations. Use the Terraform syntax to create a Terraform template and save the template as a .tf file.

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

    Run the following vim command to create a template:

    shell@Alicloud:~$ vim provider.tf

    The following code block shows a sample content of the provider.tf file:

    Note
    • You can view the fields of the resource parameter in the CDN folder of Alibaba Cloud Provider on the Terraform official website.

    • For information about how to configure the cdn_type, scope, and sources fields, see parameters in AddCdnDomain.

    • For information about the fields of the function_name parameter and how to configure the function_arg parameter, see Feature settings for domain names.

    # Specify the Alibaba Cloud provider.
    provider "alicloud" {}
    
    # Add a domain name.
    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 address 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 Terraform 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 command output indicates that the Terraform 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 need to preview the code to ensure that the code is correct and enter yes to confirm. Then, Terraform starts to apply the configurations, which may require a few minutes.

    Note

    The plus sign (+) before each parameter indicates new resources. The sign changes to a minus sign (-) when you delete resources and changes to a minus sign and a plus sign (-/+) when you modify parameters to redeploy resources. A right arrow () indicates 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 command output indicates that the configurations are applied. 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:~$