'AWS Lambda Error: Could not unzip uploaded file

I am trying to update the code in my lambda function using the aws-sdk package for node.js. So I wrote the following script to run for deploying the code

var AWS = require('aws-sdk');
AWS.config.loadFromPath('./config.json');
var lambda = new AWS.Lambda();

var params = {
    FunctionName: 'FunctionName',
    ZipFile: 'fileb://deploy_package.zip'
}

lambda.updateFunctionCode(params, function(err, data) {
    if (err) console.log(err, err.stack);
    else console.log(data);
});

the problem is that when I run the script I get the following error:

{ InvalidParameterValueException: Could not unzip uploaded file. Please check your file, then try to upload again.
    at Object.extractError (C:\Users\jaarnold\Desktop\AlexaSkills\circleCI\node_modules\aws-sdk\lib\protocol\json.js:48:27)
    at Request.extractError (C:\Users\jaarnold\Desktop\AlexaSkills\circleCI\node_modules\aws-sdk\lib\protocol\rest_json.js:52:8)
    at Request.callListeners (C:\Users\jaarnold\Desktop\AlexaSkills\circleCI\node_modules\aws-sdk\lib\sequential_executor.js:105:20)
    at Request.emit (C:\Users\jaarnold\Desktop\AlexaSkills\circleCI\node_modules\aws-sdk\lib\sequential_executor.js:77:10)
    at Request.emit (C:\Users\jaarnold\Desktop\AlexaSkills\circleCI\node_modules\aws-sdk\lib\request.js:683:14)
    at Request.transition (C:\Users\jaarnold\Desktop\AlexaSkills\circleCI\node_modules\aws-sdk\lib\request.js:22:10)
    at AcceptorStateMachine.runTo (C:\Users\jaarnold\Desktop\AlexaSkills\circleCI\node_modules\aws-sdk\lib\state_machine.js:14:12)
    at C:\Users\jaarnold\Desktop\AlexaSkills\circleCI\node_modules\aws-sdk\lib\state_machine.js:26:10
    at Request.<anonymous> (C:\Users\jaarnold\Desktop\AlexaSkills\circleCI\node_modules\aws-sdk\lib\request.js:38:9)
    at Request.<anonymous> (C:\Users\jaarnold\Desktop\AlexaSkills\circleCI\node_modules\aws-sdk\lib\request.js:685:12)
  message: 'Could not unzip uploaded file. Please check your file, then try to upload again.',
  code: 'InvalidParameterValueException',
  time: 2018-06-18T15:09:12.212Z,
  requestId: '8ec20526-7309-11e8-aa20-4fb186ac4f30',
  statusCode: 400,
  retryable: false,
  retryDelay: 65.40106275377198 } 'InvalidParameterValueException: Could not unzip uploaded file. Please check your file, then try to upload again.\n    at Object.extractError (C:\\Users\\jaarnold\\Desktop\\AlexaSkills\\circleCI\\node_modules\\aws-sdk\\lib\\protocol\\json.js:48:27)\n    at Request.extractError (C:\\Users\\jaarnold\\Desktop\\AlexaSkills\\circleCI\\node_modules\\aws-sdk\\lib\\protocol\\rest_json.js:52:8)\n    at Request.callListeners (C:\\Users\\jaarnold\\Desktop\\AlexaSkills\\circleCI\\node_modules\\aws-sdk\\lib\\sequential_executor.js:105:20)\n    at Request.emit (C:\\Users\\jaarnold\\Desktop\\AlexaSkills\\circleCI\\node_modules\\aws-sdk\\lib\\sequential_executor.js:77:10)\n    at Request.emit (C:\\Users\\jaarnold\\Desktop\\AlexaSkills\\circleCI\\node_modules\\aws-sdk\\lib\\request.js:683:14)\n    at Request.transition (C:\\Users\\jaarnold\\Desktop\\AlexaSkills\\circleCI\\node_modules\\aws-sdk\\lib\\request.js:22:10)\n    at AcceptorStateMachine.runTo (C:\\Users\\jaarnold\\Desktop\\AlexaSkills\\circleCI\\node_modules\\aws-sdk\\lib\\state_machine.js:14:12)\n    at C:\\Users\\jaarnold\\Desktop\\AlexaSkills\\circleCI\\node_modules\\aws-sdk\\lib\\state_machine.js:26:10\n    at Request.<anonymous> (C:\\Users\\jaarnold\\Desktop\\AlexaSkills\\circleCI\\node_modules\\aws-sdk\\lib\\request.js:38:9)\n    at Request.<anonymous> (C:\\Users\\jaarnold\\Desktop\\AlexaSkills\\circleCI\\node_modules\\aws-sdk\\lib\\request.js:685:12)'

I have tried deploying the zip file from my computer as well as CircleCI and I have also tried using the AWS CLI, all with the same result.

I have everything I need in the zip file and in the config.json. What could I be doing wrong here?



Solution 1:[1]

Your ZipFile params is incorrect, it must be a buffer. Try change to :

var fs = require('fs');

var params = {
    FunctionName: 'FunctionName',
    ZipFile: fs.readFileSync('deploy_package.zip')
}

lambda.updateFunctionCode(params, function(err, data) {
    if (err) console.log(err, err.stack);
    else console.log(data);
});

Solution 2:[2]

I encountered this problem and solved it.

Looks like you are zipping the zip file. Thats why it is unable to zip it once again.

PS: If you are using AWS-S3, Uncheck the "GZIP files" option.

Solution 3:[3]

Node js . use like

fs.readFile('lambda/function.zip', (err, data) => {
   if (data) {
      console.log(data)
      lambda.createFunction(data);
  }
})

Solution 4:[4]

My solution is close to those mentioned above but slightly different. Maybe because of that difference it solved this issue for me:

import AWS from 'aws-sdk'
import fs from 'fs'

const file = fs.readFileSync(`myFileName.zip`)

const params = {
  Code: {
    ZipFile: Buffer.from(file),
  },
  ...
}

// Then standard SDK Lambda implementation:

const lambda = new AWS.Lambda({ apiVersion: '2015-03-31' })
lambda.createFunction(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mahdi Ridho
Solution 2 kranthi kumar
Solution 3 kuldeep chopra
Solution 4 Roman