Using AWS API to start and stop EC2 instances

If you’ve already setup API access into your AWS account and those credientals have access to your EC2 instances, then you may be wondering, how to I start up an instance or stop one?

This will be a really short post becuase it’s really simple.

Assuming you’re already setup with an AWS cli and it’s completely configured, all you need is to get the instance ID of the the EC2 you want to start.

Login to your AWS account, go to EC2 and view your instances.

Right under “Instance ID” you’ll see the instance ID of your server. This one is blanked out but they typically start with an i-, followed by a bunch of random characters.

Here is the command to start the instance:

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

Obviously, replace the instance id (i-zzzzzzzzzzzzzzzzzzz) with your own.

To stop your instance:

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

What is really cool about this is you can list out instances you want to start. Say you want to start 3 different instances. This is how you’d do it:

aws ec2 stop-instances --instance-ids i-zzzzzzzzzzzzzzzzzzz1 i-zzzzzzzzzzzzzzzzzzz2 i-zzzzzzzzzzzzzzzzzzz3

Replacing each instance ID and add as many as you need to start or stop.

That’s it, really simple stuff!

Have an amazing Builditlinux day!

Leave a Reply