
使用 AWS CLI 部署 Node.js Lambda 函数时,若错误地用 --code fileb://... 指向 ZIP 文件,会触发 'in ' requires string as left operand, not int 这一隐晦报错;根本原因在于参数名和前缀不匹配,应改用 --zip-file 参数而非 --code。
使用 aws cli 部署 node.js lambda 函数时,若错误地用 `--code fileb://...` 指向 zip 文件,会触发 `'in
在 AWS CLI v2 中,--code 参数仅接受结构化输入(如 JSON 对象),用于指定代码来源(例如 S3 存储桶路径或本地 ZIP 的 Base64 编码内容),而不能直接搭配 fileb:// 前缀使用。当你执行:
aws lambda create-function \ --function-name "helloWorld" \ --runtime nodejs20.x \ --role "arn:aws:iam::123456789012:role/lambda-execution-role" \ --handler "helloWorld.handler" \ --code fileb://helloWorld.zip # ❌ 错误:--code 不支持 fileb:// 直接读取二进制文件
CLI 内部尝试将 fileb://helloWorld.zip 解析为 JSON 字符串键值对,却在类型检查中发现 ZIP 是二进制流(被解析为 int 类型字节),导致 Python 层抛出 TypeError: 'in <string>' requires string as left operand, not int</string> —— 这是底层 boto3 在解析 --code 参数时的典型类型冲突。
✅ 正确做法是:使用 --zip-file 参数(专为本地 ZIP 设计),并配合 fileb:// 前缀:
aws lambda create-function \ --function-name "helloWorld" \ --runtime nodejs20.x \ --role "arn:aws:iam::123456789012:role/lambda-execution-role" \ --handler "helloWorld.handler" \ --zip-file fileb://helloWorld.zip # ✅ 正确:--zip-file 明确支持二进制 ZIP 文件
⚠️ 注意事项:
-
--zip-file是create-function和update-function-code的专用参数,不可用于其他命令; - ZIP 文件必须包含正确的入口文件结构(如
helloWorld.js位于根目录,且handler字段与文件名、导出名严格匹配); - 确保 ZIP 未损坏:可手动解压验证
unzip -l helloWorld.zip输出是否含helloWorld.js; - GitHub Actions 中建议添加校验步骤,例如:
- name: Verify ZIP contents run: unzip -l helloWorld.zip | grep -q "helloWorld.js"
? 补充说明:若需从 S3 部署,才应使用 --code S3Bucket=...,S3Key=...;而本地 ZIP 必须用 --zip-file fileb://... —— 这是 AWS CLI 的明确设计约定,混淆二者是该错误的唯一根源。










