aws-samples / amazon-cloudfront-secure-static-site

Create a secure static website with CloudFront for your registered domain.
Apache License 2.0
389 stars 140 forks source link

Invalid request provided: AWS::CloudFront::OriginAccessControl #66

Closed thomasleplus closed 1 year ago

thomasleplus commented 1 year ago

I merged PR #65 into my private repo but when I run the CloudFormation stack, I get the following error:

Resource handler returned message: "Invalid request provided: AWS::CloudFront::OriginAccessControl" (RequestToken: <REDACTED>, HandlerErrorCode: InvalidRequest)

The error doesn't give much information but a quick web search seems to indicate that InvalidRequest errors are usually caused by syntax errors in the CF template. So I ran cfn-lint and cfn_nag on my template but I get no complaint from either tool.

I've also tried adding a description to the OriginAccessControl in case it's not optional despite what the doc says. That didn't help either. Any suggestion would be greatly appreciated.

This is my YAML in case I am missed something during the merge:

  OriginAccessControl:
    Type: AWS::CloudFront::OriginAccessControl
    Properties:
      OriginAccessControlConfig:
        Name: !Sub 'oac-${AWS::StackName}-${AWS::Region}'
        Description: !Sub 'CloudFront OAC for ${SubDomain}.${DomainName}'
        OriginAccessControlOriginType: s3
        SigningBehavior: always
        SigningProtocol: sigv4
ConnorKirk commented 1 year ago

Thanks for opening an issue. I've recreated it.

I will investigate to find the cause.

ConnorKirk commented 1 year ago

Can you try using a shorter stack name @thomasleplus?

My hypothesis is that this error occurs when the Name property is longer than allowed. I've been able to recreate the error when using a long stack name, and able to deploy the template successfully when using a short stack name (five characters).

I think we encounter this issue because the Name property is set to be !Sub 'oac-${AWS::StackName}-${AWS::Region}'. OriginAccessControlConfig resources must have a unique name across all regions. to ensure this, the Name is derived from both the StackName and Region.

The default stack name (amazon-cloudfront-secure-static-site-templates-main) when using the quick deploy link is too long, triggering this issue.

There isn't an ideal fix. Some options are:

thomasleplus commented 1 year ago

Hi @ConnorKirk,

Shortening the OAC config name did the trick!

I went with using the subdomain and domain name instead of the stack name because it's short enough and unique enough for my use case:

OriginAccessControlConfig:
  Name: !Sub
    - 'oac-${Subdomain}-${DashedDomainName}'
    - DashedDomainName: !Join ['-', !Split ['.', !Ref DomainName]]

Thanks!

thomasleplus commented 1 year ago

As far as I am concerned this issue is resolved. If others have the same issue, I am happy to make a PR with my changes but I am not sure if my solution above would work for everyone (for example what if someone doesn't use a subdomain).

ConnorKirk commented 1 year ago

Thanks @thomasleplus

Glad to hear you have resolved your issue. I'm still considering the best resolution for users of the sample. I'm working on PR to fix the issue. Once that is merged I will close this. I'll keep it open in the mean time in case another user encounters the same problem.

ConnorKirk commented 1 year ago

Fixed in #69

ConnorKirk commented 1 year ago

In the end I used the Stack ID, as this is constant length, but different in each stack. thank you for your help in identifying and resolving the issue.

Here's the relevant snippet in case it is helpful.

 Name: !Sub
          - amzn-secure-static-site-${guid}
          - guid: !Select [2, !Split ['/', !Ref 'AWS::StackId']]