0
0
お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

CloudFormationでAmazon Lexボットのコードフックを有効化する方法

Last updated at Posted at 2024-07-13

はじめに

Amazon Lexで対象のインテントが選択された際にLexからlabmbdaを呼ぶには、次の画像のようにコードフックを有効化しておく必要があります。
マネジメントコンソールからチェックボックスに✅を入れるだけで設定できますが、CloudFormationでLexのBotを作成する場合もここに✅を入れたいです。
本記事ではCloudFormationのテンプレートで任意のインテントのコードフックを有効化を実装します。

image.png

ゴール

CloudFormationのテンプレートで、任意のインテントにおけるコードフックを有効化を実装する。

対象の方

  • LexのBotの作成をCloudFormationでやってみたい方
  • CloudFormationでインテントのコードフックを有効化したい方

結論

対象のインテントにおいて DialogCodeHook のEnabledをtrueにすることでコードフックの有効化ができます。

DialogCodeHook:
  Enabled: true

Botを作成するテンプレート

次のテンプレートではIAMロールを設定して、「SampleDemoIntent」と「FallbackIntent」の2つのインテントで構成されたBotを作成しています。
FallbackIntentではコードフックを有効化していませんが、SampleDemoIntentでは、DialogCodeHookを Enabled: true で有効化しています。

lex-bot-template.yaml
AWSTemplateFormatVersion: 2010-09-09

Parameters:
  BotName:
    Type: String
    Default: "SampleBot"

Resources:
  # IAM Role
  BotRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lexv2.amazonaws.com
            Action:
              - "sts:AssumeRole"
      Path: "/"
      Policies:
        - PolicyName: LexRuntimeRolePolicy
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - "polly:SynthesizeSpeech"
                  - "comprehend:DetectSentiment"
                Resource: "*"

  # Lex Bot
  SampleBot:
    Type: AWS::Lex::Bot
    Properties:
      Name: !Ref BotName
      RoleArn: !GetAtt BotRole.Arn
      DataPrivacy:
        ChildDirected: false
      IdleSessionTTLInSeconds: 300
      Description: "Bot with Fallback and SampleDemo intents"
      AutoBuildBotLocales: false
      BotLocales:
        - LocaleId: "ja_JP"
          Description: "Japanese locale"
          NluConfidenceThreshold: 0.40
          Intents:
            - Name: "FallbackIntent"
              Description: "Default intent when no other intent matches"
              ParentIntentSignature: "AMAZON.FallbackIntent"
              DialogCodeHook:
                Enabled: false
            - Name: "SampleDemoIntent"
              Description: "Sample demo intent"
              SampleUtterances:
                - Utterance: "サンプルほげほげ"
              DialogCodeHook:
                Enabled: true
                
  # Version
  SampleBotVersion:
    Type: AWS::Lex::BotVersion
    Properties:
      BotId: !Ref SampleBot
      BotVersionLocaleSpecification:
        - LocaleId: ja_JP
          BotVersionLocaleDetails:
            SourceBotVersion: DRAFT

  # Alias
  BotAlias:
    Type: AWS::Lex::BotAlias
    Properties:
      BotId: !Ref SampleBot
      BotAliasName: "SampleBotAlias"
      BotVersion: !GetAtt SampleBotVersion.BotVersion
      SentimentAnalysisSettings:
        DetectSentiment: false

デプロイしてみる

SampleDemoIntent の方ではコードフックが有効化されている事が確認できます。

image.png

FallbackIntent の方は、DialogCodeHookを記載していないのでコードフックは有効化されていない事が確認できました。

image.png

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0