You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AWS SDK v3通过内联代码更新Lambda函数报错咨询

AWS Lambda内联代码更新报错问题

我尝试使用AWS SDK v3,以字符串形式的内联代码而非本地文件来更新AWS Lambda函数,编写的代码如下:

client.updateFunctionCode({
  FunctionName: "myFunc",
  ZipFile: Buffer.from("console.log('hey')", "base64")
})

但收到错误提示:

Could not unzip uploaded file. Please check your file, then try to upload again.

根据官方文档说明:

If the function's package type is Zip, then you must specify the deployment package as a .zip file archive. Enter the Amazon S3 bucket and key of the code .zip file location. You can also provide the function code inline using the ZipFile field.

我想咨询是我误解了文档内容,还是该功能并不被AWS SDK支持?


你误解了文档的要求——ZipFile字段要求传入的是已经压缩成zip格式的文件的base64编码,而不是直接把代码字符串转成base64。

Lambda的部署包必须是zip格式,哪怕是内联提供也一样。你现在直接把代码字符串转base64传进去,Lambda服务端尝试解压这个内容,自然会失败,因为它不是有效的zip文件。

正确的做法是先把代码文件(比如index.js)压缩成zip,再把这个zip文件转成base64,然后传入ZipFile字段。举个例子:

// 先创建代码内容并压缩成zip
const archiver = require('archiver');
const { PassThrough } = require('stream');

async function getZipBase64() {
  const stream = new PassThrough();
  const archive = archiver('zip');
  archive.pipe(stream);
  archive.append("exports.handler = async () => { console.log('hey'); return { statusCode: 200 }; }", { name: 'index.js' });
  await archive.finalize();

  const chunks = [];
  for await (const chunk of stream) {
    chunks.push(chunk);
  }
  return Buffer.concat(chunks).toString('base64');
}

// 然后更新函数代码
const zipBase64 = await getZipBase64();
client.updateFunctionCode({
  FunctionName: "myFunc",
  ZipFile: zipBase64
});

需要注意两点:

  • 压缩时必须包含正确的入口文件(比如示例中的index.js),且文件名要和Lambda函数配置的处理程序一致(比如index.handler)
  • Node.js函数的代码必须导出handler函数,否则Lambda执行时会提示找不到处理程序

内容的提问来源于stack exchange,提问作者Aleksand Kondaurov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 14:21:07