Skip to content

Commit c733535

Browse files
fix(docs): Update CDN usage example (#1005)
relates to STACKITCDN-1003
1 parent 2a077d1 commit c733535

File tree

2 files changed

+498
-110
lines changed

2 files changed

+498
-110
lines changed
Lines changed: 249 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,255 @@
11
---
2-
page_title: "Using STACKIT CDN with your own domain"
2+
page_title: "Using STACKIT CDN to service static files from an HTTP Origin with STACKIT CDN"
33
---
4-
# Using STACKIT CDN with your own domain
5-
6-
## Overview
7-
8-
This guide outlines the process of creating a STACKIT CDN distribution and configuring it to make use of an existing domain using STACKIT DNS.
9-
10-
## Steps
11-
12-
1. **Create a STACKIT CDN and DNS Zone**
13-
14-
Create the CDN distribution and the DNS zone.
15-
16-
```terraform
17-
resource "stackit_cdn_distribution" "example_distribution" {
18-
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
19-
config = {
20-
backend = {
21-
type = "http"
22-
origin_url = "mybackend.onstackit.cloud"
23-
}
24-
regions = ["EU", "US", "ASIA", "AF", "SA"]
25-
blocked_countries = ["DE", "AT", "CH"]
26-
}
4+
5+
# Using STACKIT CDN to service static files from an HTTP Origin with STACKIT CDN
6+
7+
This guide will walk you through the process of setting up a STACKIT CDN distribution to serve static files from a
8+
generic HTTP origin using Terraform. This is a common use case for developers who want to deliver content with low
9+
latency and high data transfer speeds.
10+
11+
---
12+
13+
## Prerequisites
14+
15+
Before you begin, make sure you have the following:
16+
17+
* A **STACKIT project** and a user account with the necessary permissions for the CDN.
18+
* A **Service Account Key**: you can read about creating one here: [Create a Service Account Key
19+
](https://docs.stackit.cloud/stackit/en/create-a-service-account-key-175112456.html)
20+
21+
---
22+
23+
## Step 1: Configure the Terraform Provider
24+
25+
First, you need to configure the STACKIT provider in your Terraform configuration. Create a file named `main.tf` and add
26+
the following code. This block tells Terraform to download and use the STACKIT provider.
27+
28+
```terraform
29+
terraform {
30+
required_providers {
31+
stackit = {
32+
source = "stackitcloud/stackit"
2733
}
28-
29-
resource "stackit_dns_zone" "example_zone" {
30-
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
31-
name = "My DNS zone"
32-
dns_name = "myapp.runs.onstackit.cloud"
33-
contact_email = "[email protected]"
34-
type = "primary"
34+
}
35+
}
36+
37+
variable "service_account_key" {
38+
type = string
39+
description = "Your STACKIT service account key."
40+
sensitive = true
41+
default = "path/to/sa-key.json"
42+
}
43+
44+
variable "project_id" {
45+
type = string
46+
default = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" # Your project ID
47+
}
48+
49+
provider "stackit" {
50+
# The STACKIT provider is configured using the defined variables.
51+
default_region = "eu01"
52+
service_account_key_path = var.service_account_key
53+
}
54+
55+
```
56+
57+
## Step 2: Create the DNS Zone
58+
59+
The first resource you'll create is the DNS zone, which will manage the records for your domain.
60+
61+
```terraform
62+
resource "stackit_dns_zone" "example_zone" {
63+
project_id = var.project_id
64+
name = "My DNS zone"
65+
dns_name = "myapp.runs.onstackit.cloud"
66+
contact_email = "[email protected]"
67+
type = "primary"
68+
}
69+
```
70+
71+
## Step 3: Create the CDN Distribution
72+
73+
Next, define the CDN distribution. This is the core service that will cache and serve your content from its origin.
74+
75+
```terraform
76+
resource "stackit_cdn_distribution" "example_distribution" {
77+
project_id = var.project_id
78+
79+
config = {
80+
# Define the backend configuration
81+
backend = {
82+
type = "http"
83+
84+
# Replace with the URL of your HTTP origin
85+
origin_url = "https://your-origin-server.com"
3586
}
36-
```
37-
38-
2. **Add CNAME record to your DNS zone**
39-
40-
If you want to redirect your entire domain to the CDN, you can instead use an A record.
41-
```terraform
42-
resource "stackit_dns_record_set" "example" {
43-
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
44-
zone_id = stackit_dns_zone.example_zone.zone_id
45-
name = "cdn"
46-
type = "CNAME"
47-
records = ["${stackit_cdn_distribution.domains[0].name}."]
87+
88+
# The regions where content will be hosted
89+
regions = ["EU", "US", "ASIA", "AF", "SA"]
90+
blocked_countries = []
91+
}
92+
93+
}
94+
```
95+
96+
## Step 4: Create the DNS CNAME Record
97+
98+
Finally, create the **CNAME record** to point your custom domain to the CDN. This step must come after the CDN is
99+
created because it needs the CDN's unique domain name as its target.
100+
101+
```terraform
102+
resource "stackit_dns_record_set" "cname_record" {
103+
project_id = stackit_dns_zone.example_zone.project_id
104+
zone_id = stackit_dns_zone.example_zone.zone_id
105+
106+
# This is the custom domain name which will be added to your zone
107+
name = "cdn"
108+
type = "CNAME"
109+
ttl = 3600
110+
111+
# Points to the CDN distribution's unique domain.
112+
# Notice the added dot at the end of the domain name to point to a FQDN.
113+
records = ["${stackit_cdn_distribution.example_distribution.domains[0].name}."]
114+
}
115+
116+
```
117+
118+
This record directs traffic from your custom domain to the STACKIT CDN infrastructure.
119+
120+
## Step 5: Add a Custom Domain to the CDN
121+
122+
To provide a user-friendly URL, associate a custom domain (like `cdn.myapp.runs.onstackit.cloud`) with your
123+
distribution.
124+
125+
```terraform
126+
resource "stackit_cdn_custom_domain" "example_custom_domain" {
127+
project_id = stackit_cdn_distribution.example_distribution.project_id
128+
distribution_id = stackit_cdn_distribution.example_distribution.distribution_id
129+
130+
# Creates "cdn.myapp.runs.onstackit.cloud" dynamically
131+
name = "${stackit_dns_record_set.cname_record.name}.${stackit_dns_zone.example_zone.dns_name}"
132+
}
133+
134+
```
135+
136+
This resource links the subdomain you created in the previous step to the CDN distribution.
137+
138+
## Complete Terraform Configuration
139+
140+
Here is the complete `main.tf` file, which follows the logical order of operations.
141+
142+
```terraform
143+
# This configuration file sets up a complete STACKIT CDN distribution
144+
# with a custom domain managed by STACKIT DNS.
145+
146+
# -----------------------------------------------------------------------------
147+
# PROVIDER CONFIGURATION
148+
# -----------------------------------------------------------------------------
149+
150+
terraform {
151+
required_providers {
152+
stackit = {
153+
source = "stackitcloud/stackit"
48154
}
49-
```
50-
51-
3. **Create a STACKIT CDN Custom Domain**
52-
```terraform
53-
# Create a CDN custom domain
54-
resource "stackit_cdn_custom_domain" "example" {
55-
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
56-
distribution_id = stackit_cdn_distribution.example_distribution.distribution_id
57-
name = "${stackit_dns_record_set.example.name}.${stackit_dns_zone.example_zone.dns_name}"
155+
}
156+
}
157+
158+
variable "service_account_key" {
159+
type = string
160+
description = "Your STACKIT service account key."
161+
sensitive = true
162+
default = "path/to/sa-key.json"
163+
}
164+
165+
variable "project_id" {
166+
type = string
167+
description = "Your STACKIT project ID."
168+
default = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
169+
}
170+
171+
provider "stackit" {
172+
# The STACKIT provider is configured using the defined variables.
173+
default_region = "eu01"
174+
service_account_key_path = var.service_account_key
175+
}
176+
177+
# -----------------------------------------------------------------------------
178+
# DNS ZONE RESOURCE
179+
# -----------------------------------------------------------------------------
180+
# The DNS zone manages all records for your domain.
181+
# It's the first resource to be created.
182+
# -----------------------------------------------------------------------------
183+
184+
resource "stackit_dns_zone" "example_zone" {
185+
project_id = var.project_id
186+
name = "My DNS zone"
187+
dns_name = "myapp.runs.onstackit.cloud"
188+
contact_email = "[email protected]"
189+
type = "primary"
190+
}
191+
192+
# -----------------------------------------------------------------------------
193+
# CDN DISTRIBUTION RESOURCE
194+
# -----------------------------------------------------------------------------
195+
# This resource defines the CDN, its origin, and caching regions.
196+
# -----------------------------------------------------------------------------
197+
198+
resource "stackit_cdn_distribution" "example_distribution" {
199+
project_id = var.project_id
200+
201+
config = {
202+
# Define the backend configuration
203+
backend = {
204+
type = "http"
205+
206+
# Replace with the URL of your HTTP origin
207+
origin_url = "https://your-origin-server.com"
58208
}
59-
```
60-
61-
Now, you can access your content on the url `cdn.myapp.runs.onstackit.cloud`.
209+
210+
# The regions where content will be hosted
211+
regions = ["EU", "US", "ASIA", "AF", "SA"]
212+
blocked_countries = []
213+
}
214+
}
215+
216+
# -----------------------------------------------------------------------------
217+
# CUSTOM DOMAIN AND DNS RECORD
218+
# -----------------------------------------------------------------------------
219+
# These resources link your CDN to a user-friendly custom domain and create
220+
# the necessary DNS record to route traffic.
221+
# -----------------------------------------------------------------------------
222+
223+
resource "stackit_dns_record_set" "cname_record" {
224+
project_id = stackit_dns_zone.example_zone.project_id
225+
zone_id = stackit_dns_zone.example_zone.zone_id
226+
# This is the custom domain name which will be added to your zone
227+
name = "cdn"
228+
type = "CNAME"
229+
ttl = 3600
230+
# Points to the CDN distribution's unique domain.
231+
# The dot at the end makes it a fully qualified domain name (FQDN).
232+
records = ["${stackit_cdn_distribution.example_distribution.domains[0].name}."]
233+
234+
}
235+
236+
resource "stackit_cdn_custom_domain" "example_custom_domain" {
237+
project_id = stackit_cdn_distribution.example_distribution.project_id
238+
distribution_id = stackit_cdn_distribution.example_distribution.distribution_id
239+
240+
# Creates "cdn.myapp.runs.onstackit.cloud" dynamically
241+
name = "${stackit_dns_record_set.cname_record.name}.${stackit_dns_zone.example_zone.dns_name}"
242+
}
243+
244+
# -----------------------------------------------------------------------------
245+
# OUTPUTS
246+
# -----------------------------------------------------------------------------
247+
# This output will display the final custom URL after `terraform apply` is run.
248+
# -----------------------------------------------------------------------------
249+
250+
output "custom_cdn_url" {
251+
description = "The final custom domain URL for the CDN distribution."
252+
value = "https://${stackit_cdn_custom_domain.example_custom_domain.name}"
253+
}
254+
255+
```

0 commit comments

Comments
 (0)