

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# ステップ 1: インフラストラクチャをセットアップする
<a name="tutorial-lambda-sam-setup-infrastructure"></a>

 このトピックでは、 AWS SAM を使用して AWS SAM テンプレートと Lambda 関数のファイルを作成する方法について説明します。次に、 コマンド AWS SAM `package`と `deploy` コマンドを使用して、インフラストラクチャ内のコンポーネントを生成します。インフラストラクチャの準備ができたら、CodeDeploy アプリケーションとデプロイグループ、更新およびデプロイする Lambda 関数、Lambda 関数をデプロイするときに実行される検証テストを含む 2 つの Lambda 関数があります。完了したら、 CloudFormation を使用して Lambda コンソールまたは でコンポーネントを表示 AWS CLI し、Lambda 関数をテストできます。

**Topics**
+ [ファイルを作成する](tutorial-lambda-create-files.md)
+ [SAM AWS アプリケーションをパッケージ化する](tutorial-lambda-sam-package.md)
+ [SAM AWS アプリケーションをデプロイする](tutorial-lambda-sam-deploy.md)
+ [(オプション) インフラストラクチャの検査とテスト](tutorial-lambda-sam-confirm-components.md)

# ファイルを作成する
<a name="tutorial-lambda-create-files"></a>

 インフラストラクチャを作成するには、次のファイルを作成する必要があります。
+ `template.yml`
+ `myDateTimeFunction.js`
+ `beforeAllowTraffic.js`
+ `afterAllowTraffic.js`

**Topics**
+ [AWS SAM テンプレートを作成する](tutorial-lambda-sam-template.md)
+ [Lambda 関数のファイルを作成します](tutorial-lambda-sam-create-lambda-function.md)
+ [[BeforeAllowTraffic] Lambda 関数のファイルを作成する](tutorial-lambda-sam-create-lambda-before-traffic.md)
+ [[AfterAllowTraffic] Lambda 関数のファイルを作成する](tutorial-lambda-sam-create-lambda-after-traffic.md)

# AWS SAM テンプレートを作成する
<a name="tutorial-lambda-sam-template"></a>

インフラストラクチャ内のコンポーネントを指定する AWS SAM テンプレートファイルを作成します。

**AWS SAM テンプレートを作成するには**

1.  `SAM-Tutorial` という名前のディレクトリを作成します。

1.  `SAM-Tutorial` ディレクトリに `template.yml` という名前のファイルを作成します。

