Repository navigation
Expand file tree
/
Copy pathresource.tf
More file actions
224 lines (198 loc) · 6.97 KB
/
Copy pathresource.tf
File metadata and controls
224 lines (198 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# ===================================================================
# CacheFly CDN - example demonstrating multiple CDN services and domain
#
# ===================================================================
terraform {
required_version = ">= 1.0"
required_providers {
cachefly = {
source = "cachefly/cachefly"
}
}
}
provider "cachefly" {
api_token = ""
}
# Variables for configuration
variable "project_name" {
description = "Name of your project"
type = string
default = "my-app"
}
variable "environment" {
description = "Environment (dev, staging, prod)"
type = string
default = "prod"
}
variable "base_domain" {
description = "Your base domain (e.g., example.com)"
type = string
default = "thobingo.online"
}
# Local values for computed configurations
locals {
name_prefix = "${var.project_name}-${var.environment}"
}
# ===================================================================
# SERVICES - CDN Services
# ===================================================================
# Main web application service
resource "cachefly_service" "web_app" {
name = "${local.name_prefix}-web"
unique_name = "${local.name_prefix}-web-05"
description = "Web application CDN for ${var.project_name}"
auto_ssl = true
}
# API service
resource "cachefly_service" "api" {
name = "${local.name_prefix}-api"
unique_name = "${local.name_prefix}-api-05"
description = "API CDN for ${var.project_name}"
auto_ssl = true
}
# Static assets service
resource "cachefly_service" "assets" {
name = "${local.name_prefix}-assets"
unique_name = "${local.name_prefix}-assets-05"
description = "Static assets CDN for ${var.project_name}"
auto_ssl = true
}
# ===================================================================
# SERVICE DOMAINS - Custom Domains
# ===================================================================
# Main website domain
resource "cachefly_service_domain" "web_main" {
service_id = cachefly_service.web_app.id
name = var.base_domain
description = "Main website domain"
validation_mode = "DNS"
}
# www subdomain for web
resource "cachefly_service_domain" "web_www" {
service_id = cachefly_service.web_app.id
name = "www.${var.base_domain}"
description = "WWW subdomain for website"
validation_mode = "DNS"
}
# API subdomain
resource "cachefly_service_domain" "api_subdomain" {
service_id = cachefly_service.api.id
name = "api.${var.base_domain}"
description = "API subdomain"
validation_mode = "DNS"
}
# CDN subdomain for assets
resource "cachefly_service_domain" "assets_cdn" {
service_id = cachefly_service.assets.id
name = "cdn.${var.base_domain}"
description = "CDN subdomain for static assets"
validation_mode = "DNS"
}
# ===================================================================
# DATA SOURCES - Lookup and Verification
# ===================================================================
# Look up services to verify they were created
data "cachefly_service" "web_app_verify" {
id = cachefly_service.web_app.id
}
# Look up domains for the web service
data "cachefly_service_domains" "web_domains" {
service_id = cachefly_service.web_app.id
}
# Look up specific domain
data "cachefly_service_domain" "main_domain_verify" {
service_id = cachefly_service.web_app.id
id = cachefly_service_domain.web_main.id
}
# ===================================================================
# OUTPUTS - Useful Information
# ===================================================================
output "services_summary" {
description = "Summary of all created services"
value = {
web_app = {
id = cachefly_service.web_app.id
name = cachefly_service.web_app.name
unique_name = cachefly_service.web_app.unique_name
status = cachefly_service.web_app.status
ssl_enabled = cachefly_service.web_app.auto_ssl
}
api = {
id = cachefly_service.api.id
name = cachefly_service.api.name
unique_name = cachefly_service.api.unique_name
status = cachefly_service.api.status
ssl_enabled = cachefly_service.api.auto_ssl
}
assets = {
id = cachefly_service.assets.id
name = cachefly_service.assets.name
unique_name = cachefly_service.assets.unique_name
status = cachefly_service.assets.status
ssl_enabled = cachefly_service.assets.auto_ssl
}
}
}
output "domains_summary" {
description = "Summary of all attached domains"
value = {
web_domains = {
main = {
id = cachefly_service_domain.web_main.id
name = cachefly_service_domain.web_main.name
validation_status = cachefly_service_domain.web_main.validation_status
}
www = {
id = cachefly_service_domain.web_www.id
name = cachefly_service_domain.web_www.name
validation_status = cachefly_service_domain.web_www.validation_status
}
}
api_domain = {
id = cachefly_service_domain.api_subdomain.id
name = cachefly_service_domain.api_subdomain.name
validation_status = cachefly_service_domain.api_subdomain.validation_status
}
assets_domain = {
id = cachefly_service_domain.assets_cdn.id
name = cachefly_service_domain.assets_cdn.name
validation_status = cachefly_service_domain.assets_cdn.validation_status
}
}
}
output "cdn_endpoints" {
description = "Your CDN endpoints - use these in your applications"
value = {
website = {
main_domain = var.base_domain
www_domain = "www.${var.base_domain}"
cachefly_url = "https://${cachefly_service.web_app.unique_name}.cachefly.net"
}
api = {
api_domain = "api.${var.base_domain}"
cachefly_url = "https://${cachefly_service.api.unique_name}.cachefly.net"
}
assets = {
cdn_domain = "cdn.${var.base_domain}"
cachefly_url = "https://${cachefly_service.assets.unique_name}.cachefly.net"
}
}
}
output "verification" {
description = "Verification that everything is working correctly"
value = {
all_services_active = alltrue([
cachefly_service.web_app.status == "ACTIVE",
cachefly_service.api.status == "ACTIVE",
cachefly_service.assets.status == "ACTIVE"
])
web_domains_count = length(data.cachefly_service_domains.web_domains.domains)
data_sources_work = data.cachefly_service_domain.main_domain_verify.name == cachefly_service_domain.web_main.name
setup_complete = alltrue([
cachefly_service.web_app.status == "ACTIVE",
cachefly_service.api.status == "ACTIVE",
cachefly_service.assets.status == "ACTIVE",
length(data.cachefly_service_domains.web_domains.domains) >= 2
])
}
}