Getting Started with AWS CLI
Jan 16, 2024Introduction
The AWS Command Line Interface (CLI) is an open-source utility that facilitates direct interaction with AWS services via the command line, enhancing automation capabilities through scripting. This blog post will explore the various versions of AWS CLI and provide step-by-step instructions on setting up the AWS CLI on popular operating systems.
AWS CLI V1 vs. V2
AWS CLI comes in two major versions: V1 and V2. The advent of AWS CLI V2 brought several enhancements that make it a more advisable choice. Key improvements in V2 include easier installation and updating processes that are independent of the system's Python environment, thus minimizing conflicts with other Python applications. It also introduces improved command input features like auto-suggestions and command auto-completion, enhancing user-friendliness. V2 offers additional output formats and better support for handling binary data, simplifying the work with complex outputs. Notably, V2 shows enhanced performance, with reduced start-up times and faster command execution. New features such as Single Sign-On (SSO) with AWS IAM Identity Center support in V2 simplify managing access to AWS services across teams or organizations. Therefore, we should always try to use AWS CLI V2 unless there is a specific reason.
Setting Up AWS CLI
Prerequisites
- An AWS account. If you don't have one, sign up at aws.amazon.com.
- An IAM user with necessary permissions. It's recommended to use an IAM user instead of your AWS root account.
Installation
-
Download the AWS CLI: Depending on your operating system (Windows, macOS, or Linux), download the appropriate version from the AWS CLI website.
-
Install the AWS CLI: Follow the installation instructions provided on the website for your specific operating system.
-
Verify Installation: Open your command-line interface and run
aws --version
to check if the installation was successful.
Configuration
We can configure AWS CLI in one of the following ways:
- Configure AWS CLI with IAM Identity Center (Recommended):
-
Run AWS SSO Configuration Command: Start with the command
aws configure sso
. -
SSO URL: Enter the SSO URL from your AWS SSO management console.
-
Region Specification: Specify the region associated with your SSO instance.
-
Enter SSO Username: Provide your SSO username.
-
Web Browser Authentication: You'll be prompted to authenticate in a web browser. This step typically involves your usual SSO credentials and potentially multi-factor authentication.
-
Choose Account and Role: After authentication, select the appropriate AWS account and IAM role that you need to access.
-
Session Duration: Optionally, you can set the duration of your SSO session.
-
Configuration Completion: Once these steps are completed, AWS CLI will store this configuration, allowing you to use AWS services via the CLI with SSO authentication.
-
-
Configure AWS CLI with Access Keys (Legacy way, not recommended): Run
aws configure
to set up your AWS CLI installation. You'll need to enter:- AWS Access Key ID
- AWS Secret Access Key
- Default region name
- Default output format (optional)
These details are obtained from your AWS Management Console.
-
aws s3 ls
to list your S3 buckets. This command verifies that your AWS CLI is configured correctly and can communicate with AWS services.
Once configured, we can run aws s3 ls --profile profilename to list our S3 buckets. This command verifies that our AWS CLI is configured correctly and can communicate with AWS services.
Basic AWS CLI Commands
- Working with S3:
- List Buckets:
aws s3 ls
- Copy File to S3:
aws s3 cp myfile.txt s3://mybucket/
- List Buckets:
- Managing IAM Users:
aws iam list-users
- Listing Instances:
aws ec2 describe-instances
Automating Tasks
One of the most significant advantages of AWS CLI is the ability to automate tasks using scripts. For example, we can create a script to back up files to S3 or manage EC2 instances.
Security Best Practices
- Regularly rotate your AWS Access Keys.
- Use IAM roles and policies to grant the least privilege necessary.
- Enable MFA for added security.
Conclusion
AWS CLI is a robust tool that can greatly enhance your productivity and efficiency when managing AWS services. By following this guide, you should now have a basic understanding of how to get started with AWS CLI. Happy cloud computing!
Stay connected with news and updates!
Join our mailing list to receive the latest news and updates from our team.
Don't worry, your information will not be shared.
We hate SPAM. We will never sell your information, for any reason.