AWS CodeStar 東京リージョンでAlexaスキルのテンプレートが選択できるようになったので試してみる

2018.12.24

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、Mr.Moです。

AWS CodeStar 東京リージョンでもAlexaスキルのプロジェクトテンプレートが選択できるようになったみたいなので試していきたいと思います。
以前から米国リージョンなどではAlexaスキルのプロジェクトテンプレートが選択できていました。(実は裏技を使えば東京リージョンでも使えていた。。)
今回のUPDATEをうけて色々内容が変わっているようですので、新旧にどのような違いがあるのかを意識しながら見ていきたいと思います。

https://aws.amazon.com/jp/about-aws/whats-new/2018/12/quickly-create-build-and-deploy-amazon-alexa-skills-from-aws/

AWS CodeStar now lets you automatically create a new Alexa Skill from within your Amazon Web Services (AWS) account. Previously, you needed to create the skill in the Alexa Skill console and link it with an AWS Lambda function in your AWS account. You would also need to manually register changes any time you updated your skill. These manual steps are no longer needed if you develop your Alexa Skills with CodeStar. To get started with Alexa skill templates on AWS CodeStar, go to our getting started guide to follow step by step instructions.

Additionally, both AWS CloudFormation and AWS CodePipeline now support Alexa Skills. This enables you to customize your deployment configurations with CloudFormation or utilize CodePipeline to bring continuous delivery to your development process. Visit our documentation to learn more about developing and deploying Alexa skills with AWS CodeStar, CloudFormation, and CodePipeline.

Alexa Skill templates in AWS CodeStar are available in the Asia Pacific (Tokyo), EU (Ireland), US East (N. Virginia) and US West (Oregon) regions.

さっそくCodeStarで新規プロジェクト作成

画面左のフィルター欄にAlexaスキルのチェックボックスが追加されていますね。フィルターをかけると「Hellow World Skill」のプロジェクトテンプレートが用意されているのが分かります。米国リージョンでは以前はFactスキルのテンプレートなど他のテンプレートも用意されていたと記憶しています。
今回はNode.jsの「Hellow World Skill」プロジェクトテンプレートを選択します。

CodeStar プロジェクト詳細設定画面

赤枠の部分をご覧ください。以前には無かったAmazon Developerアカウントとの連携を実施するようになっていますね。
「Connect Amazon Developer Account」ボタンを押すと認証を求められます。認証が上手く行けば下記の赤枠の状態になるはずです。

あとは特別な設定は出てこないので画面の指示に従い先に進めていきましょう。
CodeStarの新規プロジェクト作成が終わると、開発に必要なリソースを自動作成しにいってくれるのですがその中でも今回は下記を中心に見ていきます。

  • GitHubのリポジトリ
  • CodePipeline
  • Lambda
  • Alexa Developer Console

GitHubのリポジトリ

CodeStarは新規プロジェクト作成時に設定したGitHubのリポジトリに対して、自動的にテンプレートのプロジェクトを配置してくれます。 配置されたテンプレートに新旧で違いがありそうですので記載してみます。

パッと見のファイル構造がだいぶ変わっていますね。実は以前はAlexa SDK V1を使用したテンプレートが配置されていました。新しい方はもちろんV2になっています。 また、CloudFormationの設定ファイルが大きく変わっているようで。

  • 旧(template.yml)
AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
- AWS::CodeStar

Parameters:
  ProjectId:
    Type: String
    Description: AWS CodeStar projectID used to associate new resources to team members

# Enable blue/green deployments using this Globals section. For instructions, see the AWS CodeStar User Guide:
# https://docs.aws.amazon.com/codestar/latest/userguide/how-to-modify-serverless-project.html?icmpid=docs_acs_rm_tr
#
# Globals:
#   Function:
#     AutoPublishAlias: live
#     DeploymentPreference:
#       Enabled: true
#       Type: Canary10Percent5Minutes

Resources:
  AlexaSkillFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs8.10
      Role:
        Fn::ImportValue:
          !Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]
      Events:
        AlexaSkillEvent:
          Type: AlexaSkill
  • 新(cfn-template.yml)
AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
- AWS::CodeStar

Parameters:
  ProjectId:
    Type: String
    Description: AWS CodeStar project ID.
  CodeDeployRole:
    Type: String
    Description: IAM role to allow AWS CodeDeploy to manage deployment of AWS Lambda functions
  Stage:
    Type: String
    Description: The name for a project pipeline stage, such as Staging or Prod, for which resources are provisioned and deployed.
    Default: ''

Globals:
  Function:
    AutoPublishAlias: live
    DeploymentPreference:
      Enabled: true
      Type: Canary10Percent5Minutes
      Role: !Ref CodeDeployRole

