本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用容器映像部署 Node.js Lambda 函數
您可以透過三種方式為 Node.js Lambda 函數建置容器映像:
-
AWS 基礎映像會預先載入語言執行期、用來管理 Lambda 與函數程式碼之間互動的執行期界面用戶端,以及用於本機測試的執行期界面模擬器。
-
AWS 僅限作業系統的基礎映像
包含 Amazon Linux 發行版本和執行期界面模擬器 。這些映像常用於為編譯語言 (如 Go 和 Rust) 和 Lambda 不提供基礎映像的語言或語言版本 (如 Node.js 19) 建置容器映像。您還可以使用僅限作業系統的基礎映像來實作自訂執行期。若要使映像與 Lambda 相容,您必須在映像中加入適用於 Node.js 的執行期介面用戶端。 -
您可以使用其他容器登錄檔中的替代基礎映像 (例如 Alpine Linux 或 Debian)。您也可以使用組織建立的自訂映像。若要使映像與 Lambda 相容,您必須在映像中加入適用於 Node.js 的執行期介面用戶端。
提示
若要縮短 Lambda 容器函數變成作用中狀態所需的時間,請參閱 Docker 文件中的使用多階段建置
本頁面會說明如何為 Lambda 建置、測試和部署容器映像。
AWS Node.js 的基礎映像
AWS 為 Node.js 提供下列基礎映像:
| Tags (標籤) | 執行期 | 作業系統 | Dockerfile | 棄用 |
|---|---|---|---|---|
22 |
Node.js 22 | Amazon Linux 2023 | Dockerfile for Node.js 22 on GitHub |
2027 年 4 月 30 日 |
20 |
Node.js 20 | Amazon Linux 2023 | Dockerfile for Node.js 20 on GitHub |
2026 年 4 月 30 日 |
Amazon ECR 儲存庫:gallery.ecr.aws/lambda/nodejs
Node.js 20 和更新版本的基礎映像是以 Amazon Linux 2023 最小容器映像為基礎。舊版基礎映像使用 Amazon Linux 2。與 Amazon Linux 2 相比,AL2023 具有多項優點,包括更小的部署足跡和更新版本的程式庫,如 glibc。
以 AL2023 為基礎的映像使用 microdnf (符號連結為 dnf) 而不是 yum 作為套件管理工具,後者是 Amazon Linux 2 中的預設套件管理工具。microdnf 是 dnf 的獨立實作。對於以 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-based映像,包括搭配 AWS Serverless Application Model (AWS SAM),您必須使用 Docker 20.10.10 版或更新版本。
使用 Node.js AWS 的基礎映像
若要完成本節中的步驟,您必須執行下列各項:
-
Docker
(最低版本 25.0.0) -
Docker buildx 外掛程式
。 -
Node.js
從 Node.js AWS 的基礎映像建立容器映像
-
建立專案的目錄,然後切換至該目錄。
mkdir example cd example -
使用
npm建立新 Node.js 專案。若要接受互動體驗中提供的預設選項,請按下Enter。npm init -
建立稱為
index.js的新檔案。您可以將下列範例函數程式碼新增至檔案進行測試,或使用您自己的函數程式碼。範例 CommonJS 處理常式
exports.handler = async (event) => { const response = { statusCode: 200, body: JSON.stringify('Hello from Lambda!'), }; return response; }; -
如果您的函數依賴 以外的程式庫 適用於 JavaScript 的 AWS SDK,請使用 npm
將它們新增至您的套件。 -
建立包含下列組態的新 Dockerfile。
-
將
FROM屬性設定為基礎映像的 URI。 -
使用 COPY 命令將函數程式碼和執行時期相依項複製到
{LAMBDA_TASK_ROOT},一個 Lambda 定義的環境變數。 -
將
CMD引數設定為 Lambda 函數處理常式。
請注意,範例 Dockerfile 不包含 USER 指令
。當您將容器映像部署到 Lambda 時,Lambda 會自動定義一個具有最低權限許可的預設 Linux 使用者。這與標準 Docker 行為不同,後者會在未提供 USER指令時預設為root使用者。範例 Dockerfile
FROMpublic.ecr.aws/lambda/nodejs:22# Copy function code COPYindex.js${LAMBDA_TASK_ROOT} # Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile) CMD [ "index.handler" ] -
-
使用 docker build
命令建立 Docker 映像檔。以下範例將映像命名為 docker-image並為其提供test標籤。若要讓您的映像與 Lambda 相容,您必須使用 --provenance=false選項。docker buildx build --platform linux/amd64 --provenance=false -tdocker-image:test.注意
此命令會指定
--platform linux/amd64選項,確保無論建置機器的架構為何,您的容器都與 Lambda 執行環境相容。如果您打算使用 ARM64 指令集架構建立 Lambda 函數,務必將命令變更為改用--platform linux/arm64選項。
-
使用 docker run 命令啟動 Docker 影像。在此範例中,
docker-image為映像名稱,test為標籤。docker run --platform linux/amd64 -p 9000:8080docker-image:test此命令將映像作為容器執行,並在
localhost:9000/2015-03-31/functions/function/invocations建立本機端點。注意
如果您為 ARM64 指令集架構建立 Docker 映像檔,請務必將
--platform linux/選項改用選項。arm64--platform linux/amd64 -
從新的終端機視窗,將事件張貼至本機端點。
-
取得容器 ID。
docker ps -
使用 docker kill
命令停止容器。在此命令中,將 3766c4ab331c替換為上一步驟中的容器 ID。docker kill3766c4ab331c
若要將映像上傳至 Amazon ECR 並建立 Lambda 函數
-
使用 get-login-password
命令,向 Amazon ECR 登錄檔驗證 Docker CLI。 -
將
--region值設定為您要 AWS 區域 在其中建立 Amazon ECR 儲存庫的 。 -
111122223333以您的 AWS 帳戶 ID 取代 。
aws ecr get-login-password --regionus-east-1| docker login --username AWS --password-stdin111122223333.dkr.ecr.us-east-1.amazonaws.com -
-
使用 create-repository
命令在 Amazon ECR 中建立儲存庫。 aws ecr create-repository --repository-namehello-world--regionus-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" } } } -
從上一步驟的輸出中複製
repositoryUri。 -
執行 docker tag
命令,將 Amazon ECR 儲存庫中的本機映像標記為最新版本。在此命令中: -
docker-image:test為 Docker 映像檔的名稱和標籤。這是您在 docker build命令中指定的映像名稱和標籤。 -
將
<ECRrepositoryUri>替換為複製的repositoryUri。確保在 URI 的末尾包含:latest。
docker tag docker-image:test<ECRrepositoryUri>:latest範例:
docker tagdocker-image:test111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest -
-
執行 docker push
命令,將本機映像部署至 Amazon ECR 儲存庫。確保在儲存庫 URI 的末尾包含 :latest。docker push111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest -
建立函數的執行角色 (若您還沒有的話)。在下一個步驟中您需要角色的 Amazon Resource Name (ARN)。
-
建立 Lambda 函數。對於
ImageUri,從之前的設定中指定儲存庫 URI。確保在 URI 的末尾包含:latest。aws lambda create-function \ --function-namehello-world\ --package-type Image \ --code ImageUri=111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest \ --rolearn:aws:iam::111122223333:role/lambda-ex注意
只要映像與 Lambda 函數位於相同的區域,您就可以使用不同 AWS 帳戶中的映像來建立函數。如需詳細資訊,請參閱 Amazon ECR 跨帳戶許可。
-
調用函數。
aws lambda invoke --function-namehello-worldresponse.json您應該看到如下回應:
{ "ExecutedVersion": "$LATEST", "StatusCode": 200 } -
若要查看函數的輸出,請檢查
response.json檔案。
若要更新函數程式碼,您必須再次建置映像、將新映像上傳到 Amazon ECR 存放庫,然後使用 update-function-code
Lambda 會將映像標籤解析為特定映像摘要。這表示如果您將用來部署函數的映像標籤指向 Amazon ECR 中的新映像,Lambda 不會自動更新函數以使用新映像。
若要將新映像部署至相同的 Lambda 函數,必須使用 update-function-code--publish 選項會使用更新的容器映像來建立新的函數版本。
aws lambda update-function-code \ --function-namehello-world\ --image-uri111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest\ --publish
透過執行期介面用戶端使用替代基礎映像
如果您使用僅限作業系統的基礎映像或替代的基礎映像,則必須在映像中加入執行期介面用戶端。執行期介面用戶端會讓您擴充 Runtime API,管理 Lambda 與函數程式碼之間的互動。
使用 npm 套件管理員安裝 Node.js 執行期界面用戶端
npm install aws-lambda-ric
您還可以從 GitHub 下載 Node.js 執行時間界面用戶端
下列範例示範如何使用非AWS 基礎映像建置 Node.js 的容器映像。範例 Dockerfile 使用 bookworm 基礎映像。Dockerfile 包含執行期界面用戶端。
若要完成本節中的步驟,您必須執行下列各項:
-
Docker
(最低版本 25.0.0) -
Docker buildx 外掛程式
。 -
Node.js
從非AWS 基礎映像建立容器映像
-
建立專案的目錄,然後切換至該目錄。
mkdir example cd example -
使用
npm建立新 Node.js 專案。若要接受互動體驗中提供的預設選項,請按下Enter。npm init -
建立稱為
index.js的新檔案。您可以將下列範例函數程式碼新增至檔案進行測試,或使用您自己的函數程式碼。範例 CommonJS 處理常式
exports.handler = async (event) => { const response = { statusCode: 200, body: JSON.stringify('Hello from Lambda!'), }; return response; }; -
建立新的 Dockerfile。下列 Dockerfile 使用
bookworm基礎映像,而非 AWS 基礎映像。Dockerfile 包含執行期介面用戶端,可讓映像與 Lambda 相容。Dockerfile 使用多階段建置 。第一階段會建立組建映像,這是安裝函數相依項的標準 Node.js 環境。第二階段會建立更細微的映像,包含函數程式碼及其相依項。這會減少最終映像的大小。 -
將
FROM屬性設為基礎映像識別符。 -
使用
COPY命令複製函數程式碼和執行期相依項。 -
將
ENTRYPOINT設為您希望 Docker 容器在啟動時執行的模組。在此案例中,模組是執行期界面用戶端。 -
將
CMD引數設定為 Lambda 函數處理常式。
請注意,範例 Dockerfile 不包含 USER 指令
。當您將容器映像部署到 Lambda 時,Lambda 會自動定義一個具有最低權限許可的預設 Linux 使用者。這與標準 Docker 行為不同,後者會在未提供 USER指令時預設為root使用者。範例 Dockerfile
# Define custom function directory ARG FUNCTION_DIR="/function" FROMnode:20-bookwormas build-image # Include global arg in this stage of the build ARG FUNCTION_DIR # Install build dependencies RUN apt-get update && \ apt-get install -y \ g++ \ make \ cmake \ unzip \ libcurl4-openssl-dev # Copy function code RUN mkdir -p ${FUNCTION_DIR} COPY . ${FUNCTION_DIR} WORKDIR ${FUNCTION_DIR} # Install Node.js dependencies RUN npm install # Install the runtime interface client RUN npm install aws-lambda-ric # Grab a fresh slim copy of the image to reduce the final size FROMnode:20-bookworm-slim# Required for Node runtimes which use npm@8.6.0+ because # by default npm writes logs under /home/.npm and Lambda fs is read-only ENV NPM_CONFIG_CACHE=/tmp/.npm # 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/npx", "aws-lambda-ric"] # Pass the name of the function handler as an argument to the runtime CMD ["index.handler"] -
-
使用 docker build
命令建立 Docker 映像檔。以下範例將映像命名為 docker-image並為其提供test標籤。若要讓您的映像與 Lambda 相容,您必須使用 --provenance=false選項。docker buildx build --platform linux/amd64 --provenance=false -tdocker-image:test.注意
此命令會指定
--platform linux/amd64選項,確保無論建置機器的架構為何,您的容器都與 Lambda 執行環境相容。如果您打算使用 ARM64 指令集架構建立 Lambda 函數,務必將命令變更為改用--platform linux/arm64選項。
使用 執行期界面模擬器
若要在本機電腦上安裝並執行執行期介面模擬器
-
在您的專案目錄中執行以下命令,從 GitHub 下載執行期介面模擬器 (x86-64 架構),並安裝在本機電腦上。
-
使用 docker run 命令啟動 Docker 影像。請注意以下內容:
-
docker-image是映像名稱,而test是標籤。 -
/usr/local/bin/npx aws-lambda-ric index.handler是 Dockerfile 中的ENTRYPOINT,後面接著CMD。
此命令將映像作為容器執行,並在
localhost:9000/2015-03-31/functions/function/invocations建立本機端點。注意
如果您為 ARM64 指令集架構建立 Docker 映像檔,請務必將
--platform linux/選項改用選項。arm64--platform linux/amd64 -
-
將事件張貼至本機端點。
-
取得容器 ID。
docker ps -
使用 docker kill
命令停止容器。在此命令中,將 3766c4ab331c替換為上一步驟中的容器 ID。docker kill3766c4ab331c
若要將映像上傳至 Amazon ECR 並建立 Lambda 函數
-
使用 get-login-password
命令,向 Amazon ECR 登錄檔驗證 Docker CLI。 -
將
--region值設定為您要 AWS 區域 在其中建立 Amazon ECR 儲存庫的 。 -
111122223333以您的 AWS 帳戶 ID 取代 。
aws ecr get-login-password --regionus-east-1| docker login --username AWS --password-stdin111122223333.dkr.ecr.us-east-1.amazonaws.com -
-
使用 create-repository
命令在 Amazon ECR 中建立儲存庫。 aws ecr create-repository --repository-namehello-world--regionus-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" } } } -
從上一步驟的輸出中複製
repositoryUri。 -
執行 docker tag
命令,將 Amazon ECR 儲存庫中的本機映像標記為最新版本。在此命令中: -
docker-image:test為 Docker 映像檔的名稱和標籤。這是您在 docker build命令中指定的映像名稱和標籤。 -
將
<ECRrepositoryUri>替換為複製的repositoryUri。確保在 URI 的末尾包含:latest。
docker tag docker-image:test<ECRrepositoryUri>:latest範例:
docker tagdocker-image:test111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest -
-
執行 docker push
命令,將本機映像部署至 Amazon ECR 儲存庫。確保在儲存庫 URI 的末尾包含 :latest。docker push111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest -
建立函數的執行角色 (若您還沒有的話)。在下一個步驟中您需要角色的 Amazon Resource Name (ARN)。
-
建立 Lambda 函數。對於
ImageUri,從之前的設定中指定儲存庫 URI。確保在 URI 的末尾包含:latest。aws lambda create-function \ --function-namehello-world\ --package-type Image \ --code ImageUri=111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest \ --rolearn:aws:iam::111122223333:role/lambda-ex注意
只要映像與 Lambda 函數位於相同的區域,您就可以使用不同 AWS 帳戶中的映像來建立函數。如需詳細資訊,請參閱 Amazon ECR 跨帳戶許可。
-
調用函數。
aws lambda invoke --function-namehello-worldresponse.json您應該看到如下回應:
{ "ExecutedVersion": "$LATEST", "StatusCode": 200 } -
若要查看函數的輸出,請檢查
response.json檔案。
若要更新函數程式碼,您必須再次建置映像、將新映像上傳到 Amazon ECR 存放庫,然後使用 update-function-code
Lambda 會將映像標籤解析為特定映像摘要。這表示如果您將用來部署函數的映像標籤指向 Amazon ECR 中的新映像,Lambda 不會自動更新函數以使用新映像。
若要將新映像部署至相同的 Lambda 函數,必須使用 update-function-code--publish 選項會使用更新的容器映像來建立新的函數版本。
aws lambda update-function-code \ --function-namehello-world\ --image-uri111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest\ --publish