Part 2 of 3: HumanGov: Ansible is the Answer! | Terraform | Python | Git | AWS Cloud9 | AWS EC2 | AWS DynamoDB | AWS S3

1 of 23. Open Cloud9

2 of 23. Download the HumanGov application to the local Git Repository on AWS Cloud9

cd ~/environment/human-gov-application echo "*.zip" >> .gitignore mkdir src cd src wget https://tcb-bootcamps.s3.amazonaws.com/tcb5001-devopscloud-bootcamp/v2/module4-ansible/humangov-app.zip unzip humangov-app.zip

3 of 23. Push the application source to the local Git repository

git status git add . git add ../.gitignore git commit -m "HumanGov app 1st commit"

4 of 23. Connect to the EC2 instance

Change permissions on your key, or else you will get an error.

aws ec2 describe-instances \ --query 'Reservations[*].Instances[*].{Instance:InstanceId,Name:Tags[?Key==`Name`]|[0].Value,PublicIP:PublicIpAddress,PrivateIP:PrivateIpAddress,State:State.Name}' \ --output table chmod 400 /home/ec2-user/environment/humangov-ec2-key.pem ssh -i /home/ec2-user/environment/humangov-ec2-key.pem ubuntu@172.31.18.172

5 of 23. Update and upgrade apt packages

This (and following) steps are within the Ubuntu host, to demonstrate how tedious it is to do this manually.

sudo apt-get update sudo apt-get upgrade -y

6 of 23. Install required packages

sudo apt-get install -y nginx python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools python3-venv unzip

7 of 23. Ensure UFW allows Nginx HTTP traffic

bash sudo ufw allow 'Nginx HTTP'

8 of 23. Create project directory

export project_path=/home/ubuntu/humangov export username=ubuntu mkdir -p $project_path sudo chown $username:$username $project_path sudo chmod 0755 $project_path

9 of 23. Create Python virtual environment

python3 -m venv $project_path/humangovenv

10 of 23. Copy the application zip file to the destination

Switch back to the Cloud9 terminal briefly to copy the file over.

exit scp -i /home/ec2-user/environment/humangov-ec2-key.pem humangov-app.zip ubuntu@172.31.18.172:/home/ubuntu/humangov ssh -i /home/ec2-user/environment/humangov-ec2-key.pem ubuntu@172.31.18.172

11 of 23. Unzip the application zip file

export project_path=/home/ubuntu/humangov export username=ubuntu export project_name=humangov unzip $project_path/humangov-app.zip -d $project_path

12 of 23. Install Python packages from requirements.txt into the virtual environment

$project_path/humangovenv/bin/pip install -r $project_path/requirements.txt

13 of 23. Create systemd service file for Gunicorn

Note: That the Values for "Environment" need to match your AWS environment

sudo tee /etc/systemd/system/$project_name.service < cat /etc/systemd/system/humangov.service

14 of 23. Change permissions of the user's home directory

sudo chmod 0755 /home/$username

15 of 23. Remove the default nginx configuration file

ls /etc/nginx/sites-enabled/ sudo rm /etc/nginx/sites-enabled/default ls /etc/nginx/sites-enabled/

16 of 23. Configure Nginx to proxy requests (values need to be replaced in the template)

sudo tee /etc/nginx/sites-available/$project_name <

17 of 23. Enable and start Gunicorn service

sudo systemctl enable $project_name sudo systemctl start $project_name sudo systemctl status $project_name

18 of 23. Enable Nginx configuration

Create a symbolic link for nginx to point to the project folder

sudo ln -s /etc/nginx/sites-available/$project_name /etc/nginx/sites-enabled/

19 of 23. Restart Nginx and the humangov service

sudo systemctl restart $project_name sudo systemctl status $project_name sudo systemctl restart nginx sudo systemctl status nginx

20 of 23. Connect to the website.

Grab the public DNS for the EC2, and connect to it.

aws ec2 describe-instances \ --query 'Reservations[*].Instances[*].{Instance:InstanceId,Name:Tags[?Key==`Name`]|[0].Value,PublicDNS:PublicDnsName,PublicIP:PublicIpAddress,PrivateIP:PrivateIpAddress,State:State.Name}' \ --output table ????PDF ????

21 of 23. Test the app further, with adding an employee

Hit the add employee button, and add someone.

22 of 23. Check your AWS infrastructure

Check the DynamoDB table (explore items). Check S3. You will find evidence from the employee record.

23 of 23. Destroy the infrastructure on AWS using Terraform

Go back to your cloud9 environment to perform the destroy.

cd ~/environment/human-gov-infrastructure/terraform terraform destroy

References

AWS Cloud9 Documentation

Amazon Elastic Compute Cloud Documentation

Git - Reference

3.12.1 Documentation

12. Virtual Environments and Packages -- Python 3.12.1 documentation

Creating Amazon EC2 Instances for NGINX Open Source and NGINX Plus | NGINX Documentation

Gunicorn - WSGI server -- Gunicorn 21.2.0 documentation

System and Service Manager

How To Serve Flask Applications with Gunicorn and Nginx on Ubuntu 22.04

How To Install Nginx on Ubuntu 22.04

Comments

Popular posts from this blog

Containing the Chaos! | A Three-Part Series Demonstrating the Usefulness of Containerization to HumanGov

Ansible is the Answer! | A Three-Part Series Demonstrating the Usefulness of Ansible to HumanGov