Skip to content

Lambda CloudFront Event pathPattern unsupported  #7151

Closed
mikaelvesavuori/serverless-graphql-api-gcp
#4
@naciriii

Description

@naciriii

Bug Report

When Using CloudFront event to trigger lambda@edge wit specifying the pathPattern property, Serverless adds the PathPattern property to DefaultCacheBehavior, yet the PathPattern property is only supported in CacheBehaviors array under custom CacheBehavior, on deployment Serverless throws an error of unsupported property PathPattern after building the cloudformation

Description

  1. What did you do?
  • Set a Lambda@edge which is triggered by cloudformation event and use pathPattern property as below :
functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: /hello
          method: get  
  ciao:
    handler: handler.ciao
    events:
      - cloudFront:
          eventType: origin-request
          pathPattern: /hello*
          origin:
            OriginPath: /dev
            DomainName:
              Fn::Join:
              - ''
              - - Ref: ApiGatewayRestApi
                - ".execute-api."
                - Ref: AWS::Region
                - ".amazonaws.com"
            
  1. What happened?
  • an Error of unsupported property is thrown :
  Serverless Error ---------------------------------------
 
  ServerlessError: An error occurred: CloudFrontDistribution - Property validation failure: [Encountered unsupported properties in {/DistributionConfig/DefaultCacheBehavior}: [PathPattern]].
  • and the built cloudformation like below , serverless adds PathPattern under DefaultCacheBehavior:
    "CloudFrontDistribution": {
      "Type": "AWS::CloudFront::Distribution",
      "Properties": {
        "DistributionConfig": {
          "Comment": "cftest dev",
          "Enabled": true,
          "DefaultCacheBehavior": {
            "ViewerProtocolPolicy": "allow-all",
            "TargetOriginId": "Ciao/dev",
            "ForwardedValues": {
              "QueryString": false
            },
            "LambdaFunctionAssociations": [
              {
                "EventType": "origin-request",
                "LambdaFunctionARN": {
                  "Ref": "CiaoLambdaVersionbaOwVkkDjWQyngQTIdY7a7UQPP3aBeaAOfS93vvLo"
                }
              }
            ],
            "PathPattern": "/hello*"
          },
          "Origins": [
            {
              "OriginPath": "/dev",
              "DomainName": {
                "Fn::Join": [
                  "",
                  [
                    {
                      "Ref": "ApiGatewayRestApi"
                    },
                    ".execute-api.",
                    {
                      "Ref": "AWS::Region"
                    },
                    ".amazonaws.com"
                  ]
                ]
              },
              "Id": "Ciao/dev"
            }
          ]
        }
      }
    }
  1. What should've happened?
  • Serverless should've handled this case and created a custom CacheBehavior that has PathPattern instead of adding the PathPattern property under DefaultCacheBehavior and the compiled cloudformation shoul've been something similar to this (created manually):
    "CloudFrontDistribution": {
      "Type": "AWS::CloudFront::Distribution",
      "Properties": {
        "DistributionConfig": {
          "Origins": [
            {
              "Id": "Ciao/dev",
              "DomainName": {
                "Fn::Join": [
                  "",
                  [
                    {
                      "Ref": "ApiGatewayRestApi"
                    },
                    ".execute-api.",
                    {
                      "Ref": "AWS::Region"
                    },
                    ".amazonaws.com"
                  ]
                ]
              },
              "CustomOriginConfig": {
                "HTTPPort": "80",
                "HTTPSPort": "443",
                "OriginProtocolPolicy": "https-only",
                "OriginSSLProtocols": [
                  "TLSv1",
                  "TLSv1.1",
                  "TLSv1.2"
                ]
              },
              "OriginPath": "/dev"
            }
          ],
          "Enabled": true,
          "HttpVersion": "http2",
          "Comment": "Serverless dev-testapp",
          "PriceClass": "PriceClass_All",
          "CacheBehaviors": [
            {
              "PathPattern": "/hello*",
              "AllowedMethods": [
                "DELETE",
                "GET",
                "HEAD",
                "OPTIONS",
                "PATCH",
                "POST",
                "PUT"
              ],
              "CachedMethods": [
                "HEAD",
                "GET"
              ],
              "ForwardedValues": {
                "QueryString": true,
                "Headers": [],
                "Cookies": {
                  "Forward": "all"
                }
              },
              "MinTTL": "0",
              "DefaultTTL": "0",
              "TargetOriginId": "Ciao/dev",
              "ViewerProtocolPolicy": "redirect-to-https",
              "Compress": false,
              "LambdaFunctionAssociations": [
                {
                  "EventType": "origin-request",
                  "LambdaFunctionARN": {
                    "Ref": "CiaoLambdaVersionbaOwVkkDjWQyngQTIdY7a7UQPP3aBeaAOfS93vvLo"
                  }
                }
              ]
            }
          ],
          "DefaultCacheBehavior": {
            "AllowedMethods": [
              "DELETE",
              "GET",
              "HEAD",
              "OPTIONS",
              "PATCH",
              "POST",
              "PUT"
            ],
            "CachedMethods": [
              "HEAD",
              "GET"
            ],
            "ForwardedValues": {
              "QueryString": true,
              "Headers": [],
              "Cookies": {
                "Forward": "all"
              }
            },
            "MinTTL": "0",
            "DefaultTTL": "0",
            "TargetOriginId": "Ciao/dev",
            "ViewerProtocolPolicy": "redirect-to-https",
            "Compress": false
          },
          "CustomErrorResponses": []
        }
      }
    }
  1. What's the content of your serverless.yml file?
service: cftest
provider:
  name: aws
  runtime: nodejs10.x
  profile: test2

    - Effect: "Allow"
      Action:
        - "cloudfront:*"
      Resource: "*"
functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: /hello
          method: get  
  ciao:
    handler: handler.ciao
    events:
      - cloudFront:
          eventType: origin-request
          pathPattern: /hello*
          origin:
            OriginPath: /dev
            DomainName:
              Fn::Join:
              - ''
              - - Ref: ApiGatewayRestApi
                - ".execute-api."
                - Ref: AWS::Region
                - ".amazonaws.com"
  1. What's the output you get when you use the SLS_DEBUG=* environment variable (e.g. SLS_DEBUG=* serverless deploy)
..................
Serverless: Operation failed!
Serverless: View the full error output: https://us-east-1.console.aws.amazon.com/cloudformation/home?region=us-east-1#/stack/detail?stackId=arn%3Aaws%3Acloudformation%3Aus-east-1%3A374550413426%3Astack%2Fcftest-dev%2F98713dc0-2bb3-11ea-87a1-0a36a8f29061
 
  Serverless Error ---------------------------------------
 
  ServerlessError: An error occurred: CloudFrontDistribution - Property validation failure: [Encountered unsupported properties in {/DistributionConfig/DefaultCacheBehavior}: [PathPattern]].

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions