選取您的 Cookie 偏好設定

我們使用提供自身網站和服務所需的基本 Cookie 和類似工具。我們使用效能 Cookie 收集匿名統計資料,以便了解客戶如何使用我們的網站並進行改進。基本 Cookie 無法停用,但可以按一下「自訂」或「拒絕」以拒絕效能 Cookie。

如果您同意,AWS 與經核准的第三方也會使用 Cookie 提供實用的網站功能、記住您的偏好設定,並顯示相關內容,包括相關廣告。若要接受或拒絕所有非必要 Cookie,請按一下「接受」或「拒絕」。若要進行更詳細的選擇,請按一下「自訂」。

使用容器映像部署 Python Lambda 函數

焦點模式
使用容器映像部署 Python Lambda 函數 - AWS Lambda

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

您可以透過三種方式為 Python Lambda 函數建置容器映像:

提示

若要縮短 Lambda 容器函數變成作用中狀態所需的時間,請參閱 Docker 文件中的使用多階段建置。若要建置有效率的容器映像,請遵循撰寫 Dockerfiles 的最佳實務

本頁面會說明如何為 Lambda 建置、測試和部署容器映像。

Python 的 AWS 基礎映像

AWS 針對 Python 提供以下基礎映像:

標籤 執行期 作業系統 Dockerfile 棄用

3.13

Python 3.13 Amazon Linux 2023 Dockerfile for Python 3.13 on GitHub

未排程

3.12

Python 3.12 Amazon Linux 2023 Dockerfile for Python 3.12 on GitHub

未排程

3.11

Python 3.11 Amazon Linux 2 Dockerfile for Python 3.11 on GitHub

未排程

3.10

Python 3.10 Amazon Linux 2 Dockerfile for Python 3.10 on GitHub

未排程

3.9

Python 3.9 Amazon Linux 2 Dockerfile for Python 3.9 on GitHub

未排程

Amazon ECR 儲存庫︰gallery.ecr.aws/lambda/python

Python 3.12 和更新版本的基礎映像以 Amazon Linux 2023 最小容器映像為基礎。Python 3.8-3.11 基礎映像以 Amazon Linux 2 映像為基礎。與 Amazon Linux 2 相比,以 AL2023 為基礎的映像具有多項優點,包括更小的部署足跡和更新版本的程式庫,如 glibc

以 AL2023 為基礎的映像使用 microdnf (符號連結為 dnf) 而不是 yum 作為套件管理工具,後者是 Amazon Linux 2 中的預設套件管理工具。microdnfdnf 的獨立實作。對於以 AL2023 為基礎的映像中包含的套件清單,請參閱 Comparing packages installed on Amazon Linux 2023 Container Images 中的 Minimal Container 欄。如需 AL2023 和 Amazon Linux 2 之間差異的詳細資訊,請參閱 AWS 運算部落格上的 Introducing the Amazon Linux 2023 runtime for AWS Lambda

注意

若要在本機執行以 AL2023 為基礎的映像,包括搭配 AWS Serverless Application Model (AWS SAM),必須使用 Docker 20.10.10 版或更新版本。

基礎映像中的相依性搜尋路徑

當您在程式碼中使用 import 陳述式時,Python 執行期會在其搜尋路徑中搜尋目錄,直到找到模組或套件為止。在預設情況下,執行期會先搜尋 {LAMBDA_TASK_ROOT} 目錄。如果您在映像中納入含執行期程式庫的版本,則此版本的優先順序會高於執行期中包含的版本。

搜尋路徑中包含的其他步驟取決於您使用的 Python Lambda 基礎映像版本:

  • Python 3.11 及更高版本:含執行期的程式庫和使用 pip 安裝的程式庫已安裝在 /var/lang/lib/python3.11/site-packages 目錄。此目錄的優先順序會高於搜尋路徑中的 /var/runtime。您可以使用 pip 安裝更新的版本來覆寫 SDK。您可以使用 pip 來確認含執行期的 SDK 及其相依性是否與您安裝的任何套件相容。

  • Python 3.8-3.10:含執行期的程式庫已安裝在 /var/runtime 目錄。使用 pip 安裝的程式庫已安裝在 /var/lang/lib/python3.x/site-packages 目錄。/var/runtime 目錄的優先順序會高於搜尋路徑中的 /var/lang/lib/python3.x/site-packages

您可以新增下列程式碼片段,以查看 Lambda 函數的完整搜尋路徑。

import sys search_path = sys.path print(search_path)

使用適用於 Python 的 AWS 基礎映像

若要完成本節中的步驟,您必須執行下列各項:

若要完成本節中的步驟,您必須執行下列各項:

