All Products
Search
Document Center

CDN:Use Terraform to add and configure a domain name

Last Updated:Dec 05, 2024

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

Before you begin

  • Before you can use CDN, activate CDN. For more information, see Activate CDN.

  • Use a RAM user that has the minimum required permissions to perform the operations in this topic. This minimizes the risk of leaking the AccessKey pair of your Alibaba Cloud account. For information about how to attach the policy that contains the minimum required permissions to the RAM user, see Create a RAM user and Grant permissions to a RAM user. In this example, the following policy is used:

    {
      "Version": "1",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "cdn:AddCdnDomain",
            "cdn:DescribeCdnDomainDetail",
            "cdn:DescribeDomainCertificateInfo",
            "cdn:ListTagResources",
            "cdn:DeleteCdnDomain",
            "cdn:BatchSetCdnDomainConfig",
            "cdn:DescribeCdnDomainConfigs",
            "cdn:DeleteSpecificConfig"
          ],
          "Resource": "*"
        }
      ]
    }
  • Prepare the Terraform environment. You can use one of the following methods to use Terraform:

    • Use Terraform in Terraform Explorer: Alibaba Cloud provides Terraform Explorer, an online runtime environment for Terraform. You can use Terraform after you log on to Terraform Explorer without the need to install Terraform. For more information, see Use Terraform in Terraform Explorer. This method is suitable for scenarios in which you want to use and debug Terraform in a fast and convenient manner at no additional cost.

    • Use Terraform in Cloud Shell: Terraform is preinstalled in Cloud Shell and identity credentials are configured. You can directly run Terraform commands in Cloud Shell. For more information, see Use Terraform in Cloud Shell. This method is suitable for scenarios in which you want to use and debug Terraform in a fast and convenient manner at low costs.

    • Install and configure Terraform on your on-premises machine: This method is suitable for scenarios in which network conditions are poor or a custom development environment is used. For more information, see Install and configure Terraform in the local PC.

Resources used

Note

Fees are generated for specific resources in this example. Release or unsubscribe from the resources when you no longer need them.

Step 1: Add a domain name to CDN

  1. Create a working directory and a file named main.tf in the directory. Then, copy the following content to the main.tf file:

    resource "random_integer" "default" {
      min = 10000
      max = 99999
    }
    
    # Add a domain name.
    resource "alicloud_cdn_domain_new" "domain" {
      domain_name = "mycdndomain-${random_integer.default.result}.alicloud-provider.cn"
      cdn_type    = "download"
      scope       = "overseas"
      sources {
        content  = "myoss-${random_integer.default.result}.oss-rg-china-mainland.aliyuncs.com"
        type     = "oss"
        priority = "20"
        port     = 80
        weight   = "15"
      }
    }
  2. Run the following command to initialize the Terraform runtime environment:

    terraform init

    If the following information is returned, Terraform is initialized:

    Initializing the backend...
    Initializing provider plugins...
    ...
    Terraform has been successfully initialized!
    ...
  3. Run the following command to add a domain name:

    terraform apply

    During the execution, enter yes as prompted and press the Enter key. Wait until the command is executed. If the following information appears, the domain name is added.

    Note

    If the error message "code: 400, Owner verification of the root domain failed" appears, the domain name is added to the CDN for the first time, and you need to verify the ownership of the domain name. For more information, see Verify the ownership of a domain name.

    You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
    
    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
    
    
    Apply complete!  Resources: 2 added, 0 changed, 0 destroyed.

Step 2: Configure a rule for the domain name

  1. Add the following content to the main.tf file.

    # Configure an IP address whitelist for the domain name.
    resource "alicloud_cdn_domain_config" "config-ip" {
      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"
      }
    }
  2. Create an execution plan and preview the changes.

    terraform plan
  3. Run the following command to configure an IP address whitelist for the domain name:

    terraform apply

    During command execution, follow the instructions to type yes and press Enter. Wait until the command is run. If the following information is returned, the rule is configured.

    Apply complete!  Resources: 1 added, 0 changed, 0 destroyed.

Check the result

Run the terraform show command

Run the following command to query the resources that have been created by Terraform:

terraform show

image

Log on to the CDN console

Log on to the CDN console and view the IP address blacklist and whitelist configured for the added domain name.

image

Clear resources

If you no longer require the preceding resources that are created or managed by using Terraform, run the following command to release the resources. For more information about the terraform destroy command, see Common commands.

terraform destroy

Example

Sample code

resource "random_integer" "default" {
  min = 10000
  max = 99999
}

# Add a domain name.
resource "alicloud_cdn_domain_new" "domain" {
  domain_name = "mycdndomain-${random_integer.default.result}.alicloud-provider.cn"
  cdn_type    = "download"
  scope       = "overseas"
  sources {
    content  = "myoss-${random_integer.default.result}.oss-rg-china-mainland.aliyuncs.com"
    type     = "oss"
    priority = "20"
    port     = 80
    weight   = "15"
  }
}

# Configure an IP address whitelist for the domain name.
resource "alicloud_cdn_domain_config" "config-ip" {
  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"
  }
}

If you want to experience more examples, go to the quickstarts page and view the examples in the folder of the corresponding service.