Authentication
Guide to programmatically authenticating with AWS using the AWS CLI and SDK
Last updated
Guide to programmatically authenticating with AWS using the AWS CLI and SDK
Last updated
Programmatic access to AWS requires secure authentication. This guide covers various methods to authenticate with AWS services using command-line tools (AWS CLI) and SDKs, following security best practices as of 2025.
installed (current version as of May 2025: 2.17.x)
Basic understanding of AWS IAM concepts
Terminal or command-line interface
This is the most basic method, but should be used carefully and mainly for development environments.
Create Access Keys in AWS Console:
Log in to the AWS Management Console
Navigate to IAM → Users → Security credentials
Create access key (ideally for a specific purpose)
Configure AWS CLI:
You'll be prompted to enter:
AWS Access Key ID
AWS Secret Access Key
Default region name (e.g., us-west-2)
Default output format (json, yaml, text, table)
This creates configuration files in ~/.aws/credentials
and ~/.aws/config
.
For managing multiple AWS accounts or roles:
Then use the profile with any command:
More secure for CI/CD pipelines or temporary sessions:
Then run AWS CLI commands normally without specifying credentials.
For EC2 instances, Lambda functions, or other AWS services:
Create an IAM role with appropriate permissions
Attach the role to your EC2 instance or service
The AWS CLI will automatically use the instance profile credentials
No manual configuration is needed with this approach, making it the most secure option for resources running within AWS.
For enterprise environments with centralized identity management:
Configure AWS SSO in your organization
Configure the AWS CLI for SSO:
Authenticate via the browser:
For mobile or web applications:
For enhanced security with Multi-Factor Authentication:
Create a temporary session:
Use the temporary credentials:
Assuming roles is powerful for cross-account access or privilege escalation:
Store the returned credentials as environment variables or in AWS CLI profile.
Use IAM Roles whenever possible instead of access keys
Implement the principle of least privilege for all credentials
Rotate access keys regularly (ideally every 90 days)
Never hardcode credentials in application code
Use MFA for all IAM users with console access
Audit authentication methods with AWS CloudTrail
Use temporary credentials instead of long-term access keys
Implement identity federation for enterprise environments
Check credential precedence: AWS CLI follows a specific order to find credentials
Verify IAM permissions: Ensure the user/role has appropriate permissions
Check region configuration: Some services are region-specific
Validate MFA token if using MFA-protected API access
Examine AWS CLI version: Some authentication methods require newer versions