若要從 Python 的 AWS 基礎映像建立容器映像
  1. 建立專案的目錄,然後切換至該目錄。

    mkdir example cd example
  2. 建立稱為 lambda_function.py 的新檔案。您可以將下列範例函數程式碼新增至檔案進行測試,或使用您自己的函數程式碼。

    範例 Python 函數
    import sys def handler(event, context): return 'Hello from AWS Lambda using Python' + sys.version + '!'
  3. 建立稱為 requirements.txt 的新檔案。如果您使用上一個步驟的範例函數程式碼,請將檔案保留空白,因為沒有任何相依項。否則,請列出每個所需的程式庫。例如,如果您的函數使用 AWS SDK for Python (Boto3),您的 requirements.txt 看起來應該像這樣:

    範例 requirements.txt
    boto3
  4. 建立包含下列組態的新 Dockerfile。

    請注意,範例 Dockerfile 不包含 USER 指令。當您將容器映像部署到 Lambda 時,Lambda 會自動定義一個具有最低權限許可的預設 Linux 使用者。這與標準 Docker 行為不同,後者會在未提供 USER 指令時預設為 root 使用者。

    範例 Dockerfile
    FROM public.ecr.aws/lambda/python:3.12 # Copy requirements.txt COPY requirements.txt ${LAMBDA_TASK_ROOT} # Install the specified packages RUN pip install -r requirements.txt # Copy function code COPY lambda_function.py ${LAMBDA_TASK_ROOT} # Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile) CMD [ "lambda_function.handler" ]
  5. 使用 docker build 命令建立 Docker 映像檔。以下範例將映像命名為 docker-image 並為其提供 test 標籤

    docker build --platform linux/amd64 -t docker-image:test .
    注意

    此命令會指定 --platform linux/amd64 選項,確保無論建置機器的架構為何,您的容器都與 Lambda 執行環境相容。如果您打算使用 ARM64 指令集架構建立 Lambda 函數,務必將命令變更為改用 --platform linux/arm64 選項。

若要從 Python 的 AWS 基礎映像建立容器映像
  1. 建立專案的目錄,然後切換至該目錄。

    mkdir example cd example
  2. 建立稱為 lambda_function.py 的新檔案。您可以將下列範例函數程式碼新增至檔案進行測試,或使用您自己的函數程式碼。

    範例 Python 函數
    import sys def handler(event, context): return 'Hello from AWS Lambda using Python' + sys.version + '!'
  3. 建立稱為 requirements.txt 的新檔案。如果您使用上一個步驟的範例函數程式碼,請將檔案保留空白,因為沒有任何相依項。否則,請列出每個所需的程式庫。例如,如果您的函數使用 AWS SDK for Python (Boto3),您的 requirements.txt 看起來應該像這樣:

    範例 requirements.txt
    boto3
  4. 建立包含下列組態的新 Dockerfile。

    請注意,範例 Dockerfile 不包含 USER 指令。當您將容器映像部署到 Lambda 時,Lambda 會自動定義一個具有最低權限許可的預設 Linux 使用者。這與標準 Docker 行為不同,後者會在未提供 USER 指令時預設為 root 使用者。

    範例 Dockerfile
    FROM public.ecr.aws/lambda/python:3.12 # Copy requirements.txt COPY requirements.txt ${LAMBDA_TASK_ROOT} # Install the specified packages RUN pip install -r requirements.txt # Copy function code COPY lambda_function.py ${LAMBDA_TASK_ROOT} # Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile) CMD [ "lambda_function.handler" ]
  5. 使用 docker build 命令建立 Docker 映像檔。以下範例將映像命名為 docker-image 並為其提供 test 標籤

    docker build --platform linux/amd64 -t docker-image:test .
    注意

    此命令會指定 --platform linux/amd64 選項,確保無論建置機器的架構為何,您的容器都與 Lambda 執行環境相容。如果您打算使用 ARM64 指令集架構建立 Lambda 函數,務必將命令變更為改用 --platform linux/arm64 選項。

  1. 使用 docker run 命令啟動 Docker 影像。在此範例中,docker-image 為映像名稱,test 為標籤。

    docker run --platform linux/amd64 -p 9000:8080 docker-image:test

    此命令將映像作為容器執行,並在 localhost:9000/2015-03-31/functions/function/invocations 建立本機端點。

    注意

    如果您為 ARM64 指令集架構建立 Docker 映像檔,請務必將--platform linux/arm64選項改用選項。--platform linux/amd64

  2. 從新的終端機視窗,將事件張貼至本機端點。

    Linux/macOS

    在 Linux 或 macOS 中,執行下列 curl 命令:

    curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'

    此命令會透過空白事件調用函數,並傳回一個回應。如果您使用自己的函數程式碼而不是範例函數程式碼,則可能需要使用 JSON 承載調用該函數。範例:

    curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}'
    PowerShell

    在 PowerShell 中,執行下列 Invoke-WebRequest 命令:

    Invoke-WebRequest -Uri "http://localhost:9000/2015-03-31/functions/function/invocations" -Method Post -Body '{}' -ContentType "application/json"

    此命令會透過空白事件調用函數,並傳回一個回應。如果您使用自己的函數程式碼而不是範例函數程式碼,則可能需要使用 JSON 承載調用該函數。範例:

    Invoke-WebRequest -Uri "http://localhost:9000/2015-03-31/functions/function/invocations" -Method Post -Body '{"payload":"hello world!"}' -ContentType "application/json"
  3. 取得容器 ID。

    docker ps
  4. 使用 docker kill 命令停止容器。在此命令中,將 3766c4ab331c 替換為上一步驟中的容器 ID。

    docker kill 3766c4ab331c

  1. 使用 docker run 命令啟動 Docker 影像。在此範例中,docker-image 為映像名稱,test 為標籤。

    docker run --platform linux/amd64 -p 9000:8080 docker-image:test

    此命令將映像作為容器執行,並在 localhost:9000/2015-03-31/functions/function/invocations 建立本機端點。

    注意

    如果您為 ARM64 指令集架構建立 Docker 映像檔,請務必將--platform linux/arm64選項改用選項。--platform linux/amd64

  2. 從新的終端機視窗,將事件張貼至本機端點。

    Linux/macOS

    在 Linux 或 macOS 中,執行下列 curl 命令:

    curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'

    此命令會透過空白事件調用函數,並傳回一個回應。如果您使用自己的函數程式碼而不是範例函數程式碼,則可能需要使用 JSON 承載調用該函數。範例:

    curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}'
    PowerShell

    在 PowerShell 中,執行下列 Invoke-WebRequest 命令:

    Invoke-WebRequest -Uri "http://localhost:9000/2015-03-31/functions/function/invocations" -Method Post -Body '{}' -ContentType "application/json"

    此命令會透過空白事件調用函數,並傳回一個回應。如果您使用自己的函數程式碼而不是範例函數程式碼,則可能需要使用 JSON 承載調用該函數。範例:

    Invoke-WebRequest -Uri "http://localhost:9000/2015-03-31/functions/function/invocations" -Method Post -Body '{"payload":"hello world!"}' -ContentType "application/json"

    在 Linux 或 macOS 中,執行下列 curl 命令:

    curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'

    此命令會透過空白事件調用函數,並傳回一個回應。如果您使用自己的函數程式碼而不是範例函數程式碼,則可能需要使用 JSON 承載調用該函數。範例:

    curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}'
  3. 取得容器 ID。

    docker ps
  4. 使用 docker kill 命令停止容器。在此命令中,將 3766c4ab331c 替換為上一步驟中的容器 ID。

    docker kill 3766c4ab331c
