搜索
首页后端开发Python教程使用 Docker 创建 AWS 层。

Creating AWS layer with Docker.

当我们需要在 AWS 中为 lambda 函数创建一个层,并且该层对其操作有一些 SO 依赖项时,所以我们遇到了问题,AWS 文档对此进行了说明,可能还不够。

因此,在这种情况下,我们可以在所需的层中构建缺少的二进制文件。

好吧,对于这个例子,我们将使用 Python 3.x 和 Pdf2Image 库来制作演示

先决条件

  • Docker 安装
  • Python 3.x
  • AWS CLI 配置(可选)
  • 访问 AWS Lambda

1.准备环境

mkdir lambda-layer
cd lambda-layer
mkdir python
cd python

2.安装python依赖项

pip3 install [your_dependencies] \
    --platform manylinux2014_x86_64 \
    --target . \
    --only-binary=:all: \
    --implementation cp \
    --python-version [TU_VERSION_PYTHON] \
    --no-deps

pdf2image 示例:

pip3 install pdf2image Pillow \
    --platform manylinux2014_x86_64 \
    --target . \
    --only-binary=:all: \
    --implementation cp \
    --python-version 3.10 \
    --no-deps

3.Dockerfile基础

FROM ubuntu:22.04 as builder

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y \
    python3.10 \
    python3-pip \
    binutils \
    zip \
    [TUS_PAQUETES_ADICIONALES] \
    --no-install-recommends \
    && rm -rf /var/lib/apt/lists/*


WORKDIR /lambda
RUN mkdir -p /opt/python/lib/python3.10/site-packages/bin

COPY python/ /opt/python/lib/python3.10/site-packages/

RUN cp [TUS_BINARIOS] /opt/python/lib/python3.10/site-packages/bin/ && \
    chmod 755 /opt/python/lib/python3.10/site-packages/bin/*

RUN cd /opt && zip -r9 /lambda/layer.zip python/

FROM alpine:3.18
COPY --from=builder /lambda/layer.zip /
CMD ["/bin/sh"]

4. 构建和提取

# image build
docker build -t lambda-layer .

# extract layer.zip
docker run --rm -v "$(pwd)":/out lambda-layer cp /layer.zip /out/

5.使用图层

完成前面的步骤后,我们可以像往常一样上传图层并将其导入到我们的项目中

import os
import sys

#Configuring paths
SITE_PACKAGES = '/opt/python/lib/python3.10/site-packages'
BIN_DIR = os.path.join(SITE_PACKAGES, 'bin')
os.environ['PATH'] = f"{BIN_DIR}:{os.environ['PATH']}"
sys.path.append(SITE_PACKAGES)

#importing dependencies
from pdf2image import [your_import]

def lambda_handler(event, context):
    try:
        # your code here
        return {
            'statusCode': 200,
            'body': 'Success'
        }
    except Exception as e:
        return {
            'statusCode': 500,
            'body': f'Error: {str(e)}'
        }

参考

  • AWS Lambda 层文档
  • Python 包管理
  • Docker 文档

以上是使用 Docker 创建 AWS 层。的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python:深入研究汇编和解释Python:深入研究汇编和解释May 12, 2025 am 12:14 AM

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

Python是一种解释或编译语言,为什么重要?Python是一种解释或编译语言,为什么重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

对于python中的循环时循环与循环:解释了关键差异对于python中的循环时循环与循环:解释了关键差异May 12, 2025 am 12:08 AM

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

循环时:实用指南循环时:实用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

Python:它是真正的解释吗?揭穿神话Python:它是真正的解释吗?揭穿神话May 12, 2025 am 12:05 AM

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

与同一元素的Python串联列表与同一元素的Python串联列表May 11, 2025 am 12:08 AM

concateNateListsinpythonwithTheSamelements,使用:1)operatototakeepduplicates,2)asettoremavelemavphicates,or3)listCompreanspearensionforcontroloverduplicates,每个methodhasdhasdifferentperferentperferentperforentperforentperforentperfortenceandordormplications。

解释与编译语言:Python的位置解释与编译语言:Python的位置May 11, 2025 am 12:07 AM

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允许ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

循环时:您什么时候在Python中使用?循环时:您什么时候在Python中使用?May 11, 2025 am 12:05 AM

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具