gpt4 book ai didi

node.js - 无服务器 AWS Lambda 边缘 : MalformedHandlerName

转载 作者:行者123 更新时间:2023-12-04 07:54:18 27 4
gpt4 key购买 nike

我正在按照以下示例设置 Lambda Edge 函数:
https://www.serverless.com/blog/lambda-at-edge-support-added
代码 : ./handler.js

// SAME CODE STRUCTURE AS ORIGINAL, JUST ADDING A HEADER TO TEST
'use strict';

exports.handler = (event, context, callback) => {
const response = event.Records[0].cf.response;
const headers = response.headers;
headers['test-lambda-edge'] = [{ key: 'test-lambda-edge', value: 'working' }]
callback(null, response);
};
配置 : ./serverless.yml
# SAME CONFIG AS ORIGINAL, JUST CHANGING eventType and origin
service: mylambda
provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: 20201221
functions:
cfLambda:
handler: functions/handler.cloudfront
events:
- cloudFront:
eventType: origin-response
origin: https://example.org
函数部署正确:
$ sls deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading mylambda.zip file to S3 (554 B)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
................
Serverless: Stack update finished...
Service Information
service: mylambda
stage: dev
region: us-east-1
stack: mylambda-dev
resources: 8
api keys:
None
endpoints:
CloudFront - ---------------.cloudfront.net
functions:
cfLambda: mylambda-dev-cfLambda
layers:
None

我正在使用 Amazon CloudFront 修改响应 header 用于测试的测试模板。
尝试 1) 使用与文章 命名相同的处理程序
我收到以下错误,我可以理解为文章似乎错误,未定义云端功能:

"errorMessage": "functions/handler.cloudfront is undefined or notexported"


尝试 2) 尝试修复 serverless.yml
我替换了 handler: functions/handler.cloudfronthandler: functions/handler匹配代码,我得到以下错误:

{"errorType":"Runtime.MalformedHandlerName","errorMessage":"Badhandler","stack":["Runtime.MalformedHandlerName: Bad handler"," at_splitHandlerString


如果我查看官方文档,处理程序命名是相同的(即 exports.handler = ):
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-edge-how-it-works-tutorial.html
'use strict';
exports.handler = (event, context, callback) => {
问:我应该如何在代码/配置中命名我的处理程序?

最佳答案

如果您使用的目录结构如下所示:

├── handler.js
└── serverless.yml
并且导出的处理程序的名称是 handler住在 handler.js文件,那么正确的配置将如下所示:
service: mylambda
provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: 20201221
functions:
cfLambda:
handler: handler.handler
events:
- cloudFront:
eventType: origin-response
origin: https://example.org
第一部分是模块的路径,之后是 .您引用该模块中导出的特定函数。对于 handler: functions/handler.cloudfront要工作,您必须导出 cloudfront ./functions/handler.js中的函数文件。

关于node.js - 无服务器 AWS Lambda 边缘 : MalformedHandlerName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66780749/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com