A Python CLI tool for authenticating with AWS Cognito and retrieving temporary AWS credentials through Cognito Identity Pool. This tool supports password authentication, MFA setup, and TOTP-based multi-factor authentication.
By default, the tool prompts for username and password interactively. You can also provide credentials as command-line arguments for automation.
Note: This tool works only with AWS Cognito native users (username/password authentication). It does NOT support federated users from external identity providers (SAML, OIDC, social providers).
- User authentication with AWS Cognito User Pool
- Support for password change on first login
- TOTP software token MFA setup with QR code generation
- Multi-factor authentication support
- Retrieve temporary AWS credentials via Cognito Identity Pool
- Multiple output formats (shell export, JSON)
- Command-line arguments for automation
- Python 3.10+
- AWS Account with Cognito User Pool and Identity Pool configured
- AWS IAM role for authenticated Cognito users
- Cognito native user account (not federated from external IdP)
- Clone the repository:
git clone https://github.com/yourusername/aws-cognito-cli-login.git
cd aws-cognito-cli-login- Create and activate a virtual environment (recommended):
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Create
.envfile with your Cognito configuration:
COGNITO_URL=cognito-idp.us-east-1.amazonaws.com
COGNITO_USER_POOL_ID=us-east-1_XXXXXXXXX
COGNITO_IDENTITY_POOL_ID=us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
COGNITO_CLIENT_ID=xxxxxxxxxxxxxxxxxxxxxxxxxx
APP_NAME=MyAppusage: main.py [-h] [-u USERNAME] [-p PASSWORD] [-f {export,json}] [-v] [--change-password]
AWS Cognito CLI Login
options:
-h, --help show this help message and exit
-u USERNAME, --username USERNAME
Cognito username
-p PASSWORD, --password PASSWORD
Cognito password
-f {export,json}, --format {export,json}
Output format (default: export)
-v, --verbose Verbose output
--change-password Change passwordBasic authentication:
python main.pySpecify username and password:
python main.py -u myusername -p mypasswordJSON output format:
python main.py -u myusername -f jsonVerbose logging:
python main.py -u myusername -vChange password:
python main.py -u myusername --change-passwordAWS Cognito resources can be deployed using Terraform code in the terraform/ directory:
cd terraform
terraform init
terraform plan
terraform applyTo customize the deployment:
terraform apply -var="name=my-cognito-app" -var="region=us-east-1"Attach custom IAM policies to the authenticated role:
terraform apply -var='authenticated_role_policy_arns=["arn:aws:iam::aws:policy/ReadOnlyAccess"]'After deployment, get the outputs to configure your .env file:
terraform outputTerraform outputs:
user_pool_endpoint→COGNITO_URLuser_pool_id→COGNITO_USER_POOL_IDidentity_pool_id→COGNITO_IDENTITY_POOL_IDuser_pool_client_id→COGNITO_CLIENT_ID
- User authenticates with AWS Cognito User Pool using username/password
- Handles authentication challenges (new password, MFA setup, MFA verification)
- Receives ID token from successful authentication
- Exchanges ID token for AWS credentials via AWS Cognito Identity Pool
- Outputs temporary AWS credentials (Access Key, Secret Key, Session Token)
- Boto3 Amazon Cognito Federated Identities -
CognitoIdentity.Clientdocumentation - Boto3 Amazon Cognito user pools API
CognitoIdentityProvider.Clientdocumentation - AWS Cognito - TOTP software token MFA documentation
