带有 Lambda@Edge 的 CloudFront 未被触发

CloudFront with Lambda@Edge not getting triggenred

我正在尝试部署以下 Lambda 函数

'use strict';

const userAgent = require('useragent')
const path = require('path')

module.exports.viewer = async (event, context, callback) => {
  const request = event.Records[0].cf.request
  const headers = request.headers
  const userAgentString = headers['user-agent'] && headers['user-agent'][0] ? headers['user-agent'][0].value : null
  const agent = userAgent.lookup(userAgentString)

  const browsersToInclude = [
    { browser: 'Chrome', version: 23 },
    { browser: 'Opera', version: 15 },
    { browser: 'Android', version: 53 },
    { browser: 'Chrome Mobile', version: 55 },
    { browser: 'Opera Mobile', version: 37 },
    { browser: 'UC Browser', version: 11 },
    { browser: 'Samsung Internet', version: 4 }
  ]

  const supportingBrowser = browsersToInclude
    .find(browser => browser.browser === agent.family && agent.major >= browser.version)

  if (supportingBrowser) {
    const fileFormat = path.extname(request.uri).replace('.', '')
    request.headers['original-resource-type'] = [{
      key: 'Original-Resource-Type',
      value: `image/${fileFormat}`
    }]

    const olduri = request.uri
    const newuri = olduri.replace(/(\.jpg|\.png|\.jpeg)$/g, '.webp')
    request.uri = newuri
  }

  return callback(null, request)
};

使用无服务器

service: viewer-request

provider:
  name: aws
  runtime: nodejs12.x
  region: ${opt:region, 'us-east-1'}
  stage: ${opt:stage, 'staging'}
  memorySize: 128
  timeout: 5


functions:
  viewer:
    handler: handler.viewer
    events:
      - preExistingCloudFront:
          distributionId: F4CC9XIJS6USAF
          eventType: viewer-request 
          pathPattern: '/images/*' 
          includeBody: false 

resources:
 Resources:
  IamRoleLambdaExecution:
    Type: "AWS::IAM::Role"
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
                - edgelambda.amazonaws.com

plugins:
  - serverless-offline
  - serverless-lambda-edge-pre-existing-cloudfront

此功能正在部署并创建触发,但当请求来自云端时,它没有被触发。我注意到云端日志中有以下内容

2020-08-18  20:31:35    EWR52-C4    580 72.79.124.96    GET d111111abcdef8.cloudfront.net   /images/menu/majorGroup/led_troffer_t34.png 403 https://stage.domain.com/indoor     Mozilla/5.0%20(Macintosh;%20Intel%20Mac%20OS%20X%2010_15_5)%20AppleWebKit/537.36%20(KHTML,%20like%20Gecko)%20Chrome/84.0.4147.125%20Safari/537.36   -   -   Error   ZwIiw9kc9y4PQX-mP2Ua6aQ8RVHGK4ZIne1ZIdiUYUjgCzlxf03QaQ==    stage.domain.com    https   64  0.855   -   TLSv1.2 ECDHE-RSA-AES128-GCM-SHA256 Error   HTTP/2.0    -   -   65422   0.854   Error   image/webp  -   -   -

我在这里缺少什么?

在 serverless.yal 中的 provider 添加以下内容解决了问题。

  iamRoleStatements:
    - Effect: Allow
      Action:
        - s3:*
      Resource: "*"
    - Effect: Allow
      Action:
        - logs:CreateLogGroup
        - logs:CreateLogStream
        - logs:PutLogEvents
      Resource: "*"