Resources:
  CustomDefaultFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: 'lambda/custom'
      Handler: index.handler
      Runtime: nodejs8.10
      Role: !GetAtt LambdaExecutionRole.Arn
      Events:
        AlexaSkillEvent:
          Type: AlexaSkill
  LambdaExecutionRole:
    Description: Creating service role in IAM for AWS Lambda
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Sub 'CodeStar-${ProjectId}-Execution${Stage}'
      AssumeRolePolicyDocument:
        Statement:
        - Effect: Allow
          Principal:
            Service: [lambda.amazonaws.com]
          Action: sts:AssumeRole
      Path: /
      ManagedPolicyArns:
        -  arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
      PermissionsBoundary: !Sub 'arn:${AWS::Partition}:iam::${AWS::AccountId}:policy/CodeStar_${ProjectId}_PermissionsBoundary'

Outputs:
  overrides:
      Value: !Sub |-
        {
          "manifest": {
            "apis": {
              "custom": {
                "endpoint": {
                  "uri": "${CustomDefaultFunction.Alias}"
                }
              }
            }
          }
        }

このあたりの設定の違いも日を改めてじっくり見たいところです。今はクリスマスな時期もあって時間がありません次に進みましょう 笑。

CodePipeline

GitHubのリポジトリに自動的にテンプレートが配置されていたように、CodePipelineにもCodeStarによって自動的に設定が完了されています。 ちょっと気になる点を見つけてしまったので下記に記載します。

  • なんか追加されている

なんか追加されています。突然すみません、でも以前は無かった設定です。この追加された「DeploySkillPackage」をもう少し詳しく見てみましょう。

  • DeploySkillPackageの詳細

なるほど、CodePipelineでAlexa Developer Consoleに設定を自動アップロードできるようになったんですね。Alexa Developer Consoleにアップロードされる設定はGitHubリポジトリに配置された下記のファイルになります。

  • alexa-new-cs/interactionModels/custom/en-US.json
  • alexa-new-cs/skill.json

デフォルトでは英語版の設定しか入っていませんが、ja-JP.jsonファイルを作成し、skill.jsonに日本語版の設定をいれればAlexa Developer Console上のスキルに日本語の設定が反映されます。(後ほど日本語の設定を記載したいと思います。)
Alexa Developer Consoleの状態を見てみましょう。

  • Alexa Developer Console

今回のスキルが追加されています。Alexa Developer Consoleとの連携が簡単にできるようになったのは大きいですね。

Lambda

Lambdaへの自動デプロイは以前から提供されていました。簡単に現状を見てみましょう。

  • Lambda画面

パッと見以前との違いはあまり無いようです。エイリアスの設定をする思想が組み込まれているようには見えます。

スキルに日本語の設定を入れて動かしてみる

さっそく簡単に日本語の設定を入れて動かしてみましょう。
まず「alexa-new-cs/interactionModels/custom/ja-JP.json」のファイルを追加します。

  • alexa-new-cs/interactionModels/custom/ja-JP.json
{
    "interactionModel": {
        "languageModel": {
            "invocationName": "メリクリスキルテスト",
            "intents": [
                {
                    "name": "AMAZON.CancelIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.HelpIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.StopIntent",
                    "samples": []
                },
                {
                    "name": "HelloWorldIntent",
                    "slots": [],
                    "samples": [
                        "メリークリスマス"
                    ]
                }
            ],
            "types": []
        }
    }
}

次にskill.jsonに日本語の設定を入れましょう。

  • skill.json
{
  "manifest": {
    "publishingInformation": {
      "locales": {
        "en-US": {
          "summary": "A basic skill made from Alexa Skills Kit template",
          "examplePhrases": [
            "Alexa open hello node",
            "Alexa tell hello node hello",
            "Alexa ask hello node say hello"
          ],
          "name": "hello node",
          "description": "A basic skill made from Alexa Skills Kit template"
        },
        "ja-JP": {
          "summary": "メリクリスキルテストです",
          "examplePhrases": [
            "アレクサ メリクリスキルテストを開いて"
          ],
          "name": "メリクリスキルテスト",
          "description": "メリクリスキルテストの説明です"
        }
      },
      "isAvailableWorldwide": true,
      "testingInstructions": "Sample Testing Instructions.",
      "category": "EDUCATION_AND_REFERENCE",
      "distributionCountries": []
    },
    "apis": {
      "custom": {
      }
    },
    "manifestVersion": "1.0"
  }
}

準備は整いました。それではおもむろにシュミレーターでテストを実行してみましょう。

無事動きましたね!

まとめ

注意しないといけなそうなのはAlexa Developer Console上で修正などの作業をよくすると思いますが、CodePipelineによる設定自動アップロードが動くとGitHubリポジトリ上にある設定ファイルで設定が上書きされてしまうので、Alexa Developer Console上で行った作業が消えてしまう恐れがある点です。この辺り検証して上手くにつきあえばCodeStarを使うとAlexaスキル開発はすごく楽になりそうですね。