cloudformation-coverage-roadmap icon indicating copy to clipboard operation
cloudformation-coverage-roadmap copied to clipboard

AWS::CloudFront::CachePolicy and AWS::CloudFront::OriginRequestPolicy

Open joaopenteado opened this issue 6 years ago • 47 comments

1. Title

AWS::CloudFront::CachePolicy AWS::CloudFront::OriginRequestPolicy

2. Scope of request

Last week, CloudFront introduced reusable cache policies and origin request policies and deprecated the previous way of specifying these behaviors through the distribution configuration. I imagine the CloudFormation team is already aware of this change since the corresponding documentation in in CloudFormation is already up to date.

3. Expected behavior

The required parameters should probably stick with what the CreateCachePolicy and CreateOriginRequestPolicy API calls require, I think there's not much room for discussion regarding this.

Both of these should somehow return their ID to use along with the AWS::CloudFront::Distribution resource, otherwise there would be no meaning in including these resources in the first place.

4. Suggest specific test cases

When creating a new distribution with AWS::CloudFront::Distribution you would probably want to specify its cache policy and origin request policy along with it, as most have done up until now with the legacy way of specifying these parameters.

It's not a very good practice to hard-code the IDs of the cache policy and origin request policy, especially since if you use the same stack for multi-account deployments you would want to avoid both hard-coded values and parameters altogether, and the only option you would be left with would be to create an AWS::AccountId-based mapping with each account's cache policy/origin request policy ID, which as I'm sure you're aware, can lead to problems if your AccountID starts with a zero and you have to package your template before deploying it (awscli team I'm looking at you).

5. Helpful Links to speed up research and evaluation

Feature announcement

Relevant CloudFront developer guide documentation

Relevant CloudFront API documentation for cache policies (CreateCachePolicy, UpdateCachePolicy and DeleteCachePolicy)

Relevant CloudFront API documentation for origin request policies (CreateOriginRequestPolicy, UpdateOriginRequestPolicy and DeleteOriginRequestPolicy)

CloudFormation documentation page for AWS::CloudFront::Distribution DefaultCacheBehavior, which is already up to date regarding these changes.

6. Category

Networking & Content Delivery

joaopenteado avatar Jul 27 '20 13:07 joaopenteado

AWS::CloudFront https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/ReleaseHistory.html

PatMyron avatar Sep 10 '20 17:09 PatMyron

I know this isn't a support channel (and I have a ticket open anyway) but I tried this today in eu-west-1 and us-east-1 and both times received an Invalid request provided message. Should this be working?

bootc avatar Sep 10 '20 18:09 bootc

Ditto ap-southeast-2.

2020-09-14T03:20:42.598000+00:00 CloudFrontCachePolicy CREATE_FAILED

    Invalid request provided: AWS::CloudFront::CachePolicy
    {"CachePolicyConfig":<snip>}

timdawborn avatar Sep 14 '20 03:09 timdawborn

I Just ran into the same issue (eu-central-1).

And while you're on it, please implement Brotli on the the AWS::CloudFront::CachePolicy ParametersInCacheKeyAndForwardedToOrigin type (EnableAcceptEncodingBrotli).

henning-krause avatar Sep 21 '20 08:09 henning-krause

Is there a workaround for this? cachepolicy is not working in ap-southeast-1

thanakijwanavit avatar Sep 22 '20 02:09 thanakijwanavit

Yep would like also to have EnableAcceptEncodingBrotli working 🙏

yvele avatar Sep 23 '20 14:09 yvele

For those experiencing the Invalid request provided error, one thing to try is to check the Name of the CachePolicy; remove any spaces or special characters, and keep it alphanumeric. That unblocked me.

njlynch avatar Oct 06 '20 17:10 njlynch

This seems to be fully supported and working now 👍

Lanchez avatar Oct 07 '20 10:10 Lanchez

I'm getting failure with the following policy on us-east-1:

  DefaultCachePolicy:
    Type: 'AWS::CloudFront::CachePolicy'
    Properties:
      CachePolicyConfig:
        DefaultTTL: 86400
        MaxTTL: 31536000
        MinTTL: 1
        Name: 'web-default-cache-policy'
        ParametersInCacheKeyAndForwardedToOrigin:
          CookiesConfig:
            CookieBehavior: 'none'
          EnableAcceptEncodingBrotli: true
          EnableAcceptEncodingGzip: true
          HeadersConfig:
            HeaderBehavior: 'none'
          QueryStringsConfig:
            QueryStringBehavior: 'none'
  AppCloudFrontDistribution:
    Type: 'AWS::CloudFront::Distribution'
    Properties:
      DistributionConfig:
        DefaultCacheBehavior:
          CachePolicyId: !Ref DefaultCachePolicy
          Compress: true

Does that look right? Is it a bug w/aws? The error I get during change_set_execute is: Internal error reported from downstream service during operation

image

erichaus avatar Oct 07 '20 19:10 erichaus

I had the same error. Turned out that minTtl was larger than maxTtl, which caused the error. Also, defaultTtl must be larger than minTtl.

spg avatar Oct 13 '20 18:10 spg

I was getting Internal error reported from downstream service during operation 'AWS::CloudFront::CachePolicy' and solved it by making sure its Name was unique. If you have multiple distributions they each need a unique cache policy name or reference to a shared resource.

gpoitch avatar Oct 13 '20 19:10 gpoitch

@burritoIand Make sure you dont already have a policy with that name, take a look in CloudFront. I had encountered a similar error and I figure out I already had a policy with same name.

BogdanDarius avatar Oct 14 '20 14:10 BogdanDarius

@BogdanDarius thanks for the suggestion, I just checked and it only has the default policies. I also tried adding the stackname to the name param per @gpoitch but no dice!

image

erichaus avatar Oct 14 '20 17:10 erichaus

@burritoIand can you share your yaml file maybe?

BogdanDarius avatar Oct 14 '20 17:10 BogdanDarius

hey @BogdanDarius, will paste again:

  DefaultCachePolicy:
    Type: 'AWS::CloudFront::CachePolicy'
    Properties:
      CachePolicyConfig:
        DefaultTTL: 86400
        MaxTTL: 31536000
        MinTTL: 1
        Name: !Sub 'CachePolicy-${AWS::StackName}'
        ParametersInCacheKeyAndForwardedToOrigin:
          CookiesConfig:
            CookieBehavior: 'none'
          EnableAcceptEncodingBrotli: true
          EnableAcceptEncodingGzip: true
          HeadersConfig:
            HeaderBehavior: 'none'
          QueryStringsConfig:
            QueryStringBehavior: 'none'

When my build is running it successfully creates the changeset, but then fails during "CHANGE_SET_EXECUTE" with above error. I've been sort of background tinkering with this but I suppose I could try to create the policy using the cli and see if I get any more information on the error? Not sure how to get more info than "error reported downstream"

erichaus avatar Oct 14 '20 18:10 erichaus

It'd be great if Name could be omitted to autogenerate a name.

jdub avatar Oct 20 '20 09:10 jdub

Would love to find out if it there is indeed a bug preventing creating the cache policy with cloudformation...

erichaus avatar Oct 22 '20 17:10 erichaus

Hit another issue with OriginRequestPolicy. HeaderBehavior set to allViewerAndWhitelistCloudFront fails with "Invalid request provided". Other values seem to work.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-originrequestpolicy-headersconfig.html#cfn-cloudfront-originrequestpolicy-headersconfig-headerbehavior

gpoitch avatar Oct 26 '20 19:10 gpoitch

I also hit the Internal error reported from downstream service during operation 'AWS::CloudFront::OriginRequestPolicy' error.

Turns our I did not have the necessary cloudfront:CreateOriginRequestPolicy etc IAM rules in place.

elgordino avatar Oct 28 '20 21:10 elgordino

@elgordino that was it! thank you

erichaus avatar Oct 29 '20 06:10 erichaus