#!/bin/bash # Skapa separata CloudFront-distributioner för varje ccTLD # Detta är den rekommenderade approach för många domäner ORIGIN_DOMAIN="quixzoom-landing-prod.s3.eu-north-1.amazonaws.com" # Marknader med zone IDs declare -A MARKETS=( ["se"]="Z10259721J71K43ZJPPU1" ["de"]="Z02042236EVWD34RIL8" ["co.uk"]="Z06391212CQRAVQ1FMNHV" ["fr"]="Z0590787VZDW5D5XHQAV" ["nl"]="Z08393705I60UK5C6T3D" ["it"]="Z01178703OEP5JBWVMCVN" ["es"]="Z03537332N1170YZA6Y96" ["fi"]="Z01156752B3NC8OLB0VQ3" ["dk"]="Z03418436Y57RW7ET3I2" ["at"]="Z06127861GC9YECVG8NJ5" ["be"]="Z02403741O3VID61R4CWK" ["ch"]="Z05948653CJP2I13KROV9" ["no"]="Z0115626AXXUA0YDZ41I" ["ie"]="Z01175865XIP34GSASTH" ["pl"]="Z061274130QWJWIMGJGK6" ) # Hämta certifikat ARNs CERTS=$(aws acm list-certificates --query 'CertificateSummaryList[*].[DomainName,CertificateArn]' --output text) echo "Skapar CloudFront-distributioner..." echo "" for tld in "${!MARKETS[@]}"; do zone_id="${MARKETS[$tld]}" domain="quixzoom.$tld" # Hitta certifikat för denna domän cert_arn=$(echo "$CERTS" | grep "^$domain" | awk '{print $2}') if [ -z "$cert_arn" ]; then echo "⚠️ Inget certifikat hittat för $domain, hoppar över" continue fi echo "=== $domain ===" echo " Cert: $cert_arn" # Kontrollera om distribution redan finns existing=$(aws cloudfront list-distributions --query "DistributionList.Items[?Aliases.Items[?contains(@, '$domain')]].Id" --output text 2>/dev/null) if [ -n "$existing" ] && [ "$existing" != "None" ]; then echo " Distribution finns redan: $existing" continue fi # Skapa distribution cat > /tmp/cf-distribution.json << EOF { "CallerReference": "$(date +%s)-$domain", "Aliases": { "Quantity": 2, "Items": ["$domain", "www.$domain"] }, "DefaultRootObject": "index.html", "Origins": { "Quantity": 1, "Items": [{ "Id": "S3-$domain", "DomainName": "$ORIGIN_DOMAIN", "S3OriginConfig": { "OriginAccessIdentity": "" }, "OriginPath": "/markets/${tld//./}" }] }, "DefaultCacheBehavior": { "TargetOriginId": "S3-$domain", "ViewerProtocolPolicy": "redirect-to-https", "AllowedMethods": { "Quantity": 2, "Items": ["GET", "HEAD"], "CachedMethods": { "Quantity": 2, "Items": ["GET", "HEAD"] } }, "Compress": true, "ForwardedValues": { "QueryString": false, "Cookies": {"Forward": "none"} }, "MinTTL": 0, "DefaultTTL": 86400, "MaxTTL": 31536000 }, "Comment": "quiXzoom $domain landing pages", "PriceClass": "PriceClass_100", "Enabled": true, "ViewerCertificate": { "ACMCertificateArn": "$cert_arn", "SSLSupportMethod": "sni-only", "MinimumProtocolVersion": "TLSv1.2_2021" } } EOF # Skapa distribution result=$(aws cloudfront create-distribution --distribution-config file:///tmp/cf-distribution.json 2>&1) if [ $? -eq 0 ]; then dist_id=$(echo "$result" | grep -o '"Id": "[^"]*"' | head -1 | cut -d'"' -f4) dist_domain=$(echo "$result" | grep -o '"DomainName": "[^"]*"' | head -1 | cut -d'"' -f4) echo " ✅ Skapad: $dist_id ($dist_domain)" # Uppdatera Route53 ALIAS cat > /tmp/route53-change.json << R53EOF { "Changes": [{ "Action": "UPSERT", "ResourceRecordSet": { "Name": "$domain", "Type": "A", "AliasTarget": { "HostedZoneId": "Z2FDTNDATAQYW2", "DNSName": "$dist_domain", "EvaluateTargetHealth": false } } }] } R53EOF aws route53 change-resource-record-sets --hosted-zone-id "$zone_id" --change-batch file:///tmp/route53-change.json > /dev/null 2>&1 echo " ✅ Route53 uppdaterad" else echo " ❌ Fel: $(echo "$result" | head -1)" fi echo "" done echo "✅ Klart!"