Giter Club home page Giter Club logo

infrastructure_automation_terraform's Introduction

infrastructure_automation_terraform

Integrating Terraform and Ansible to automate and configure Infrastructure

1. Introduction

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.

2. Objectives

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.

3. Methodology

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

a. Install Terraform and Ansible on the host machine to set up the development environment.

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

b. Install Python and pip, followed by Ansible using pip.

sudo yum install -y python python-pip
pip install ansible

c. Install AWS CLI and configure it with AWS account credentials for accessing AWS services.

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

unzip awscliv2.zip

sudo ./aws/install

Part 2: Configuring AWS CLI in Host

a. Create an AWS user in the AWS Management Console.

image

b. Attach the "AmazonEC2FullAccess" policy to the user, granting necessary permissions.

image

c. Generate access keys for the user and configure AWS CLI on the host machine.

image

Part 3: Creating Key Pair in AWS Console.

a. Create a key pair named "jenkins_key" in .pem format in the AWS console.

image

b. Download the key pair and copy it to the Terraform host.

image

c. Set appropriate permissions (400) to the key pair file for security.

image

Part 4: Setting Up Terraform Project.

a. Create a project directory for the Terraform project

mkdir terraform_automation_project 
cd terraform_automation_project

b. Write Terraform scripts including provider.tf, variable.tf, terraform.tfvars, main.tf, and output.tf.

c. Use local-exec provisioner in main.tf to wait for EC2 instance creation.

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. image

d. Utilise ansible_host and ansible_playbook resource providers in Terraform for inventory creation and playbook execution.

image

Part 5: Ansible Playbook Configuration.

a. Create ansible_dir for writing the playbook install.yml.

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 }}"    

c. Retrieve the initialadminpassword of Jenkins using debug and register module.

image

d. Configure a host file for Ansible and add a Terraform plugin line to redirect to the inventory created by Terraform.

hosts file: image

e. Create a config file for Ansible to ensure proper execution.

image

Part 6: Copying Key Pair for SSH.

a. Copy the jenkins_key.pem file to the directory mentioned in ansible.cfg file for SSH connection to the Terraform-created EC2 instance.

image

Part 7: Running Terraform Script.

a. Run terraform init to initialise all resource providers and validate the script.

image

b. Run terraform plan and terraform apply to execute the Terraform script.

image image

Now running terraform apply: image image

Part 8: Verifying Installation of Python, Java and Jenkins.

a. After script execution, the EC2 instance will be running and configured with Python, Java, and Jenkins.

image

b. Access the public URL of the instance provided in the output section of the script to access Jenkins.

image

c. Use the initial admin password to log in to Jenkins and complete the setup.

image image image

d. Crosscheck the installation of Python, Java, and Jenkins on the EC2 instance by logging in to the instance using the AWS console.

image

4. Project Outcome

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.

5. Conclusion

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.

infrastructure_automation_terraform's People

Contributors

rishabh-manhas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.