若要將映像上傳至 Amazon ECR 並建立 Lambda 函數
  1. 使用 get-login-password 命令,向 Amazon ECR 登錄檔驗證 Docker CLI。

    • --region 值設定為您要在其中建立 Amazon ECR 儲存庫的 AWS 區域。

    • 用您的 AWS 帳戶 ID 取代 111122223333

    aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 111122223333.dkr.ecr.us-east-1.amazonaws.com
  2. 使用 create-repository 命令在 Amazon ECR 中建立儲存庫。

    aws ecr create-repository --repository-name hello-world --region us-east-1 --image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE
    注意

    Amazon ECR 儲存庫必須位於與 Lambda 函數相同的 AWS 區域 中。

    如果成功,您將會看到以下回應:

    { "repository": { "repositoryArn": "arn:aws:ecr:us-east-1:111122223333:repository/hello-world", "registryId": "111122223333", "repositoryName": "hello-world", "repositoryUri": "111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world", "createdAt": "2023-03-09T10:39:01+00:00", "imageTagMutability": "MUTABLE", "imageScanningConfiguration": { "scanOnPush": true }, "encryptionConfiguration": { "encryptionType": "AES256" } } }
  3. 從上一步驟的輸出中複製 repositoryUri

  4. 執行 docker tag 命令,將 Amazon ECR 儲存庫中的本機映像標記為最新版本。在此命令中:

    • docker-image:test 為 Docker 映像檔的名稱和標籤。這是您在 docker build 命令中指定的映像名稱和標籤。

    • <ECRrepositoryUri> 替換為複製的 repositoryUri。確保在 URI 的末尾包含 :latest

    docker tag docker-image:test <ECRrepositoryUri>:latest

    範例:

    docker tag docker-image:test 111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
  5. 執行 docker push 命令,將本機映像部署至 Amazon ECR 儲存庫。確保在儲存庫 URI 的末尾包含 :latest

    docker push 111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
  6. 建立函數的執行角色 (若您還沒有的話)。在下一個步驟中您需要角色的 Amazon Resource Name (ARN)。

  7. 建立 Lambda 函數。對於 ImageUri,從之前的設定中指定儲存庫 URI。確保在 URI 的末尾包含 :latest

    aws lambda create-function \ --function-name hello-world \ --package-type Image \ --code ImageUri=111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest \ --role arn:aws:iam::111122223333:role/lambda-ex
    注意

    只要映像檔與 Lambda 函數位於相同的區域,您就可以使用不同 AWS 帳戶中的映像檔建立函數。如需詳細資訊,請參閱 Amazon ECR跨帳戶許可

  8. 調用函數。

    aws lambda invoke --function-name hello-world response.json

    您應該看到如下回應:

    { "ExecutedVersion": "$LATEST", "StatusCode": 200 }
  9. 若要查看函數的輸出,請檢查 response.json 檔案。

若要更新函數程式碼,您必須再次建置映像、將新映像上傳到 Amazon ECR 存放庫,然後使用 update-function-code 命令將映像部署到 Lambda 函數。

Lambda 會將映像標籤解析為特定映像摘要。這表示如果您將用來部署函數的映像標籤指向 Amazon ECR 中的新映像,Lambda 不會自動更新函數以使用新映像。

