Code solution with no GUI involvement and 100% complete automation
Terraform | Ansible | Nginx | Jenkins | Docker
Prerequisites
- Deploy 2 EC2 (Jenkins and Ansible) instances and configure them.
- Create ssh keyless authentication between Jenkins and Ansible.
- Need to download terraform command in the Ansible Node to run terraform commands.
Do ssh to the Ansible Node.
Initialize the terraform to download the needed modules.
terraform init

Now we can build Infrastructure using terraform and configure it with Ansible.
Terraform will create a new VPC set up (not in the default VPC and gateways etc ) and create a new EC2 instance.
Ansible will install Nginx and deploy the nginx.conf file which uses Nginx as a reverse proxy also deploys the docker containers.
terraform apply

It downloads the Nginx, Docker and deploys the docker containers in ports 80 and 8000.

Jenkins
Jenkins builds the docker images whenever the Developer commits or pushes the code to the GitHub Repository.
Note: App1 and App2 are the same. You can create 2 repository for the same code to make the same scenario.

Jobs trigger whenever the Developer commits or pushes the code to the GitHub Repository. So for this setup, you need to set the GitHub Webhook for the Jenkins.
App1 repo > Settings > Webhooks
App2 repo > Settings > Webhooks
This is done for both Repositories i.e App1 and App2 individually.
Here IP of the Jenkins Server and Port no. where the Jenkins services is running (by default 8080).

Job App1






sudo ssh root@ip ‘ansible-playbook -i `cat /home/ec2-user/infra/ip`, — private-key /home/ec2-user/infra/tf-key.pem /home/ec2-user/infra/app1Redeploy.yml’
Job App2
Do all the steps the same as App1 but change the command.
sudo ssh root@ip ‘ansible-playbook -i `cat /home/ec2-user/infra/ip`, — private-key /home/ec2-user/infra/tf-key.pem /home/ec2-user/infra/app2Redeploy.yml’
At last, we do not need this we can run the command to delete the whole infrastructure that we made.
terraform delete

Code available in Github.