Sometimes you need to run the aws cli commands from the cronjob especially if you have the backup scripts that runs periodically and uploads to S3 bucket storage. So you might see the cron jobs run successfully as scheduled but the upload to S3 storage which uses the aws cli commands might not work . Unless you use the –debug with aws cli commands you won’t see anything in the logs .
Once you configure the aws cli initially , there is a folder called .aws created under your root home directory .
The issue with crontab is the crontab sets HOME to ‘/’ and then the aws commands will not find the ‘.aws’ and the cron user won’t be able to fine the ‘.aws’ folder .
This can be solved with the environment variables . The AWS cli uses the following environment variables :
AWS_SHARED_CREDENTIALS_FILE
AWS_CONFIG_FILE
So you can add the following environment credentials to the environment variables . Also make sure you have a back up of ‘/etc/environment’ incase something goes wrong .
echo 'AWS_SHARED_CREDENTIALS_FILE="/home/ubuntu/.aws/credentials"' >> /etc/environment
echo 'AWS_CONFIG_FILE="/home/ubuntu/.aws/config"' >> /etc/environment
WARNING : Make sure the append is ‘>>’ . If its just ‘>’ , it will empty everything else on that file .
Leave a Reply