aws,

How to stop all running EC2 instances from the command line

Jun 27, 2021 · 2 mins read · Post a comment

Stopping preproduction, staging environment EC2 instances could be described sometimes as a good practice. In the next few lines, I’ll show you how you can easily stop all running EC2 instances from the CLI in a single command.

Prerequisites

  • AWS CLI

Stop all running EC2 instances

Step 1. Open Terminal and check if AWS CLI is installed.

aws --version

Output:

aws-cli/2.1.39 Python/3.9.4 Darwin/20.5.0 source/x86_64 prompt/off

Step 2. Check if AWS CLI is configured.

aws configure list

Output:

      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                <not set>             None    None
access_key     ****************ABCD      config_file    ~/.aws/config
secret_key     ****************ABCD      config_file    ~/.aws/config
    region                us-west-2              env    AWS_DEFAULT_REGION

Step 3. Get a list of all running EC2 instances.

aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query 'Reservations[].Instances[].InstanceId'

Depending on the number of instances, you will get a JSON response including the instances IDs.

Output:

[
    "i-<some-instance-id>",
    "i-<some-other-instance-id>"
]

Step 4. Replace new lines with spaces using the Linux command line utility tr.

aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query 'Reservations[].Instances[].InstanceId' --output text | tr '\n' ' '

Output:

i-<some-instance-id> i-<some-other-instance-id> %

Step 5. It’s always a good practice to execute a dry run.

aws ec2 stop-instances --dry-run --instance-ids $(aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query 'Reservations[].Instances[].InstanceId' --output text | tr '\n' ' ')

Output:

An error occurred (DryRunOperation) when calling the StopInstances operation: Request would have succeeded, but DryRun flag is set.

Step 6. Remove the --dry-run flag and stop all running EC2 instances.

aws ec2 stop-instances --instance-ids $(aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query 'Reservations[].Instances[].InstanceId' --output text | tr '\n' ' ')

Conclusion

The next key step is automation. You could use a cronjob on your local machine, HA server or even better, deploy a lambda function that will trigger the stop command. On the other hand, don’t forget to create a command that will start all stopped EC2 instances every morning. This will be covered in the next upcoming posts.
Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.