

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Langkah 3: Buat fungsi Lambda kait siklus hidup
<a name="tutorial-ecs-with-hooks-create-hooks"></a>

Di bagian ini, Anda menerapkan satu fungsi Lambda untuk hook penyebaran Amazon ECS Anda. `AfterAllowTestTraffic` Fungsi Lambda menjalankan uji validasi sebelum aplikasi Amazon ECS yang diperbarui diinstal. Untuk tutorial ini, fungsi Lambda kembali. `Succeeded` Selama penerapan dunia nyata, tes validasi kembali `Succeeded` atau`Failed`, tergantung pada hasil tes validasi. Juga selama penerapan dunia nyata, Anda dapat menerapkan fungsi pengujian Lambda untuk satu atau beberapa kait `BeforeInstall` peristiwa siklus hidup penerapan Amazon ECS lainnya (,,, dan). `AfterInstall` `BeforeAllowTraffic` `AfterAllowTraffic` Untuk informasi selengkapnya, lihat [Daftar kait peristiwa siklus hidup untuk penerapan Amazon ECS](reference-appspec-file-structure-hooks.md#reference-appspec-file-structure-hooks-list-ecs).

 Peran IAM diperlukan untuk membuat fungsi Lambda Anda. Peran tersebut memberikan izin fungsi Lambda untuk menulis CloudWatch ke Log dan menyetel status hook siklus hidup CodeDeploy . 

**Untuk membuat IAM role**

1. Buka konsol IAM di [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/).

1. Dari panel navigasi, pilih **Peran**, lalu pilih **Buat peran**.

1.  Buat peran dengan properti berikut: 
   +  **Entitas tepercaya**: **AWS Lambda**. 
   +  **Izin**: **AWSLambdaBasicExecutionRole**. Ini memberikan izin fungsi Lambda Anda untuk menulis CloudWatch ke Log. 
   +  **Nama peran**: **`lambda-cli-hook-role`**. 

   Untuk informasi selengkapnya, lihat [Membuat peran AWS Lambda eksekusi](https://docs.aws.amazon.com/lambda/latest/dg/with-userapp.html#with-userapp-walkthrough-custom-events-create-iam-role). 

1.  Lampirkan izin `codedeploy:PutLifecycleEventHookExecutionStatus` ke peran yang Anda buat. Ini memberikan izin fungsi Lambda Anda untuk menyetel status hook siklus CodeDeploy hidup selama penerapan. Untuk informasi selengkapnya, lihat [Menambahkan izin identitas IAM](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html#add-policies-console) di *Panduan AWS Identity and Access Management Pengguna* dan [PutLifecycleEventHookExecutionStatus](https://docs.aws.amazon.com/codedeploy/latest/APIReference/API_PutLifecycleEventHookExecutionStatus.html)di Referensi *CodeDeploy API*. 

**Untuk membuat fungsi Lambda `AfterAllowTestTraffic` hook**

1.  Buat file bernama `AfterAllowTestTraffic.js` dengan isi berikut ini. 

   ```
   '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");
    		}
    	});
    }
   ```

1.  Buat paket penyebaran Lambda. 

   ```
   zip AfterAllowTestTraffic.zip AfterAllowTestTraffic.js 
   ```

1.  Gunakan `create-function` perintah untuk membuat fungsi Lambda untuk hook Anda`AfterAllowTestTraffic`. 

   ```
   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
   ```

1.  Catat ARN fungsi Lambda Anda dalam tanggapannya. `create-function` Anda menggunakan ARN ini saat memperbarui AppSpec file CodeDeploy penerapan di langkah berikutnya. 