skip to Main Content

How can i set cookies in the AWS Cloudfront Module i cant find anything in the offical Documentation from AWS Terraform Module

module "cdn" {
  source = "terraform-aws-modules/cloudfront/aws"

  ordered_cache_behavior = [
    {
      path_pattern           = "/wp-admin/*"
      target_origin_id       = "loadbalancer"
      viewer_protocol_policy = "redirect-to-https"

      allowed_methods = ["GET", "HEAD", "OPTIONS", "PUT", "POST", "PATCH", "DELETE"]
      headers         = ["*"]
      forward_cookies = "all"
      compress        = true
      query_string    = true
    },


    {
      path_pattern           = "/wp-login.php/"
      target_origin_id       = "loadbalancer"
      viewer_protocol_policy = "redirect-to-https"

      allowed_methods = ["GET", "HEAD", "OPTIONS", "PUT", "POST", "PATCH", "DELETE"]
      headers         = ["*"]
      forward_cookies = "all"
      compress        = true
      query_string    = true
    }
  ]

i tryed diffrent approches like set


cookies[*]
forward_cookies = "all"

    forwarded_values {
      query_string = false
      headers      = ["Origin"]

      cookies {
        forward = "all"
      }
    }

When i run it i dont get any error but it set in Cloudfront anything to none. The same happens when i try to set any cookies. Did someone have a Solution to the Problem or should i use the offical ressources.

2

Answers


  1. Looking at the source code, it appears you need to specify cookies_forward = "all", instead of forward_cookies. Like this:

      ordered_cache_behavior = [
        {
          path_pattern           = "/wp-admin/*"
          target_origin_id       = "loadbalancer"
          viewer_protocol_policy = "redirect-to-https"
    
          allowed_methods = ["GET", "HEAD", "OPTIONS", "PUT", "POST", "PATCH", "DELETE"]
          headers         = ["*"]
          cookies_forward = "all"
          compress        = true
          query_string    = true
        },
    
    Login or Signup to reply.
  2. Rather than using a cookies_forward = "all" attribute, you could use a Cache Policy specifying not to cache cookies.

    To achieve that, you could either use one of the default cache policies provided by AWS like this:

    data "aws_cloudfront_cache_policy" "caching_disabled" {
      name = "Managed-CachingDisabled"
    }
    

    And then set it within one of your ordered_cache_behavior blocks like this:

      ordered_cache_behavior = [
        {
          ...
          cache_policy_id = data.aws_cloudfront_cache_policy.caching_disabled.id
          ...
        }
    

    Or you can create your own, like this:

    resource "aws_cloudfront_cache_policy" "forward_all_cookies" {
      name        = "Forward-All-Cookies"
      comment     = "Policy with cookie caching disabled"
      default_ttl = 0
      max_ttl     = 0
      min_ttl     = 0
      parameters_in_cache_key_and_forwarded_to_origin {
        cookies_config {
          cookie_behavior = "none"
        }
        headers_config {
          header_behavior = "none"
        }
        query_strings_config {
          query_string_behavior = "none"
        }
      }
    }
    

    And then set it within one of your ordered_cache_behavior blocks like this:

      ordered_cache_behavior = [
        {
          ...
          cache_policy_id = aws_cloudfront_cache_policy.forward_all_cookies
          ...
        }
    

    Further reference: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-managed-cache-policies.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search