Basicly I'm not sure how to get my aws commands to run in crontab. I know I need to give crontab some environmental variables so that it can run the aws commands but, I don't quite know how to do that. Has anyone been able to do this before?
PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin:/opt/aws/bin
* * * * * export EC2_HOME=/opt/aws/apitools/ec2
* * * * * export JAVA_HOME=/usr/lib/jvm/jre
0 8 * * 1-5 ec2-start-instances instance_id
0 7 * * 1-5 ec2-start-instances instance_id
0 7 * * 1-5 ec2-start-instances instance_id
I am on an EC2 Amazon machine and I'm able to run the aws commands in the EC2-user shell. I'm just having trouble getting the commands to run from crontab.
The mail sent to the ec2 user says "/opt/aws/bin/ec2-start-instances: line 9: EC2_HOME: EC2_HOME is not set"
First, you should upgrade to the new AWS CLI.
Then you can put your access keys and secret keys into ~/.aws/config, and you won't have a problem. The new tools support multiple profiles, and don't rely on env variables.
Edit: If your problem is simply setting the env for jobs run from cron, take a look at:
Where can I set environment variables that crontab will use?
This should work for you... Needed to setup a few systems with automated cron git pull scripts, which would then execute crontab/anacron run-parts bash scripts which called upon ec2 java tools. Normally in an amz-linux-ami (their RH clone) the creating of env vars goes as follows:
(going to stick to relevant files)
Do shell login -->
source /etc/profile
which will loop through /etc/profile.d andsource *.{users shell extension}
in this caseaws-apitools-common.sh
Here is the relevant line:
So the script
aws-apitools-common.sh
searches for apitools and amitools, then sources those tools$aws_product/environment.sh
. ex:source /opt/aws/apitools/ec2/environment.sh
Now this is probably what you are looking for (/opt/aws/apitools/ec2/environment.sh):
In short the environmental vars you want set/export in your crontab script, and or your anacron run-parted scripts would be (personally i load in key.conf files which are generated from a git repo, and having a git pull fire off every hour, so keys and the env cat be updated just like the scripts themselves. Then the job script sources the conf file):
Cheers!
I found this answer on the AWS Developer Forum that worked for me. Just copied it here to make it easier to reach: