-
Notifications
You must be signed in to change notification settings - Fork 1
User Data Template
Juhi edited this page Jun 17, 2020
·
4 revisions
Any new instance creation usually takes some time for setup. The setup includes lot of basic package installations like python, pip. Running these commands manually by doing SSH is boring and time consuming. To avoid this, we can setup the user data during instance launch
This template is an example of a bash script. Copy paste below template to user data at the time of instance launch.
#!/bin/bash
This is to indicate bash type script
cd /home/ec2-user
sudo yum -y groupinstall development
sudo yum -y install zlib-devel
sudo yum -y install tk-devel
sudo yum -y install openssl-devel
wget https://github.com/openssl/openssl/archive/OpenSSL_1_0_2l.tar.gz
tar -zxvf OpenSSL_1_0_2l.tar.gz
cd openssl-OpenSSL_1_0_2l/
./config shared
make
sudo make install
export LD_LIBRARY_PATH=/usr/local/ssl/lib/
cd ..
rm OpenSSL_1_0_2l.tar.gz
rm -rf openssl-OpenSSL_1_0_2l/
wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
tar zxvf Python-3.6.6.tgz
cd Python-3.6.6
sudo yum install -y gcc
./configure --prefix=/opt/python3
make
sudo yum install -y openssl-devel
sudo make install
sudo ln -s /opt/python3/bin/python3 /usr/bin/python3
cd ../
rm -rf Python-3.6.6.tgz
python3 -m venv my_venv
sudo yum install -y postgresql postgresql-devel python-devel
sudo yum install -y python-pip
sudo yum update -y
sudo yum install -y libpq-dev python-dev
sudo pip install psycopg2
printf "[Unit]\nDescription=<description for systemd service>\nAfter=network.target" > /lib/systemd/system/<service file name>.service
printf "\n\n[Service]\nExecStart=/bin/sh -c 'source /home/ec2-user/my_venv/bin/activate && source /home/ec2-user/final_config_env && /home/ec2-user/my_venv/bin/python3 <python file absolute path>'\n" | tee -a /lib/systemd/system/<service file name>.service
printf "WorkingDirectory=<working dir absolute path>\nStandardOutput=inherit\nStandardError=inherit\nRestart=always\nUser=ec2-user" | tee -a /lib/systemd/system/<service file name>.service
printf "\n\n[Install]\nWantedBy=multi-user.target" | tee -a /lib/systemd/system/<service file name>.service
sudo systemctl enable <service file name>.service
sudo my_venv/bin/pip install boto3
sudo my_venv/bin/pip install boto
mkdir .aws
printf "[default]\nregion = <region-name>" > .aws/config
printf "[default]\naws_access_key_id = <access key ID>\naws_secret_access_key = <secret key>" > .aws/credentials
su ec2-user bash -c '(crontab -l ; echo "*/1 * * * * source /home/my_venv/my_venv/bin/activate && source /home/my_venv/final_config_env && /home/my_venv/bin/python3 <env var sync script absolute path>") 2>&1 | grep -v "no crontab" | sort | uniq | crontab -'
sudo yum update -y
sudo yum install -y ruby
wget https://aws-codedeploy-eu-west-3.s3.eu-west-3.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
sudo service codedeploy-agent status