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

Create and manage CDN resources with Terraform

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.

Create a new CDN resource

Use the following steps to create a CDN resource and integrate it with your websites (content sources).

Step 1. Open the configuration file

Open the main.tf file where you configured the Gcore provider for Terraform.

Step 2. Create an origin group

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.

resource "gcore_cdn_origingroup" "make up Terraform name of the origin group; you can use any name, it will be linked to the origin in the Terraform system" { name = "make up a name of the origin group that will be displayed in the Gcore Customer Portal"

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.

 

origin {         source = "domain of your origin website" enabled = true     } 
  • To add a backup origin, add the following code. Specify your website domain and remove the brackets.

 

origin { source = "domain of your origin website" enabled = true backup = true }

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: 

  • example_terraform — name of the origin group that will be displayed in Terraform
  • example group — the name of the origin group that will be displayed in the Gcore Customer Portal
  • The Use next upstream option is disabled
  • one.com and two.com — the active origins
  • three.com — the backup origin

Then the code in the configuration file will look as follows: 

resource "gcore_cdn_origingroup" "example_terraform" {    name     = "example group"     use_next = false    origin {        source  = "one.com"    enabled = true    }    origin {        source  = "two.com"    enabled = true    }    origin {    source  = "three.com"    enabled = true    backup  = true  }  } 

Step 3. Add a CDN resource to your origin group

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.   

resource "gcore_cdn_resource" "Terraform name for your resource" {    cname               = "custom domain like cdn.[your site's domain]; for example, if your site is example.com, enter cdn.example.com" origin_group = gcore_cdn_origingroup.Terraform name for your origin group that will be requested for content.id  

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:

 

secondary_hostnames = ["additional custom domain"] 
  • To deliver different types of content from more than two separate custom domains:

 

secondary_hostnames = ["additional custom domain 1","additional custom domain 2, continue adding up to 10 domains in quotation marks and separating them by commas"] 

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: 

  • cdn_example_com — name of the resource that will be displayed in Terraform
  • сdn.one.com — custom domain of the CDN resource that will be displayed in the file paths and in the Gcore Customer Portal
  • example_terraform — name of the origin group that will be displayed in Terraform
  • HTTPS — protocol that will be used by the CDN to access an origin
  • cdn.two.com and cdn.three.com — additional custom domains

Then the code in the configuration file will look as follows:  

resource "gcore_cdn_resource" "cdn_example_com" { cname = "сdn.one.com" origin_group = gcore_cdn_origingroup.example_terraform.id origin_protocol = "HTTPS" secondary_hostnames = ["cdn.two.com","cdn.three.com"] } 

Step 4. Verify the configuration

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:

configuration file

Make sure all data is correct and save the changes.  

Step 5. Preview your 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. 

Step 6. Apply the configuration

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.

Step 7. Configure CDN resource in the Customer Portal

1. Log in to your Gcore account, go to the "CDN" tab and click the custom domain of the resource created in Terraform.

CDN tab

2. On the page that opens, click "Setup guide". 

Setup guide

3. Copy the domain name such as *.gcdn.co. from the sliding panel.

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.  

Configure CDN resource options

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. 

Step 1. Update the configuration file

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 { 

Step 2. Provide the required options for Gcore resources

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. 

Step 3. Add custom values to the configuration

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:  

  • 345600 seconds (4 days) is the caching time for responses with 200, 206, 301, and 302 codes
  • 1000 seconds is the caching time for responses with a 403 code
  • 50 seconds is the caching time for responses with a 404 code

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" 
  } 

Step 4. Verify the updated configuration

The configuration file now contains the code that creates a CDN resource with your option values. Here's an example of the file:

configuration file

Double-check and save the changes in the configuration file.

Step 5. Preview the updates

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. 

Step 6. Apply the updates

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