Introduction to AWS CLI
The AWS Command Line Interface (AWS CLI) provides a unified tool to manage your AWS services from your terminal or command prompt.
The AWS Command Line Interface (AWS CLI) is a tool that enables you to interact with AWS services from your terminal or command prompt. It provides a powerful way to automate tasks and manage resources [source_page 2]. It is a powerful command-line tool that allows you to interact with various AWS services directly from your terminal. It’s available for Linux, macOS, and Microsoft Windows, offering a flexible alternative to the graphical AWS Management Console [source_page 2].
The AWS CLI offers the ability to manage various AWS services (e.g., EC2, S3, IAM) through commands [source_page 2]. It provides automation capabilities through scripting and programming, leading to enhanced productivity through command-line efficiency [source_page 2]. It facilitates easier management of numerous resources compared to the AWS Management Console [source_page 2]. It supports multiple operating systems (Linux, macOS, Windows) [source_page 2] and allows configuration of default settings (access key ID, secret access key, default region, output format) using `aws configure` [source_page 2].
There are three primary ways to interact with AWS services: AWS Management Console (a user-friendly graphical interface), AWS CLI (a command-line interface for greater automation and scripting), and SDKs (Software Development Kits - programming language-specific packages for integrating AWS into applications) [source_page 2].
The AWS CLI automates repetitive tasks [source_page 4], integrates with scripting languages (Bash, Python) [source_page 4], is faster than console interaction [source_page 4], and provides access to all 250+ AWS services [source_page 4].
Installation and Configuration
procedure
The AWS CLI is available across various operating systems, and its installation and configuration are fundamental steps to start managing AWS resources programmatically.
The AWS CLI is available for Linux, macOS, and Microsoft Windows, offering a flexible alternative to the graphical AWS Management Console [source_page 2].
Prerequisites
- Access Key Pairs are required for programmatic access with AWS CLI and SDK. These consist of an Access Key ID and a Secret Access Key and must be kept secure and rotated regularly [source_page 10].
- For CI/CD deployments using AWS CLI via GitHub Actions, an IAM user needs programmatic access (Access Key ID and Secret Access Key) and specific policies like `AWSElasticBeanstalkFullAccess` and `AmazonS3FullAccess` [source_page 9].
1
Install AWS CLI on Linux: Download the AWS CLI package using `curl`, extract it with `unzip`, and run the installer using `sudo ./aws/install`. Verify the installation by running `aws --version` [source_page 2].
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version
2
Install AWS CLI on Windows: Download and run the AWS CLI MSI installer. Verify the installation using `aws --version` [source_page 2].
3
Install AWS CLI on macOS: Download and install the AWS CLI package via a URL. Verify the installation using `aws --version` [source_page 2].
4
Configure AWS CLI: Run the `aws configure` command. The CLI will prompt you to enter your `Access Key ID`, `Secret Access Key`, `Default Region Name` (e.g., `us-east-1`), and `Default Output Format` (e.g., `json`) [source_page 2].
This configures the default credentials and settings for your AWS CLI interactions.
aws configure
AWS CLI Command Structure and Options
Understanding the fundamental command structure and available options is crucial for efficient and effective use of the AWS CLI.
The basic format of an AWS CLI command is: `aws <service> <operation> [options and parameters]` [source_page 2]. The `<service>` specifies the AWS service (e.g., `ec2`, `s3`) [source_page 2]. The `<operation>` is the action to perform (e.g., `stop-instances`, `run-instances`) [source_page 2]. `[options and parameters]` are additional settings and arguments, with parameters preceded by two hyphens (`--`) [source_page 2].
Technical Specs: Example: `aws ec2 stop-instances --instance-ids i-1234567890abcdef0 --output json`
The AWS CLI provides essential options to refine command execution:
- `--query`: Filters results on the client-side using JMESPath syntax, allowing for selecting specific parts of the JSON response [source_page 2].
- `--filter`: Filters results on the server-side, improving efficiency for large datasets [source_page 2].
- `--dry-run`: Checks permissions without executing the command, which is useful for testing [source_page 2].
The AWS CLI offers various output formats: JSON (JavaScript Object Notation): The default format, ideal for programmatic use. Table: A human-readable tabular format (can be set using `export AWS_DEFAULT_OUTPUT="table"`). Text: A tab-delimited text format, suitable for scripts (can be set using `export AWS_DEFAULT_OUTPUT="text"`) [source_page 2].
You can get detailed information about commands and options by using the `aws help`, `aws <service> help`, or `aws <service> <operation> help` commands [source_page 2].
An initial command execution error was encountered due to missing double quotes around the region parameter during a Systems Manager agent installation command [source_page 1]. This highlights the importance of correct syntax when passing parameters.
Common AWS CLI Commands and Integrations
The AWS CLI allows you to manage various AWS services through commands, facilitating automation and interaction with your cloud resources [source_page 2].
Here are common commands for interacting with key AWS services and how the CLI integrates with others.
Amazon EC2 Commands
Commands for managing Elastic Compute Cloud (EC2) instances and related resources.
Launch EC2 instances
`aws ec2 run-instances`
Retrieve information about EC2 instances
`aws ec2 describe-instances`
Create an EBS volume
`aws ec2 create-volume`
Create a VPC
`aws ec2 create-vpc`
Stop instances (example)
`aws ec2 stop-instances --instance-ids i-1234567890abcdef0 --output json`
Amazon S3 Commands
Commands for managing Simple Storage Service (S3) buckets and objects.
List S3 objects or buckets
`aws s3 ls`
Copy files to/from S3
`aws s3 cp`
Move files in S3
`aws s3 mv`
Delete S3 objects
`aws s3 rm`
Create an S3 bucket (example)
`aws s3 mb s3://saa-quiz-app-static`
Copy files to S3 recursively (example)
`aws s3 cp web/static/ s3://saa-quiz-app-static/static/ --recursive`
AWS IAM Commands
Commands for managing Identity and Access Management (IAM) resources.
List IAM users
`aws iam list-users`
Enable MFA Delete for S3 buckets
Can only be enabled using AWS CLI, not the AWS Management Console
AWS Secrets Manager Commands
Commands for storing and managing secrets securely.
Create a secret (example)
`aws secretsmanager create-secret --name saa-quiz-app/rds-credentials --secret-string '{"username":"postgres","password":"YOUR_DB_PASSWORD","host":"YOUR_RDS_ENDPOINT","port":"5432","dbname":"saa_quiz_db"}'`
AWS CloudWatch Commands
Commands for creating and managing CloudWatch alarms.
Put metric alarm (CPU utilization example)
`aws cloudwatch put-metric-alarm --alarm-name saa-quiz-high-cpu --alarm-description "Alert when CPU exceeds 80%" --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period 300 --threshold 80 --comparison-operator GreaterThanThreshold --evaluation-periods 2`
Put metric alarm (5xx errors example)
`aws cloudwatch put-metric-alarm --alarm-name saa-quiz-5xx-errors --alarm-description "Alert on 5xx errors" --metric-name HTTPCode_Target_5XX_Count --namespace AWS/ApplicationELB --statistic Sum --period 300 --threshold 10 --comparison-operator GreaterThanThreshold --evaluation-periods 1`
AWS Systems Manager Agent Installation Parameters (PowerShell for SSM Agent)
When installing the SSM Agent on hybrid resources, the installation command, though often a PowerShell script, utilizes parameters that are common in AWS command-line tools.
Region parameter
`--region "us-east-1"`
Activation key parameter
`--activation-key "YOUR_ACTIVATION_CODE"`
Activation ID parameter
Required for installation along with activation key
Example Command Snippet
`powershell & "C:\Program Files\Amazon\SSM\amazonaws.com" /register-instance --register-with-amazon --region "us-east-1" --private-key "C:\Program Files\Amazon\SSM\etc\amazon-ssm-agent.pem" --activation-key "YOUR_ACTIVATION_CODE" --expected-container-path "C:\Program Files\Amazon\SSM" --installer-type "msi" --target-instance-id "i-0123456789abcdef0"`
AWS CLI within AWS CloudShell
AWS CloudShell offers a convenient, browser-based environment pre-configured with the AWS CLI, streamlining command-line interactions with AWS services.
AWS CloudShell provides an in-browser shell environment, eliminating the need to install CLI locally [source_page 4]. It comes pre-configured with the AWS CLI and SDKs [source_page 4].
AWS CloudShell is accessible via a CloudShell icon in the AWS Management Console’s top ribbon [source_page 4]. No manual configuration of access keys is required; it uses existing IAM permissions [source_page 4]. Users can directly execute AWS CLI commands (e.g., `aws s3 ls`, `aws iam list-users`) within this environment [source_page 4].