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
AWS Account: Ensure you have an active AWS account.
IAM User with Necessary Permissions: You will need an IAM user with programmatic access and the appropriate permissions to manage EC2 instances.
AWS CLI Installed: Install the AWS CLI on your local machine (Linux, macOS, or Windows).
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:
Open the terminal or command prompt.
Run the following command:
aws configure
You will be prompted to enter the following information:
AWS Access Key ID: Get it from your IAM user credentials.
AWS Secret Access Key: Also from your IAM user credentials.
Default region name: The AWS region where your EC2 instances are located (e.g., us-east-1).
Default output format: You can choose json, text, or table (default is json).
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
In the AWS Management Console, go to IAM → Policies and click Create 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/*"
}
]
}
Name the policy (e.g., EC2StartStopPolicy) and create it.
3.2 Attach the Policy to an IAM User
In the IAM dashboard, navigate to Users, select the IAM user you want to use with the AWS CLI.
Click Permissions and then Add Permissions.
Choose Attach policies directly, search for the EC2StartStopPolicy you created, and attach it to the 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
Add the following aliases:
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"'
Save and exit the file (press Ctrl + X, then Y, and Enter).
Reload the shell configuration:
source ~/.bashrc # for Bash
source ~/.zshrc # for Zsh
For Windows (PowerShell)
Open the PowerShell profile:
notepad $PROFILE
Add the following aliases:
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"'
Save the file and restart PowerShell or run:
. $PROFILENow you can simply use:
ec2start to start your EC2 instance.
ec2stop to stop it.
ec2status to check the current status.
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!