LukasZ
LukasZ

Reputation: 1

ec2 launch bash command does not work

I am running this code while launching ec2 instance, python is installed, but the folder is not created.

#!/bin/bash

sudo yum update -y

sudo yum install python36 -y

mkdir venv

cd venv

virtualenv -p /usr/bin/pyton3.6 python36

echo "source /home/ec2-user/venv/python36/bin/activate" > /home/ec2-user/.bashrc

pip install boto3

Upvotes: 0

Views: 608

Answers (2)

LukasZ
LukasZ

Reputation: 1

Thank you for inputs. This worked. Mainly:

  • clear paths
  • activate virtual environment for boto3 install

'#!/bin/bash

sudo yum update -y

sudo yum install python36 -y

mkdir /home/ec2-user/venv

cd /home/ec2-user/venv

virtualenv -p /usr/bin/python3.6 python36

echo "source /home/ec2-user/venv/python36/bin/activate" >> /home/ec2-user/.bashrc

source /home/ec2-user/venv/python36/bin/activate

pip install boto3

Upvotes: 0

janos
janos

Reputation: 124648

A couple of things could go wrong with that script. I suggest a more robust way to write it:

#!/bin/bash

cd "$(dirname "$0")"

sudo yum update -y
sudo yum install python36 -y

if [ ! -d venv ]; then
    mkdir venv
    virtualenv -p /usr/bin/pyton3.6 venv/python36

    echo "source venv/python36/bin/activate" >> ~/.bashrc

    source venv/python36/bin/activate
    pip install boto3
fi

Improved points:

  • Make sure we are in the right directory, by doing a cd into the directory of the script
  • Do not hardcode the user home directory location, use ~
  • Do not truncate ~/.bashrc if already exists
  • Before installing boto3, it's important to activate the virtual env, otherwise pip will not install it inside the virtual env (it will try to install system-wide)

Upvotes: 3

Related Questions