步驟 3:建立生命週期掛鉤 Lambda 函數 - AWS CodeDeploy

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

步驟 3:建立生命週期掛鉤 Lambda 函數

在本節中,您會為 Amazon ECS 部署的AfterAllowTestTraffic掛鉤實作一個 Lambda 函數。Lambda 函數會在安裝更新的 Amazon ECS 應用程式之前執行驗證測試。在此教學課程中,Lambda 函數會傳回 Succeeded。在真實世界部署期間,驗證測試會傳回 SucceededFailed,取決於驗證測試的結果。此外,在真實世界部署期間,您也可以為一或多個其他 Amazon ECS 部署生命週期事件掛鉤 (BeforeInstallBeforeAllowTrafficAfterInstall和 ) 實作 Lambda 測試函數AfterAllowTraffic。如需詳細資訊,請參閱Amazon ECS 部署的生命週期事件掛鉤清單

建立 Lambda 函數需要IAM角色。此角色會授予 Lambda 函數許可,以寫入 CloudWatch 日誌並設定 CodeDeploy 生命週期掛鉤的狀態。

建立 IAM 角色
  1. 在 開啟IAM主控台https://console.aws.amazon.com/iam/

  2. 從導覽窗格,選擇 Roles (角色),然後選擇 Create role (建立角色)

  3. 建立具備下列屬性的角色:

    • Trusted entity (信任實體)AWS Lambda

    • 許可:AWSLambdaBasicExecutionRole。這將授予您的 Lambda 函數寫入 CloudWatch Logs 的許可。

    • Role name (角色名稱)lambda-cli-hook-role

    如需詳細資訊,請參閱建立 AWS Lambda 執行角色。

  4. 將許可 codedeploy:PutLifecycleEventHookExecutionStatus 連接至您建立的角色。這將授予 Lambda 函數許可,以在部署期間設定 CodeDeploy 生命週期掛鉤的狀態。如需詳細資訊,請參閱 AWS Identity and Access Management 使用者指南CodeDeploy API參考 PutLifecycleEventHookExecutionStatus中的新增IAM身分許可

若要建立 AfterAllowTestTraffic hook Lambda 函數
  1. 使用下列內容建立名為 AfterAllowTestTraffic.js 的檔案。

    'use strict'; const AWS = require('aws-sdk'); const codedeploy = new AWS.CodeDeploy({apiVersion: '2014-10-06'}); exports.handler = (event, context, callback) => { console.log("Entering AfterAllowTestTraffic hook."); // Read the DeploymentId and LifecycleEventHookExecutionId from the event payload var deploymentId = event.DeploymentId; var lifecycleEventHookExecutionId = event.LifecycleEventHookExecutionId; var validationTestResult = "Failed"; // Perform AfterAllowTestTraffic validation tests here. Set the test result // to "Succeeded" for this tutorial. console.log("This is where AfterAllowTestTraffic validation tests happen.") validationTestResult = "Succeeded"; // Complete the AfterAllowTestTraffic hook by sending CodeDeploy the validation status var params = { deploymentId: deploymentId, lifecycleEventHookExecutionId: lifecycleEventHookExecutionId, status: validationTestResult // status can be 'Succeeded' or 'Failed' }; // Pass CodeDeploy the prepared validation test results. codedeploy.putLifecycleEventHookExecutionStatus(params, function(err, data) { if (err) { // Validation failed. console.log('AfterAllowTestTraffic validation tests failed'); console.log(err, err.stack); callback("CodeDeploy Status update failed"); } else { // Validation succeeded. console.log("AfterAllowTestTraffic validation tests succeeded"); callback(null, "AfterAllowTestTraffic validation tests succeeded"); } }); }
  2. 建立 Lambda 部署套件。

    zip AfterAllowTestTraffic.zip AfterAllowTestTraffic.js
  3. 使用 create-function命令為您的AfterAllowTestTraffic勾點建立 Lambda 函數。

    aws lambda create-function --function-name AfterAllowTestTraffic \ --zip-file fileb://AfterAllowTestTraffic.zip \ --handler AfterAllowTestTraffic.handler \ --runtime nodejs10.x \ --role arn:aws:iam::aws-account-id:role/lambda-cli-hook-role
  4. create-function回應ARN中記下您的 Lambda 函數。您可以在下一個步驟中更新 CodeDeploy 部署 AppSpec 的檔案ARN時使用此選項。