Automating AWS EC2 Instance Start, Stop, and Status Check with AWS CLI and Aliases

Managing EC2 instances via the AWS Console is straightforward, but what if you want to automate the process using the AWS Command Line Interface (CLI)? By leveraging the CLI, you can start, stop, and check the status of your EC2 instances with a single command. To make it even more efficient, you can create custom aliases to avoid typing lengthy commands every time.

In this post, I will guide you through the complete steps to achieve this, including setting up the AWS CLI, creating IAM policies for EC2 management, and creating aliases for faster instance control.


Prerequisites


Step 1: Installing the AWS CLI

Before you can manage your EC2 instances via the CLI, you need to install the AWS CLI on your machine.

For Linux / macOS:

Open a terminal and run the following commands:

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

unzip awscliv2.zip

sudo ./aws/install


For Windows:

Download and install the AWS CLI MSI installer from the official AWS website: AWS CLI Download.

To verify the installation, run:

aws --version

Step 2: Configuring AWS CLI (aws configure)

Once the CLI is installed, you need to configure it with your AWS credentials. You can get these credentials by creating an IAM User in the AWS Management Console.

To configure the AWS CLI:

Run the following command:
aws configure

This will save your credentials and settings in the ~/.aws/credentials and ~/.aws/config files.


Step 3: Creating an IAM Policy for EC2 Control

To start, stop, and check the status of EC2 instances, you need an IAM policy with the necessary permissions. Follow these steps to create a custom IAM policy.

3.1 Create the Policy

Select the JSON tab and paste the following policy:

{

  "Version": "2012-10-17",

  "Statement": [

    {

      "Effect": "Allow",

      "Action": [

        "ec2:StartInstances",

        "ec2:StopInstances",

        "ec2:DescribeInstances"

      ],

      "Resource": "arn:aws:ec2:*:*:instance/*"

    }

  ]

}


3.2 Attach the Policy to an IAM User


Step 4: Using AWS CLI to Start, Stop, and Check EC2 Instances

Once your IAM user has the necessary permissions, you can control your EC2 instances from the command line.

To Start an EC2 instance:

aws ec2 start-instances --instance-ids i-xxxxxxxxxxxxxxxxx


To Stop an EC2 instance:

aws ec2 stop-instances --instance-ids i-xxxxxxxxxxxxxxxxx


To Check the status of an EC2 instance:

aws ec2 describe-instances --instance-ids i-xxxxxxxxxxxxxxxxx --query "Reservations[*].Instances[*].State.Name"


Replace i-xxxxxxxxxxxxxxxxx with your actual EC2 instance ID. You can find this ID in the AWS Console or by running the following command:

aws ec2 describe-instances --query "Reservations[*].Instances[*].InstanceId"


Step 5: Creating Aliases for Quick Commands

Manually typing these long commands can be tedious. Luckily, you can create aliases to simplify the process. Here’s how you can create aliases for your EC2 start, stop, and status commands on Linux, macOS, and Windows.

For Linux/macOS (Bash/Zsh)

Open your .bashrc (for Bash) or .zshrc (for Zsh) file:
nano ~/.bashrc   # for Bash

nano ~/.zshrc    # for Zsh

alias ec2start='aws ec2 start-instances --instance-ids i-xxxxxxxxxxxxxxxxx'

alias ec2stop='aws ec2 stop-instances --instance-ids i-xxxxxxxxxxxxxxxxx'

alias ec2status='aws ec2 describe-instances --instance-ids i-xxxxxxxxxxxxxxxxx --query "Reservations[*].Instances[*].State.Name"'


Reload the shell configuration:
source ~/.bashrc   # for Bash

source ~/.zshrc    # for Zsh

Open the PowerShell profile:
notepad $PROFILE

Set-Alias ec2start 'aws ec2 start-instances --instance-ids i-xxxxxxxxxxxxxxxxx'

Set-Alias ec2stop 'aws ec2 stop-instances --instance-ids i-xxxxxxxxxxxxxxxxx'

Set-Alias ec2status 'aws ec2 describe-instances --instance-ids i-xxxxxxxxxxxxxxxxx --query "Reservations[*].Instances[*].State.Name"'


Conclusion

By following these steps, you can easily manage your AWS EC2 instances using the AWS CLI and automate routine tasks with aliases. This setup simplifies starting, stopping, and checking the status of instances, making it especially useful for those who regularly work with EC2 instances in development or production environments.

With a few configuration steps, your AWS management can become significantly more efficient!