Alibaba Cloud CDN is integrated with Terraform. This tutorial shows you how to use Terraform to add a CDN accelerated domain name and configure an IP address whitelist for the accelerated domain name.
Prerequisites
Before you can use CDN, activate CDN. For more information, see Activate CDN.
Use a RAM user with minimum required permissions to reduce the risk of AccessKey pair leaks. For information about how to attach the required policy to a RAM user, see Create a RAM user and Manage RAM user permissions. This example uses the following policy:
{ "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: Terraform Explorer is an online runtime environment where you can use Terraform without installing it. For more information, see Terraform Explorer. This method is suitable for quick debugging at no additional cost.
Use Terraform in Cloud Shell: Terraform is preinstalled in Cloud Shell with identity credentials already configured. You can run Terraform commands directly. For more information, see Create resources with Terraform. This method is suitable for quick debugging at low costs.
Install and configure Terraform on your on-premises machine: This method is suitable for environments with limited network connectivity or custom development setups. For more information, see Install and configure Terraform in the local PC.
Resources used
Some resources in this example incur fees. Release or unsubscribe from the resources when you no longer need them.
alicloud_cdn_domain_new: adds a domain name to CDN.
alicloud_cdn_domain_config: configures a rule for an CDN-accelerated domain name.
Step 1: Add an accelerated domain name
Create a working directory and a file named main.tf in the directory. Then, copy the following content to the main.tf file:
NoteThe domain names in the following sample code, such as
mydcdndomain-xxx.alicloud-provider.cnandmycdndomain-xxx.alicloud-provider.cn, work only in the managed environment for running the sample code with a few clicks. You cannot pass ownership verification for these domain names. To run this tutorial in your local environment, replace the value ofdomain_namewith a domain name that you own and for which you have completed ownership verification.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" } }Run the following command to initialize the Terraform runtime environment:
terraform initIf the following information is returned, Terraform is initialized:
Initializing the backend... Initializing provider plugins... ... Terraform has been successfully initialized! ...Run the following command to add a domain name:
terraform applyDuring execution, enter
yeswhen prompted and press Enter. Wait until the command completes. If the following information appears, the domain name is added.NoteIf the error message "code: 400, Owner verification of the root domain failed" appears, the domain name is added to the CDN system for the first time. You must verify the ownership of the domain name as described in Verify domain name ownership.
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 accelerated domain name
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" } }Create an execution plan and preview the changes.
terraform planRun the following command to configure an IP address whitelist for the domain name:
terraform applyDuring execution, enter
yeswhen prompted and press Enter. Wait until the command completes. 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 created by Terraform:
terraform showshell@Alicloud:~/cdn$ terraform show
# alicloud_cdn_domain_config.config-ip:
resource "alicloud_cdn_domain_config" "config-ip" {
config_id = "394085720977408"
domain_name = "xxx"
function_name = "ip_allow_list_set"
id = "xxx:ip_allow_list_set:394085720977408"
parent_id = "0"
status = "success"
function_args {
arg_name = "ip_list"
arg_value = "192.168.0.1"
}
}
# alicloud_cdn_domain_new.domain:
resource "alicloud_cdn_domain_new" "domain" {
cdn_type = "download"
cname = "xxx.com.w.cdngslb.com"
domain_name = "xxx.com"
id = "xxx.com"
resource_group_id = "rg-acfmykd63gtpfpa"
scope = "overseas"
status = "online"
certificate_config {
cert_type = "cas"
server_certificate_status = "off"
}
sources {
content = "xxx.oss-rg-china-mainland.aliyuncs.com"
port = 80
priority = 20
type = "oss"
weight = 15
}
}
Log on to the CDN console
Log on to the CDN console and view the IP address blacklist/whitelist configured for the added domain name.
On the management page of the target domain name, click Access Control in the left-side navigation pane and then click the IP address blacklist/whitelist tab. The type is IP address whitelist, and the IP address 192.168.0.1 is added to the whitelist.
Clear resources
If you no longer need the resources created by Terraform, run the following command to release them. For more information about the terraform destroy command, see Common commands.
terraform destroyExample
Sample code
For more examples, visit the quickstarts page and view the examples in the folder of the corresponding service.