API
The Gcore Customer Portal is being updated. Screenshots may not show the current version.
CDN
CDN
BillingCDN resources overviewOrigin groupPurgePrefetchReports
API
Chosen image
Home/CDN/Terraform/Create a CDN rule

Create a CDN rule via Terraform

If a CDN resource was created via Terraform, you can add a rule to it via Terraform. To do this, use the following instructions.  

1. Open the main.tf file. 

2. The file is supposed to contain the code for the creation of your CDN resource. If it is missing, add the code according to the Create a new CDN resource section.  

Don't worry, Terraform won't duplicate a resource. Terraform requires the code used to create a resource only to identify a resource that should be changed. 

3. At this step, you will add the necessary strings for creating a rule to the configuration file. 

Add the code below to a new string. Replace the hints in the brackets with your values and remove the brackets.  

resource "gcore_cdn_rule" "make up Terraform name for the rule; you can use any name, it will be linked to the rule inside the Terraform system" { resource_id = gcore_cdn_resource.Terraform name of the CDN resource for which you are adding the rule.id name = "make up a name for the rule that will be displayed in the Gcore Customer Portal" rule = "specify path to the files for which you are adding the rule; the path should always start with "^/" or "/"" rule_type   = 0 

4. If you want to add options to the rule, paste the code below. Replace the hints in the brackets with your values and remove the brackets. 

options {  code to configure the necessary options; for set-up guides, refer to the Terraform documentation for the Gcore provider, an example of configuration can be found in the "Configure CDN resource options" section  } 

If you don't need options, do not add the options code segment. 

5. Be aware to add a curly bracket to a new string below. 

} 

Here is an example of a configuration file. Let's say you want to add a rule with the following characteristics: 

  • example_rule — the name of the rule for Terraform
  • cdn_example_com — the name of the CDN resource in Terraform
  • PNG images — the name of the rule from the Gcore Customer Portal
  • /folder/images/*.png — the path to the files
  • The "WebP Compression" option with a final quality of 66 is required

The final code in the configuration file will look as follows:

configuration file

6. Save the changes in the configuration file. 

7. Access the "Terraform" folder in the command-line interface unless you are already in it, and run the terraform plan command — it will show what changes Terraform is going to make. If the code contains an error, the output will give a brief description of it.  

8. Run the terraform apply command — it will make changes to the CDN. Terraform will ask you to confirm the action — enter "yes".

Was this article helpful?

Not a Gcore user yet?

Learn more about our next-gen CDN

Go to the product page