1.  次の YAML コードを `template.yml` にコピーします。これが使用する AWS SAM テンプレートです。

   ```
   AWSTemplateFormatVersion : '2010-09-09'
   Transform: AWS::Serverless-2016-10-31
   Description: A sample SAM template for deploying Lambda functions.
   
   Resources:
   # Details about the myDateTimeFunction Lambda function
     myDateTimeFunction:
       Type: AWS::Serverless::Function
       Properties:
         Handler: myDateTimeFunction.handler
         Runtime: nodejs18.x
   # Instructs your myDateTimeFunction is published to an alias named "live".      
         AutoPublishAlias: live
   # Grants this function permission to call lambda:InvokeFunction
         Policies:
           - Version: "2012-10-17"		 	 	 
             Statement: 
             - Effect: "Allow"
               Action: 
                 - "lambda:InvokeFunction"
               Resource: '*'
         DeploymentPreference:
   # Specifies the deployment configuration      
             Type: Linear10PercentEvery1Minute
   # Specifies Lambda functions for deployment lifecycle hooks
             Hooks:
               PreTraffic: !Ref beforeAllowTraffic
               PostTraffic: !Ref afterAllowTraffic
               
   # Specifies the BeforeAllowTraffic lifecycle hook Lambda function
     beforeAllowTraffic:
       Type: AWS::Serverless::Function
       Properties:
         Handler: beforeAllowTraffic.handler
         Policies:
           - Version: "2012-10-17"		 	 	 
   # Grants this function permission to call codedeploy:PutLifecycleEventHookExecutionStatus        
             Statement: 
             - Effect: "Allow"
               Action: 
                 - "codedeploy:PutLifecycleEventHookExecutionStatus"
               Resource:
                 !Sub 'arn:aws:codedeploy:${AWS::Region}:${AWS::AccountId}:deploymentgroup:${ServerlessDeploymentApplication}/*'
           - Version: "2012-10-17"		 	 	 
   # Grants this function permission to call lambda:InvokeFunction        
             Statement: 
             - Effect: "Allow"
               Action: 
                 - "lambda:InvokeFunction"
               Resource: !Ref myDateTimeFunction.Version
         Runtime: nodejs18.x
   # Specifies the name of the Lambda hook function      
         FunctionName: 'CodeDeployHook_beforeAllowTraffic'
         DeploymentPreference:
           Enabled: false
         Timeout: 5
         Environment:
           Variables:
             NewVersion: !Ref myDateTimeFunction.Version
             
   # Specifies the AfterAllowTraffic lifecycle hook Lambda function
     afterAllowTraffic:
       Type: AWS::Serverless::Function
       Properties:
         Handler: afterAllowTraffic.handler
         Policies:
           - Version: "2012-10-17"		 	 	 
             Statement: 
   # Grants this function permission to call codedeploy:PutLifecycleEventHookExecutionStatus         
             - Effect: "Allow"
               Action: 
                 - "codedeploy:PutLifecycleEventHookExecutionStatus"
               Resource:
                 !Sub 'arn:aws:codedeploy:${AWS::Region}:${AWS::AccountId}:deploymentgroup:${ServerlessDeploymentApplication}/*'
           - Version: "2012-10-17"		 	 	 
             Statement: 
   # Grants this function permission to call lambda:InvokeFunction          
             - Effect: "Allow"
               Action: 
                 - "lambda:InvokeFunction"
               Resource: !Ref myDateTimeFunction.Version
         Runtime: nodejs18.x
   # Specifies the name of the Lambda hook function      
         FunctionName: 'CodeDeployHook_afterAllowTraffic'
         DeploymentPreference:
           Enabled: false
         Timeout: 5
         Environment:
           Variables:
             NewVersion: !Ref myDateTimeFunction.Version
   ```

このテンプレートは以下を指定します。詳細については、[AWS SAM template concepts (テンプレートの概念)](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-template-basics.html) を参照してください。

**`myDateTimeFunction` と呼ばれるLambda 関数**  
 この Lambda 関数が発行されると、テンプレート中の `AutoPublishAlias` の行は、それを `live` という名前のエイリアスにリンクします。このチュートリアルの後半で、この関数の更新により、本番トラフィックを元のバージョンから更新されたバージョンに段階的に移行 AWS CodeDeploy する によるデプロイがトリガーされます。

**2 つの Lambda デプロイ検証関数**  
 次の Lambda 関数は、CodeDeploy ライフサイクルフック中に実行されます。関数には、更新された `myDateTimeFunction` のデプロイを検証するコードが含まれています。検証テストの結果は、`PutLifecycleEventHookExecutionStatus` のAPI メソッドを使用して CodeDeploy に渡されます。検証テストが失敗すると、デプロイは失敗し、ロールバックされます。  
