The AWS Command Line Interface (CLI) is a powerful tool that enables you to interact with AWS services, including EC2, from the command line. By using AWS CLI, you can automate tasks, manage resources, and run scripts more efficiently. This article will guide you through some of the essential EC2 operations using AWS CLI, helping you to work smarter and faster.
Before we delve into EC2 operations, ensure you have set up your AWS CLI by running `aws configure` and providing your Access Key ID, Secret Access Key, default region, and default output format. Now, let's explore EC2 management with AWS CLI.
Launching and Managing EC2 Instances
Launching an EC2 instance is a common scenario. To launch an instance using AWS CLI, run the following command:
```bash aws ec2 run-instances --image-id ami-0123456789abcdef0 --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-0123456789abcdef0 --subnet-id subnet-0123456789abcdef0 ```
Replace the image ID, key pair, security group ID, and subnet ID with appropriate values for your environment. The instance type and count are also customizable.
Describing Running Instances
To list all running EC2 instances, use the describe-instances command:
```bash aws ec2 describe-instances --filters 'Name=instance-state-name,Values=running' ```
This command returns a detailed view of your running instances, including instance ID, instance type, private IP, and public IP.
Stopping and Starting EC2 Instances
Stopping an instance helps save costs when it's not in use. To stop an instance, use:
```bash aws ec2 stop-instances --instance-ids i-0123456789abcdef0 ```
Start it again later with:
```bash aws ec2 start-instances --instance-ids i-0123456789abcdef0 ```
Terminating and Creating EC2 Snapshots
Before terminating an instance, it's good practice to create a snapshot of the instance's volumes to preserve data. Use the following commands to create a snapshot and terminate the instance:
```bash aws ec2 create-snapshot --volume-id vol-0123456789abcdef0 --description 'MyVolumeSnapshot' ```
Once the snapshot is created, terminate the instance:
```bash aws ec2 terminate-instances --instance-ids i-0123456789abcdef0 ```
Managing EC2 Snapshots
To list all snapshots, use this command:
```bash aws ec2 describe-snapshots --owner self ```
Delete a snapshot with:
```bash aws ec2 delete-snapshot --snapshot-id snap-0123456789abcdef0 ```
Working with EC2 Security Groups
Security groups control inbound and outbound traffic for your EC2 instances. To create a new security group, use:
```bash aws ec2 create-security-group --group-name my-security-group --description ' команду: Доработают ОПИСАНИЕ ' ```
To add a rule to the security group, run:
```bash aws ec2 authorize-security-group-ingress --group-id sg-0123456789abcdef0 --protocol tcp --port 22 --cidr 0.0.0.0/0 ```
This allows incoming SSH traffic from any IP address.
Describing and Deleting Security Groups
To describe all security groups, run:
```bash aws ec2 describe-security-groups ```
To delete a security group, use:
```bash aws ec2 delete-security-group --group-id sg-0123456789abcdef0 ```
Remember to remove all rules from the security group before attempting to delete it.
Embracing AWS CLI for EC2 management amplifies productivity and streamlines your workflow. As you become more comfortable with these commands, consider incorporating them into scripts and automation processes to save time and lower human error. Happy exploring!











