Skip to content

User Data Template

Juhi edited this page Jun 17, 2020 · 4 revisions

Motivation

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

Template

This template is an example of a bash script. Copy paste below template to user data at the time of instance launch.

Bash script

#!/bin/bash
This is to indicate bash type script

Install python

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  

Create virtual ENV and install psycopg2 OS dependencies

python3 -m venv my_venv  
sudo yum install -y postgresql postgresql-devel python-devel  

Install pip

sudo yum install -y python-pip
sudo yum update -y
sudo yum install -y libpq-dev python-dev
sudo pip install psycopg2

Add systemd service

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

Env var sync

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 -'

CodeDeploy Agent

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
Clone this wiki locally