AWSCognito User Pool

CloudFormationでCognito UserPoolを作る

AWS CloudFormation » ユーザーガイド » リリース履歴にはまだ載っていないのですが、CloudFormationからCognito UserPoolを作れるようになりました。 ということで早速スタック […]

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

AWS CloudFormation » ユーザーガイド » リリース履歴にはまだ載っていないのですが、CloudFormationからCognito UserPoolを作れるようになりました。

ということで早速スタックを1つ作ってみましょう。

完成品

身も蓋もないですが、こちらが完成品です。

---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Example CloudFormation Template to create Cognito UserPool'
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'
Outputs:
  UserPoolId:
    Description: 'The name of the user pool'
    Value: !Ref UserPool

作り方

「作りたいリソースを一旦手組みして、作成されたリソースのパラメータをYAML化する」という流れが多分一番ストレスなく作れます。

ということでまずAWSコンソールなりAWS-CLIなりからCognito UserPoolを作りましょう。

作成できたらAWS-CLIを使って、どんな設定を書けばいいかを確認します。
aws cognito-idp describe-user-poolコマンドを使用すると、さっき設定した内容がJSON形式で確認できます。
AWS-CLIドキュメントはこちら

$ aws --region {YOUR_REGION} cognito-idp describe-user-pool --user-pool-id {YOUR_USER_POOL_ID}

全部値を出すのはしんどいので、jqでキーだけ抜粋してみました。

$ aws --region {YOUR_REGION} cognito-idp describe-user-pool --user-pool-id {YOUR_USER_POOL_ID} | jq .UserPool |jq keys
[
  "AdminCreateUserConfig",
  "AliasAttributes",
  "AutoVerifiedAttributes",
  "CreationDate",
  "EmailConfiguration",
  "EmailVerificationMessage",
  "EmailVerificationSubject",
  "EstimatedNumberOfUsers",
  "Id",
  "LambdaConfig",
  "LastModifiedDate",
  "MfaConfiguration",
  "Name",
  "Policies",
  "SchemaAttributes",
  "SmsAuthenticationMessage",
  "SmsVerificationMessage",
  "UserPoolTags"
]

“AWS::Cognito::UserPool”のプロパティが以下の通りなので、AWS-CLIで出力された値を転記していけばOKです。

{
  "Type" : "AWS::Cognito::UserPool",
  "Properties" : {
    "AdminCreateUserConfig" : AdminCreateUserConfig,
    "AliasAttributes" : [ String ],
    "AutoVerifiedAttributes" : [ String ],
    "DeviceConfiguration" : DeviceConfiguration,
    "EmailConfiguration" : EmailConfiguration,
    "EmailVerificationMessage" : String,
    "EmailVerificationSubject" : String,
    "LambdaConfig" : LambdaConfig,
    "MfaConfiguration" : String,
    "Policies" : Policies,
    "UserPoolName" : String,
    "Schema" : [ SchemaAttribute ],
    "SmsAuthenticationMessage" : String,
    "SmsConfiguration" : SmsConfiguration,
    "SmsVerificationMessage" : String,
    "UserPoolTags" : { String:String, ... }
  }
}

カスタム属性を追加したい場合

サンプルコードにさらっと混ぜてありますが、Schemaプロパティ内に他の値と同じように定義すればOKです。

CloudFormationでの例

      Schema:
        - AttributeDataType: "String"
          DeveloperOnlyAttribute: false
          Mutable: true
          Name: "custom-attributes"
          StringAttributeConstraints:
            MaxLength: "2048"
            MinLength: "0"
          Required: false

作成結果

出力結果の表示

CloudFormationですので、Refを使うことでリソースIDが取れます。

Fn::GetAttでARNやProviderNameなどもとれる様子ですので、ドキュメントを確認してみましょう。

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-userpool.html#w1ab2c19c12d189c13

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

WP Kyotoサポーター募集中

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

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

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

Related Category posts