0

I am using an EC2 instance with ubuntu 18.04 and python 3.6. I am running a python script uses aws cli calls for various things. Before running the script, I configured the aws cli with my access keys, default region etc.

When I run the script directly from the command line using:

$ /home/ubuntu/anaconda3/bin/python /home/ubuntu/blahblah/script_name.py

it runs just fine, the aws cli calls work fine, etc. The aws cli commands run the same way they do if I run them from the command line directly.

BUT, I need the script to start up and run automatically when i start the EC2 instance. So I made a service that calls this script and launch it through systemd. When I do this the service starts, the python script starts but it fails partway through when calling the aws cli. It gets an error message that suggests that the aws cli is not configured. The error message says, "You must specify a region. You can also configure your region by running "aws configure"."

I also ran the command

$aws configure list

inside the script. When run from the command line directly, it stated the configuration details I entered. When I run it through the systemd service it tells me (seen through $journalctl -u service_name.service) that the configuration values [access_key, secret_key, region] are 'not set'.

I also tried reconfiguring aws cli using 'sudo' and then calling the aws cli commands with sudo in front but got the same issue.

Any idea what is wrong?

John Corson
  • 123
  • 1
  • 6

1 Answers1

0

I have it working. I followed the instructions in this link, https://www.slashroot.in/how-to-install-and-configure-aws-cli-on-ubuntu-16-04. In the section on 'how to configure awsd cli for all users' it instructs to edit the /etc/environment file as seen below:

ubuntu@localhost:~$ cat /etc/environment PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games" AWS_ACCESS_KEY_ID=your_key AWS_SECRET_ACCESS_KEY=your_ultra_secret_key AWS_DEFAULT_REGION=your_favorite_region

After doing this and then switching to sudo in front of all aws cli commands, they worked whether called from command line, inside the python script launched from command line OR systemd service!

John Corson
  • 123
  • 1
  • 6