1) Install AWS CLI


Configure it with your Access ID and Access Key

aws configure

Access key ID :AKIARKVK52ZJW7V2T4PFMK
Secret access key: vHehC4hcXi87e7E3ueO6AJmaG9ou2

AWS Access Key ID: Give your Access Key ID
AWS Secret Access Key: Give your Access Key

Default region name [ap-south-1]: us-east-1 (Give whatever region you want)
Default output format [json]:

2) Creating the EC2 Instance

Syntax

aws ec2 run-instances \
–image-id ami-0ae8f15ae66fe8cda \ # Replace with your desired AMI ID
–instance-type t2.micro \ # Replace with your desired instance type
–key-name virginia.pem \ # Replace with your key pair name
–security-groups sg-03507ecf7a754f334 \ # Replace with your security group ID
–count 1

aws ec2 run-instances –image-id –count 1 –instance-type t2.micro –key-name –security-group-ids –subnet-id

Example

aws ec2 run-instances –image-id ami-0ae8f15ae66fe8cda –count 1 –instance-type t2.micro –key-name virginia –security-group-ids sg-03507ecf7a754f334

Now run this command to see all the instances plus this new one too.

aws ec2 describe-instances

3) Stopping the EC2 Instance

aws ec2 stop-instances –instance-ids i-xxxxxxxxxxxx

Example

aws ec2 stop-instances –instance-ids i-00d85a79cdfd6786d

4) Starting the EC2 Instance

aws ec2 start-instances –instance-ids i-xxxxxxxxxxxx

Example

aws ec2 start-instances –instance-ids i-00d85a79cdfd6786d

5) Rebooting an EC2 Instance

aws ec2 reboot-instances –instance-ids i-xxxxxxxxxxxx

6) Changing Instance Type

aws ec2 modify-instance-attribute –instance-id i-xxxxxxxxxxxx –attribute instanceType –value t3.medium

Replace t3.medium with your desired instance type.

7) Terminating an EC2 Instance

aws ec2 terminate-instances –instance-ids i-xxxxxxxxxxxx

aws ec2 terminate-instances –instance-ids i-00d85a79cdfd6786d