+  `CodeDeployHook_beforeAllowTraffic` は、`BeforeAllowTraffic` フック中に実行します。
+  `CodeDeployHook_afterAllowTraffic` は、`AfterAllowTraffic` フック中に実行します。
両方の関数の名前は `CodeDeployHook_` で始まります。`CodeDeployRoleForLambda` のロールは、このプレフィックスで始まる名前の Lambda 関数でのみ、Lambda の `invoke` のメソッドへの呼び出しを許可します。詳細については、[AWS Lambda デプロイの AppSpec 'hooks' セクション](reference-appspec-file-structure-hooks.md#appspec-hooks-lambda) および *CodeDeploy API Reference* 中の [PutLifecycleEventHookExecutionStatus](https://docs.aws.amazon.com/codedeploy/latest/APIReference/API_PutLifecycleEventHookExecutionStatus.html) を参照してください。

**更新された Lambda 関数の自動検出**  
 `AutoPublishAlias` 条件は、`myDateTimeFunction` 関数が変更されたときに検出し、`live` エイリアスを使用してデプロイするようにフレームワークに指示します。

**デプロイ設定**  
 デプロイ設定により、CodeDeploy アプリケーションが Lambda 関数の元のバージョンから新しいバージョンにトラフィックを移行するレートが決まります。このテンプレートは、事前定義されたデプロイ設定 `Linear10PercentEvery1Minute` を指定します。  
 AWS SAM テンプレートでカスタムデプロイ設定を指定することはできません。詳細については、「[CodeDeploy を使用してデプロイ設定を作成する](deployment-configurations-create.md)」を参照してください。

**デプロイライフサイクルフック関数**  
 `Hooks` セクションでは、ライフサイクルイベントフック中に実行する関数を指定します。`PreTraffic` は、`PostTraffic` フック中に実行する関数を指定します。`BeforeAllowTraffic` は、フック中に `AfterAllowTraffic` が実行する関数を指定します。

**Lambda が別の Lambda 関数を呼び出すための許可**  
 指定された`lambda:InvokeFunction`アクセス許可は、SAM AWS アプリケーションで使用されるロールに Lambda 関数を呼び出すアクセス許可を付与します。これは、`CodeDeployHook_beforeAllowTraffic` および `CodeDeployHook_afterAllowTraffic` の関数が、検証テスト中に、デプロイされた Lambda 関数を呼び出す場合に必要です。

# Lambda 関数のファイルを作成します
<a name="tutorial-lambda-sam-create-lambda-function"></a>

このチュートリアルの後半で更新してデプロイする関数のファイルを作成します。

**注記**  
 Lambda 関数は、 AWS Lambdaでサポートされている任意のランタイムを使用できます。詳細については、「[AWS Lambda ランタイム](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html)」を参照してください。

**Lambda 関数を作成するには**

1.  テキストファイルを作成し、`myDateTimeFunction.js` という名前で `SAM-Tutorial` ディレクトリに保存します。

1.  次の Node.js コードを `myDateTimeFunction.js` にコピーします。

   

   ```
   'use strict';
       
       exports.handler = function(event, context, callback) {
       
         if (event.body) {
           event = JSON.parse(event.body);
         }
       
         var sc; // Status code
         var result = ""; // Response payload
       
         switch(event.option) {
           case "date": 
             switch(event.period) {
               case "yesterday":
                 result = setDateResult("yesterday");
                 sc = 200;
                 break;
               case "today":
                 result = setDateResult();
                 sc = 200;
                 break;
               case "tomorrow":
                 result = setDateResult("tomorrow");
                 sc = 200;
                 break;
               default:
                 result = {
                   "error": "Must specify 'yesterday', 'today', or 'tomorrow'."
                 };
                 sc = 400;
                 break;
             }
             break;
             
       /*      Later in this tutorial, you update this function by uncommenting 
               this section. The framework created by AWS SAM detects the update 
               and triggers a deployment by CodeDeploy. The deployment shifts 
               production traffic to the updated version of this function.
               
               case "time":
               var d = new Date();
               var h = d.getHours();
               var mi = d.getMinutes();
               var s = d.getSeconds();
       
               result = {
                 "hour": h,
                 "minute": mi,
                 "second": s
               };
               sc = 200;
               break;
       */
             default:
               result = {
                 "error": "Must specify 'date' or 'time'."
               };
               sc = 400;
             break;
         }
       
         const response = {
           statusCode: sc,
           headers: { "Content-type": "application/json" },
           body: JSON.stringify( result )
         };
       
         callback(null, response);
       
         function setDateResult(option) {
       
           var d = new Date(); // Today
           var mo; // Month
           var da; // Day
           var y; // Year
       
           switch(option) {
             case "yesterday":
               d.setDate(d.getDate() - 1);
               break;
             case "tomorrow":
               d.setDate(d.getDate() + 1);
             default:
              break;
           }
       
           mo = d.getMonth() + 1; // Months are zero offset (0-11)
           da = d.getDate();
           y = d.getFullYear();
       
           result = {
             "month": mo,
             "day": da,
             "year": y
           };
       
           return result;
         }
       };
   ```

Lambda 関数は、昨日、今日、または明日に対して年月日を返します。このチュートリアルの後半では、指定した日または時間に関する情報(年月日または現在の時、分、秒など)を返す関数を更新するコードをコメント解除します。によって作成されたフレームワークは、関数の更新バージョン AWS SAM を検出してデプロイします。

**注記**  
 この Lambda 関数は、 AWS Cloud9 チュートリアルでも使用されます。 はクラウドベースの統合開発環境 AWS Cloud9 です。でこの関数を作成、実行、更新、デバッグする方法については AWS Cloud9、[AWS Lambda 「」のチュートリアル AWS Cloud9](https://docs.aws.amazon.com/cloud9/latest/user-guide/tutorial-lambda.html)を参照してください。

# [BeforeAllowTraffic] Lambda 関数のファイルを作成する
<a name="tutorial-lambda-sam-create-lambda-before-traffic"></a>

`beforeAllowTraffic` のフック Lambda 関数のファイルを作成します。

1.  テキストファイルを作成し、`beforeAllowTraffic.js` という名前で `SAM-Tutorial` ディレクトリに保存します。

1.  次の Node.js コードを `beforeAllowTraffic.js` にコピーします。この関数は、デプロイの `BeforeAllowTraffic` フック中に実行されます。

   ```
   'use strict';
       
       const AWS = require('aws-sdk'); 
       const codedeploy = new AWS.CodeDeploy({apiVersion: '2014-10-06'});
       var lambda = new AWS.Lambda();
       
       exports.handler = (event, context, callback) => {
       
       	console.log("Entering PreTraffic Hook!");
       	
       	// Read the DeploymentId and LifecycleEventHookExecutionId from the event payload
         var deploymentId = event.DeploymentId;
       	var lifecycleEventHookExecutionId = event.LifecycleEventHookExecutionId;
       
       	var functionToTest = process.env.NewVersion;
       	console.log("BeforeAllowTraffic hook tests started");
       	console.log("Testing new function version: " + functionToTest);
       
       	// Create parameters to pass to the updated Lambda function that
       	// include the newly added "time" option. If the function did not
       	// update, then the "time" option is invalid and function returns
       	// a statusCode of 400 indicating it failed.
       	var lambdaParams = {
       		FunctionName: functionToTest,    
       		Payload: "{\"option\": \"time\"}", 
       		InvocationType: "RequestResponse"
       	};
       
       	var lambdaResult = "Failed";
       	// Invoke the updated Lambda function.
       	lambda.invoke(lambdaParams, function(err, data) {
       		if (err){	// an error occurred
       			console.log(err, err.stack);
       			lambdaResult = "Failed";
       		}
       		else{	// successful response
       			var result = JSON.parse(data.Payload);
       			console.log("Result: " +  JSON.stringify(result));
             console.log("statusCode: " + result.statusCode);
             
             // Check if the status code returned by the updated
             // function is 400. If it is, then it failed. If 
             // is not, then it succeeded.
       			if (result.statusCode != "400"){
               console.log("Validation succeeded");
       				lambdaResult = "Succeeded";
             }
             else {
               console.log("Validation failed");
             }
       
       			// Complete the PreTraffic Hook by sending CodeDeploy the validation status
       			var params = {
       				deploymentId: deploymentId,
       				lifecycleEventHookExecutionId: lifecycleEventHookExecutionId,
       				status: lambdaResult // 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("CodeDeploy Status update failed");
       					console.log(err, err.stack);
       					callback("CodeDeploy Status update failed");
       				} else {
       					// Validation succeeded.
       					console.log("CodeDeploy status updated successfully");
       					callback(null, "CodeDeploy status updated successfully");
       				}
       			});
       		}  
       	});
       }
   ```

# [AfterAllowTraffic] Lambda 関数のファイルを作成する
<a name="tutorial-lambda-sam-create-lambda-after-traffic"></a>

`afterAllowTraffic` のフック Lambda 関数のファイルを作成します。

1.  テキストファイルを作成し、`afterAllowTraffic.js` という名前で `SAM-Tutorial` ディレクトリに保存します。

1.  次の Node.js コードを `afterAllowTraffic.js` にコピーします。この関数は、デプロイの `AfterAllowTraffic` フック中に実行されます。

   ```
   'use strict';
       
       const AWS = require('aws-sdk');
       const codedeploy = new AWS.CodeDeploy({apiVersion: '2014-10-06'});
       var lambda = new AWS.Lambda();
       
       exports.handler = (event, context, callback) => {
       
       	console.log("Entering PostTraffic Hook!");
       	
       	// Read the DeploymentId and LifecycleEventHookExecutionId from the event payload
         var deploymentId = event.DeploymentId;
       	var lifecycleEventHookExecutionId = event.LifecycleEventHookExecutionId;
       
       	var functionToTest = process.env.NewVersion;
       	console.log("AfterAllowTraffic hook tests started");
       	console.log("Testing new function version: " + functionToTest);
       
       	// Create parameters to pass to the updated Lambda function that
       	// include the original "date" parameter. If the function did not 
       	// update as expected, then the "date" option might be invalid. If 
       	// the parameter is invalid, the function returns
       	// a statusCode of 400 indicating it failed.
       	var lambdaParams = {
       		FunctionName: functionToTest,    
       		Payload: "{\"option\": \"date\", \"period\": \"today\"}", 
       		InvocationType: "RequestResponse"
       	};
       
       	var lambdaResult = "Failed";
       	// Invoke the updated Lambda function.
       	lambda.invoke(lambdaParams, function(err, data) {
       		if (err){	// an error occurred
       			console.log(err, err.stack);
       			lambdaResult = "Failed";
       		}
       		else{	// successful response
       			var result = JSON.parse(data.Payload);
       			console.log("Result: " +  JSON.stringify(result));
             console.log("statusCode: " + result.statusCode);
             
             // Check if the status code returned by the updated
             // function is 400. If it is, then it failed. If 
             // is not, then it succeeded.
       			if (result.statusCode != "400"){
               console.log("Validation of time parameter succeeded");
       				lambdaResult = "Succeeded";
             }
             else {
               console.log("Validation failed");
             }
       
       			// Complete the PostTraffic Hook by sending CodeDeploy the validation status
       			var params = {
       				deploymentId: deploymentId,
       				lifecycleEventHookExecutionId: lifecycleEventHookExecutionId,
       				status: lambdaResult // 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("CodeDeploy Status update failed");
       					console.log(err, err.stack);
       					callback("CodeDeploy Status update failed");
       				} else {
       					// Validation succeeded.
       					console.log("CodeDeploy status updated successfully");
       					callback(null, "CodeDeploy status updated successfully");
       				}
       			});
       		}  
       	});
       }
   ```

# SAM AWS アプリケーションをパッケージ化する
<a name="tutorial-lambda-sam-package"></a>

 これで、`SAM-Tutorial` ディレクトリに次の 4 つのファイルがあるはずです。
+ `beforeAllowTraffic.js`
+ `afterAllowTraffic.js`
+ `myDateTimeFunction.js`
+ `template.yml`

 これで、 AWS SAM **sam package** コマンドを使用して、Lambda 関数と CodeDeploy アプリケーションのアーティファクトを作成してパッケージ化する準備ができました。アーティファクトは S3 バケットにアップロードされます。コマンドの出力は、`package.yml` という新しいファイルです。このファイルは、次のステップで AWS SAM **sam deploy** コマンドによって使用されます。

**注記**  
 **sam package** のコマンドのさらなる詳細については、*AWS SAM デベロッパーガイド* の [AWS Serverless Application Model CLI command reference](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-command-reference.html) を参照してください。

 `SAM-Tutorial` ディレクトリで、以下を実行します。

```
sam package \
  --template-file template.yml \
  --output-template-file package.yml \
  --s3-bucket amzn-s3-demo-bucket
```

`s3-bucket` のパラメータには、このチュートリアルの前提条件として作成した Amazon S3 バケットを指定します。は、 AWS SAM **sam deploy** コマンドで使用される新しいファイルの名前`output-template-file`を指定します。

# SAM AWS アプリケーションをデプロイする
<a name="tutorial-lambda-sam-deploy"></a>

 `package.yml` ファイルで AWS SAM **sam deploy** コマンドを使用して、 を使用して Lambda 関数と CodeDeploy アプリケーションおよびデプロイグループを作成します CloudFormation。

**注記**  
**sam deploy** のコマンドのさらなる詳細については、*AWS Serverless Application Model デベロッパーガイド* の [AWS SAM CLI command reference](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-command-reference.html) を参照してください。

 `SAM-Tutorial` ディレクトリで、次のコマンドを実行します。

```
sam deploy \
  --template-file package.yml \
  --stack-name my-date-time-app \
  --capabilities CAPABILITY_IAM
```

 IAM ロールの作成を `--capabilities CAPABILITY_IAM` に許可するには、 CloudFormation のパラメータが必要です。

# (オプション) インフラストラクチャの検査とテスト
<a name="tutorial-lambda-sam-confirm-components"></a>

 このトピックでは、インフラストラクチャコンポーネントを表示し、Lambda 関数をテストする方法を示します。

**`sam deploy` の実行後にスタックの結果を表示するには**

1. [https://console.aws.amazon.com/cloudformation](https://console.aws.amazon.com/cloudformation/) で CloudFormation コンソールを開きます。

1.  ナビゲーションペインで、[**Stacks**] を選択します。`my-date-time-app` スタックが上部に表示されます。

1.  [**イベント**] タブを選択して、完了したイベントを確認します。スタックの作成の進行中に、イベントを表示できます。スタックの作成が完了すると、すべてのスタック作成イベントを表示できます。

1.  スタックを選択した状態で、[**リソース**] を選択します。**タイプ** 列に、Lambda関数、`myDateTimeFunction` 、`CodeDeployHook_beforeAllowTraffic` および `CodeDeployHook_afterAllowTraffic` が表示されます。Lambda 関数の **Physical ID** の各列には、Lambda コンソールで関数を表示するためのリンクが含まれています。
**注記**  
 `myDateTimeFunction` Lambda 関数の名前には CloudFormation スタックの名前が付加され、識別子が追加されているため、 のようになります`my-date-time-app-myDateTimeFunction-123456ABCDEF`。

1. [https://console.aws.amazon.com/codedeploy/](https://console.aws.amazon.com/codedeploy/) で、CodeDeploy コンソールを開きます。

1.  ナビゲーションペインで [**デプロイ**] を展開し、[**アプリケーション**] を選択します。

1.  で作成された新しい CodeDeploy アプリケーションは、 で始まる名前 CloudFormation で表示されます`my-date-time-app-ServerlessDeploymentApplication`。このアプリケーションを選択します。

1.  `my-date-time-app-myDateTimeFunctionDeploymentGroup` で始まる名前のデプロイグループが表示されます。このデプロイグループを選択します。

    [**デプロイ設定**] に、[**CodeDeployDefault.LambdaLinear10PercentEvery1Minute**] が表示されます。

**(オプション) 関数をテストするには (コンソール)**

1. [https://console.aws.amazon.com/lambda/](https://console.aws.amazon.com/lambda/) で AWS Lambda コンソールを開きます。

1.  ナビゲーションペインで、`my-date-time-app-myDateTimeFunction` 関数を選択します。コンソールでは、名前に識別子が含まれているため、`my-date-time-app-myDateTimeFunction-123456ABCDEF` のようになります。

1.  **[テスト]** を選択します。

1.  [**イベント名**] にテストイベントの名前を入力します。

1.  テストイベントに以下を入力し、[**作成**] を選択します。

   ```
   {
     "option": "date",
     "period": "today"
   }
   ```

1.  **[テスト]** を選択します。テストイベントのリストには、テストイベントのみが表示されます。

    [**実行結果**] に [**成功**] と表示されます。

1.  [**実行結果**] で、[**詳細**] を展開して結果を表示します。現在の年月日が表示されます。

**(オプション) 関数をテストするには (AWS CLI)**

1.  Lambda 関数の ARN を配置します。関数を表示しているときに、Lambda コンソールの上部に表示されます。

1.  以下のコマンドを実行してください。[*your-function-arn*] を関数 ARN に置き換えます。

   ```
   aws lambda invoke \
   --function your-function-arn \
   --cli-binary-format raw-in-base64-out \
   --payload "{\"option\": \"date\", \"period\": \"today\"}" out.txt
   ```

1.  `out.txt` を開き、結果に現在の年月日が含まれていることを確認します。