Ray Chan
Ray Chan

Reputation: 61

Activating conda environment with cloud-init

We are trying to run batch scripts on load on a AWS EC2 instance using userdata (which I understand is based off of cloud-init). Since the code runs in a conda environment, we are trying to activate it prior to running the Python/Pandas code. We noticed that the PATH variable isn't getting set correctly. (even though it was set correctly prior to making the image, and is set correctly for all users after SSH'ing into instance)

We've tried modifiying the path in the shell script, but nothing is sticking. We ran the following code:

#!/bin/bash
export=/opt/conda/bin:$PATH
which python
which conda
conda activate etl

We checked $PATH before and after running export=/opt/conda/bin (no change). which python returns the wrong python, and which conda returns not found.

$PATH before and after is: /sbin:/usr/sbin:/bin:/usr/bin

Upvotes: 5

Views: 1866

Answers (1)

Ray Chan
Ray Chan

Reputation: 61

After hours of work, the two critical lines needed (regardless of your usage of the Miniconda AMI) is:

#!/bin/bash
echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc #or the path to your conda.sh
source ~/.bashrc
conda activate <full path to environment>

for some reason, cloud-init ignores all other attempts to modify path

Upvotes: 1

Related Questions