NodeJS 上载的文件必须为非空zip(服务:AWSL λ内部;状态代码:400;错误代码:参数值无效异常;

zzoitvuj  于 2023-01-12  发布在  Node.js
关注(0)|答案(5)|浏览(129)

我有一个简单的Express/Node.js应用程序,其中包含以下文件,没有子目录(除了. serverless和node_modules)。

  • .无服务器
  • 节点模块
  • app.js
  • lambda.js
  • package.json
  • package-lock.json
  • serverless.yml

AWS抛出此错误-发生错误:ApiLambda函数-上传的文件必须为非空zip(服务:AWSL λ内部;状态代码:400;错误代码:参数值无效异常;申请编号:5779d4c3-苯丙氨酸-苯丙氨酸-苯丙氨酸-苯丙氨酸-苯丙氨酸-苯丙氨酸-苯丙氨酸;代理:空)。
当我访问代码上传到的S3存储桶时,. zip文件不是空的。我可以看到. zip文件夹及其所有内容,但是,从AWS CLI执行serverless deploy操作失败,引发了上面的异常。如果能帮助解决这个问题,我将不胜感激。谢谢!
下面是代码。

  • app.js
const express = require("express")
const app = express();

app.use(express.json());
app.use(express.urlencoded({extended: false}));

app.get("/", (req, res) => {
    res.status(200).send("Root endpoint")
})

app.post("/postReq", (req, res) => {
    res.status(200).send(req.body);
})

app.listen(8080, () => console.log("App started."))
  • lambda.js
'use strict'
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')
const server = awsServerlessExpress.createServer(app)

exports.handler = (event, context) => { awsServerlessExpress.proxy(server, event, context) }
  • package.json
{
  "name": "my-lambda",
  "version": "1.0.0",
  "description": "Lambda Function",
  "main": "app.js",
  "scripts": {
    "start": "node app.js",
    "deploy": "serverless deploy"
  },
  "author": "Aseem Savio",
  "license": "ISC",
  "dependencies": {
    "aws-serverless-express": "^3.4.0",
    "express": "^4.17.1"
  }, 
  "devDependencies": {
    "serverless": "^2.21.1"
  }
}
  • serverless.yml
service: my-lambda-function

provider: 
  name: aws
  runtime: nodejs10.x
  memorySize: 512
  timeout: 15
  stage: production
  region: ap-south-1

functions:
  api:
    handler: lambda.handler
    events:
      - http: ANY {proxy+}
      - http: ANY /

以下是控制台日志。

aseemsavio@aseem-ubuntu:~/projects/Node.js/my-lambda$ npm run deploy

> my-lambda@1.0.0 deploy
> serverless deploy

Serverless: Configuration warning at 'functions.api.events[0].http': value 'ANY {proxy+}' does not satisfy pattern /^(?:\*|(GET|POST|PUT|PATCH|OPTIONS|HEAD|DELETE|ANY) (\/\S*))$/i
Serverless:  
Serverless: Learn more about configuration validation here: http://slss.io/configuration-validation
Serverless:  
Serverless: Deprecation warning: Starting with next major version, default value of provider.lambdaHashingVersion will be equal to "20201221"
            More Info: https://www.serverless.com/framework/docs/deprecations/#LAMBDA_HASHING_VERSION_V2
Serverless: Deprecation warning: Starting with next major version, API Gateway naming will be changed from "{stage}-{service}" to "{service}-{stage}".
            Set "provider.apiGateway.shouldStartNameWithService" to "true" to adapt to the new behavior now.
            More Info: https://www.serverless.com/framework/docs/deprecations/#AWS_API_GATEWAY_NAME_STARTING_WITH_SERVICE
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service my-lambda.zip file to S3 (917.12 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
................
Serverless: Operation failed!
Serverless: View the full error output: https://ap-south-1.console.aws.amazon.com/cloudformation/home?region=ap-south-1#/stack/detail?stackId=arn%3Aaws%3Acloudformation%3Aap-south-1%3A617584887932%3Astack%2Fmy-lambda-production%2F6d6f6ed0-607d-11eb-a05e-02c8952955f8
 
  Serverless Error ---------------------------------------
 
  An error occurred: ApiLambdaFunction - Uploaded file must be a non-empty zip (Service: AWSLambdaInternal; Status Code: 400; Error Code: InvalidParameterValueException; Request ID: 5779d4c3-beaa-4bc2-b525-c1e89a82635a; Proxy: null).
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com
 
  Your Environment Information ---------------------------
     Operating System:          linux
     Node Version:              15.6.0
     Framework Version:         2.21.1 (local)
     Plugin Version:            4.4.2
     SDK Version:               2.3.2
     Components Version:        3.5.1
 
npm ERR! code 1
npm ERR! path /home/aseemsavio/projects/Node.js/my-lambda
npm ERR! command failed
npm ERR! command sh -c serverless deploy

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/aseemsavio/.npm/_logs/2021-01-27T09_38_04_670Z-debug.log
aseemsavio@aseem-ubuntu:~/projects/Node.js/my-lambda$

调试文件中的完整日志如下所示:

0 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'run', 'deploy' ]
1 info using npm@7.4.0
2 info using node@v15.6.0
3 timing config:load:defaults Completed in 1ms
4 timing config:load:file:/usr/local/lib/node_modules/npm/npmrc Completed in 0ms
5 timing config:load:builtin Completed in 0ms
6 timing config:load:cli Completed in 1ms
7 timing config:load:env Completed in 1ms
8 timing config:load:file:/home/aseemsavio/projects/Node.js/my-lambda/.npmrc Completed in 0ms
9 timing config:load:project Completed in 0ms
10 timing config:load:file:/home/aseemsavio/.npmrc Completed in 0ms
11 timing config:load:user Completed in 0ms
12 timing config:load:file:/usr/local/etc/npmrc Completed in 1ms
13 timing config:load:global Completed in 1ms
14 timing config:load:cafile Completed in 0ms
15 timing config:load:validate Completed in 0ms
16 timing config:load:setUserAgent Completed in 0ms
17 timing config:load:setEnvs Completed in 1ms
18 timing config:load Completed in 5ms
19 verbose npm-session 39866318e36c1c25
20 timing npm:load Completed in 11ms
21 timing command:run-script Completed in 49470ms
22 verbose stack Error: command failed
22 verbose stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/@npmcli/promise-spawn/index.js:64:27)
22 verbose stack     at ChildProcess.emit (node:events:379:20)
22 verbose stack     at maybeClose (node:internal/child_process:1065:16)
22 verbose stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:296:5)
23 verbose pkgid my-lambda@1.0.0
24 verbose cwd /home/aseemsavio/projects/Node.js/my-lambda
25 verbose Linux 5.8.0-38-generic
26 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "deploy"
27 verbose node v15.6.0
28 verbose npm  v7.4.0
29 error code 1
30 error path /home/aseemsavio/projects/Node.js/my-lambda
31 error command failed
32 error command sh -c serverless deploy
33 verbose exit 1
xiozqbni

