LoginSignup
4
2

More than 1 year has passed since last update.

【AWS】CDKv2で環境構築 Cognitoユーザープール編

Last updated at Posted at 2022-06-26

はじめに

現在運用中の環境をテスト用として複製する際にCDKv2を用いて
コード化することになったのでその学びです。

DynamoDB編はこちら
Lambda編はこちら
apigateway編はこちら
StepFunctions編はこちら

準備

このへんの記事でまずは環境設定
https://aws.amazon.com/jp/getting-started/guides/setup-cdk/

今回はTypeScriptを用いて実装しました。

$ cdk init --language typescript

ユーザープール作成

設定項目の説明も記載してます!

main-stack.ts
import * as cognito from 'aws-cdk-lib/aws-cognito';

export class MainStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // defines a Cognito -----------------------------------------

    const UserPool = new cognito.UserPool(this, 'Sample-users-pool', {
      userPoolName: 'sample-users', // ユーザープール名
      selfSignUpEnabled: true, // セルフサービスのサインアップ設定
      signInAliases: { email: true }, // Cognito ユーザープールのサインイン
      standardAttributes: {
        givenName: { required: true }, // 必須の属性
        familyName: { required: true },
      },
      customAttributes: { // カスタム属性設定
        'family_name_kana': new cognito.StringAttribute({ minLen: 1, maxLen: 256, mutable: true }),
        'given_name_kana': new cognito.StringAttribute({ minLen: 1, maxLen: 256, mutable: true }),
      },
      passwordPolicy: { // 設定しない場合はコンソールの場合と同じデフォルト設定
        minLength: 10,
        requireLowercase: false,
        requireUppercase: false,
        requireDigits: false,
        requireSymbols: false,
        tempPasswordValidity: cdk.Duration.days(7),
      },
      lambdaTriggers: {
        postConfirmation: sample_fn, // 今回は登録確認後のトリガー設定 他も有り
      },
      accountRecovery: cognito.AccountRecovery.EMAIL_ONLY, // ユーザーアカウントの復旧設定
      removalPolicy: cdk.RemovalPolicy.DESTROY, // cdk destroyでの削除設定 デフォルトは削除されない
    });

    // 今回は設定しなかったがCoginitoドメイン設定とカスタムドメイン設定は以下
    // ドメイン設定や証明書設定(バージニア北部)のからみで一部手作業にしたため

    // UserPool.addDomain('MSample-users-domain', {
    //   cognitoDomain: {
    //     domainPrefix: 'sample-users',
    //   },
    //   customDomain: {
    //     domainName: 'auth.sample.com',
    //     certificate: domainCert, // これは仮の変数で中身はCertigicatManager module
    //   }
    // });

    // アプリケーションクライアントの設定
    UserPool.addClient('Application', {
      userPoolClientName: 'application', // クライアント名
      generateSecret: false, // シークレットの作成設定
      enableTokenRevocation: true, // 高度な認証設定のトークンの取り消しを有効化
      preventUserExistenceErrors: true, // 高度な認証設定のユーザー存在エラーの防止を有効化
      oAuth: {
        flows: { // OAuth 付与タイプ設定
          authorizationCodeGrant: true, // 認証コード付与
          implicitCodeGrant: true, // 暗黙的な付与
        },
        callbackUrls: [ // 許可されているコールバックURL設定
          'https://sample.com/app',
          'https://oauth.pstmn.io/v1/callback', // ポストマンアプリ用
        ],
        logoutUrls: [ // 許可されているサインアウトURL設定
          'https://sample.com/app',
        ],
        scopes: [ // カスタムスコープ
          cognito.OAuthScope.EMAIL,
          cognito.OAuthScope.OPENID,
          cognito.OAuthScope.PHONE,
          cognito.OAuthScope.PROFILE,
        ],
      }
    });
  }
}

ドキュメント

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2