silvermine / serverless-plugin-cloudfront-lambda-edge

Adds Lambda@Edge support to Serverless
MIT License
296 stars 41 forks source link

Lambda not Associated with Cloudfront #65

Closed iassainov closed 3 years ago

iassainov commented 3 years ago

Upon the sls deploy --stage dev, my lambda function is not edge and does not have a trigger as Cloudfront.

I checked the Cloudfront distribution behavior, and the Lambda ARN is there with the correct type and version.

It seems like the function is not "Edge" because the Cloudfront trigger is not there in the Lambda console.

Screen Shot 2021-03-31 at 2 48 16 PM Screen Shot 2021-03-31 at 2 48 37 PM

serverless.yml:

service: shipa-serverless-lambda-edge

frameworkVersion: '2'

provider:
  name: aws
  runtime: nodejs12.x
  stage: ${opt:stage, 'dev'} # opt.stage is a CLI variable, dev fallback
  region: us-east-1 # edge must be in Virginia
  lambdaHashingVersion: 20201221

plugins:
  - '@silvermine/serverless-plugin-cloudfront-lambda-edge'

custom:
  redirects:
    dev:
      originId: custom/${self:custom.redirects.dev.originDomain}
      originDomain: dev-shipacom-p.s3-website-eu-west-1.amazonaws.com
      s3Domain: dev-shipacom-p.s3.eu-west-1.amazonaws.com
  lambdaAtEdge:
    retain: true

functions:
  redirects:
    name: '${self:service}-redirects-${self:provider.stage}'
    handler: src/redirects.handler
    memorySize: 128
    timeout: 1
    lambdaAtEdge:
      distribution: 'WebsiteDistribution'
      eventType: 'origin-request'

resources:
   Resources:
      WebsiteDistribution:
         Type: 'AWS::CloudFront::Distribution'
         Properties:
            DistributionConfig:
               DefaultCacheBehavior:
                  TargetOriginId: '${self:custom.redirects.dev.originId}'
                  ViewerProtocolPolicy: 'redirect-to-https'
                  DefaultTTL: 600 # ten minutes
                  MaxTTL: 600 # ten minutes
                  Compress: true
                  ForwardedValues:
                     QueryString: false
                     Cookies:
                        Forward: 'none'
               DefaultRootObject: 'index.html'
               Enabled: true
               PriceClass: 'PriceClass_100'
               HttpVersion: 'http2'
               ViewerCertificate:
                  CloudFrontDefaultCertificate: true
               Origins:
                  -
                     Id: '${self:custom.redirects.dev.originId}'
                     S3OriginConfig: {}
                     DomainName: ${self:custom.redirects.dev.s3Domain}

What am I missing?

iassainov commented 3 years ago

I just found that the function was associated with the cloudfront.

Leaving this for other people: to check if the function is associated, go to Cloudfront --> Monitoring --> lambda@edge, and there you will see the lambda function invocations. It does not matter if the lambda function does not have a Cloudfront trigger in the Lambda console.