📝

Cognito ユーザープールをマネジメントコンソールから作成した時に作成されるリソースを CloudFormation から作成してみた

に公開

Resource type support - AWS CloudFormation
本記事執筆時点では IaC ジェネレーターでサポートされていない以下のリソースを CloudFormation から作成してみました。

  • ユーザープール
  • ユーザープールドメイン
  • アプリケーションクライアント
  • マネージドログインブランディングのスタイル

テンプレート

AWSTemplateFormatVersion: "2010-09-09"
Description: Create a Cognito User Pool

Resources:
  MyUserPool:
    Type: AWS::Cognito::UserPool
    Properties:
      UserPoolName: MyUserPool
      DeletionProtection: ACTIVE
      AutoVerifiedAttributes:
        - email
      UsernameAttributes:
        - email
      UsernameConfiguration:
        CaseSensitive: false
      MfaConfiguration: "OFF"
      AccountRecoverySetting:
        RecoveryMechanisms:
          - Name: verified_email
            Priority: 1

  MyUserPoolDomain:
    Type: AWS::Cognito::UserPoolDomain
    Properties:
      Domain: my-custom-domain-suffix-1234
      UserPoolId: !Ref MyUserPool
      ManagedLoginVersion: 2

  MyUserPoolClient:
    Type: AWS::Cognito::UserPoolClient
    Properties:
      ClientName: MyWebAppClient
      UserPoolId: !Ref MyUserPool
      GenerateSecret: true
      RefreshTokenValidity: 5
      AccessTokenValidity: 60
      IdTokenValidity: 60
      TokenValidityUnits:
        AccessToken: minutes
        IdToken: minutes
        RefreshToken: days
      ExplicitAuthFlows:
        - ALLOW_REFRESH_TOKEN_AUTH
        - ALLOW_USER_SRP_AUTH
        - ALLOW_USER_AUTH
      SupportedIdentityProviders:
        - COGNITO
      CallbackURLs:
        - https://d84l1y8p4kdic.cloudfront.net
      AllowedOAuthFlows:
        - code
      AllowedOAuthScopes:
        - openid
        - phone
        - email
      AllowedOAuthFlowsUserPoolClient: true
      PreventUserExistenceErrors: ENABLED
      AuthSessionValidity: 3

  MyUserPoolUICustomization:
    Type: AWS::Cognito::ManagedLoginBranding
    Properties:
      UserPoolId: !Ref MyUserPool
      ClientId: !Ref MyUserPoolClient
      UseCognitoProvidedValues: true

一部異なる点

コンソールから作成した場合と一部異なる点は以下の通りです。

  • ドメイン
    • コンソールから作成した場合はランダムな文字列
    • 上記テンプレートでは固定ドメイン名

ランダムな文字列にする必要がある場合には、CDK を使用する方法で実現可能だと思われます。

まとめ

今回は Cognito ユーザープールをマネジメントコンソールから作成した時に作成されるリソースを CloudFormation から作成してみました。
どなたかの参考になれば幸いです。

参考資料

Discussion