繁体   English   中英

在 AWS Lambda 上安装更新版本的 sqlite3 以与 Python 一起使用

[英]Install newer version of sqlite3 on AWS Lambda for use with Python

我有一个 Python 脚本在 AWS Lambda 上的 Docker 容器中运行。 我正在使用推荐的 AWS 映像 ( public.ecr.aws/lambda/python:3.9 ),它附带 SQLite 版本 3.7.17(从 2013 年开始。),当我在 M1 Mac 上本地测试容器时:我看到了这个:

$ docker run --env-file .env --entrypoint bash -ti my-image
bash-4.2# uname -a
Linux e9ed14d35cbe 5.10.104-linuxkit #1 SMP PREEMPT Thu Mar 17 17:05:54 UTC 2022 aarch64 aarch64 aarch64 GNU/Linux
bash-4.2# sqlite3 --version
3.7.17 2013-05-20 00:56:22 118a3b35693b134d56ebd780123b7fd6f1497668

但是,我使用更新的 SQLite 功能,所以我需要找到一种方法来使用更新版本的库。 最直接的解决方案是按照this answer中的建议安装二进制 package 。 文档说它应该像使用 pip 安装一样简单。 不幸的是,当我尝试在 Docker 容器中使用这种方法时,我得到了这个:

bash-4.2# pip3 install pysqlite3-binary
ERROR: Could not find a version that satisfies the requirement pysqlite3-binary (from versions: none)
ERROR: No matching distribution found for pysqlite3-binary

当我尝试使用 pipenv 将它安装在容器外部时,我得到了同样的错误(这是我实际用于 package 管理的内容):

🕙 01:08:24 ❯ pipenv install pysqlite3-binary
Installing pysqlite3-binary...
Error:  An error occurred while installing pysqlite3-binary!
Error text:
ERROR: Could not find a version that satisfies the requirement pysqlite3-binary (from versions: none)
ERROR: No matching distribution found for pysqlite3-binary

✘ Installation Failed

难道我做错了什么? 如果没有,我怎样才能获得 Python 可以在这个容器中使用的 SQLite 的最新版本? 我真的需要按照此处的建议在 Dockerfile 中使用单独的构建阶段并将 rpm 组件复制到此处所述的位置吗? 对于许多人可能一直需要做的事情,这感觉像是很多工作。


更新:我使用EPEL8 (IIUC) 的 3.26 版在容器内尝试了 rpm 方法,但它失败了,出现了一堆这样的依赖错误:

bash-4.2# curl --output-dir /tmp -sO https://vault.centos.org/centos/8/BaseOS/aarch64/os/Packages/sqlite-3.26.0-15.el8.aarch64.rpm
bash-4.2# yum localinstall /tmp/sqlite-3.26.0-15.el8.aarch64.rpm
Loaded plugins: ovl
Examining /tmp/sqlite-3.26.0-15.el8.aarch64.rpm: sqlite-3.26.0-15.el8.aarch64

# etc.

--> Finished Dependency Resolution
Error: Package: sqlite-3.26.0-15.el8.aarch64 (/sqlite-3.26.0-15.el8.aarch64)
           Requires: libc.so.6(GLIBC_2.28)(64bit)

# Plus 6 other package dependency errors

Error: Package: nss-softokn-3.67.0-3.amzn2.0.1.aarch64 (@amzn2-core)
           Requires: libsqlite3.so.0()(64bit)
           Removing: sqlite-3.7.17-8.amzn2.1.1.aarch64 (@amzn2-core)
               libsqlite3.so.0()(64bit)
           Updated By: sqlite-3.26.0-15.el8.aarch64 (/sqlite-3.26.0-15.el8.aarch64)
               Not found
           Obsoleted By: sqlite-3.26.0-15.el8.aarch64 (/sqlite-3.26.0-15.el8.aarch64)
               Not found
           Available: sqlite-3.7.17-8.amzn2.0.2.aarch64 (amzn2-core)
               libsqlite3.so.0()(64bit)
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

当我尝试--skip-broken时,它只是完全跳过安装 3.26 package。


更新 2:我尝试从pysqlite3-binary手动下载 Python 3.9 轮。 但是,看起来该项目只为x86_64生产轮子,而不是为 Lambda 使用的aarch64平台。~~(这是不正确的,请参阅答案。)所以大概这就是为什么pip没有找到它的原因。

问题是我在 M1 Mac 上本地运行 Docker 进行测试。 因此, aarch64架构。 Lambda 确实允许您使用 ARM,但幸运的是它仍然默认为x86_64 我确认我的 Lambda function 正在运行x86_64 ,这是二进制轮使用的,这很好:

在此处输入图像描述

所以我需要做三件事:

  1. 更改我的Pipfile以有条件地仅在 x86_64 上安装二进制 package:

     pysqlite3-binary = { version = "*", platform_machine = "== 'x86_64'" }
  2. 调整 sqlite 导入,如原始答案中所述:

     try: import pysqlite3 as sqlite3 except ModuleNotFoundError: import sqlite3 # for local testing because pysqlite3-binary couldn't be installed on macos print(f"{sqlite3.sqlite_version=}")
  3. 将我的 Docker 容器设置为在本地以 x86 仿真模式启动。

     $ DOCKER_DEFAULT_PLATFORM=linux/amd64 docker build -t my-image. $ DOCKER_DEFAULT_PLATFORM=linux/amd64 docker run -ti my-image

等等,瞧!

sqlite3.sqlite_version='3.39.2'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM