stack/infra/main/website.tf

83 lines
2.1 KiB
Terraform
Raw Permalink Normal View History

2023-05-05 03:59:08 +00:00
locals {
content_types = {
css = "text/css"
html = "text/html"
js = "application/javascript"
json = "application/json"
txt = "text/plain"
png = "image/png"
svg = "image/svg+xml"
}
}
2023-03-31 08:38:53 +00:00
resource "digitalocean_spaces_bucket" "website" {
2023-04-14 03:22:35 +00:00
name = "website-${random_id.suffix.hex}"
2023-03-31 08:38:53 +00:00
region = var.region
2023-05-05 03:59:08 +00:00
acl = "public-read"
2023-03-31 08:38:53 +00:00
}
resource "digitalocean_spaces_bucket_object" "website" {
2023-05-05 03:31:53 +00:00
for_each = fileset("${var.out_dir}/website", "**")
2023-04-14 03:22:35 +00:00
region = var.region
2023-03-31 08:38:53 +00:00
bucket = digitalocean_spaces_bucket.website.name
source = "${var.out_dir}/website/${each.value}"
key = each.value
etag = filemd5("${var.out_dir}/website/${each.value}")
2023-05-05 03:59:08 +00:00
content_type = lookup(local.content_types, element(split(".", each.value), length(split(".", each.value)) - 1), "text/plain")
2023-04-14 03:22:35 +00:00
acl = "public-read"
2023-05-05 03:59:08 +00:00
content_encoding = "utf-8"
2023-04-14 03:22:35 +00:00
}
resource "tls_private_key" "website" {
algorithm = "RSA"
2023-03-31 08:38:53 +00:00
}
2023-04-14 03:22:35 +00:00
resource "digitalocean_certificate" "website" {
name = "le-terraform-example"
type = "lets_encrypt"
domains = [
digitalocean_domain.default.name,
format("%s%s", "www.", digitalocean_domain.default.name)
]
}
resource "digitalocean_cdn" "website" {
origin = digitalocean_spaces_bucket.website.bucket_domain_name
certificate_name = digitalocean_certificate.website.name
depends_on = [
digitalocean_spaces_bucket.website
]
}
# ## Handle record for CDN redirect
# resource "digitalocean_record" "cdn" {
# domain = digitalocean_domain.default.name
# type = "CNAME"
# name = digitalocean_cdn.distrust_co.origin
# value = "${digitalocean_domain.default.name}."
# depends_on = [
# digitalocean_cdn.distrust_co
# ]
# }
# # Output the endpoint for the CDN resource
# output "fqdn" {
# value = digitalocean_cdn.distrust_co.endpoint
# }
# #
# output "cdn_origin" {
# value = digitalocean_cdn.distrust_co.origin
# }
# # Handle record for distrust.co
# resource "digitalocean_record" "distrust_co-cdn" {
# domain = digitalocean_domain.default.name
# type = "A"
# name = "@"
# value = "143.198.235.76"
# depends_on = [
# digitalocean_cdn.distrust_co
# ]
# }