本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用容器映像部署 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 建置、測試和部署容器映像。
Node.js 的 AWS 基礎映像
AWS 針對 Node.js 提供以下基礎映像:
標籤 | 執行期 | 作業系統 | Dockerfile | 棄用 |
---|---|---|---|---|
20 |
Node.js 20 | Amazon Linux 2023 | 用於 Node.js 20 的碼頭文件 GitHub |
|
18 |
Node.js 18 | Amazon Linux 2 | 用於 Node.js 18 的碼頭文件 GitHub |
|
16 |
Node.js 16 | Amazon Linux 2 | 用於 Node.js 16 的碼頭文件 GitHub |
2024 年 6 月 12 日 |
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 為基礎之映像檔中包含的套件清單,請參閱比較 Amazon Linux 2023 容器映像上安裝的套件中的最小容器欄。如需有關 AL2023 和 Amazon Linux 2 之間差異的詳細資訊,請參閱AWS運算部落格AWS Lambda上的介紹 Amazon Linux 2023 執行階段
注意
要在本地運行基於 AL2023 的映像,包括使用AWS Serverless Application Model(AWS SAM),您必須使用碼頭版本 20.10.10 或更高版本。
使用適用於 Node.js 的 AWS 基礎映像
若要完成本節中的步驟,您必須執行下列各項:
-
泊塢視窗
(Node.js 20 及更高版本的基本映像檔的最低版本 20.10.10) -
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; };
-
如果您的函數依賴於 AWS SDK for JavaScript 以外的程式庫,則請使用 npm
將其新增至您的套件。 -
建立包含下列組態的新 Dockerfile。
-
將
FROM
屬性設定為基礎映像的 URI。 -
使用 COPY 指令
{LAMBDA_TASK_ROOT}
,將函數程式碼和執行階段相依性複製到 Lambda 定義的環境變數。 -
將
CMD
引數設定為 Lambda 函數處理常式。
範例 Dockerfile
FROM
public.ecr.aws/lambda/nodejs:20
# 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
標籤。 docker build --platform linux/amd64 -t
docker-image
:test
.注意
此命令會指定
--platform linux/amd64
選項,確保無論建置機器的架構為何,您的容器都與 Lambda 執行環境相容。如果您打算使用 ARM64 指令集架構建立 Lambda 函數,務必將命令變更為改用--platform linux/arm64
選項。
-
使用 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
-
從新的終端機視窗,將事件張貼至本機端點。
-
取得容器 ID。
docker ps
-
使用 docker kill
命令停止容器。在此命令中,將 3766c4ab331c
替換為上一步驟中的容器 ID。docker kill
3766c4ab331c
若要將映像上傳至 Amazon ECR 並建立 Lambda 函數
-
執行命get-login-password
令以向您的 Amazon ECR 登錄驗證碼頭 CLI。 -
將
--region
值設定為您要在其中建立 Amazon ECR 儲存庫的 AWS 區域。 -
用您的 AWS 帳戶 ID 取代
111122223333
。
aws ecr get-login-password --region
us-east-1
| docker login --username AWS --password-stdin111122223333
.dkr.ecr.us-east-1
.amazonaws.com -
-
使用 create-repository
命令在 Amazon ECR 中建立儲存庫。 aws ecr create-repository --repository-name
hello-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 映像檔的名稱和標籤。 -
將
<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 -
-
執行 docker push
命令,將本機映像部署至 Amazon ECR 儲存庫。確保在儲存庫 URI 的末尾包含 :latest
。docker push
111122223333
.dkr.ecr.us-east-1
.amazonaws.com/hello-world
:latest -
建立函數的執行角色 (若您還沒有的話)。在下一個步驟中您需要角色的 Amazon Resource Name (ARN)。
-
建立 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 \ --rolearn:aws:iam::111122223333:role/lambda-ex
注意
只要映像檔與 Lambda 函數位於相同的區域,您就可以使用不同 AWS 帳戶中的映像檔建立函數。如需詳細資訊,請參閱 Amazon ECR 跨帳戶許可。
-
調用函數。
aws lambda invoke --function-name
hello-world
response.json您應該看到如下回應:
{ "ExecutedVersion": "$LATEST", "StatusCode": 200 }
-
若要查看函數的輸出,請檢查
response.json
檔案。
若要更新函數程式碼,您必須再次建立映像、將新映像上傳到 Amazon ECR 存放庫,然後使用update-function-code
透過執行期介面用戶端使用替代基礎映像
如果您使用僅限作業系統的基礎映像或替代的基礎映像,則必須在映像中加入執行期介面用戶端。執行期介面用戶端會讓您擴充 Lambda 執行階段 API,管理 Lambda 與函數程式碼之間的互動。
使用 npm 套件管理員安裝 Node.js 執行期界面用戶端
npm install aws-lambda-ric
您也可以從下載 Node.js 執行階段介面用戶端
14.x
16.x
18.x
20.x
以下範例示範如何使用非 AWS 基礎映像為 Node.js 建置容器映像。範例 Dockerfile 使用 buster
基礎映像。Dockerfile 包含執行期界面用戶端。
若要完成本節中的步驟,您必須執行下列各項:
若要從非 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 使用
buster
基礎映像,而非 AWS 基礎映像。Dockerfile 包含執行期介面用戶端,可讓映像與 Lambda 相容。Dockerfile 使用多階段建置 。第一階段會建立組建映像,這是安裝函數相依項的標準 Node.js 環境。第二階段會建立更細微的映像,包含函數程式碼及其相依項。這會減少最終映像的大小。 -
將
FROM
屬性設為基礎映像識別符。 -
使用
COPY
命令複製函數程式碼和執行期相依項。 -
將
ENTRYPOINT
設為您希望 Docker 容器在啟動時執行的模組。在此案例中,模組是執行期界面用戶端。 -
將
CMD
引數設定為 Lambda 函數處理常式。
範例 Dockerfile
# Define custom function directory ARG FUNCTION_DIR="/function" FROM
node:20-buster
as 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-buster-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
標籤。 docker build --platform linux/amd64 -t
docker-image
:test
.注意
此命令會指定
--platform linux/amd64
選項,確保無論建置機器的架構為何,您的容器都與 Lambda 執行環境相容。如果您打算使用 ARM64 指令集架構建立 Lambda 函數,務必將命令變更為改用--platform linux/arm64
選項。
使用 執行期界面模擬器 以在本機測試映像。您可以將模擬器建置到映像中,也可以將其安裝在本機電腦。
若要在本機電腦上安裝並執行執行期介面模擬器
-
從您的項目目錄中運行以下命令以下載運行時接口仿真器(x86-64 架構) GitHub 並將其安裝在本地計算機上。
-
使用 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 kill
3766c4ab331c
若要將映像上傳至 Amazon ECR 並建立 Lambda 函數
-
執行命get-login-password
令以向您的 Amazon ECR 登錄驗證碼頭 CLI。 -
將
--region
值設定為您要在其中建立 Amazon ECR 儲存庫的 AWS 區域。 -
用您的 AWS 帳戶 ID 取代
111122223333
。
aws ecr get-login-password --region
us-east-1
| docker login --username AWS --password-stdin111122223333
.dkr.ecr.us-east-1
.amazonaws.com -
-
使用 create-repository
命令在 Amazon ECR 中建立儲存庫。 aws ecr create-repository --repository-name
hello-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 映像檔的名稱和標籤。 -
將
<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 -
-
執行 docker push
命令,將本機映像部署至 Amazon ECR 儲存庫。確保在儲存庫 URI 的末尾包含 :latest
。docker push
111122223333
.dkr.ecr.us-east-1
.amazonaws.com/hello-world
:latest -
建立函數的執行角色 (若您還沒有的話)。在下一個步驟中您需要角色的 Amazon Resource Name (ARN)。
-
建立 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 \ --rolearn:aws:iam::111122223333:role/lambda-ex
注意
只要映像檔與 Lambda 函數位於相同的區域,您就可以使用不同 AWS 帳戶中的映像檔建立函數。如需詳細資訊,請參閱 Amazon ECR 跨帳戶許可。
-
調用函數。
aws lambda invoke --function-name
hello-world
response.json您應該看到如下回應:
{ "ExecutedVersion": "$LATEST", "StatusCode": 200 }
-
若要查看函數的輸出,請檢查
response.json
檔案。
若要更新函數程式碼,您必須再次建立映像、將新映像上傳到 Amazon ECR 存放庫,然後使用update-function-code