Skip to content
The Chat Archive
qwq-32b· Alibaba (Qwen) LMArena Human Preference 140K

Napisz mi moduł terraform który pobiera ruch z alb do cloudfront

  1. User

    Napisz mi moduł terraform który pobiera ruch z alb do cloudfront

  2. Assistant

    Oto przykładowy moduł Terraform, który konfiguruje CloudFront, aby służył za CDN dla Application Load Balancer (ALB). Moduł będzie:

    1. Tworzyć dystrybucję CloudFront
    2. Konfigurować ALB jako origin
    3. Otwierać porty w grupie bezpieczeństwa ALB dla CloudFront

    Struktura modułu

    cloudfront-alb-module/
    ├── main.tf
    ├── variables.tf
    ├── outputs.tf
    └── terraform.tfvars.example
    

    variables.tf

    variable "alb_dns_name" {
      description = "DNS name of the Application Load Balancer"
      type        = string
    }
    
    variable "certificate_arn" {
      description = "ARN of the ACM certificate for CloudFront"
      type        = string
    }
    
    variable "security_group_id" {
      description = "Security group ID for ALB to allow CloudFront traffic"
      type        = string
    }
    
    variable "origin_protocol_policy" {
      description = "HTTP or HTTPS protocol between CloudFront and ALB"
      type        = string
      default     = "https"
    }
    
    variable "cache_behavior_paths" {
      description = "Cache behavior paths (default: all traffic)"
      type        = list(string)
      default     = ["/*"]
    }
    

    main.tf

    # Pobierz listę publicznych adresów IP CloudFront
    data "aws_cloudfront_ip_ranges" "cf_ranges" {}
    
    # Konfiguracja CloudFront Distribution
    resource "aws_cloudfront_distribution" "alb_distribution" {
      comment          = "CloudFront for ALB"
      default_root_object = "index.html"
    
      enabled             = true
      price_class         = "PriceClass_100"
      http_version        = "http2"
    
      # Konfiguracja origin (ALB)
      origin {
        domain_name = var.alb_dns_name
        origin_id   = "alb-origin"
    
        custom_origin_config {
          http_port              = 443
          https_port             = 443
          origin_protocol_policy = var.origin_protocol_policy
        }
      }
    
      # Konfiguracja cache behavior
      default_cache_behavior {
        target_origin_id = "alb-origin"
    
        allowed_methods = ["GET", "HEAD", "OPTIONS", "PUT", "POST", "PATCH", "DELETE"]
        cached_methods  = ["GET", "HEAD"]
    
        viewer_protocol_policy = "redirect-to-https"
        compress               = true
    
        # Konfiguracja cache policy (opcjonalnie)
        # cache_policy_id = "658327ea-f89d-4fab-a63d-3d5fexample"
      }
    
      # Dodatkowe cache behaviors (jeśli potrzebne)
      # cache_behavior {
      #   path_pattern = "/api/*"
      #   target_origin_id = "alb-origin"
      # }
    
      # Konfiguracja SSL
      viewer_certificate {
        acm_certificate_arn = var.certificate_arn
        ssl_support_method  = "sni-only"
      }
    }
    
    # Otwórz porty w grupie bezpieczeństwa ALB dla CloudFront
    resource "aws_security_group_rule" "cloudfront_access" {
      count = length(data.aws_cloudfront_ip_ranges.cf_ranges.ipv4_prefixes)
    
      type              = "ingress"
      from_port         = 443
      to_port           = 443
      protocol          = "tcp"
      cidr_blocks       = [data.aws_cloudfront_ip_ranges.cf_ranges.ipv4_prefixes[count.index]]
      security_group_id = var.security_group_id
    }
    
    # Opcjonalnie: Dodaj IPv6 (jeśli potrzebne)
    resource "aws_security_group_rule" "cloudfront_access_ipv6" {
      count = length(data.aws_cloudfront_ip_ranges.cf_ranges.ipv6_prefixes)
    
      type              = "ingress"
      from_port         = 443
      to_port           = 443
      protocol          = "tcp"
      ipv6_cidr_blocks  = [data.aws_cloudfront_ip_ranges.cf_ranges.ipv6_prefixes[count.index]]
      security_group_id = var.security_group_id
    }
    

    outputs.tf

    output "cloudfront_domain" {
      description = "CloudFront domain name (e.g., d123.cloudfront.net)"
      value       = aws_cloudfront_distribution.alb_distribution.domain_name
    }
    
    output "cloudfront_id" {
      description = "CloudFront distribution ID"
      value       = aws_cloudfront_distribution.alb_distribution.id
    }
    

    Przykładowe użycie (terraform.tfvars)

    alb_dns_name = "my-alb-1234567890.us-east-1.elb.amazonaws.com"
    certificate_arn = "arn:aws:acm:us-east-1:123456789012:certificate/abcd-1234-efgh"
    security_group_id = "sg-0123456789abcdef0"
    

    Kroki wdrożenia

    1. Zainicjuj moduł:

      terraform init
      
    2. Planuj zmiany:

      terraform plan -var-file="terraform.tfvars"
      
    3. Zastosuj zmiany:

      terraform apply -var-file="terraform.tfvars"
      

    Ważne uwagi

    1. Certifikat SSL:

      • Musi być w regionie us-east-1 i być weryfikowany dla domeny CloudFront (np. *.cloudfront.net).
      • Możesz użyć AWS Certificate Manager.
    2. Zabezpieczenia ALB:

      • Upewnij się, że ALB ma włączony protokół HTTPS na porcie 443.
      • Możesz dodać dodatkowe reguły zabezpieczeń, jeśli potrzebujesz.
    3. Domena CNAME:

      • Po wdrożeniu, skonfiguruj CNAME w Twoim DNS (np. www.example.com wskazujący na d123.cloudfront.net).
    4. Optymalizacje:

      • Dla lepszej wydajności, dostosuj polityki cache w cache_behavior.
      • Możesz dodać Origin Request Policies do filtrowania nagłówków.

    Przykładowa odpowiedź

    {
      "cloudfront_domain": "d1234567890abcdef.cloudfront.net",
      "cloudfront_id": "E1234567890ABCDEF"
    }
    

    Po wdrożeniu, ruch będzie przekierowywany z CloudFront do ALB. Upewnij się, że domena CNAME wskazuje na CloudFront i sprawdź działanie w CloudFront Dashboard.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.

Identical first prompt