AWS CLI Basics
What is the AWS CLI?
The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.
Installing the AWS CLI
To install the AWS CLI:
- Go to the AWS CLI page.
- Download the appropriate installer for your operating system (Windows, macOS, or Linux).
- Run the downloaded installer and follow the on-screen instructions.
- Verify the installation by opening a terminal and typing
aws --version
. You should see the version information displayed.
Configuring the AWS CLI
After installing the AWS CLI, you need to configure it with your AWS credentials and region:
- Open a terminal and run
aws configure
. - Enter your AWS Access Key ID when prompted.
- Enter your AWS Secret Access Key when prompted.
- Enter your default region name (e.g., us-west-2) when prompted.
- Enter your default output format (e.g., json) when prompted.
Basic AWS CLI Commands
Here are some basic AWS CLI commands:
- List S3 Buckets:
aws s3 ls
- Create an S3 Bucket:
aws s3 mb s3://my-bucket
- Copy a File to S3:
aws s3 cp myfile.txt s3://my-bucket/
- List EC2 Instances:
aws ec2 describe-instances
- Start an EC2 Instance:
aws ec2 start-instances --instance-ids i-1234567890abcdef0
- Stop an EC2 Instance:
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
Example: Creating an S3 Bucket and Uploading a File
Let's create an S3 bucket and upload a file to it:
Step-by-Step Example:
- Open a terminal.
- Run
aws s3 mb s3://my-example-bucket
to create a new S3 bucket named "my-example-bucket". - Run
aws s3 cp example.txt s3://my-example-bucket/
to upload a file named "example.txt" to the bucket. - Run
aws s3 ls s3://my-example-bucket/
to list the contents of the bucket and verify that the file was uploaded successfully.
Using AWS CLI with EC2
The AWS CLI can also be used to manage EC2 instances:
- Describe EC2 Instances:
aws ec2 describe-instances
- Start an EC2 Instance:
aws ec2 start-instances --instance-ids i-1234567890abcdef0
- Stop an EC2 Instance:
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
Example: Starting and Stopping an EC2 Instance
Let's start and stop an EC2 instance:
Step-by-Step Example:
- Open a terminal.
- Run
aws ec2 describe-instances
to list your EC2 instances and find the instance ID of the instance you want to manage. - Run
aws ec2 start-instances --instance-ids i-1234567890abcdef0
(replace with your instance ID) to start the instance. - Run
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
(replace with your instance ID) to stop the instance.
Advanced AWS CLI Features
The AWS CLI offers many advanced features, such as:
- Filtering Output: Use the
--query
option to filter the output of your AWS CLI commands. For example,aws ec2 describe-instances --query 'Reservations[*].Instances[*].InstanceId'
will return only the instance IDs. - Using Profiles: Create multiple configuration profiles to manage different AWS accounts or roles. Use the
--profile
option to specify which profile to use for a command. For example,aws s3 ls --profile myprofile
will use the "myprofile" configuration. - Scripting and Automation: Combine AWS CLI commands with shell scripts to automate tasks. For example, a script to back up a directory to S3 could use
aws s3 sync mydirectory/ s3://my-bucket/
.
Conclusion
The AWS CLI is a powerful tool that allows you to manage your AWS services from the command line. By understanding the basics of the AWS CLI, including how to install, configure, and use it to manage resources such as S3 and EC2, you can effectively automate and streamline your AWS operations.