Search code examples
cookiesamazon-cloudfronttroposphere

How do I set cookies for CloudFront distribution using troposphere?


Using troposhphere, I am trying to create CloudFront distribution.

CacheBehaviors = [
                CacheBehavior(
                    TargetOriginId = Join("", ["cloudfront-", Ref("ParamOriginName")]),
                    PathPattern = '/en/Login/*',
                    AllowedMethods = ["GET", "HEAD", "OPTIONS", "PUT", "POST","PATCH"],
                    ForwardedValues = ForwardedValues(
                        QueryString = True,
                        Headers = ["user-agent",
                                "Host",
                                "CloudFront-Forwarded-Proto", 
                                "CloudFront-Is-Desktop-Viewer", 
                                "CloudFront-Is-Mobile-Viewer",
                                "CloudFront-Is-SmartTV-Viewer",
                                "CloudFront-Is-Tablet-Viewer",
                                "CloudFront-Viewer-Country",
                                "Origin",
                                "Referer"]
                        ),
                    MaxTTL = 86400,
                    MinTTL = 14400,
                    DefaultTTL = 43200,
                    ViewerProtocolPolicy = "redirect-to-https",
                    Compress = True,
                ),

This seems to build the json template fine. But I also need to forward cookies. After the line QueryString I inserted Cookies = "All" but this failed the build.

The error message:
TypeError: <class 'troposphere.cloudfront.ForwardedValues'>: None.Cookies is <class 'str'>, expected <class 'troposphere.cloudfront.Cookies'>

What do I need to add so that it does not fail the build and forwards cookies


Solution

  • I worked it out after reading this document

    After the line QueryString = True, I added

    Cookies = Cookies(Forward = "all"), and it now builds without errors and adds the correct cookies value to the json template