How To Install And Configure The AWS CLI On Linux OS

How To Install And Configure The AWS CLI On Linux OS

The AWS Command Line Interface (CLI) is a powerful tool that allows users to interact with AWS services directly from the terminal or command prompt. It provides a unified tool to manage AWS resources, enabling automation, scripting, and easy management of AWS services. Here’s an overview of the main uses and benefits of the AWS CLI:

Key Uses of AWS CLI

  1. Manage AWS Resources

    • The AWS CLI allows you to create, modify, delete, and manage AWS resources like EC2 instances, S3 buckets, Lambda functions, and more. For example, you can launch a new EC2 instance, manage security groups, or upload files to S3, all from the command line.

    • Example:

        aws ec2 start-instances --instance-ids i-1234567890abcdef0
      
  2. Automation and Scripting

    • The CLI is often used in scripts to automate routine tasks. For instance, you can write a script that automatically backs up databases, uploads files to S3, or scales infrastructure based on demand.

    • Example:

        aws s3 sync /local/folder s3://mybucket/backup
      
  3. Infrastructure as Code

    • AWS CLI can be used in conjunction with tools like AWS CloudFormation or Terraform to define and deploy infrastructure as code. This makes it easier to version control and automate the deployment of complex infrastructures.

    • Example:

        aws cloudformation deploy --template-file template.yaml --stack-name mystack
      
  4. Data Management

    • AWS CLI simplifies the management of data within AWS services. For instance, you can easily upload, download, or sync data with S3 buckets, manage DynamoDB tables, or query RDS databases.

    • Example:

        aws dynamodb scan --table-name mytable
      
  5. Monitoring and Logging

    • The CLI can be used to monitor AWS services and view logs. For example, you can retrieve CloudWatch metrics, check the status of instances, or view logs for troubleshooting.

    • Example:

        aws cloudwatch get-metric-statistics --metric-name CPUUtilization --start-time 2023-08-13T00:00:00Z --end-time 2023-08-13T23:59:59Z --period 300 --namespace AWS/EC2 --statistics Average
      
  6. Deployment and Continuous Integration/Continuous Deployment (CI/CD)

    • AWS CLI is integral to many CI/CD pipelines. It is used to automate the deployment of code, manage version control, and integrate with other services like CodeDeploy, CodePipeline, and CodeBuild.

    • Example:

        aws deploy create-deployment --application-name MyApp --deployment-group-name MyAppDG --github-location repository=myrepo,commitId=1234567890abcdef
      
  7. Cost Management

    • You can use the AWS CLI to retrieve billing and cost data, enabling you to monitor and control your AWS spending. It can also be used to automate cost reports.

    • Example:

        aws ce get-cost-and-usage --time-period Start=2023-08-01,End=2023-08-31 --granularity MONTHLY --metrics "BlendedCost"
      
  8. Security Management

    • The CLI allows for the management of IAM (Identity and Access Management) users, roles, and policies. You can automate the creation and management of security credentials, permissions, and policies.

    • Example:

        aws iam create-user --user-name myuser
        aws iam attach-user-policy --user-name myuser --policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
      

Benefits of Using AWS CLI

  • Efficiency: The CLI allows you to perform tasks quickly without needing to navigate the AWS Management Console, which can be especially useful for repetitive tasks.

  • Automation: Automating tasks using the CLI reduces the likelihood of human error and speeds up deployment and management processes.

  • Simplicity: It provides a consistent interface across all AWS services, making it easier to learn and use compared to working with multiple service-specific tools.

  • Scriptability: The CLI’s commands can be easily integrated into shell scripts or batch files, enabling complex automation workflows.

  • Flexibility: The CLI can be used across different operating systems (Linux, macOS, Windows), making it a versatile tool for managing AWS environments.

Installing and configuring the AWS CLI (Command Line Interface) on a Linux OS is a straightforward process. Here’s a step-by-step guide to get you started:

1. Install AWS CLI on Linux

You can use the package manager for your Linux distribution to install the AWS CLI. Here's how:

For Amazon Linux, CentOS, RHEL, Fedora:

sudo yum install aws-cli -y

For Ubuntu, Debian:

sudo apt-get update
sudo apt-get install awscli -y

Using the AWS CLI Bundled Installer (For All Distributions)

If your distribution doesn’t support the package manager method, you can use the bundled installer:

  1. Download the installer:

     curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
    
  2. Unzip the package:

     unzip awscliv2.zip
    
  3. Run the installer:

     sudo ./aws/install
    
  4. Verify the installation:

     aws --version
    
    • This should return something like aws-cli/2.x.x Python/3.x.x Linux/4.x.x indicating that AWS CLI is installed successfully.

2. Configure the AWS CLI

After installation, you need to configure the AWS CLI with your credentials and default region.

  1. Run the configure command:

     aws configure
    
  2. Enter your AWS Access Key ID:

    • You can obtain this from your AWS Management Console under "My Security Credentials" or from your AWS account administrator.

    • Example: AKIAIOSFODNN7EXAMPLE

  3. Enter your AWS Secret Access Key:

    • This is also available in the "My Security Credentials" section of the AWS Console.

    • Example: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

  4. Enter your default region name:

    • This should be the AWS region you want to operate in by default, such as us-west-2, us-east-1, etc.

    • Example: us-west-2

  5. Enter your default output format:

    • Choose between json, text, or table.

    • Example: json

Your configuration will be saved to ~/.aws/config and ~/.aws/credentials.

3. Verify Configuration

To verify that your AWS CLI is configured correctly, run a simple command like:

aws s3 ls
  • This command lists all the S3 buckets in your account, confirming that your AWS CLI is configured and able to communicate with AWS services.

4. Additional Configuration (Optional)

You can manually edit the ~/.aws/config and ~/.aws/credentials files to add profiles or configure specific settings.

Creating a New Profile:

To set up multiple profiles, add them to the ~/.aws/config file:

[profile myprofile]
region = us-west-2
output = json

And add corresponding credentials in the ~/.aws/credentials file:

[myprofile]
aws_access_key_id = YOUR_ACCESS_KEY_ID
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY

You can then use this profile with:

aws s3 ls --profile myprofile

Conclusion

By following these steps, you’ll have the AWS CLI installed and configured on your Linux machine, allowing you to manage AWS resources directly from the command line. This setup is essential for automating tasks, scripting, and integrating AWS services into your workflow.