若要將新映像部署至相同的 Lambda 函數,必須使用 update-function-code 命令,即使 Amazon ECR 中的映像標籤保持不變亦如此。在以下範例中,--publish 選項會使用更新的容器映像來建立新的函數版本。

aws lambda update-function-code \ --function-name hello-world \ --image-uri 111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest \ --publish

若要將映像上傳至 Amazon ECR 並建立 Lambda 函數
  1. 使用 get-login-password 命令,向 Amazon ECR 登錄檔驗證 Docker CLI。

    • --region 值設定為您要在其中建立 Amazon ECR 儲存庫的 AWS 區域。

    • 用您的 AWS 帳戶 ID 取代 111122223333

    aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 111122223333.dkr.ecr.us-east-1.amazonaws.com
  2. 使用 create-repository 命令在 Amazon ECR 中建立儲存庫。

    aws ecr create-repository --repository-name hello-world --region us-east-1 --image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE
    注意

    Amazon ECR 儲存庫必須位於與 Lambda 函數相同的 AWS 區域 中。

    如果成功,您將會看到以下回應:

    { "repository": { "repositoryArn": "arn:aws:ecr:us-east-1:111122223333:repository/hello-world", "registryId": "111122223333", "repositoryName": "hello-world", "repositoryUri": "111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world", "createdAt": "2023-03-09T10:39:01+00:00", "imageTagMutability": "MUTABLE", "imageScanningConfiguration": { "scanOnPush": true }, "encryptionConfiguration": { "encryptionType": "AES256" } } }
  3. 從上一步驟的輸出中複製 repositoryUri

  4. 執行 docker tag 命令,將 Amazon ECR 儲存庫中的本機映像標記為最新版本。在此命令中:

    • docker-image:test 為 Docker 映像檔的名稱和標籤。這是您在 docker build 命令中指定的映像名稱和標籤。

    • <ECRrepositoryUri> 替換為複製的 repositoryUri。確保在 URI 的末尾包含 :latest

    docker tag docker-image:test <ECRrepositoryUri>:latest

    範例:

    docker tag docker-image:test 111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
  5. 執行 docker push 命令,將本機映像部署至 Amazon ECR 儲存庫。確保在儲存庫 URI 的末尾包含 :latest

    docker push 111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
  6. 建立函數的執行角色 (若您還沒有的話)。在下一個步驟中您需要角色的 Amazon Resource Name (ARN)。

  7. 建立 Lambda 函數。對於 ImageUri,從之前的設定中指定儲存庫 URI。確保在 URI 的末尾包含 :latest

    aws lambda create-function \ --function-name hello-world \ --package-type Image \ --code ImageUri=111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest \ --role arn:aws:iam::111122223333:role/lambda-ex
    注意

    只要映像檔與 Lambda 函數位於相同的區域,您就可以使用不同 AWS 帳戶中的映像檔建立函數。如需詳細資訊,請參閱 Amazon ECR跨帳戶許可

  8. 調用函數。

    aws lambda invoke --function-name hello-world response.json

    您應該看到如下回應:

    { "ExecutedVersion": "$LATEST", "StatusCode": 200 }
  9. 若要查看函數的輸出,請檢查 response.json 檔案。

若要更新函數程式碼,您必須再次建置映像、將新映像上傳到 Amazon ECR 存放庫,然後使用 update-function-code 命令將映像部署到 Lambda 函數。

Lambda 會將映像標籤解析為特定映像摘要。這表示如果您將用來部署函數的映像標籤指向 Amazon ECR 中的新映像,Lambda 不會自動更新函數以使用新映像。

若要將新映像部署至相同的 Lambda 函數,必須使用 update-function-code 命令,即使 Amazon ECR 中的映像標籤保持不變亦如此。在以下範例中,--publish 選項會使用更新的容器映像來建立新的函數版本。

aws lambda update-function-code \ --function-name hello-world \ --image-uri 111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest \ --publish

透過執行期介面用戶端使用替代基礎映像

如果您使用僅限作業系統的基礎映像或替代的基礎映像,則必須在映像中加入執行期介面用戶端。執行期介面用戶端會讓您擴充 針對自訂執行時期使用 Lambda 執行時期 API,管理 Lambda 與函數程式碼之間的互動。

使用 pip 套件管理員安裝 Python 執行期介面用戶端

pip install awslambdaric

您還可以從 GitHub 下載 Python 執行時間介面用戶端

以下範例示範如何使用非 AWS 基礎映像為 Python 建置容器映像。範例 Dockerfile 使用官方 Python 基礎映像。Dockerfile 包含 Python 執行期界面用戶端。

若要完成本節中的步驟,您必須執行下列各項:

若要完成本節中的步驟,您必須執行下列各項:

