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

AWS Lambda执行失败:botocore[crt]依赖缺失问题求助

问题:Lambda部署CloudFront KVS交互函数时提示缺少botocore[crt]依赖

我用Python和boto3部署Lambda函数,用来和CloudFront密钥存储(KVS)交互,但一直报错说Lambda环境里缺botocore[crt]依赖,明明我已经在配置里加了这个依赖。

错误信息:

Missing Dependency: This operation requires an additional dependency. Use pip install botocore[crt] before proceeding.

我按照AWS官方文档的打包步骤来做,只是把pip install --target ./package boto3换成了pip install --target ./package -r requirements.txt。我用的是M1芯片的Mac,Python版本3.11.9,试过arm64和x86_64两种运行环境都不行。

示例代码:

import json
import boto3

cf_kvs_client = boto3.client("cloudfront-keyvaluestore")
cf_client = boto3.client("cloudfront")
KVS_ARN = "blah"

def lambda_handler(event, context):
    return fetch_current_values()

def fetch_current_values() -> str:
    current_values, etag = get_keys()
    return json.dumps({"etag": etag, "versions": current_values})

def get_keys() -> tuple[str, str]:
    kvs_etag = cf_kvs_client.describe_key_value_store(KvsARN=KVS_ARN)["ETag"]
    kvs_response = cf_kvs_client.list_keys(KvsARN=KVS_ARN)["Items"]
    return kvs_response, kvs_etag

requirements.txt内容:

boto3[crt]==1.35.62
botocore[crt]==1.35.62
jmespath==1.0.1
python-dateutil==2.9.0.post0
s3transfer==0.10.3
six==1.16.0
urllib3==2.2.3

解决方案

1. 用Docker模拟Lambda环境安装依赖

M1芯片的arm架构和Lambda的运行环境(Amazon Linux)存在兼容性差异,本地直接安装的依赖可能包含Mac特有的二进制文件,导致Lambda无法识别crt组件。用Docker模拟对应架构的Lambda环境安装依赖:

  • arm64架构:
    docker run --rm -v "$PWD":/var/task public.ecr.aws/sam/build-python3.11:latest /bin/sh -c "pip install --target ./package -r requirements.txt"
    
  • x86_64架构:
    docker run --rm -v "$PWD":/var/task --platform linux/amd64 public.ecr.aws/sam/build-python3.11:latest /bin/sh -c "pip install --target ./package -r requirements.txt"
    

2. 清理pip缓存后重新安装

本地pip缓存可能导致依赖不完整,先清理缓存再执行上述Docker安装命令:

pip cache purge

3. 简化requirements.txt

boto3[crt]会自动拉取兼容版本的botocore[crt]及其他依赖,无需手动列全所有子依赖,简化后的requirements.txt:

boto3[crt]==1.35.62

这样能避免版本冲突,同时减少打包体积。

4. 验证依赖安装结果

安装完成后,检查package/botocore目录下是否存在crt相关的子目录或二进制文件(比如_crt.cpython-311-x86_64-linux-gnu.so),如果没有,说明依赖安装失败,需要重新执行安装步骤。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 07:08:22