AWS CLI Setup Best Practices
A checklist of habits that keep your AWS CLI and SDK credentials secure, portable, and easy to reason about across accounts.
Busque em todas as páginas da documentação
A checklist of habits that keep your AWS CLI and SDK credentials secure, portable, and easy to reason about across accounts.
- [ ] as a rule to verify against your current setup.aws sso login issues short-lived credentials that expire and refresh, removing static secrets from disk.role_arn + source_profile profile beats copying a second account's keys around.mfa_serial to role-assumption profiles so a code is required before elevation.~/.aws/credentials or ~/.aws/config. Add .aws/ patterns to your global gitignore; a leaked key is a breach.aws configure prompts or SSO over exporting keys inline where they land in .bash_history.chmod 600 ~/.aws/credentials).prod-readonly, staging-deploy, and dev-admin communicate intent far better than profile1.config block.sso-session block for SSO profiles. One shared login covers many account/role profiles instead of repeating the start URL.aws configure list-profiles so a typo does not point you at the wrong account.default so an unqualified command is safe.get-caller-identity before acting. Confirm which identity a shell resolves to before running anything destructive.aws sso logout clears the cached token when you step away.sso-session features.AWS_PROFILE, not code edits. Keep profile choice in the environment so scripts stay portable.fromIni/fromSSO supply credentials only, so pass region or set AWS_REGION.Here is the secret-free client both SDKs should aim for.
# --- Python (boto3) ---
import boto3
# No keys, no region literal: resolved from the profile/env the CLI set up.
# The same code runs on a laptop (SSO/profile) and in production (instance role).
s3 = boto3.client("s3")
print([b["Name"] for b in s3.list_buckets()["Buckets"]])// --- TypeScript (AWS SDK v3) ---
import { S3Client, ListBucketsCommand } from "@aws-sdk/client-s3";
// No keys: resolved from the profile/env the CLI set up. Region comes from
// AWS_REGION or the active profile, so the same code is portable.
const s3 = new S3Client({});
const out = await s3.send(new ListBucketsCommand({}));
console.log(out.Buckets?.map((b) => b.Name));SSO credentials are short-lived and refresh automatically, so a leaked value expires quickly. Long-lived keys never expire on their own and are the biggest local security risk.
In ~/.aws/config, inside each profile's block. Region is not a secret and does not belong in the credentials file.
By account and role, for example prod-readonly or staging-deploy. Descriptive names prevent running a command against the wrong account.
Never commit the shared files or hardcode keys. A leaked credential in git is a direct breach.
Create the new key, update the profile, verify with aws sts get-caller-identity, then delete the old key. Never delete first.
No. Build clients with no credentials and let the default provider chain resolve them, so the same code is portable and secret-free.
fromIni and fromSSO return only credentials, not region. Without region on the client or AWS_REGION, a v3 client throws a region error.
Run aws sts get-caller-identity. It returns the account and ARN the current environment resolves to.
Short-lived credentials from OIDC-based role assumption, or securely injected temporary credentials. Avoid static keys in CI.
Use an sso-session block that multiple profiles reference. A single aws sso login then serves every profile pointing at it.
Yes. Restrict the credentials file to your user with chmod 600 ~/.aws/credentials so other local users cannot read it.
Version 1 is end-of-support and lacks SSO login and the sso-session format. Version 2 is the current, supported major.
Stack versions: This page was written for the AWS CLI v2, boto3 1.43.x (Python 3.10+), and the AWS SDK for JavaScript v3 (Node.js 18+).
Revisado por Chris St. John·Última atualização: 23 de jul. de 2026