若要從非 AWS 基礎映像建立容器映像
  1. 建立專案的目錄,然後切換至該目錄。

    mkdir example cd example
  2. 建立稱為 lambda_function.py 的新檔案。您可以將下列範例函數程式碼新增至檔案進行測試,或使用您自己的函數程式碼。

    範例 Python 函數
    import sys def handler(event, context): return 'Hello from AWS Lambda using Python' + sys.version + '!'
  3. 建立稱為 requirements.txt 的新檔案。如果您使用上一個步驟的範例函數程式碼,請將檔案保留空白,因為沒有任何相依項。否則,請列出每個所需的程式庫。例如,如果您的函數使用 AWS SDK for Python (Boto3),您的 requirements.txt 看起來應該像這樣:

    範例 requirements.txt
    boto3
  4. 建立新的 Dockerfile。下列 Dockerfile 使用官方 Python 基礎映像,而非 AWS 基礎映像。Dockerfile 包含執行期介面用戶端,可讓映像與 Lambda 相容。下列範例 Dockerfile 使用多階段建置

    • FROM 屬性設定為基礎映像。

    • 將 ENTRYPOINT 設為您希望 Docker 容器在啟動時執行的模組。在此案例中,模組是執行期界面用戶端。

    • CMD 設定為 Lambda 函數處理常式。

    請注意,範例 Dockerfile 不包含 USER 指令。當您將容器映像部署到 Lambda 時,Lambda 會自動定義一個具有最低權限許可的預設 Linux 使用者。這與標準 Docker 行為不同,後者會在未提供 USER 指令時預設為 root 使用者。

    範例 Dockerfile
    # Define custom function directory ARG FUNCTION_DIR="/function" FROM python:3.12 AS build-image # Include global arg in this stage of the build ARG FUNCTION_DIR # Copy function code RUN mkdir -p ${FUNCTION_DIR} COPY . ${FUNCTION_DIR} # Install the function's dependencies RUN pip install \ --target ${FUNCTION_DIR} \ awslambdaric # Use a slim version of the base Python image to reduce the final image size FROM python:3.12-slim # Include global arg in this stage of the build ARG FUNCTION_DIR # Set working directory to function root directory WORKDIR ${FUNCTION_DIR} # Copy in the built dependencies COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR} # Set runtime interface client as default command for the container runtime ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ] # Pass the name of the function handler as an argument to the runtime CMD [ "lambda_function.handler" ]
  5. 使用 docker build 命令建立 Docker 映像檔。以下範例將映像命名為 docker-image 並為其提供 test 標籤

    docker build --platform linux/amd64 -t docker-image:test .
    注意

    此命令會指定 --platform linux/amd64 選項,確保無論建置機器的架構為何,您的容器都與 Lambda 執行環境相容。如果您打算使用 ARM64 指令集架構建立 Lambda 函數,務必將命令變更為改用 --platform linux/arm64 選項。

若要從非 AWS 基礎映像建立容器映像
  1. 建立專案的目錄,然後切換至該目錄。

    mkdir example cd example
  2. 建立稱為 lambda_function.py 的新檔案。您可以將下列範例函數程式碼新增至檔案進行測試,或使用您自己的函數程式碼。

    範例 Python 函數
    import sys def handler(event, context): return 'Hello from AWS Lambda using Python' + sys.version + '!'
  3. 建立稱為 requirements.txt 的新檔案。如果您使用上一個步驟的範例函數程式碼,請將檔案保留空白,因為沒有任何相依項。否則,請列出每個所需的程式庫。例如,如果您的函數使用 AWS SDK for Python (Boto3),您的 requirements.txt 看起來應該像這樣:

    範例 requirements.txt
    boto3
  4. 建立新的 Dockerfile。下列 Dockerfile 使用官方 Python 基礎映像,而非 AWS 基礎映像。Dockerfile 包含執行期介面用戶端,可讓映像與 Lambda 相容。下列範例 Dockerfile 使用多階段建置

    • FROM 屬性設定為基礎映像。

    • 將 ENTRYPOINT 設為您希望 Docker 容器在啟動時執行的模組。在此案例中,模組是執行期界面用戶端。

    • CMD 設定為 Lambda 函數處理常式。

    請注意,範例 Dockerfile 不包含 USER 指令。當您將容器映像部署到 Lambda 時,Lambda 會自動定義一個具有最低權限許可的預設 Linux 使用者。這與標準 Docker 行為不同,後者會在未提供 USER 指令時預設為 root 使用者。

    範例 Dockerfile
    # Define custom function directory ARG FUNCTION_DIR="/function" FROM python:3.12 AS build-image # Include global arg in this stage of the build ARG FUNCTION_DIR # Copy function code RUN mkdir -p ${FUNCTION_DIR} COPY . ${FUNCTION_DIR} # Install the function's dependencies RUN pip install \ --target ${FUNCTION_DIR} \ awslambdaric # Use a slim version of the base Python image to reduce the final image size FROM python:3.12-slim # Include global arg in this stage of the build ARG FUNCTION_DIR # Set working directory to function root directory WORKDIR ${FUNCTION_DIR} # Copy in the built dependencies COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR} # Set runtime interface client as default command for the container runtime ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ] # Pass the name of the function handler as an argument to the runtime CMD [ "lambda_function.handler" ]
  5. 使用 docker build 命令建立 Docker 映像檔。以下範例將映像命名為 docker-image 並為其提供 test 標籤

    docker build --platform linux/amd64 -t docker-image:test .
    注意

    此命令會指定 --platform linux/amd64 選項,確保無論建置機器的架構為何,您的容器都與 Lambda 執行環境相容。如果您打算使用 ARM64 指令集架構建立 Lambda 函數,務必將命令變更為改用 --platform linux/arm64 選項。

