💡 Ask Tutor

AWS CLI & SDK – Automating AWS Like a Pro

Managing AWS via the web console is great for learning, but serious professionals use automation tools like the AWS Command Line Interface (CLI) and SDKs (Software Development Kits) to streamline workflows, deploy applications, and interact programmatically with AWS services.

🧑‍💻 What is AWS CLI?

AWS CLI is a unified tool to manage AWS services via terminal/command line. It supports all major services and can run commands from a local computer, a remote server, or a CI/CD pipeline.

📌 Ideal for automation, scripting, and DevOps tasks.

💻 Installing AWS CLI

✅ For Windows:

✅ For macOS:

Bash
brew install awscli

✅ For Linux:

Bash
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

🔐 Configuring AWS CLI

After installation, run:

Bash
aws configure

You’ll be prompted to enter:

  • AWS Access Key ID
  • AWS Secret Access Key
  • Default Region (e.g., us-east-1)
  • Output Format (json, table, or text)

🔒 Tip: Never hard-code these keys in scripts. Use environment variables or IAM roles for security.

⚙️ AWS CLI Basics – Real Examples

✅ List all S3 buckets:

Bash
aws s3 ls

✅ Upload a file to S3:

Bash
aws s3 cp hello.txt s3://my-bucket-name/

✅ Launch an EC2 instance:

Bash
aws ec2 run-instances \
  --image-id ami-0abcdef1234567890 \
  --count 1 \
  --instance-type t2.micro \
  --key-name MyKeyPair \
  --security-groups MySecurityGroup

✅ Create a new IAM user:

Bash
aws iam create-user --user-name newadmin

📂 CLI Output Formats

Choose between:

  • json – best for parsing programmatically
  • table – human-readable
  • text – compact, script-friendly

Example:

Bash
aws ec2 describe-instances --output table

🛡️ AWS CLI Profiles for Multi-Account Management

Use named profiles when working with multiple AWS accounts.

Bash
aws configure --profile prod-account

Call with:

Bash
aws s3 ls --profile prod-account

🧰 AWS SDK – Write Code That Talks to AWS

The AWS SDK allows developers to integrate AWS directly into their applications using languages like Python, JavaScript, Java, PHP, and Go.


🔶 SDK for Python (Boto3)

✅ Installation:

Bash
pip install boto3

✅ Basic S3 Upload:

Bash
import boto3

s3 = boto3.client('s3')
s3.upload_file('local.jpg', 'my-bucket', 'images/local.jpg')

✅ EC2 Instance Listing:

Bash
ec2 = boto3.client('ec2')
response = ec2.describe_instances()

for reservation in response['Reservations']:
    for instance in reservation['Instances']:
        print(instance['InstanceId'], instance['State']['Name'])

🟢 SDK for JavaScript (Node.js)

✅ Installation:

Bash
npm install aws-sdk

✅ Upload to S3:

JavaScript
const AWS = require('aws-sdk');
const fs = require('fs');

const s3 = new AWS.S3();

const uploadParams = {
  Bucket: 'my-bucket',
  Key: 'file.txt',
  Body: fs.createReadStream('file.txt'),
};

s3.upload(uploadParams, function(err, data) {
  if (err) console.log("Error", err);
  else console.log("Upload Success", data.Location);
});

When to Use CLI vs SDK?

Use CaseBest Tool
Automating deploymentsAWS CLI, Bash
CI/CD pipelinesCLI or SDK
Application integrationsSDK
DevOps + Infrastructure as CodeCLI
Scripting & cron jobsCLI

Real-World Scenarios

ScenarioSolution
Backup database to S3 every nightCron job using CLI script
Upload user files via web formSDK integration (Python, Node.js)
Auto-tagging resourcesCLI or Lambda with SDK
Build infrastructure on commitSDK in CodePipeline or CLI in GitHub Actions
Create dashboards based on usageSDK + CloudWatch logs

🛡️ CLI & SDK Security Tips

  • Use IAM roles for EC2 or Lambda instead of hardcoding keys
  • Store credentials securely using AWS Secrets Manager
  • Rotate credentials regularly
  • Never commit .aws/credentials or .env files to GitHub
  • Use least privilege principle in IAM policies

📊 Monitoring and Troubleshooting

  • Use --debug flag to troubleshoot CLI commands
  • Enable CloudTrail to log all CLI and SDK calls
  • Monitor usage with CloudWatch Metrics
📌 Summary
ToolPurposeUse Case
AWS CLICommand-line interaction with AWSScripts, automation, DevOps
AWS SDKIntegrate AWS in code applicationsWeb apps, integrations, functions
Boto3AWS SDK for PythonML, automation, analytics
aws-sdkAWS SDK for JavaScriptFrontend/backend apps
Secure UseUse IAM roles, Secrets ManagerPrevent leaks and misuse