# Static hosting for https://threadbare.substandard.blog: the longread WASM SPA. # # scripts/deploy-threadbare.sh builds the SPA, uploads each build as an immutable # tree under s3://threadbare.substandard.blog/releases//, and writes the # sha to terraform.tfvars.json. The apply here only flips CloudFront's # origin_path to that tree. Roll back by setting threadbare_release_sha to a # previously uploaded sha and re-applying. # # The Route53 zone is defined in main.tf; this file only adds the subdomain. locals { threadbare_domain = "threadbare.substandard.blog" # The SPA talks to public.api.bsky.app, plc.directory and arbitrary PDS # hosts, so connect-src has to allow any HTTPS origin. WebAssembly needs # wasm-unsafe-eval; no inline scripts or styles are emitted by the build. threadbare_content_security_policy = join(" ", [ "default-src 'self';", "script-src 'self' 'wasm-unsafe-eval';", "style-src 'self';", "img-src 'self' data: https:;", "connect-src 'self' https:;", "font-src 'self';", "object-src 'none';", "base-uri 'none';", "form-action 'none';", "frame-ancestors 'none';", "upgrade-insecure-requests", ]) } variable "threadbare_release_sha" { description = "Git sha of the releases/ tree in the threadbare bucket; written to terraform.tfvars.json by scripts/deploy-threadbare.sh" type = string default = "000000000000" validation { condition = can(regex("^[0-9a-f]{12}(-dirty)?$", var.threadbare_release_sha)) error_message = "Run scripts/deploy-threadbare.sh, which writes the deployed commit's short sha to terraform.tfvars.json." } } resource "aws_acm_certificate" "threadbare" { domain_name = local.threadbare_domain validation_method = "DNS" lifecycle { create_before_destroy = true } } resource "aws_route53_record" "threadbare_cert_validation" { for_each = { for option in aws_acm_certificate.threadbare.domain_validation_options : option.domain_name => { name = option.resource_record_name type = option.resource_record_type record = option.resource_record_value } } zone_id = aws_route53_zone.substandard.zone_id name = each.value.name type = each.value.type records = [each.value.record] ttl = 300 } resource "aws_acm_certificate_validation" "threadbare" { certificate_arn = aws_acm_certificate.threadbare.arn validation_record_fqdns = [for record in aws_route53_record.threadbare_cert_validation : record.fqdn] } resource "aws_s3_bucket" "threadbare" { bucket = local.threadbare_domain } resource "aws_s3_bucket_public_access_block" "threadbare" { bucket = aws_s3_bucket.threadbare.id block_public_acls = true block_public_policy = true ignore_public_acls = true restrict_public_buckets = true } resource "aws_cloudfront_origin_access_control" "threadbare" { name = "threadbare" origin_access_control_origin_type = "s3" signing_behavior = "always" signing_protocol = "sigv4" } resource "aws_cloudfront_response_headers_policy" "threadbare_security" { name = "threadbare-security-headers" comment = "HSTS, CSP, nosniff, referrer and frame policy for threadbare.substandard.blog" security_headers_config { strict_transport_security { access_control_max_age_sec = 63072000 include_subdomains = true preload = false override = true } content_type_options { override = true } frame_options { frame_option = "DENY" override = true } referrer_policy { referrer_policy = "strict-origin-when-cross-origin" override = true } content_security_policy { content_security_policy = local.threadbare_content_security_policy override = true } } custom_headers_config { items { header = "Permissions-Policy" value = "camera=(), microphone=(), geolocation=(), payment=(), usb=(), interest-cohort=()" override = true } } } resource "aws_cloudfront_distribution" "threadbare" { enabled = true aliases = [local.threadbare_domain] price_class = "PriceClass_100" comment = "threadbare: longread WASM SPA" default_root_object = "index.html" origin { domain_name = aws_s3_bucket.threadbare.bucket_regional_domain_name origin_id = "s3" origin_path = "/releases/${var.threadbare_release_sha}" origin_access_control_id = aws_cloudfront_origin_access_control.threadbare.id } default_cache_behavior { target_origin_id = "s3" viewer_protocol_policy = "redirect-to-https" allowed_methods = ["GET", "HEAD"] cached_methods = ["GET", "HEAD"] # AWS managed CachingOptimized policy cache_policy_id = "658327ea-f89d-4fab-a63d-7e88639e58f6" response_headers_policy_id = aws_cloudfront_response_headers_policy.threadbare_security.id } # S3 answers a missing key with 403 behind the origin access control. Hash # routing keeps every real view under /, but direct links to a missing asset # should still land on the app rather than raw XML. custom_error_response { error_code = 403 response_code = 200 response_page_path = "/index.html" error_caching_min_ttl = 0 } custom_error_response { error_code = 404 response_code = 200 response_page_path = "/index.html" error_caching_min_ttl = 0 } restrictions { geo_restriction { restriction_type = "none" } } viewer_certificate { acm_certificate_arn = aws_acm_certificate_validation.threadbare.certificate_arn ssl_support_method = "sni-only" minimum_protocol_version = "TLSv1.2_2021" } } resource "aws_s3_bucket_policy" "threadbare_cloudfront_read" { bucket = aws_s3_bucket.threadbare.id policy = jsonencode({ Version = "2012-10-17" Statement = [ { Effect = "Allow" Principal = { Service = "cloudfront.amazonaws.com" } Action = "s3:GetObject" Resource = "${aws_s3_bucket.threadbare.arn}/*" Condition = { StringEquals = { "AWS:SourceArn" = aws_cloudfront_distribution.threadbare.arn } } } ] }) } resource "aws_route53_record" "threadbare_a" { zone_id = aws_route53_zone.substandard.zone_id name = local.threadbare_domain type = "A" alias { name = aws_cloudfront_distribution.threadbare.domain_name zone_id = aws_cloudfront_distribution.threadbare.hosted_zone_id evaluate_target_health = false } } resource "aws_route53_record" "threadbare_aaaa" { zone_id = aws_route53_zone.substandard.zone_id name = local.threadbare_domain type = "AAAA" alias { name = aws_cloudfront_distribution.threadbare.domain_name zone_id = aws_cloudfront_distribution.threadbare.hosted_zone_id evaluate_target_health = false } }