AWS CLI & SDK
Control all of AWS from the terminal — querying resources, scripting infrastructure, and using the SDK in code
Skills taught: aws.cli, aws.sdk, aws.configure, aws.profiles, aws.output-formats
Setup and configuration
# Install (Linux/macOS)
curl 'https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip' -o aws.zip
unzip aws.zip && sudo ./aws/install
aws --version # aws-cli/2.x.x
# Configure default profile
aws configure
# AWS Access Key ID [None]: AKIA...
# AWS Secret Access Key [None]: xxxxxxxx
# Default region name [None]: ap-south-1 ← your closest region
# Default output format [None]: json
# Stored in:
# ~/.aws/credentials — keys (never commit this)
# ~/.aws/config — region, output format
# Named profiles (for multiple accounts)
aws configure --profile production
aws configure --profile staging
# Use a specific profile
aws s3 ls --profile production
export AWS_PROFILE=production # set for the shell session
# Verify which identity you're using
aws sts get-caller-identity
# { "UserId": "AIDA...", "Account": "123456789012", "Arn": "arn:aws:iam::123456789012:user/alice" }
Common CLI commands by service
# ── S3 ──────────────────────────────────────────────────
aws s3 ls # list buckets
aws s3 ls s3://my-bucket/ # list contents
aws s3 cp file.txt s3://my-bucket/ # upload
aws s3 cp s3://my-bucket/file.txt ./ # download
aws s3 sync ./dist s3://my-bucket/ # sync directory (like rsync)
aws s3 rm s3://my-bucket/file.txt # delete
aws s3 mb s3://new-bucket-name # make bucket
aws s3 rb s3://bucket --force # remove bucket (force = empty first)
# ── EC2 ─────────────────────────────────────────────────
aws ec2 describe-instances # list all instances
aws ec2 describe-instances \
--filters Name=instance-state-name,Values=running # only running
aws ec2 start-instances --instance-ids i-1234567890abcdef0
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
aws ec2 describe-instance-types --instance-types t3.micro t3.small
# ── Lambda ──────────────────────────────────────────────
aws lambda list-functions # list functions
aws lambda invoke \
--function-name my-function \
--payload '{"key":"value"}' \
response.json # invoke a function
aws lambda get-function --function-name my-function # get details
# ── IAM ─────────────────────────────────────────────────
aws iam list-users
aws iam list-roles
aws iam get-user --user-name alice
aws iam create-user --user-name bob
# ── Output formats ──────────────────────────────────────
aws ec2 describe-instances --output json # default (full JSON)
aws ec2 describe-instances --output table # human-readable table
aws ec2 describe-instances --output text # tab-separated (for scripts)
aws ec2 describe-instances --output yaml # YAML
# ── Filtering with --query (JMESPath) ───────────────────
aws ec2 describe-instances \
--query 'Reservations[*].Instances[*].[InstanceId,State.Name,PublicIpAddress]' \
--output table
AWS SDK in TypeScript/Node.js
// Install: npm install @aws-sdk/client-s3 @aws-sdk/client-dynamodb
import { S3Client, PutObjectCommand, GetObjectCommand } from '@aws-sdk/client-s3';
// SDK automatically finds credentials in order:
// 1. Environment variables (AWS_ACCESS_KEY_ID, etc.)
// 2. ~/.aws/credentials file
// 3. EC2/ECS/Lambda IAM role (preferred for production)
const s3 = new S3Client({ region: 'ap-south-1' });
// Upload a file
async function uploadFile(bucket: string, key: string, body: Buffer) {
await s3.send(new PutObjectCommand({
Bucket: bucket,
Key: key,
Body: body,
ContentType: 'application/octet-stream',
}));
console.log(`Uploaded s3://${bucket}/${key}`);
}
// Download a file
async function downloadFile(bucket: string, key: string): Promise<string> {
const response = await s3.send(new GetObjectCommand({ Bucket: bucket, Key: key }));
return await response.Body!.transformToString();
}
// Generate a pre-signed URL (time-limited download link)
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
async function getDownloadUrl(bucket: string, key: string): Promise<string> {
return getSignedUrl(
s3,
new GetObjectCommand({ Bucket: bucket, Key: key }),
{ expiresIn: 3600 } // 1 hour
);
}
Tip: Use --dry-run for destructive EC2 operations. Commands like aws ec2 terminate-instances support --dry-run — they check your permissions and validate the command without actually executing it. Always dry-run before terminating instances or deleting resources you can't easily recover.
Frequently Asked Questions
Should I use the CLI or the console?
Use the console to explore, visualize, and learn — it's great for understanding what resources exist and their relationships. Use the CLI for automation, scripts, and repeatable operations. For production infrastructure, use neither — use Infrastructure as Code (CloudFormation or CDK) so your infrastructure is version-controlled and reproducible.