Jump to content
  • 0

Bash Script for instance start and stop on AWS EC2


rahul7827
 Share

Question

Hello Team

This might not be useful for some, I am asking just for future readers and those who would like to save the server cost.

I have many staging instances on aws ec2, which runs 24 X 7. While developers work in their working hours only.

I am trying to create a bash script which should auto start/stop the instance at a specified time (will use cron).

I tried this :

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

Giving me error like : aws command is invalid.

Is aws cli missing ?

What if the script stop the instance, will cron execute ? (Since instance is stop)

If anyone have done this or have any idea, Please suggest.

 

Thank you

  • Like 1
Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Yes you should have AWS CLI to run such commands. Beside this AWS provides SDKs for nearly every languages. Nowadays I have been improving my python knowledge so if I were you I can try that. You may check 
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html

Also AWS lambda functions may be another alternative to do such things on AWS. You can use python also as Lambda function scripts.

If you want to use it with CLI, I know someone who does this every time: @Murat Eminoglu 

  • Like 3
Link to comment
Share on other sites

  • 0
Quote

Giving me error like : aws command is invalid. 

The above error is a syntax error. Note the spaces and hyphens. 

aws ec2 stop-instances --instance-ids i-0d35245664


https://docs.aws.amazon.com/cli/latest/reference/ec2/stop-instances.html

By the way, it will not work if you add to cron directly as a binary name. You must give the full path of the binary.

/usr/local/bin/aws


A Cloudwatch rule can be created and stopped the instance when there is no load on the server. I can create something like that in my copious free time

@Burak Boto is also a good tool, thanks for the suggestion. 

Edited by Murat Eminoglu
  • Like 2
Link to comment
Share on other sites

  • 0
2 hours ago, Murat Eminoglu said:

Let me know if you are still having issues. We can jump on a meeting for your issue. 🙂

I appreciate you @Murat Eminoglu ❤️. Today i have stopped all my staging instances manually. This weekend I am occupied with some work so may be next week I could explore this more. Will let you know if i get stuck in any steps, or something may not clear to me. I will invite you for a meeting.

 

  • Like 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...