AWSCognito User PoolServerless FW

CloudFormationとServerless FrameworkでCognito User PoolとApp Clientsを作成する

前回の記事では、User Poolを作成して終わりでした。 しかしSPAなどで実際に使用する場合には、外部からUser PoolにアクセスするためのApp Clientを作成する必要があります。 このApp Client […]

広告ここから
広告ここまで

前回の記事では、User Poolを作成して終わりでした。
しかしSPAなどで実際に使用する場合には、外部からUser PoolにアクセスするためのApp Clientを作成する必要があります。

このApp ClientもCloudFormationで定義できますので、早速やっていきましょう。

完成品

今回も完成品から。
UserPoolと、App Clientを1つずつ作成します。

---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'WordPress: fault tolerant and scalable, a cloudonaut.io template'
Resources:
  UserPool:
    Type: "AWS::Cognito::UserPool"
    Properties:
      AdminCreateUserConfig:
          AllowAdminCreateUserOnly: false
          UnusedAccountValidityDays: 7
      AliasAttributes:
        - email
      AutoVerifiedAttributes:
        - email
      EmailVerificationMessage: "Your verification code is {####}."
      EmailVerificationSubject: "Your verification code"
      MfaConfiguration: 'OFF'
      Policies:
        PasswordPolicy:
          MinimumLength: 8
          RequireLowercase: true
          RequireNumbers: true
          RequireSymbols: true
          RequireUppercase: true
      UserPoolName:
        Ref: AWS::StackName
      Schema:
        - AttributeDataType: "String"
          DeveloperOnlyAttribute: false
          Mutable: true
          Name: "email"
          StringAttributeConstraints:
            MaxLength: "2048"
            MinLength: "0"
          Required: true
        - AttributeDataType: "String"
          DeveloperOnlyAttribute: false
          Mutable: true
          Name: "name"
          StringAttributeConstraints:
            MaxLength: "2048"
            MinLength: "0"
          Required: false
        - AttributeDataType: "String"
          DeveloperOnlyAttribute: false
          Mutable: true
          Name: "custom-attributes"
          StringAttributeConstraints:
            MaxLength: "2048"
            MinLength: "0"
          Required: false
      SmsAuthenticationMessage:  "Your authentication code is {####}."
      SmsVerificationMessage: "Your verification code is {####}."
      UserPoolTags:
        Name: 'Test'
  UserPoolClient:
    Type: "AWS::Cognito::UserPoolClient"
    Properties:
        ClientName:
          Ref: AWS::StackName
        GenerateSecret: false
        ExplicitAuthFlows:
          - "ADMIN_NO_SRP_AUTH"
        ReadAttributes:
          - "email"
          - "name"
          - "custom:custom-attributes"
        RefreshTokenValidity: 30
        UserPoolId:
          Ref: UserPool
        WriteAttributes:
          - "email"
          - "name"
          - "custom:custom-attributes"
Outputs:
  UserPoolClientId:
    Description: 'The name of the user pool client'
    Value:
      Ref: UserPoolClient
  UserPoolId:
    Description: 'The name of the user pool'
    Value:
      Ref: UserPool

Tips

AWS::Cognito::UserPoolClientでカスタム属性をサポートする

SPAなどからカスタム属性の値を参照したり変更したい場合、AWS::Cognito::UserPoolClientに読み書きの権限をつける必要があります。
読み込み権限をつけたい場合はReadAttributesに、書き込み権限はWriteAttributesに属性名を書きましょう。
ただし、このときAWS::Cognito::UserPoolで指定した値をそのまま入れるとエラーになります。

デフォルトにない、カスタム属性はかならずcustom:というプレフィックスがつくので、忘れずに入れましょう。

Serverless Frameworkで作る場合

resourcesの中にCloudFormationのコードをそのまま書くことができます。
AWS CloudFormation Resource Reference(serverless.com)

ということで、先ほどのテンプレートのResources:以下をそのままserverless.ymlに突っ込めばOKです。

service: usersCrud
provider: aws
functions:
  ...

resources:  // CloudFormation template syntax
  Resources:
    UserPool:
      Type: "AWS::Cognito::UserPool"
      Properties:
        ...
  Resources:
    UserPool:
      Type: "AWS::Cognito::UserPool"
      Properties:
        ...

ClientNameやUserPoolNameは、Ref: AWS::StackNameでServerless Frameworkが作成するCloudFormationスタック名にしておくのが良さそうです。
動的に生成できる名前にしておかないと、stageをわけてデプロイした時にコンフリクトする恐れがありますので要注意です。

ブックマークや限定記事(予定)など

WP Kyotoサポーター募集中

WordPressやフロントエンドアプリのホスティング、Algolia・AWSなどのサービス利用料を支援する「WP Kyotoサポーター」を募集しています。
月額または年額の有料プランを契約すると、ブックマーク機能などのサポーター限定機能がご利用いただけます。

14日間のトライアルも用意しておりますので、「このサイトよく見るな」という方はぜひご検討ください。

広告ここから
広告ここまで

Related Category posts