0

I'm launching an EC2 instance using Terraform with a user data script:

#!/bin/bash
cd /home/ec2-user
sudo yum -y update
sudo yum -y groupinstall "Development Tools"
sudo yum -y install openssl-devel bzip2-devel libffi-devel
sudo yum -y install wget
wget https://www.python.org/ftp/python/3.9.10/Python-3.9.10.tgz
tar xvf Python-3.9.10.tgz

cd Python-*/
./configure --enable-optimizations
sudo make altinstall

export PATH="/usr/local/bin:$PATH"
source ~/.bash_profile

pip3.9 install virtualenv

I know that this works fine because I've created a log for this user data script and sure enough the last few lines show that virtualenv was successfully installed. It works if I do it manually too.

A second Bash script is executed at the same time via SSH. It sleeps first to give the user data script enough time to fully execute before attempting to create a virtual instance with the virtualenv library:

#!/bin/bash


sleep 210
virtualenv myenv

This script gives the user data script plenty of time to finish, but myenv is never created. I know the second script SSH connection is fine because I've made it print to my local console. If I do execute all this manually there are no problems. If I attempt to create the venv in the user data script it fails too. The error I get:

aws_instance.example (remote-exec): /tmp/script.sh: line 5: virtualenv: command not found

Any ideas where I'm going wrong?

parmeni4
  • 15
  • 4

1 Answers1

0

User-data script is executed as root user. Try changing user to root and check if that has virtualenv available. You can change to root user in your terminal using this command.

sudo su

It looks it is not available in your case, you can fix it by following this SO answer.

Technowise
  • 1,182
  • 6
  • 11