aws-sdk-js icon indicating copy to clipboard operation
aws-sdk-js copied to clipboard

UnknownError: BadRequest on calling Lambda.UpdateFunctionCode

Open mikhasd opened this issue 3 years ago • 1 comments

Describe the bug

I'm using the UpdateFunctionCode function to upload a .zip file with my lambda code and I'm getting UnknownError: BadRequest without details on why this is a bad request.

Expected Behavior

I should be able to successfully upload my lambda .zip file

Current Behavior

I got the following error:

[AWS lambda 400 0.303s 0 retries] updateFunctionCode({
  FunctionName: 'my-lambda',
  ZipFile: '***SensitiveInformation***' 
})
err: Error [UnknownError]: Bad Request
    at Object.extractError (I:\my-lambda\node_modules\aws-sdk\lib\protocol\json.js:52:27)
    at Request.extractError (I:\my-lambda\node_modules\aws-sdk\lib\protocol\rest_json.js:49:8)
    at Request.callListeners (I:\my-lambda\node_modules\aws-sdk\lib\sequential_executor.js:106:20)
    at Request.emit (I:\my-lambda\node_modules\aws-sdk\lib\sequential_executor.js:78:10)
    at Request.emit (I:\my-lambda\node_modules\aws-sdk\lib\request.js:686:14)
    at Request.transition (I:\my-lambda\node_modules\aws-sdk\lib\request.js:22:10)
    at AcceptorStateMachine.runTo (I:\my-lambda\node_modules\aws-sdk\lib\state_machine.js:14:12)
    at I:\my-lambda\node_modules\aws-sdk\lib\state_machine.js:26:10
    at Request.<anonymous> (I:\my-lambda\node_modules\aws-sdk\lib\request.js:38:9)
    at Request.<anonymous> (I:\my-lambda\node_modules\aws-sdk\lib\request.js:688:12) {
  code: 'UnknownError',
  statusCode: 400,
  time: 2022-06-21T09:14:04.005Z,
  requestId: undefined,
  retryable: false,
  retryDelay: 18.855901608840142
}

Reproduction Steps

import * as fs from 'fs/promises'
import AWS from "aws-sdk"

async function upload(functionName, zipFilePath){
  const functionCode = await fs.readFile(zipFilePath)

  AWS.config.update({
    httpOptions: {
      proxy: process.env.HTTPS_PROXY,
      timeout: 300 * 1000,
      connectTimeout: 300 * 1000
    },
    region: 'us-east-1',
  });

  const lambda = new AWS.Lambda({logger:console})

  const response = await lambda.updateFunctionCode({
    FunctionName: functionName,
    ZipFile: functionCode ,
  }).promise()
}

Possible Solution

No response

Additional Information/Context

Note that other calls to Lambda API works perfectly

SDK version used

2.1147.0

Environment details (OS name and version, etc.)

Node.js v14.17.6

mikhasd avatar Jun 21 '22 09:06 mikhasd

Note that I was able to perform the same action with very similar code using SDK v3.

mikhasd avatar Jun 21 '22 10:06 mikhasd

Hi @mikhasd ,

I'm not sure what the problem seems to be. Perhaps your env variable has some value that the payload doesn't like, or your lambda code is zipped in an incorrect way?

Im able to successfully preform an update on my lambda code using your example:

import AWS from "aws-sdk"
import * as fs from 'fs/promises'


AWS.config.update({
    region: "us-east-1"
})
async function upload(functionName, zipFilePath){
  const functionCode = await fs.readFile(zipFilePath)
  const lambda = new AWS.Lambda()

  try {
    const response = await lambda.updateFunctionCode({
        FunctionName: functionName,
        ZipFile: functionCode ,
    }).promise()
    console.log(response)
  } catch (error) {
    console.log(error)
  }
}
upload("v2function", "./v2lambdacode.zip")
{
  FunctionName: 'v2function',
  FunctionArn: 'arn:aws:lambda:us-east-1:REDACTED',
  Runtime: 'nodejs18.x',
  Role: 'arn:aws:iam::REDACTED',
  Handler: 'index.handler',
  CodeSize: 306,
  Description: '',
  Timeout: 3,
  MemorySize: 128,
  LastModified: '2023-04-27T23:20:06.000+0000',
  CodeSha256: 'REDACTED',
  Version: '$LATEST',
  KMSKeyArn: null,
  TracingConfig: { Mode: 'PassThrough' },
  MasterArn: null,
  RevisionId: 'REDACTED',
  State: 'Active',
  StateReason: null,
  StateReasonCode: null,
  LastUpdateStatus: 'InProgress',
  LastUpdateStatusReason: 'The function is being created.',
  LastUpdateStatusReasonCode: 'Creating',
  PackageType: 'Zip',
  SigningProfileVersionArn: null,
  SigningJobArn: null,
  Architectures: [ 'x86_64' ],
  EphemeralStorage: { Size: 512 },
  SnapStart: { ApplyOn: 'None', OptimizationStatus: 'Off' },
  RuntimeVersionConfig: {
    RuntimeVersionArn: 'arn:aws:lambda:us-east-1::runtime:REDACTED'
  }
}

After that, Lambda code updates successfully.

Since you mentioned that v3 works as expected, my suggestion is to migrate your application to v3. We have officially declared maintenance mode for v2, and it will eventually get deprecated.

Please let me know if you have any other questions.

Thanks, Ran~

RanVaknin avatar Apr 27 '23 23:04 RanVaknin

This issue has not received a response in 1 week. If you still think there is a problem, please leave a comment to avoid the issue from automatically closing.

github-actions[bot] avatar May 03 '23 00:05 github-actions[bot]