If you have already worked with Terraform, you can use the abridged instructions for managing the Gcore CDN infrastructure:
1. Copy the required code from the Resources section in the Terraform documentation and paste it to the main.tf file.
2. Add your values to the code.
3. Run the terraform plan
command — it will show what changes you are going to make to the CDN settings.
4. Run the terraform apply
command to make changes to the CDN.
For detailed guidelines on how to create and manage CDN resources and resource options, check out the following sections.
Use the following steps to create a CDN resource and integrate it with your websites (content sources).
Open the main.tf file where you configured the Gcore provider for Terraform.
At this step, you need to write the code that creates an origin group — the CDN resource will pull content from those origins. An origin group has three features:
You choose which origin will be active and which origin will be backup. An active origin is accessed whenever the CDN requests content. A backup origin is accessed only when active origins return 4xx or 5xx error. A group must have at least one active origin.
You can enable or disable the "Use next upstream" option. It defines the order in which the CDN will access the remaining origins if the first origin returns a 4xx or 5xx error. If this option is on, the CDN will access active origins one by one, and then request backup origins. If it is off, the CDN will ignore the remaining active origins and will immediately request a backup origin.
You can create a group from a single origin. It must consist of an active origin. The "Use next upstream" option should be disabled.
To create an origin group via Terraform:
1. Copy the following code to the main.tf file. Replace the information in brackets with your values, and then remove the brackets.
2. Add the appropriate strings and code snippets to the file:
To enable the Use next upstream option: use_next = true
To disable the Use next upstream option: use_next = false
To add an active origin, add the following code. Specify your website domain and remove the brackets.
You can add up to ten origins to a CDN resource.
3. Add another curly bracket to a new string below.
}
Here is an example. Let's say you want to create an origin group with the following parameters:
Then the code in the configuration file will look as follows:
At this step, you will write the code that adds a CDN resource to your origin group. Continue to add the following code to the same configuration file.
1. Copy the code, replace the information in brackets with your values, and then remove the brackets.
2. Add the appropriate strings and code snippets to the file:
To configure the CDN so that it will access an origin on a protocol of a user's request — HTTP or HTTPS: origin_protocol = "MATCH"
Enable CDN to use only HTTP: origin_protocol = "HTTP"
Enable CDN to use only HTTPS: origin_protocol = "HTTPS"
To deliver different types of content from two separate custom domains:
3. At the end, add a curly bracket to a new string below.
}
Here is an example. Let's say you want to create a CDN resource with the following parameters:
Then the code in the configuration file will look as follows:
At Step 2 and Step 3, you entered the code that has created an origin group and a CDN resource. Below is an example of how your code may look in your configuration file:
Make sure all data is correct and save the changes.
Open the "Terraform" folder in command-line interface and run the terraform plan
command — it will show what changes Terraform is going to make. If the code in the configuration file contains an error, the output will give a brief description of it.
Run the terraform apply
command — it will make changes to the CDN. Terraform will ask you to confirm the action — enter "yes".
Congratulations! The origin group and CDN resource have been created!
Now you need to configure a custom domain for the CDN resource and change the file paths so that they contain the custom domain instead of the origin domain. Use the following section for detailed instructions.
1. Log in to your Gcore account, go to the "CDN" tab and click the custom domain of the resource created in Terraform.
2. On the page that opens, click "Setup guide".
3. Copy the domain name such as *.gcdn.co. from the sliding panel.
4. Go to the settings of your DNS provider and create a CNAME record for the custom domain. For the value of the CNAME record, paste the value copied at the previous step.
Here is an example. Let's say your custom domain is cdn.example.com and at Step 3 you copied the cl-sdf34f.gcdn.co domain. So, in the personal account of your DNS provider, you need to create a CNAME record for cdn.example.com with cl-sdf34f.gcdn.co. as its value.
5. Change the file paths so that they contain the custom domain instead of origin domain. For example, if your origin is example.com, and the custom domain is cdn.example.com, replace in the file paths example.com with cdn.example.com. If the source website is built on a CMS, you can change the file paths using special plugins you can find on the Internet. If the site is not built on a CMS, we recommend writing a script to replace domain name in the paths.
Congratulations! The setup is complete! You have created and integrated your CDN resource.
When you create a CDN resource via Terraform, it automatically adds the following options with default values:
We constantly add new options. The up-to-date list is always available in the Terraform documentation for the Gcore provider.
If a CDN resource was created via Terraform, you can change its options via Terraform. To do this, use the following steps.
Open the main.tf file. It's supposed to contain the code for the creation of your CDN resource.
If the main.tf file is missing, add the code according to the Create a new CDN resource section. Don't worry, Terraform won't duplicate a resource. It requires the code used for the creation of the resource only to identify a resource that should be changed.
Add the following string before the last curly bracket:
options {
Open the Terraform documentation for the Gcore provider and find the required option. Follow the guide from the Terraform documentation to enter the required option values.
Add a curly bracket to a new string below:
}
Here is an example. You want to set up CDN Caching and find this option in the Terraform documentation — edge_cache_settings. You need to set up CDN Caching with these settings:
According to the guide, you need to add the necessary settings below the options {
string.
edge_cache_settings {
custom_values = {
"403" = "1000s"
"404" = "50s"
}
enabled = true
value = "345600s"
}
The configuration file now contains the code that creates a CDN resource with your option values. Here's an example of the file:
Double-check and save the changes in the configuration file.
Access the "Terraform" folder in the command-line interface 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.
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?
Learn more about our next-gen CDN