Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EC2_HOME is not set. Run AWS Commands from Cron

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"

like image 324
goopcat Avatar asked Jan 09 '14 16:01

goopcat


1 Answers

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 and source *.{users shell extension} in this case aws-apitools-common.sh

$cat aws-apitools-common.sh 
 export AWS_PATH=/opt/aws
 export PATH=$PATH:$AWS_PATH/bin
 #Prefer JDK if present (i pulled the condition to de-clutter this info)
 export JAVA_HOME=/usr/lib/jvm/java
 export JAVA_HOME=/usr/lib/jvm/jre

Here is the relevant line:

for aws_product in $(find /opt/aws/apitools /opt/aws/amitools -maxdepth 1 -type l 2>/dev/null); do
[ -e $aws_product/environment.sh ] && source $aws_product/environment.sh; done

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):

$cat environment.sh
# Set EC2_HOME.  Called from /etc/profile.d/aws-product-common
[ -z "$EC2_HOME" ] && EC2_HOME="/opt/aws/apitools/ec2"
export EC2_HOME

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):

AWS_ACCESS_KEY="blah-blah-dingle-smith"
AWS_SECRET_KEY="yankee-doodle-shit-no-stank"
JAVA_HOME="/usr/lib/jvm/java"
EC2_HOME="/opt/aws/apitools/ec2"
EC2_URL="https://us-west-2.ec2.amazonaws.com/"
PATH="$PATH:/opt/aws/bin"  # is dir contains a symlinks of tool binaries

Cheers!

like image 109
jonretting Avatar answered Sep 19 '22 01:09

jonretting