新Poetry版本致AWS PYTHON_3_10镜像运行cdk diff异常,求替代方案
解决AWS ECR Python 3.10镜像Poetry版本兼容性问题的方案
一、构建阶段强制指定Poetry版本
无需自定义镜像,可在构建流程中覆盖镜像自带的Poetry版本:
- 在Dockerfile或构建脚本中,先卸载现有版本再安装指定的1.8.5:
pip uninstall -y poetry pip install poetry==1.8.5 - 若使用CDK的
DockerImageAsset,可将上述命令加入构建阶段的执行指令中,确保在运行poetry install等命令前完成版本替换。
二、通过项目配置约束Poetry版本
在项目的pyproject.toml中声明所需Poetry版本,配合脚本做版本校验:
- 编辑
pyproject.toml添加版本约束:[tool.poetry] poetry-version = "1.8.5" - 添加构建前的版本检查脚本,自动切换到指定版本:
REQUIRED_POETRY_VERSION="1.8.5" CURRENT_POETRY_VERSION=$(poetry --version | awk '{print $3}') if [ "$CURRENT_POETRY_VERSION" != "$REQUIRED_POETRY_VERSION" ]; then pip uninstall -y poetry pip install poetry==$REQUIRED_POETRY_VERSION fi
三、CDK中锁定ECR镜像的具体版本标签
如果AWS官方ECR仓库保留了含Poetry 1.8.5的旧镜像标签,可直接在CDK中指定该标签,避免拉取升级后的镜像:
- 示例代码(Python CDK):
from aws_cdk import aws_ecr as ecr # 引用带有Poetry 1.8.5的特定标签镜像 python_runtime_image = ecr.ECRImage.from_ecr_repository( repository=ecr.Repository.from_repository_name(self, "PythonRuntimeRepo", "aws/aws-lambda-python"), tag="3.10_20240517" # 替换为实际含Poetry 1.8.5的镜像标签 )
注:需确认官方仓库是否保留旧标签,若官方已清理旧镜像,此方法不可用。
内容的提问来源于stack exchange,提问作者vignesh asokan
相关产品推荐
相关产品推荐