使用 執行期界面模擬器 以在本機測試映像。您可以將模擬器建置到映像中,也可以使用以下步驟,將其安裝在本機電腦。

若要在本機電腦上安裝並執行執行期介面模擬器
  1. 在您的專案目錄中執行以下命令,從 GitHub 下載執行期介面模擬器 (x86-64 架構),並安裝在本機電腦上。

    Linux/macOS
    mkdir -p ~/.aws-lambda-rie && \ curl -Lo ~/.aws-lambda-rie/aws-lambda-rie https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie && \ chmod +x ~/.aws-lambda-rie/aws-lambda-rie

    若要安裝 arm64 模擬器,請將上一個命令中的 GitHub 儲存庫 URL 替換為以下內容:

    https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie-arm64
    PowerShell
    $dirPath = "$HOME\.aws-lambda-rie" if (-not (Test-Path $dirPath)) { New-Item -Path $dirPath -ItemType Directory } $downloadLink = "https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie" $destinationPath = "$HOME\.aws-lambda-rie\aws-lambda-rie" Invoke-WebRequest -Uri $downloadLink -OutFile $destinationPath

    若要安裝 arm64 模擬器,請將 $downloadLink 更換為下列項目:

    https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie-arm64
  2. 使用 docker run 命令啟動 Docker 影像。注意下列事項:

    • docker-image 是映像名稱,而 test 是標籤。

    • /usr/local/bin/python -m awslambdaric lambda_function.handler 是 Dockerfile 中的 ENTRYPOINT,後面接著 CMD

    Linux/macOS
    docker run --platform linux/amd64 -d -v ~/.aws-lambda-rie:/aws-lambda -p 9000:8080 \ --entrypoint /aws-lambda/aws-lambda-rie \ docker-image:test \ /usr/local/bin/python -m awslambdaric lambda_function.handler
    PowerShell
    docker run --platform linux/amd64 -d -v "$HOME\.aws-lambda-rie:/aws-lambda" -p 9000:8080 ` --entrypoint /aws-lambda/aws-lambda-rie ` docker-image:test ` /usr/local/bin/python -m awslambdaric lambda_function.handler

    此命令將映像作為容器執行,並在 localhost:9000/2015-03-31/functions/function/invocations 建立本機端點。

    注意

    如果您為 ARM64 指令集架構建立 Docker 映像檔,請務必將--platform linux/arm64選項改用選項。--platform linux/amd64

  3. 將事件張貼至本機端點。

    Linux/macOS

    在 Linux 或 macOS 中,執行下列 curl 命令:

    curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'

    此命令會透過空白事件調用函數,並傳回一個回應。如果您使用自己的函數程式碼而不是範例函數程式碼,則可能需要使用 JSON 承載調用該函數。範例:

    curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}'
    PowerShell

    在 PowerShell 中,執行下列 Invoke-WebRequest 命令:

    Invoke-WebRequest -Uri "http://localhost:9000/2015-03-31/functions/function/invocations" -Method Post -Body '{}' -ContentType "application/json"

    此命令會透過空白事件調用函數,並傳回一個回應。如果您使用自己的函數程式碼而不是範例函數程式碼,則可能需要使用 JSON 承載調用該函數。範例:

    Invoke-WebRequest -Uri "http://localhost:9000/2015-03-31/functions/function/invocations" -Method Post -Body '{"payload":"hello world!"}' -ContentType "application/json"
  4. 取得容器 ID。

    docker ps
  5. 使用 docker kill 命令停止容器。在此命令中,將 3766c4ab331c 替換為上一步驟中的容器 ID。

    docker kill 3766c4ab331c

使用 執行期界面模擬器 以在本機測試映像。您可以將模擬器建置到映像中,也可以使用以下步驟,將其安裝在本機電腦。

若要在本機電腦上安裝並執行執行期介面模擬器
  1. 在您的專案目錄中執行以下命令,從 GitHub 下載執行期介面模擬器 (x86-64 架構),並安裝在本機電腦上。

    Linux/macOS
    mkdir -p ~/.aws-lambda-rie && \ curl -Lo ~/.aws-lambda-rie/aws-lambda-rie https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie && \ chmod +x ~/.aws-lambda-rie/aws-lambda-rie

    若要安裝 arm64 模擬器,請將上一個命令中的 GitHub 儲存庫 URL 替換為以下內容:

    https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie-arm64
    PowerShell
    $dirPath = "$HOME\.aws-lambda-rie" if (-not (Test-Path $dirPath)) { New-Item -Path $dirPath -ItemType Directory } $downloadLink = "https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie" $destinationPath = "$HOME\.aws-lambda-rie\aws-lambda-rie" Invoke-WebRequest -Uri $downloadLink -OutFile $destinationPath

    若要安裝 arm64 模擬器,請將 $downloadLink 更換為下列項目:

    https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie-arm64
    mkdir -p ~/.aws-lambda-rie && \ curl -Lo ~/.aws-lambda-rie/aws-lambda-rie https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie && \ chmod +x ~/.aws-lambda-rie/aws-lambda-rie

    若要安裝 arm64 模擬器,請將上一個命令中的 GitHub 儲存庫 URL 替換為以下內容:

    https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie-arm64
  2. 使用 docker run 命令啟動 Docker 影像。注意下列事項:

    • docker-image 是映像名稱,而 test 是標籤。

    • /usr/local/bin/python -m awslambdaric lambda_function.handler 是 Dockerfile 中的 ENTRYPOINT,後面接著 CMD

    Linux/macOS
    docker run --platform linux/amd64 -d -v ~/.aws-lambda-rie:/aws-lambda -p 9000:8080 \ --entrypoint /aws-lambda/aws-lambda-rie \ docker-image:test \ /usr/local/bin/python -m awslambdaric lambda_function.handler
    PowerShell
    docker run --platform linux/amd64 -d -v "$HOME\.aws-lambda-rie:/aws-lambda" -p 9000:8080 ` --entrypoint /aws-lambda/aws-lambda-rie ` docker-image:test ` /usr/local/bin/python -m awslambdaric lambda_function.handler
    docker run --platform linux/amd64 -d -v ~/.aws-lambda-rie:/aws-lambda -p 9000:8080 \ --entrypoint /aws-lambda/aws-lambda-rie \ docker-image:test \ /usr/local/bin/python -m awslambdaric lambda_function.handler

    此命令將映像作為容器執行,並在 localhost:9000/2015-03-31/functions/function/invocations 建立本機端點。

    注意

    如果您為 ARM64 指令集架構建立 Docker 映像檔,請務必將--platform linux/arm64選項改用選項。--platform linux/amd64

  3. 將事件張貼至本機端點。

    Linux/macOS

    在 Linux 或 macOS 中,執行下列 curl 命令:

    curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'

    此命令會透過空白事件調用函數,並傳回一個回應。如果您使用自己的函數程式碼而不是範例函數程式碼,則可能需要使用 JSON 承載調用該函數。範例:

    curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}'
    PowerShell

    在 PowerShell 中,執行下列 Invoke-WebRequest 命令:

    Invoke-WebRequest -Uri "http://localhost:9000/2015-03-31/functions/function/invocations" -Method Post -Body '{}' -ContentType "application/json"

    此命令會透過空白事件調用函數,並傳回一個回應。如果您使用自己的函數程式碼而不是範例函數程式碼,則可能需要使用 JSON 承載調用該函數。範例:

    Invoke-WebRequest -Uri "http://localhost:9000/2015-03-31/functions/function/invocations" -Method Post -Body '{"payload":"hello world!"}' -ContentType "application/json"

    在 Linux 或 macOS 中,執行下列 curl 命令:

    curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'

    此命令會透過空白事件調用函數,並傳回一個回應。如果您使用自己的函數程式碼而不是範例函數程式碼,則可能需要使用 JSON 承載調用該函數。範例:

    curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}'
  4. 取得容器 ID。

    docker ps
  5. 使用 docker kill 命令停止容器。在此命令中,將 3766c4ab331c 替換為上一步驟中的容器 ID。

    docker kill 3766c4ab331c
