0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AWS CLI を使って Cognito ユーザープールを作成し、アプリクライアント(クライアントシークレットなし)を作成する手順(メモ)

Posted at

1. AWS CLI をインストール & 設定

AWS CLI がインストールされていない場合は、以下のコマンドでインストールしてください。

🔹 AWS CLI のインストール

# macOS
brew install awscli

# Ubuntu/Debian
sudo apt update && sudo apt install awscli -y

# Amazon Linux / CentOS
sudo yum install aws-cli -y

🔹 AWS CLI の設定

aws configure

プロンプトが表示されたら、以下を入力します。

  • AWS Access Key ID: IAM ユーザーのアクセスキー
  • AWS Secret Access Key: IAM ユーザーのシークレットアクセスキー
  • Default region name: ap-northeast-1(東京リージョン)など
  • Default output format: json(デフォルトのままでOK)

2. Cognito ユーザープールを作成

aws cognito-idp create-user-pool \
  --pool-name "MyUserPool" \
  --policies '{
      "PasswordPolicy": {
          "MinimumLength": 8,
          "RequireUppercase": true,
          "RequireLowercase": true,
          "RequireNumbers": true,
          "RequireSymbols": false
      }
  }' \
  --auto-verified-attributes "email" \
  --schema '[
      {
          "Name": "email",
          "AttributeDataType": "String",
          "Mutable": true,
          "Required": true
      }
  ]' \
  --region ap-northeast-1

✅ 実行後のレスポンス例

{
    "UserPool": {
        "Id": "ap-northeast-1_abcdefgh",
        "Name": "MyUserPool"
    }
}

ユーザープール ID (UserPool.Id) をメモしておきます。


3. アプリクライアント(クライアントシークレットなし)を作成

aws cognito-idp create-user-pool-client \
  --user-pool-id "ap-northeast-1_abcdefgh" \
  --client-name "MyAppClient" \
  --no-generate-secret \
  --explicit-auth-flows "ALLOW_USER_PASSWORD_AUTH" "ALLOW_REFRESH_TOKEN_AUTH" \
  --supported-identity-providers "COGNITO" \
  --region ap-northeast-1

✅ 実行後のレスポンス例

{
    "UserPoolClient": {
        "ClientId": "190d8ar0cc5pc1isoec2s5ncvb",
        "UserPoolId": "ap-northeast-1_abcdefgh",
        "ClientName": "MyAppClient"
    }
}

アプリクライアント ID (UserPoolClient.ClientId) をメモしておきます。


4. ユーザーを手動で作成(仮パスワード付き)

aws cognito-idp admin-create-user \
  --user-pool-id "ap-northeast-1_abcdefgh" \
  --username testuser@example.com \
  --user-attributes Name=email,Value=testuser@example.com \
  --temporary-password "TempPass#1234" \
  --message-action SUPPRESS \
  --region ap-northeast-1

5. ユーザーのパスワードを変更(管理者が手動で設定)

aws cognito-idp admin-set-user-password \
  --user-pool-id "ap-northeast-1_abcdefgh" \
  --username testuser@example.com \
  --password "NewSecurePass#5678" \
  --permanent \
  --region ap-northeast-1

--permanent を指定することで、ユーザーは次回ログイン時にパスワード変更を求められません。


6. ユーザーのログインをテスト

aws cognito-idp initiate-auth \
  --auth-flow USER_PASSWORD_AUTH \
  --client-id "190d8ar0cc5pc1isoec2s5ncvb" \
  --auth-parameters USERNAME=testuser@example.com,PASSWORD="NewSecurePass#5678" \
  --region ap-northeast-1

✅ 実行後のレスポンス例(成功時)

{
    "AuthenticationResult": {
        "AccessToken": "eyJraWQiOi...",
        "ExpiresIn": 3600,
        "IdToken": "eyJraWQiOi...",
        "RefreshToken": "eyJjdHkiOi..."
    }
}

7. API Gateway + Lambda で Cognito 認証を使用

  1. API Gateway に Cognito オーソライザーを設定
  2. クライアントアプリが IdToken または AccessTokenAuthorization ヘッダーに付与
  3. Lambda でトークンを検証して認証

Cognito の JWT トークンを Lambda に渡して認証することで、API のアクセス制御が可能になります。


まとめ

ステップ コマンド
1. ユーザープールを作成 aws cognito-idp create-user-pool
2. アプリクライアントを作成(クライアントシークレットなし) aws cognito-idp create-user-pool-client
3. ユーザーを作成 aws cognito-idp admin-create-user
4. パスワードを設定 aws cognito-idp admin-set-user-password
5. ログインテスト aws cognito-idp initiate-auth
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?