How to Install Jenkins Server on Amazon Linux EC2 Instance using Terraform and Ansible

How to Install Jenkins Server on Amazon Linux EC2 Instance using Terraform and Ansible

In this article, we can use Infrastructure as Code (IAC) and configuration management tools together to install Jenkins servers on AWS.
we can use Terraform to create a new EC2 instance on AWS, terraform can call Ansible to install and configure software and applications in the EC2 instances.

Prerequisites

Terraform
Jupyter — to write Terraform .tf files
Jupyter Terminal — to run Terraform .tf files
Key Pair — (.pem file for Access EC2 Instance)

Terraform Files

  • c9_terraform.tfvars

Folder structure:

we need to add Key Pair (.pem file for Access EC2 Instance) inside the private-key folder

We need to create a GitHub Repository jenkins_setup_ansible

Create a Role:

Let’s Create our playbook and tell our playbook to reference a role called jenkins_setup

To setup a role we can use this command

ansible-galaxy init jenkins_setup

We create our roles directory for our jenkins-setup role and add the handlers, vars and tasks sub-folder.

Now go to the task folder and create some files to install Java, Jenkins, Docker, and Maven and include that files in main.yml file

  • Java_install.yml

  • Jenkins_install.yml

  • Maven_install.yml

  • docker_install.yml

  • main.yml

Now go to the Vars folder we can main.yaml file under main.yaml you can add the variables as shown below

  • main.yml

Now create a playbook.yaml file and add a role(jenkins_setup) to the playbook.yaml file

  • playbook.yaml

Next, we have to Add the path variables like JAVA_HOME and M2_HOME to.bash_profile dynamically by using Python script and bash commands

Push these Folders and files to GitHub repository jenkins_setup_ansible using Git commands.

we have set up Terraform files and Ansible role here terraform can call Ansible to install packages like Java, Jenkins, Docker, and Maven.

Terraform commands:

Run Terraform Commands in the Jupyter terminal

  • Terraform Init:

The terraform Init command is used to initialize a working directory containing terraform configuration files. It is safe to run this command multiple times. This command will never delete your existing configuration or state. During init, the root configuration directory is consulted for backed configuration and the chosen backend is initialized using the given configuration settings

  • Terraform validate:

The terraform validate command validates the configuration files in a directory, referring only to the configuration and not accessing any remote services such as remote state, provider APIs etc. validate runs checks that verify whether a configuration is syntactically valid and internally consistent, regardless of any provided variables or existing state. It is thus useful in general verification of reusable modules including correctness of attribute names and value types.

  • Terraform plan :

The terraform plan command is used to create an execution plan. Terraform performs a refresh, unless explicitly disable and then determines what actions are necessary to achieve the desired state specified in the configuration files.

  • Terraform Apply:

The terraform apply command is used to apply the changes required to reach the desired state of the configuration or the pre-determined set of actions generated by a terraform plan execution plan.

Now we check our AWS Console our Jenkins_server were created through Terraform.

To Configure Jenkins connect with EC2 public IP using Port 8080.

http://54.190.78.198:8080/

Connect that Jenkins instance get Administrator password from mentioned path

cat /var/lib/jenkins/secrets/initialAdminPassword
2a7546bdf5bf4a0584b28db0406a5418

Click Install suggested plugins.

Create Admin User for access Jenkins

Click Save and Continue

Now we successfully installed the Jenkins server using Terraform and Ansible.

  • Terraform Destroy:

The terraform destroy command is used to destroy the terraform-managed infrastructure.

Now we successfully Destroy Jenkins server using Terraform.

Thanks for reading! Have a good one!