若要將映像上傳至 Amazon ECR 並建立 Lambda 函數
  1. 使用 get-login-password 命令,向 Amazon ECR 登錄檔驗證 Docker CLI。

    • --region 值設定為您要在其中建立 Amazon ECR 儲存庫的 AWS 區域。

    • 用您的 AWS 帳戶 ID 取代 111122223333

    aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 111122223333.dkr.ecr.us-east-1.amazonaws.com
  2. 使用 create-repository 命令在 Amazon ECR 中建立儲存庫。

    aws ecr create-repository --repository-name hello-world --region us-east-1 --image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE
    注意

    Amazon ECR 儲存庫必須位於與 Lambda 函數相同的 AWS 區域 中。

    如果成功,您將會看到以下回應:

    { "repository": { "repositoryArn": "arn:aws:ecr:us-east-1:111122223333:repository/hello-world", "registryId": "111122223333", "repositoryName": "hello-world", "repositoryUri": "111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world", "createdAt": "2023-03-09T10:39:01+00:00", "imageTagMutability": "MUTABLE", "imageScanningConfiguration": { "scanOnPush": true }, "encryptionConfiguration": { "encryptionType": "AES256" } } }
  3. 從上一步驟的輸出中複製 repositoryUri

  4. 執行 docker tag 命令,將 Amazon ECR 儲存庫中的本機映像標記為最新版本。在此命令中:

    • docker-image:test 為 Docker 映像檔的名稱和標籤。這是您在 docker build 命令中指定的映像名稱和標籤。

    • <ECRrepositoryUri> 替換為複製的 repositoryUri。確保在 URI 的末尾包含 :latest

    docker tag docker-image:test <ECRrepositoryUri>:latest

    範例:

    docker tag docker-image:test 111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
  5. 執行 docker push 命令,將本機映像部署至 Amazon ECR 儲存庫。確保在儲存庫 URI 的末尾包含 :latest

    docker push 111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
  6. 建立函數的執行角色 (若您還沒有的話)。在下一個步驟中您需要角色的 Amazon Resource Name (ARN)。

  7. 建立 Lambda 函數。對於 ImageUri,從之前的設定中指定儲存庫 URI。確保在 URI 的末尾包含 :latest

    aws lambda create-function \ --function-name hello-world \ --package-type Image \ --code ImageUri=111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest \ --role arn:aws:iam::111122223333:role/lambda-ex
    注意

    只要映像檔與 Lambda 函數位於相同的區域,您就可以使用不同 AWS 帳戶中的映像檔建立函數。如需詳細資訊,請參閱 Amazon ECR跨帳戶許可

  8. 調用函數。

    aws lambda invoke --function-name hello-world response.json

    您應該看到如下回應:

    { "ExecutedVersion": "$LATEST", "StatusCode": 200 }
  9. 若要查看函數的輸出,請檢查 response.json 檔案。

