unable to auto start/stop aws ec2 instance - amazon-ec2

i wanted to automate the ec2 instance's start & stop and configured the crontab on an instance x. I followed these steps
1) Edited the crontab -e of instance X.
2) and added these lines
15 04 * * * username ec2-start-instances i-f1814c90
15 07 * * * username ec2-stop-instances i-f1814c90
10 10 * * * username ec2-start-instances i-f1814c90
3) and restarted the cron using sudo /etc/init.d/cron restart
But still am unable to either start or stop the ec2 instance using cronjob.
thanks,
Anand

Most likely the issue is that the AWS data need to run the ec2 start and stop commands are not in the cron environment.
Its better to write a separate script that does this, instead of of making the ec2 commands on the cron like that.

This is why AWS Data Pipeline is for (working fine):
https://aws.amazon.com/premiumsupport/knowledge-center/stop-start-ec2-instances/
Just mind the trap: --region eu-west-1 NOT --region eu-west-1a (which is an availability zone).

I'd suggest to Schedule EC2 Start / Stop using AWS Lambda
You don't need anything more than a small script or two that you schedule. No instance to launch, just a quick invocation of the script you have built. Pick the programming language of your choice and use the AWS SDK to perform instance operations. A quite lightweight solution,.

Related

Executing a Unix Shell Script in AWS or GCP Environment

I have developed a shell script which connects to the AWS EKS Cluster, Execute a kubectl command, get the result as JSON string and insert into AWS RDS MySQL DB. Tested the script in AWS Cloud Shell which is fine there. Now, I want to schedule the script (using CRON Scheduler may be ) every 30 minutes. I am exploring options on where to schedule the Unix Shell Job. Below are the options I can see from web :-
From an AWS EC2 Instance
Using AWS Systems Manager
For #1, I can start an EC2 instance free tier and schedule the cron job.
For #2, not very sure on how AWS Systems Manager works.
Is there any other approach to schedule a shell job in AWS or GCP ?

Start AWS EC2 instance, run commands, stream logs to console and terminate

Trying to run few steps of CI/CD in a EC2 instance. Please don't ask for reasons.
Need to:
1) Start an instance using AWS CLI. Set few environment variables.
2) Run few bash commands.
3) Stream the command from the above commands into the console of the caller script.
4) If any of the commands fail, need to fail the calling script as well.
5) Terminate the instance.
There is a SO thread which indicates that streaming the output is not as easy. [1]
What I would do, if I had to implement this task:
Start the instance using the cli command aws ec2 run-instances and using an AMI which has the AWS SSM agent preinstalled. [2]
Run your commands using AWS SSM. [3] This has the benefit that you can run any number of commands you want - whenever you want (i.e. the commands must not be specified at instance launch, but can be chosen afterwards). You also get the status code of each command.[4]
Use the CloudWatch integration in SSM to stream your command output to CloudWatch logs. [5]
Stream the logs from CloudWatch to your own instance. [6]
Note: Instead of streaming the command output via CloudWatch, you could also periodically poll the SSM API by using aws ssm get-command-invocation. [7]
Reference
[1] How to check whether my user data passing to EC2 instance working or not?
[2] Working with SSM Agent - AWS Systems Manager
[3] Walkthrough: Use the AWS CLI with Run Command - AWS Systems Manager
[4] Understanding Command Statuses - AWS Systems Manager
[5] Streaming AWS Systems Manager Run Command output to Amazon CloudWatch Logs | AWS Management Tools Blog
[6] how to view aws log real time (like tail -f)
[7] get-command-invocation — AWS CLI 1.16.200 Command Reference
Approach 1.
Start an instance using AWS CLI.
aws ec2 start-instances --instance-ids i-1234567890abcdef0
Set few environment variables.
Use user dat of ec2 to set env. & run commands
..
Run your other logic / scripts
To terminate the instance run below command in the same instance.
instanceid=`curl http://169.254.169.254/latest/meta-data/instance-id`
aws ec2 terminate-instances --instance-ids $instanceid
Approach 2.
Use python boto3 or kitchen chef ci.

How to cron job setup in Amazon ec2

I have an Amazon EC2 instance running my website. I need to setup a Cron Job to run my file every 12hours.
if file setup via command line so please give a detail step wise.
Does anyone have any advise?
Thanks for your time.
I recently began using Amazon's linux distro on ec2 instances and after trying all kinds of things for cron all I needed was:
sudo service crond start
crontab -e
This allowed me to set a cron job as "ec2-user" without specifying the user. For example:
0 12 * * * python3 example.py
In fact, specifying a user here prevented it from running. I also posted this here.
It's just normal cron.
See: HowTo: Add Jobs To cron Under Linux or UNIX?

Amazon EC2 automatic reboot and script running

I'm trying to get a amazon ec2 instance to automatically reboot each 2 hours, and after run a shell (.sh) script on startup. Any ideas?
For rebooting after 2 hours, if it is Ubuntu (or Debian based distro) just put on root's cron (by entering sudo crontab -e):
* */2 * * * /sbin/shutdown -r now
I am very curious as to why you need to reboot every 2 hours... I hope this is not a production server... but you could run a cronjob to do the rebooting, what type of instance do you have, is it linux or windows?
I have a centos instance and I have modified /etc/rc.d/rc.local to run a script after booting up but then again you could easily run a script via cronjob to check whether something is running or not...
I guess we need more information from you as to what you are trying to do and what type of instance you have.

instructions/manual on auto launch/shutdown on EC2

need pretty trivial task
i have server, which in crontab every night will run "something" what will launch new EC2 instance, deploy there code (ruby script), run it, upon completion of the script shutdown the instance.
how to do it the best?
thanks.
Here's an approach that can accomplish this without any external computer/cron job:
EC2 AutoScaling supports schedules for running instances. You could use this to start an instance at a particular time each night.
The instance could be of an AMI that has a startup script that does the setup and running of the job. Or, you could specify a user-data script be passed to the instance that does this job for you.
The script could terminate the instance when it has completed running.
If you are running EBS boot instance, then shutdown -h now in your script will terminate the instance if you specify instance-initiated-shutdown-behavior of terminate.

Resources