

サポート終了通知: 2025 年 9 月 15 日、 AWS は Amazon Lex V1 のサポートを終了します。 V1 2025 年 9 月 15 日を過ぎると、Amazon Lex V1 コンソールまたは Amazon Lex V1 リソースにはアクセスできなくなります。Amazon Lex V2 を使用している場合は、代わりに [Amazon Lex V2 ガイド](https://docs.aws.amazon.com/lexv2/latest/dg/what-is.html)を参照してください。

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

# ステップ 1: Lambda 関数を作成する
<a name="gs2-prepare"></a>

最初に、ピザの注文を達成する Lambda 関数を作成します。この関数は、次のセクションで作成する Amazon Lex ボットで指定します。

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



1. にサインイン AWS マネジメントコンソール し、[https://console.aws.amazon.com/lambda/](https://console.aws.amazon.com/lambda/) で AWS Lambda コンソールを開きます。

1. [**Create function**] を選択します。

1. [**Create function**] ページで、[**Author from scratch**] を選択します。

   この演習では、事前に用意されたカスタムコードを使用して Lambda 関数を作成します。したがって、最初から関数を作成するオプションを選択します。

   以下の操作を実行します。

   1. 名前 (`PizzaOrderProcessor`) を入力します。

   1. [**ランタイム**] で、最新バージョンの Node.js を選択します。

   1. [**Role**] で、[**Create a new role from template(s)**] を選択します。

   1. 新しいロール名 (`PizzaOrderProcessorRole`) を入力します。

   1. [**関数の作成**] を選択してください。

1. [関数] ページで、以下の作業を行います。

   [**Function code**] セクションで、[**Edit code inline**] を選択し、次の Node.js 関数コードをコピーしてウィンドウに貼り付けます。

   ```
   'use strict';
        
   // Close dialog with the customer, reporting fulfillmentState of Failed or Fulfilled ("Thanks, your pizza will arrive in 20 minutes")
   function close(sessionAttributes, fulfillmentState, message) {
       return {
           sessionAttributes,
           dialogAction: {
               type: 'Close',
               fulfillmentState,
               message,
           },
       };
   }
    
   // --------------- Events -----------------------
    
   function dispatch(intentRequest, callback) {
       console.log(`request received for userId=${intentRequest.userId}, intentName=${intentRequest.currentIntent.name}`);
       const sessionAttributes = intentRequest.sessionAttributes;
       const slots = intentRequest.currentIntent.slots;
       const crust = slots.crust;
       const size = slots.size;
       const pizzaKind = slots.pizzaKind;
       
       callback(close(sessionAttributes, 'Fulfilled',
       {'contentType': 'PlainText', 'content': `Okay, I have ordered your ${size} ${pizzaKind} pizza on ${crust} crust`}));
       
   }
    
   // --------------- Main handler -----------------------
    
   // Route the incoming request based on intent.
   // The JSON body of the request is provided in the event slot.
   export const handler = (event, context, callback) => {
       try {
           dispatch(event,
               (response) => {
                   callback(null, response);
               });
       } catch (err) {
           callback(err);
       }
   };
   ```

1. **[保存]** を選択します。

## サンプルイベントデータを使用した Lambda 関数のテスト
<a name="gs2-lambdafunction-test"></a>

コンソールで、サンプルイベントデータを使用して手動で Lambda 関数を呼び出すことで、この関数をテストします。

**Lambda 関数をテストするには:**

1. にサインイン AWS マネジメントコンソール し、[https://console.aws.amazon.com/lambda/](https://console.aws.amazon.com/lambda/) で AWS Lambda コンソールを開きます。

1. **[Lambda function]** ページで、[Lambda function] (Lambda 関数) (`PizzaOrderProcessor).`) を選択します。

1. 関数のページで、テストイベントのリストから [**Configure test events**] を選択します。

1. [**Configure test event**] ページで、以下の操作を行います。

   1. **Create new test event**を選択します。

   1. [**Event name**] フィールドに、イベント名 (`PizzaOrderProcessorTest`) を入力します。

   1. 次の Amazon Lex イベントをウィンドウ内にコピーします。

      ```
      {
        "messageVersion": "1.0",
        "invocationSource": "FulfillmentCodeHook",
        "userId": "user-1",
        "sessionAttributes": {},
        "bot": {
          "name": "PizzaOrderingApp",
          "alias": "$LATEST",
          "version": "$LATEST"
        },
        "outputDialogMode": "Text",
        "currentIntent": {
          "name": "OrderPizza",
          "slots": {
            "size": "large",
            "pizzaKind": "meat",
            "crust": "thin"
          },
          "confirmationStatus": "None"
        }
      }
      ```

1. **[作成]** を選択します。

AWS Lambda はテストを作成し、関数ページに戻ります。**[Test]** (テスト) を選択すると、Lambda 関数が実行されます。

結果ボックスで、[**Details**] を選択します。コンソールの [**Execution result**] ペインに、次の出力が表示されます。

```
{
  "sessionAttributes": {},
  "dialogAction": {
    "type": "Close",
    "fulfillmentState": "Fulfilled",
    "message": {
      "contentType": "PlainText",
      "content": "Okay, I have ordered your large meat pizza on thin crust."
    }
}
```

## 次のステップ
<a name="gs2-next-step-create-bot"></a>

[ステップ 2: ボットを作成する](gs2-create-bot.md)