aws cloudformation CachePolicy generic error

Viewed 2495

i'm trying to create a cachePolicy that forward all ( cookies, querystrings and headers ) and acctualy doesn't cache annything at all:

    Type: AWS::CloudFront::CachePolicy
    Properties:
      CachePolicyConfig:
        Comment: Cache Policy
        DefaultTTL: 0
        MaxTTL: 0
        MinTTL: 0
        Name: !Sub ${AWS::StackName}-cache-policy
        ParametersInCacheKeyAndForwardedToOrigin:
          CookiesConfig:
            CookieBehavior: all
          EnableAcceptEncodingBrotli: true
          EnableAcceptEncodingGzip: true
          HeadersConfig:
            HeaderBehavior: whitelist
            Headers: 
              - "*"
          QueryStringsConfig:
            QueryStringBehavior: all

and my OriginRequestPolicy:

OriginRequestPolicy:
    Type: AWS::CloudFront::OriginRequestPolicy
    Properties:
      OriginRequestPolicyConfig:
        Name: !Sub ${AWS::StackName}-origin-request
        CookiesConfig:
          CookieBehavior: all
        HeadersConfig:
          HeaderBehavior: allViewer
        QueryStringsConfig:
          QueryStringBehavior: all

but whem i try to upload the stack, i get a generic error:

The following resource(s) failed to create: [OriginRequestPolicy, CachePolicy]. Rollback requested by user. OriginRequestPolicy CREATE_FAILED   Resource creation cancelled CachePolicy CREATE_FAILED   Invalid request provided: AWS::CloudFront::CachePolicy

what am i missing?

obs: I can't only create an AWS::CloudFront::OriginRequestPolicy cause it's seems that i can only have a OriginRequestPolicy if alredy exists a cacheRequestPolicy first..

2 Answers

I don't know why the "manual way" doesn't work, but i found from the documentation 2 pre-made policies that satisfy my needs:

Name: Managed-CachingDisabled
ID: 4135ea2d-6df8-44a3-9df3-4b5a84be39ad
This policy disables caching. This policy is useful for dynamic content and for requests that are not cacheable.

and

Name: Managed-AllViewer
ID: 216adef6-5c7f-47e4-b989-5492eafa07d3
This policy includes all values (query strings, headers, and cookies) in the viewer request.

doc reference:

https://docs.amazonaws.cn/en_us/AmazonCloudFront/latest/DeveloperGuide/using-managed-cache-policies.html

https://docs.amazonaws.cn/en_us/AmazonCloudFront/latest/DeveloperGuide/using-managed-origin-request-policies.html

This error seems to be caused by the name property. For me, having a '.' in the name produces the error.

Nothing is stated regarding this in the documentation unfortunately: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-cachepolicy-cachepolicyconfig.html#cfn-cloudfront-cachepolicy-cachepolicyconfig-name

This works:

AWSTemplateFormatVersion: 2010-09-09

Resources:
  CachePolicy:
    Type: AWS::CloudFront::CachePolicy
    Properties:
      CachePolicyConfig:
        DefaultTTL: 1
        MaxTTL: 1
        MinTTL: 1
        Name: test
        ParametersInCacheKeyAndForwardedToOrigin:
          CookiesConfig:
            CookieBehavior: all
          EnableAcceptEncodingBrotli: false
          EnableAcceptEncodingGzip: false
          HeadersConfig:
            HeaderBehavior: none
          QueryStringsConfig:
            QueryStringBehavior: all

Hyphen in the name works:

AWSTemplateFormatVersion: 2010-09-09

Resources:
  CachePolicy:
    Type: AWS::CloudFront::CachePolicy
    Properties:
      CachePolicyConfig:
        DefaultTTL: 1
        MaxTTL: 1
        MinTTL: 1
        Name: test-id
        ParametersInCacheKeyAndForwardedToOrigin:
          CookiesConfig:
            CookieBehavior: all
          EnableAcceptEncodingBrotli: false
          EnableAcceptEncodingGzip: false
          HeadersConfig:
            HeaderBehavior: none
          QueryStringsConfig:
            QueryStringBehavior: all

Does not work:

AWSTemplateFormatVersion: 2010-09-09

Resources:
  CachePolicy:
    Type: AWS::CloudFront::CachePolicy
    Properties:
      CachePolicyConfig:
        DefaultTTL: 1
        MaxTTL: 1
        MinTTL: 1
        Name: test-id.test
        ParametersInCacheKeyAndForwardedToOrigin:
          CookiesConfig:
            CookieBehavior: all
          EnableAcceptEncodingBrotli: false
          EnableAcceptEncodingGzip: false
          HeadersConfig:
            HeaderBehavior: none
          QueryStringsConfig:
            QueryStringBehavior: all
Related