如何调整AWS CDK构建的Python Lambda Layer至python/common/目录结构?
解决AWS CDK创建Python Lambda Layer目录结构问题
修改Bundling命令,保留common层级
核心是调整打包命令中的复制逻辑,确保src/common下的代码被放到python/common目录下,同时不影响依赖包的安装路径。
修改后的CDK代码如下:
_lambda.LayerVersion( self, "CommonPythonLambdaLayer", code=_lambda.Code.from_asset( "src/common", bundling=cdk.BundlingOptions( image=lambda_bundling_image, # type: ignore command=[ "bash", "-c", "pip install --no-cache -r requirements.txt --platform manylinux2014_aarch64 --only-binary=:all: --upgrade -t /asset-output/python && mkdir -p /asset-output/python/common && cp -au . /asset-output/python/common", ], platform="linux/arm64", ), ), )
调整说明
- 新增
mkdir -p /asset-output/python/common:在打包输出的python目录下创建common子目录,确保路径存在 - 修改复制命令为
cp -au . /asset-output/python/common:将当前工作目录(即src/common下的所有内容)复制到刚创建的common子目录中 - pip安装逻辑保持不变:依赖包依然被安装到
/asset-output/python下,符合Lambda Layer的依赖查找规则
这样打包后,Lambda Layer的目录结构会变成:
python/ common/ models.py # 此处为pip安装的依赖包
内容的提问来源于stack exchange,提问作者Inthu
相关产品推荐
相关产品推荐

