# Aws Cli

## What is AWS CLI?

AWS CLI is a tool that allows you to manage AWS services using commands from your terminal or command prompt.

Instead of opening the AWS Console in a browser, you can:

* Create EC2 instances
    
* List S3 buckets
    
* Upload files to S3
    
* Check IAM users
    
* Manage almost all AWS services
    
* All from one place using commands.
    
    In short:
    
    > AWS CLI = Control AWS using terminal commands
    
    ## Why Should We Use AWS CLI?
    
    Here are some simple reasons:
    
    * Faster than AWS Console
        
    * Easy to automate tasks
        
    * Very useful in DevOps
        
    * Can be used in scripts and CI/CD pipelines
        
    * Same commands work everywhere
        
    
    If you want to move into DevOps or Cloud roles, AWS CLI is **mandatory**.
    

## Prerequisites

Before using AWS CLI, you need:

* An AWS account
    
* IAM user with programmatic access
    
* Access Key ID
    
* Secret Access Key
    

You should **never use the root account** for CLI access.

## Installing AWS CLI

### On Linux (Ubuntu)

```plaintext
sudo apt update
sudo apt install awscli -y
```

Check version:

```plaintext
aws --version
```

### On Windows

1. Download AWS CLI MSI installer from AWS official site
    
2. Install it like a normal software
    
3. Open Command Prompt and check:
    

```plaintext
aws --version
```

## Configuring AWS CLI

After installation, you must configure AWS CLI.

Run:

```plaintext
aws configure
```

It will ask:

```plaintext
AWS Access Key ID: xxxxxxxxx
AWS Secret Access Key: xxxxxxxxx
Default region name: ap-south-1
Default output format: json
```

Now your AWS CLI is connected to your AWS account.

## AWS CLI Configuration Files

AWS CLI stores configuration in two files:

```plaintext
~/.aws/credentials
~/.aws/config
```

You can check them using:

```plaintext
cat ~/.aws/credentials
cat ~/.aws/config
```

## Basic AWS CLI Commands

### Check AWS Identity

```plaintext
aws sts get-caller-identity
```

This command confirms:

* Which account you are using
    
* Which IAM user is configured
    
    ## AWS S3 Commands
    
    ### List all S3 Buckets
    
    ```plaintext
    aws s3 ls
    ```
    
    ### Create an S3 Bucket
    
    ```plaintext
    aws s3 mb s3://my-first-cli-bucket
    ```
    
    ---
    
    ### Upload File to S3
    
    ```plaintext
    aws s3 cp file.txt s3://my-first-cli-bucket/
    ```
    
    ### Download File from S3
    
    ```plaintext
    aws s3 cp s3://my-first-cli-bucket/file.txt .
    ```
    
    ---
    
    ### Delete File from S3
    
    ```plaintext
    aws s3 rm s3://my-first-cli-bucket/file.txt
    ```
    
    ### Delete S3 Bucket
    
    ```plaintext
    aws s3 rb s3://my-first-cli-bucket --force
    ```
    
    ## AWS EC2 Commands
    
    ### List EC2 Instances
    
    ```plaintext
    aws ec2 describe-instances
    ```
    
    This command gives full details of all instances in JSON format.
    
    ---
    
    ### List Only Instance IDs
    
    ```plaintext
    aws ec2 describe-instances \
    --query "Reservations[].Instances[].InstanceId"
    ```
    
    ---
    
    ### Start EC2 Instance
    
    ```plaintext
    aws ec2 start-instances --instance-ids i-1234567890abcdef0
    ```
    
    ---
    
    ### Stop EC2 Instance
    
    ```plaintext
    aws ec2 stop-instances --instance-ids i-1234567890abcdef0
    ```
    
    ---
    
    ### Terminate EC2 Instance
    
    ```plaintext
    aws ec2 terminate-instances --instance-ids i-1234567890abcdef0
    ```
    
    Be careful. This action is permanent.
    
    ---
    
    ## AWS IAM Commands
    
    ### List IAM Users
    
    ```plaintext
    aws iam list-users
    ```
    
    ---
    
    ### List IAM Roles
    
    ```plaintext
    aws iam list-roles
    ```
    
    ---
    
    ### Get IAM User Details
    
    ```plaintext
    aws iam get-user
    ```
    
    ---
    
    ## Working With AWS Regions
    
    ### List All Regions
    
    ```plaintext
    aws ec2 describe-regions
    ```
    
    ---
    
    ### Run Command in Specific Region
    
    ```plaintext
    aws ec2 describe-instances --region us-east-1
    ```
    
    ---
    
    ## Output Formats in AWS CLI
    
    AWS CLI supports:
    
    * json
        
    * table
        
    * text
        
    
    Example:
    
    ```plaintext
    aws ec2 describe-instances --output table
    ```
    
    Table output is easier to read.
    
    ---
    
    ## AWS CLI Help Command
    
    Whenever you are stuck, use:
    
    ```plaintext
    aws help
    ```
    
    Or for a specific service:
    
    ```plaintext
    aws s3 help
    ```
    
    This is very useful for beginners.
    
    ---
    
    ## Common AWS CLI Errors
    
    ### Access Denied
    
    Reason:
    
    * IAM user does not have required permissions
        
    
    Solution:
    
    * Attach correct IAM policy
        
    
    ---
    
    ### Invalid Client Token
    
    Reason:
    
    * Wrong Access Key or Secret Key
        
    
    Solution:
    
    * Reconfigure AWS CLI
        
    
    ---
    
    ## Best Practices for AWS CLI
    
    * Never expose Access Key in public
        
    * Use IAM roles where possible
        
    * Always follow least privilege principle
        
    * Stop unused resources to avoid billing
        
    * Practice commands in free tier
        
    
    ---
    
    ## Conclusion
    
    AWS CLI is a powerful tool that makes AWS easy and fast to use.  
    At first, commands may look difficult, but with practice, it becomes natural.
    
    If you are learning AWS or planning to move into DevOps, start using AWS CLI daily.  
    It will improve your confidence and real-world skills.3
