はじめに
Amazon Lexで対象のインテントが選択された際にLexからlabmbdaを呼ぶには、次の画像のようにコードフックを有効化しておく必要があります。
マネジメントコンソールからチェックボックスに✅を入れるだけで設定できますが、CloudFormationでLexのBotを作成する場合もここに✅を入れたいです。
本記事ではCloudFormationのテンプレートで任意のインテントのコードフックを有効化を実装します。
ゴール
CloudFormationのテンプレートで、任意のインテントにおけるコードフックを有効化を実装する。
対象の方
- LexのBotの作成をCloudFormationでやってみたい方
- CloudFormationでインテントのコードフックを有効化したい方
結論
対象のインテントにおいて DialogCodeHook のEnabledをtrue
にすることでコードフックの有効化ができます。
DialogCodeHook:
Enabled: true
Botを作成するテンプレート
次のテンプレートではIAMロールを設定して、「SampleDemoIntent」と「FallbackIntent」の2つのインテントで構成されたBotを作成しています。
FallbackIntentではコードフックを有効化していませんが、SampleDemoIntentでは、DialogCodeHookを Enabled: true
で有効化しています。
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 の方ではコードフックが有効化されている事が確認できます。
FallbackIntent の方は、DialogCodeHookを記載していないのでコードフックは有効化されていない事が確認できました。
参考