[AWS CDK] Cognito を構築

[AWS CDK] Cognito を構築

2022-03-047 min read

目次

  1. 概要
  2. stack
  3. 参考にしたサイト

概要

AWS CDK v2 で Cognito を構築した際のCDK Stackです。

Stack

以下のソースが AWS CDK のスタックです。

import {
  App,
  Stack,
  StackProps,
  RemovalPolicy,
  aws_cognito as cognito,
} from "aws-cdk-lib";

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

    const project: string = "myproject";
    const stage: string = "dev";

    const userPool = new cognito.UserPool(this, `${project}-user-pool`, {
      userPoolName: `${project}-user-pool`,
      selfSignUpEnabled: true, // サインアップ有効
      standardAttributes: {
        email: { required: true, mutable: true },
      },
      signInAliases: { email: true },
      accountRecovery: cognito.AccountRecovery.EMAIL_ONLY,
      removalPolicy: RemovalPolicy.DESTROY,
    });
    const domainPrefix = `${project}`;

    new cognito.UserPoolDomain(this, "UserPoolDomain", {
      userPool: userPool,
      cognitoDomain: {
        domainPrefix: domainPrefix,
      },
    });

    userPool.addClient("client", {
      userPoolClientName: `${project}-${stage}-client`,
      oAuth: {
        scopes: [
          cognito.OAuthScope.EMAIL,
          cognito.OAuthScope.OPENID,
          cognito.OAuthScope.PROFILE,
        ],
        callbackUrls: ["http://example.com:8080/callback"],
        logoutUrls: ["http://example.com:8080/logout"],
        flows: {
          authorizationCodeGrant: true,
        },
      },
      authFlows: {
        adminUserPassword: true,
        userPassword: true,
      },
      generateSecret: true,
    });
  }
}

参考にしたサイト

Author
githubzennqiita
ただの備忘録です。

※外部送信に関する公表事項