Uploaded file must be a non-empty zip (Service: AWSLambdaInternal; Status Code: 400; Error Code: InvalidParameterValueException;
Asked Answered
R

6

22

I have a simple Express/Node.js application with the following files with no subdirectories (other than .serverless and node_modules).

  • .serverless
  • node_modules
  • app.js
  • lambda.js
  • package.json
  • package-lock.json
  • serverless.yml

AWS throws this 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).

The .zip file was not empty when I accessed the S3 bucket to which the code gets uploaded. I was able to see the .zip folder with all its contents in there, However, the serverless deploy operation fails from AWS CLI, throwing the above exception. Any help to zero in on the problem would be greatly appreciated. Thanks!

Following is the code.

  • 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 /

The following is the console logs.

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

> [email protected] 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$ 

The full logs from the debug file are as follows:

0 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'run', 'deploy' ]
1 info using [email protected]
2 info using [email protected]
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 [email protected]
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

Rathbun answered 27/1, 2021 at 9:54 Comment(4)
Hey Aseem, I think you could try to fix your problem by downgrading the node version. Many people have the same problem when using node v15.6.0, try downgrading to the current lts version v14.15.4: For reference: github.com/serverless/serverless/issues/…Motivity
@Motivity Had this same issue, all though with v15.7.0, downgraded to v15.5.1 and all works fine. You might want to consider posting this as the answerRiojas
@FlorianDe, a downgrade to the LTS version of node did work for me. Thanks.Rathbun
AseemSavio, I am glad I could help you, I have now written another detailed answer for the solution of the problem, which you are welcome to mark as Accepted. @MXD thanks for supporting even more information about the downgraded and working version (v15.5.1).Motivity
M
34

This problem is caused by the archiverjs/node-archiver dependency used inside serverless when running on node version 15.6.0 or higher currently.

As many proposed here downgrading the node version to the current LTS version v14.15.4 or v15.5.1 should work just fine as MX D mentioned in his comment.

Many people are experiencing this issue when building their bundles inside a CI/CD Pipeline, because they are using a node docker image. For example in GitLab like:

default:
    image: node:alpine

Instead of node:alpine, the error should also occur with node:latest, etc. The lines should be changed to a specific working version described above as follows:

default:
    image: node:15.5.1
Motivity answered 5/2, 2021 at 19:8 Comment(0)
P
6

Just upgrade the serverless to the newest version: https://www.serverless.com/framework/docs/getting-started#upgrade

Propaedeutic answered 7/11, 2021 at 15:59 Comment(3)
Just for anyone else's benefit, this solution worked for me. I upgraded my serverless version to 2.68.0 and that fixed the above issueParttime
Did not work for me. Mine at 2.70.0 still not workingNodababus
Worked for me, using Node v16.15.0 and Serverless version 2.72.3Merla
S
2
  • Node JS latest: 19.4.0 - Didn't work
  • Node JS 18.13.0 LTS - Didn't work
  • Node JS 14.20.0 - WORKED!!!

Installing latest serverless package not helped

Scalp answered 9/1, 2023 at 15:35 Comment(0)
H
1

Had same problem today with a node lambda. To Fix Globally Update SLS Update in package.json: webpack serverless-webpack

That fixed it. Those packages were not updated in 3 years. My bad. I also went into AWS Lambda and updated to Node 16. But that didn't fix it.

Hialeah answered 24/8, 2022 at 0:4 Comment(0)
L
0

I had the same issue when deploying to Netlify. In my case, I was accidentally deploying an empty lambda function which caused the issue. Hope that helps someone!

Lur answered 21/6, 2022 at 11:59 Comment(0)
C
0

I faced the same issue today. I upgraded through a range of serverless versions (1.83.3 -> 2.68 -> 3.0) and I still had the same error message. Eventually upgraded serverless-webpack plugin (5.1.5 -> 5.11.0) and it resolved the issue. Thanks to Jim's comment

Chauvinism answered 27/6, 2023 at 14:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.