The purpose of this project is to automate the setup of an EC2 instance using Terraform and configure it with Python, Java, and Jenkins using Ansible. The project aims to streamline the process of infrastructure creation and configuration, reducing manual effort and potential errors.
The main objectives of this project are: a. Automate the creation of an EC2 instance using Terraform. b. Configure the EC2 instance with Python, Java, and Jenkins using Ansible. c. Ensure seamless integration between Terraform and Ansible. d. Validate the setup by accessing Jenkins on the EC2 instance.
The project is divided into several key milestones, each involving specific tasks to achieve the desired outcome. The milestones are as follows: Part 1: Installing Terraform and Ansible
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install terraform
sudo yum install -y python python-pip
pip install ansible
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
mkdir terraform_automation_project
cd terraform_automation_project
b. Write Terraform scripts including provider.tf, variable.tf, terraform.tfvars, main.tf, and output.tf.
We’re executing this provisioner execute sleep command to wait for ec2 instance to be created and ssh service to be started before creating inventory file and executing playbook, otherwise it won’t be able to connect with instance and the execution will fail.
d. Utilise ansible_host and ansible_playbook resource providers in Terraform for inventory creation and playbook execution.
mkdir ansible_dir
cd ansible_dir
vi install.yml
b. Write an ansible plabook to I=install Python, Java, and Jenkins on the EC2 instance using ansible.
install.yml
---
- name: Installing Python and Jenkins
hosts: all
remote_user: ec2-user
tasks:
- name: Update Package Repositories
command: yum update -y
become: true
- name: Installing Python
dnf:
name: python3
state: present
become: true
- name: Install Java
dnf:
name: java-17-amazon-corretto.x86_64
state: present
become: true
- name: Add Jenkins Repository
get_url:
url: https://pkg.jenkins.io/redhat/jenkins.repo
dest: /etc/yum.repos.d/jenkins.repo
become: true
- name: Import Jenkins key
rpm_key:
key: https://pkg.jenkins.io/redhat/jenkins.io-2023.key
state: present
become: true
- name: Install Jenkins
dnf:
name: jenkins
state: present
become: true
- name: Start Jenkins Service
service:
name: jenkins
state: started
become: true
- name: Retrieve initial admin password
shell: cat /var/lib/jenkins/secrets/initialAdminPassword
register: initial_admin_password
become: true
- debug:
msg: "Initial admin password: {{ initial_admin_password.stdout }}"
d. Configure a host file for Ansible and add a Terraform plugin line to redirect to the inventory created by Terraform.
a. Copy the jenkins_key.pem file to the directory mentioned in ansible.cfg file for SSH connection to the Terraform-created EC2 instance.
a. After script execution, the EC2 instance will be running and configured with Python, Java, and Jenkins.
b. Access the public URL of the instance provided in the output section of the script to access Jenkins.
d. Crosscheck the installation of Python, Java, and Jenkins on the EC2 instance by logging in to the instance using the AWS console.
The project successfully automates the creation of an EC2 instance and configures it with Python, Java, and Jenkins using Terraform and Ansible. The outcome of the project is a fully functional EC2 instance with Jenkins accessible via a public URL. The automation reduces manual effort and ensures consistent and error-free infrastructure setup.
Integrating Terraform and Ansible provides a powerful automation solution for infrastructure provisioning and configuration. This project demonstrates the seamless integration of these tools to automate the setup of an EC2 instance and configure it with essential software. The automated process improves efficiency, eliminates manual errors, and standardised infrastructure deployments. The project can be further extended to incorporate additional tools and configurations as per specific requirements.