Home / DevOps / Install AWS CLI and Use Basic Commands
Intermediate

Install AWS CLI and Use Basic Commands

An intermediate guide to installing AWS CLI and using basic commands for secure and efficient AWS management via command line interface

Review AWS costs and IAM permissions before running labs; delete unused resources.

Prerequisites

1. Active AWS account with appropriate permissions 2. A computer supporting AWS CLI installation (Windows, Mac, Linux) 3. Basic command line knowledge

1. Download and Install AWS CLI

Go to the official AWS CLI site at https://aws.amazon.com/cli/ and download the version matching your operating system. Follow the installation instructions to complete the setup, ensuring AWS CLI is ready on your machine.
💡
Verify your operating system correctly before downloading to avoid installation issues

2. Verify AWS CLI Installation

After installation, open your terminal or command prompt and type aws --version to confirm AWS CLI is installed and operational.
aws --version
💡
If command is unrecognized, check your system PATH to ensure AWS CLI location is included

3. Create an IAM User with Least Privilege for AWS CLI

In the AWS Console, navigate to IAM service, select "Users", then click "Add user". Name the user, choose "Programmatic access" type, and attach least privilege policies such as 'AmazonS3ReadOnlyAccess' or a custom policy appropriate to your needs.
💡
Avoid granting more permissions than required to maintain security

4. Retrieve IAM User Access Key ID and Secret Access Key

After creating the IAM user, AWS provides the Access Key ID and Secret Access Key. Save these securely as you cannot retrieve the Secret Access Key later.
💡
Do not expose or store keys in public or insecure locations

5. Configure AWS CLI Using aws configure Command

Open your terminal or command prompt, type aws configure and input your Access Key ID, Secret Access Key, desired region, and output format like json to set up basic AWS CLI configuration.
aws configure
💡
Choose the region matching your AWS resources for correct operations

6. Test Basic AWS CLI Command: List IAM Users

Execute aws iam list-users to display existing IAM users in your account, verifying correct configuration and permissions.
aws iam list-users
💡
If errors occur related to permissions, validate your IAM policies allow this command

7. Test Basic AWS CLI Command: List All S3 Buckets

Run aws s3 ls to check your ability to connect and retrieve S3 bucket data according to assigned permissions.
aws s3 ls
💡
If no output or errors occur, analyze whether your IAM user has access to S3

8. Set Up Profiles for Multiple Accounts or Users

Use aws configure --profile <profile-name> to separate access configurations for different users or accounts, e.g., aws configure --profile dev-user sets credentials for a development environment user.
aws configure --profile <profile-name>
💡
Switch profiles with --profile <profile> flag on AWS CLI commands when working in that environment

9. Use AWS CLI Commands with Specified Profile

For example, run aws s3 ls --profile <profile-name> to list S3 buckets under that profile, testing configuration accuracy.
aws s3 ls --profile <profile-name>
💡
Ensure the profile is configured and has correct permissions before use

10. Learn Basic S3 Management Commands via AWS CLI

Practice commands like aws s3 mb s3://<bucket-name> to create buckets, aws s3 cp <file> s3://<bucket-name>/ to upload files, and aws s3 rb s3://<bucket-name> --force to safely delete buckets.
aws s3 mb s3://<bucket-name>
aws s3 cp <file> s3://<bucket-name>/
aws s3 rb s3://<bucket-name> --force
💡
Use the delete --force command carefully as it deletes the bucket and all contents

11. Troubleshooting Common Issues in AWS CLI Installation and Usage

Check your PATH variable and file key permissions. Verify AWS CLI version and update if outdated. On issues, read error messages carefully and run aws sts get-caller-identity to validate current user information.
aws sts get-caller-identity
💡
Correct IAM and key setup often resolves common issues

12. Cleanup and Uninstall AWS CLI

To discontinue use, delete configuration files ~/.aws/credentials and ~/.aws/config. Uninstall AWS CLI following your operating system standard method to prevent accidental usage.
rm -rf ~/.aws/credentials ~/.aws/config
💡
Backup configuration files before deletion if still needed

Lesson summary

Learn to install AWS CLI, configure secure access, and use basic commands to manage AWS services effectively and safely via CLI