Open2

cfnでのLexバージョン更新

enumura1enumura1

やりたい事

Lex ボットの更新デプロイにおいて最新versionにおけるインテントの更新を行いたい

環境

Lex ボット v2

前提

  • 2,3回目のデプロイは既存スタックの更新デプロイで実施

検証

◆1回目(初回)のデプロイ
使用したテンプレートは下記の通り。

1回目デプロイ時のソース
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: "SampleDemoIntent1"
              Description: "Sample demo intent"
              SampleUtterances:
                - Utterance: "サンプルほげほげ1"
              DialogCodeHook:
                Enabled: true
            - Name: "SampleDemoIntent2"
              Description: "Sample demo intent"
              SampleUtterances:
                - Utterance: "サンプルほげほげ2"
              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

これにより下記2つのインテントが作成
・Draft version
・ Version 1

◆2回目のデプロイ

1回目のデプロイとの変更点はインテント名2つの変更のみ

Name: "SampleDemoIntent3"
Name: "SampleDemoIntent4"

使用したテンプレートは下記の通り。

2回目のデプロイ時のソース
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: "SampleDemoIntent3"
              Description: "Sample demo intent"
              SampleUtterances:
                - Utterance: "サンプルほげほげ1"
              DialogCodeHook:
                Enabled: true
            - Name: "SampleDemoIntent4"
              Description: "Sample demo intent"
              SampleUtterances:
                - Utterance: "サンプルほげほげ2"
              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

これにより下記2つのバージョンが作成
・Draft version
・ Version 1

Draft versionの方ではLex ボットのインテント名が「SampleDemoIntent3、SampleDemoIntent4」にそれぞれ更新されている。

Version 1の方だとLex ボットのインテント名が1回目のデプロイ時と同じもの(「SampleDemoIntent1、SampleDemoIntent2」)になってたまま。

◆3回目のデプロイ
下記のテンプレートを使用。

2回目との変更点は下記の通り。

  • 明示的にNew Version (2回目のデプロイ用)のバージョン(=★)を作成。
  • エイリアスのBotVersionにも★のバージョンを指定。

全体のソースとしては下記の通り。

3回目のデプロイ時のソース
  # New Version (2回目のデプロイ用)
  SampleBotNewVersion:
    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 SampleBotNewVersion.BotVersion
      SentimentAnalysisSettings:
        DetectSentiment: false
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: "SampleDemoIntent3"
              Description: "Sample demo intent"
              SampleUtterances:
                - Utterance: "サンプルほげほげ1"
              DialogCodeHook:
                Enabled: true
            - Name: "SampleDemoIntent4"
              Description: "Sample demo intent"
              SampleUtterances:
                - Utterance: "サンプルほげほげ2"
              DialogCodeHook:
                Enabled: true

  # Version
  SampleBotVersion:
    Type: AWS::Lex::BotVersion
    Properties:
      BotId: !Ref SampleBot
      BotVersionLocaleSpecification:
        - LocaleId: ja_JP
          BotVersionLocaleDetails:
            SourceBotVersion: DRAFT

  # New Version (2回目のデプロイ用)
  SampleBotNewVersion:
    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 SampleBotNewVersion.BotVersion
      SentimentAnalysisSettings:
        DetectSentiment: false

デプロイ後には下記のように新たにVersion 2ができていることを確認

version2のインテントを確認すると、下記2つのインテントが作成されてることを確認

  • SampleDemoIntent3
  • SampleDemoIntent4

エイリアスも確認すると、文言的にversion1との関連付けが解除され、version2と連携をとるようになっている。

enumura1enumura1

以前のバージョンを削除するには?

3回目のデプロイで使用したテンプレートから次の記載部分を削除し、4回目のデプロイを実施。

  # Version
  SampleBotVersion:
    Type: AWS::Lex::BotVersion
    Properties:
      BotId: !Ref SampleBot
      BotVersionLocaleSpecification:
        - LocaleId: ja_JP
          BotVersionLocaleDetails:
            SourceBotVersion: DRAFT
4回目のデプロイの時のソース
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: "SampleDemoIntent3"
              Description: "Sample demo intent"
              SampleUtterances:
                - Utterance: "サンプルほげほげ1"
              DialogCodeHook:
                Enabled: true
            - Name: "SampleDemoIntent4"
              Description: "Sample demo intent"
              SampleUtterances:
                - Utterance: "サンプルほげほげ2"
              DialogCodeHook:
                Enabled: true

  # New Version (2回目のデプロイ用)
  SampleBotNewVersion:
    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 SampleBotNewVersion.BotVersion
      SentimentAnalysisSettings:
        DetectSentiment: false

cfnのデプロイ後にLex ボットのバージョンからVersion1が消えてることを確認