iann0036 / former2

Generate CloudFormation / Terraform / Troposphere templates from your existing AWS resources.
https://former2.com
MIT License
2.21k stars 267 forks source link

Cloudfront #172

Open Lusitaniae opened 3 years ago

Lusitaniae commented 3 years ago

When I try to import my Cloudfront resources I will get the following error

Properties validation failed for resource CloudFrontDistribution with message: #/DistributionConfig/DefaultCacheBehavior/ForwardedValues: required key [QueryString] not found #/DistributionConfig/DefaultCacheBehavior/ForwardedValues/Cookies: required key [Forward] not found

It seems former2 is generating the code below (I've added some parameters to the generated code)

    CloudFrontDistribution:
        Type: "AWS::CloudFront::Distribution"
        DeletionPolicy: Retain        
        Properties:
            DistributionConfig: 
                Origins: 
                  - 
                    ConnectionAttempts: 3
                    ConnectionTimeout: 10
                    DomainName: !Sub "${S3Bucket}.s3.${AWS::Region}.amazonaws.com"
                    Id: !Sub "${S3Bucket}.s3.${AWS::Region}.amazonaws.com"
                    OriginPath: ""
                    S3OriginConfig: 
                        OriginAccessIdentity: "origin-access-identity/cloudfront/${CF_OAI}"
                OriginGroups: 
                    Quantity: 0
                DefaultCacheBehavior: 
                    AllowedMethods: 
                      - "HEAD"
                      - "GET"
                      - "OPTIONS"
                    CachedMethods: 
                      - "HEAD"
                      - "GET"
                      - "OPTIONS"
                    Compress: true
                    CachePolicyId: "658327ea-f89d-4fab-a63d-7e88639e58f6"
                    OriginRequestPolicyId: "88a5eaf4-2fd4-4709-b370-b4c650ea3fcf"
                    ForwardedValues: 
                        Cookies: {}
                    SmoothStreaming: false
                    TargetOriginId: !Sub "${S3Bucket}.s3.${AWS::Region}.amazonaws.com"
                    ViewerProtocolPolicy: "redirect-to-https"
                Comment: ""
                PriceClass: "PriceClass_All"
                Enabled: true
                ViewerCertificate: 
                    CloudFrontDefaultCertificate: true
                    MinimumProtocolVersion: "TLSv1"
                Restrictions: 
                    GeoRestriction: 
                        RestrictionType: "none"
                HttpVersion: "http2"
                DefaultRootObject: "index.html"
                IPV6Enabled: true

From the documentation at https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-distribution-defaultcachebehavior.html

A DefaultCacheBehavior must include either a CachePolicyId or ForwardedValues. We recommend that you use a CachePolicyId.

So former2 is adding this snippet superfluously and breaking the deployment

                    ForwardedValues: 
                        Cookies: {}
iann0036 commented 3 years ago

Hi @Lusitaniae,

Thanks for raising!

I've pushed a fix for this. Let me know if you have any issues with the fix.