xiozqbni1#

此问题是由当前在节点版本15.6.0或更高版本上运行时在serverless内部使用的archiverjs/node-archiver依赖项引起的。
正如MX D在他的评论中提到的那样,许多人建议here**将节点版本降级到当前的LTS版本v14.15.4或v15.5.1应该可以正常工作。
很多人在CI/CD管道中构建bundle时都会遇到这个问题,因为他们使用的是节点停靠映像。

default:
    image: node:alpine

如果不是node:alpine,也会出现node:latest等错误。应将这些行更改为上述特定的工作版本,如下所示:

default:
    image: node:15.5.1
igsr9ssn

igsr9ssn2#

只需将无服务器升级到最新版本:https://www.serverless.com/framework/docs/getting-started#upgrade

dwthyt8l

dwthyt8l3#

今天节点lambda也有同样的问题。要修复全局更新SLS更新包.json:无服务器网络包
那就解决了。那些软件包3年没有更新了。我的错。我也进入了AWS Lambda并更新到了节点16。但那并没有解决它。

pkwftd7m

pkwftd7m4#

我在部署到Netlify时遇到了同样的问题。在我的例子中,我不小心部署了一个空的lambda函数,导致了这个问题。希望这能帮助到别人!

9wbgstp7

9wbgstp75#

  • NodeJS 最新:19.4.0 -不起作用
  • NodeJS 18.13.0 LTS -不工作
  • NodeJS 14.20.0 -工作正常!!!

安装最新的无服务器软件包没有帮助

相关问题