若要更新函數程式碼,您必須再次建置映像、將新映像上傳到 Amazon ECR 存放庫,然後使用 update-function-code 命令將映像部署到 Lambda 函數。

Lambda 會將映像標籤解析為特定映像摘要。這表示如果您將用來部署函數的映像標籤指向 Amazon ECR 中的新映像,Lambda 不會自動更新函數以使用新映像。

若要將新映像部署至相同的 Lambda 函數,必須使用 update-function-code 命令,即使 Amazon ECR 中的映像標籤保持不變亦如此。在以下範例中,--publish 選項會使用更新的容器映像來建立新的函數版本。

aws lambda update-function-code \ --function-name hello-world \ --image-uri 111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest \ --publish

若要將映像上傳至 Amazon ECR 並建立 Lambda 函數
  1. 使用 get-login-password 命令,向 Amazon ECR 登錄檔驗證 Docker CLI。

    • --region 值設定為您要在其中建立 Amazon ECR 儲存庫的 AWS 區域。

    • 用您的 AWS 帳戶 ID 取代 111122223333

    aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 111122223333.dkr.ecr.us-east-1.amazonaws.com
  2. 使用 create-repository 命令在 Amazon ECR 中建立儲存庫。

    aws ecr create-repository --repository-name hello-world --region us-east-1 --image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE
    注意

    Amazon ECR 儲存庫必須位於與 Lambda 函數相同的 AWS 區域 中。

    如果成功,您將會看到以下回應:

    { "repository": { "repositoryArn": "arn:aws:ecr:us-east-1:111122223333:repository/hello-world", "registryId": "111122223333", "repositoryName": "hello-world", "repositoryUri": "111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world", "createdAt": "2023-03-09T10:39:01+00:00", "imageTagMutability": "MUTABLE", "imageScanningConfiguration": { "scanOnPush": true }, "encryptionConfiguration": { "encryptionType": "AES256" } } }
  3. 從上一步驟的輸出中複製 repositoryUri

  4. 執行 docker tag 命令,將 Amazon ECR 儲存庫中的本機映像標記為最新版本。在此命令中:

    • docker-image:test 為 Docker 映像檔的名稱和標籤。這是您在 docker build 命令中指定的映像名稱和標籤。

    • <ECRrepositoryUri> 替換為複製的 repositoryUri。確保在 URI 的末尾包含 :latest

    docker tag docker-image:test <ECRrepositoryUri>:latest

    範例:

    docker tag docker-image:test 111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
  5. 執行 docker push 命令,將本機映像部署至 Amazon ECR 儲存庫。確保在儲存庫 URI 的末尾包含 :latest

    docker push 111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
  6. 建立函數的執行角色 (若您還沒有的話)。在下一個步驟中您需要角色的 Amazon Resource Name (ARN)。

  7. 建立 Lambda 函數。對於 ImageUri,從之前的設定中指定儲存庫 URI。確保在 URI 的末尾包含 :latest

    aws lambda create-function \ --function-name hello-world \ --package-type Image \ --code ImageUri=111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest \ --role arn:aws:iam::111122223333:role/lambda-ex
    注意

    只要映像檔與 Lambda 函數位於相同的區域,您就可以使用不同 AWS 帳戶中的映像檔建立函數。如需詳細資訊,請參閱 Amazon ECR跨帳戶許可

  8. 調用函數。

    aws lambda invoke --function-name hello-world response.json

    您應該看到如下回應:

    { "ExecutedVersion": "$LATEST", "StatusCode": 200 }
  9. 若要查看函數的輸出,請檢查 response.json 檔案。

若要更新函數程式碼,您必須再次建置映像、將新映像上傳到 Amazon ECR 存放庫,然後使用 update-function-code 命令將映像部署到 Lambda 函數。

Lambda 會將映像標籤解析為特定映像摘要。這表示如果您將用來部署函數的映像標籤指向 Amazon ECR 中的新映像,Lambda 不會自動更新函數以使用新映像。

若要將新映像部署至相同的 Lambda 函數,必須使用 update-function-code 命令,即使 Amazon ECR 中的映像標籤保持不變亦如此。在以下範例中,--publish 選項會使用更新的容器映像來建立新的函數版本。

aws lambda update-function-code \ --function-name hello-world \ --image-uri 111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest \ --publish

如需如何從 Alpine 基礎映像中建立 Python 映像的範例,請參閱 AWS 部落格上的Lambda 的容器映像支援

下一個主題:

上一個主題:

部署 .zip 封存檔
隱私權網站條款Cookie 偏好設定
© 2025, Amazon Web Services, Inc.或其附屬公司。保留所有權利。