Introduction to AWS CLI: Basic Commands for Beginners
The Amazon Web Services Command Line Interface (AWS CLI) is a powerful tool that enables users to interact with various AWS services using commands in the terminal or command prompt. This tutorial aims to introduce fundamental AWS CLI commands with explanations for beginners.
Prerequisites:
- AWS Account: Sign up for an AWS account if you haven’t already.
- AWS CLI Installation: Install AWS CLI on your local machine. Refer to the official AWS documentation for installation instructions on various operating systems.
Getting Started:
Configure AWS CLI
Open your terminal or command prompt and type:
aws configure
Follow the prompts to enter:
- AWS Access Key ID
- AWS Secret Access Key
- Default region (e.g., us-west-2)
- Default output format (e.g., json)
Once you have everything setup, the following are commands grouped by services or usage.
AWS Settings and Configuration:
Set Default Region:
aws configure set region your-region
Sets the default region for AWS CLI commands.
Set Output Format:
aws configure set output json
Sets the default output format for AWS CLI commands.
Enable/Disable Paging:
aws configure set cli_pager enabled/disabled
Enables or disables paging for command output.
Profile Management:
AWS profiles are configurations within the AWS Command Line Interface (CLI) that store credentials and settings for accessing AWS services. They allow users to manage multiple sets of AWS credentials and switch between them easily. Each profile can contain its own set of credentials, default region, output format, and other configurations.
List Profiles:
aws configure list-profiles
Lists all configured profiles.
Create a Profile:
aws configure --profile your-profile-name
Creates a new named profile.
Switch Profiles (Temporary):
export AWS_PROFILE=your-profile-name
Temporarily sets a specific profile for the current terminal session.
Delete a Profile:
aws configure --profile your-profile-name delete
Deletes a specific named profile.
AWS Simple Storage Server (S3)
Amazon Simple Storage Service (S3) is a scalable, secure, and highly available object storage service provided by Amazon Web Services (AWS). It allows users to store and retrieve any amount of data from anywhere on the web, offering a simple web services interface to access and manage data.
List S3 Buckets
To view all S3 buckets in your account, use:
aws s3 ls
Explanation: This command lists all the S3 buckets in your AWS account.
Create an S3 Bucket
aws s3 mb s3://your-bucket-name
This command creates a new S3 bucket with the provided name.
Upload a file to an S3 bucket:
aws s3 cp /path/to/your/file.txt s3://your-bucket-name
This command copies a file from your local system to the specified S3 bucket.
Download a File from S3 Bucket
aws s3 cp s3://your-bucket-name/file.txt /path/to/destination
It retrieves a file from the specified S3 bucket and saves it to the provided local destination.
Delete an empty S3 bucket
aws s3 rb s3://your-bucket-name
This command removes the specified empty S3 bucket.
AWS Elastic Compute Cloud (EC2)
Amazon Elastic Compute Cloud (EC2) is a core service provided by Amazon Web Services (AWS) that enables users to rent virtual servers, known as instances, on which they can run applications. EC2 offers a scalable computing capacity in the cloud, allowing users to configure instances as needed, with control over CPU, memory, storage, and networking.
Here are some usefull commands:
List all running EC2 instances
aws ec2 describe-instances
It displays information about all running EC2 instances in your AWS account.
Start a stopped EC2 instance:
aws ec2 start-instances --instance-ids your-instance-id
This command initiates a stopped EC2 instance using its instance ID.
Stop a running EC2 instance:
aws ec2 stop-instances --instance-ids your-instance-id
It halts a running EC2 instance specified by its instance ID.
AWS Identity and Access Management (IAM)
AWS Identity and Access Management (IAM) is a web service that helps securely control access to AWS services and resources. It enables users to manage users, groups, roles, and their permissions within an AWS account.
Basic IAM Operations:
List IAM users in your AWS account:
aws iam list-users
This command provides a list of all IAM users in your AWS account.
Create a new IAM user:
aws iam create-user --user-name your-username
This command generates a new IAM user with the provided username.
Attach Policy:
aws iam attach-user-policy --policy-arn your-policy-arn --user-name your-username
Attaches an IAM policy to a user.
Conclusion:
AWS CLI offers a convenient way to manage AWS services through simple commands. These basic commands provide a starting point for exploring and utilizing various AWS functionalities.
Refer to the official AWS CLI documentation for more commands and advanced usage.
Note: Always use AWS CLI commands carefully to avoid unintended actions or charges.