Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS CLI not working with cron

I have this script:

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
SHELL=/bin/bash

# Create EBS Data snapshot
/usr/local/bin/aws ec2 create-snapshot --volume-id "vol-XXXXX" --description "test"

It works perfectly if I run it from the shell, but does nothing with Cron. Why? I am using IAM roles, is it important?

like image 273
Medical physicist Avatar asked Dec 01 '22 18:12

Medical physicist


2 Answers

Running aws configure as the root user didn't help me, as I had already configured the credentials, and the script ran quite happily for the root user directly from the command line, but would still not run through cron. The error message I was receiving was;

Unable to locate credentials. You can configure credentials by running "aws configure".

What helped me was adding the following lines to the top of my crontab file, so that the environment was in place for all scripts that ran through cron.

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Hope that helps someone!

like image 159
Dave Rix Avatar answered Dec 09 '22 10:12

Dave Rix


Ok, after several hours I found the solution:

The user root was running the script but AWS was not configured for this user. I only needed to configure AWS for the user root:

# aws configure
like image 24
Medical physicist Avatar answered Dec 09 '22 09:12

Medical physicist