Skip to content

Sample auto deploy an application combining Jenkins with Terraform and Ansible

License

Notifications You must be signed in to change notification settings

dungpham91/jenkins-terraform-ansible-sample

Repository files navigation

Auto deploy an app with Jenkins - Terraform - Ansible

In this sample, we will be automating almost all the steps in the automatic installation of a new Docker server.

Some of the goals of this sample:

  • Create S3 bucket to store state for main Terraform code.
  • Automate Terraform to create EC2 with Jenkins.
  • Automatically generate Inventory for Ansible from Terraform output.
  • Jenkins executes Ansible playbook to install Docker on newly created EC2.

Table of contents

1. Create S3 bucket to store state

Unlike using Terraform under local, then the file terraform.tfstate will be saved with the Terraform code folder.

However, our goal is to automate Terraform with Jenkins so we need to save the remote state with the S3 bucket instead of local.

This step you can do under your local machine.

1.1 Install AWS CLI on your local machine

Follow the official guide to install and configure profiles.

1.2 Install Terraform on your local machine

To install Terraform, find the appropriate package (https://www.terraform.io/downloads.html) for your system and download it.

Also, it is recommended that you use an Ubuntu computer to install Terraform by following the commands below:

$ curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
$ sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
$ sudo apt-get update && sudo apt-get install terraform

1.3 Create S3 bucket

1.3.1 Move into the folder s3-backend change the required value

1.3.2 Create S3 bucket for Terraform backend

At the s3-backend directory, execute the following commands in turn:

$ terraform init
$ terraform plan # This command to check if the code has any problem, it will output the changed information to the screen
$ terraform apply # Implement resource creation

When the terraform apply command finishes, you will get an information on the terminal window like the image below. Save this information and use it in the next section.

01-create-s3-bucket

2. Install Jenkin server

Here, I assume that you already have another server Ubuntu 20.04 LTS ready to install Jenkins. I'm not going to use Terraform or talk about creating a Jenkins server. This is not the main purpose of sample.

On this new server, you are ready to install:

2.1 Install Jenkins with Docker Compose

After installing Docker and Docker Compose, create the following directory:

$ sudo mkdir -p /home/ubuntu/jenkins/jenkins_config

Move to directory /home/ubuntu/jenkins and copy the file docker-compose.yml to this directory.

Run the following command to deploy the Jenkins application:

$ sudo docker-compose up -d

Next, type the following command to print the first Jenkins password string. Here, I'm not going to go into detail on how to use or set up Jenkins, learn it yourself.

$ sudo docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword

Then you need to open the firewall port 8080 to be able to access the Jenkins service. After logging into Jenkins, install the recommended plugins and set up your admin account.

2.2 Install Terraform, AWS CLI, Ansible inside Jenkins

In fact, you can have your seprate Terraform and Ansible servers. But to keep the demo simple to implement, I installed it all inside the Jenkins container.

Login into Jenkins container:

sudo docker exec -it jenkins bash

Then, copy script file install.sh into Jenkins container and execute it by the command:

bash install.sh

If you need to create file install.sh manually inside Jenkins container, may you need to install nano inside it too - an editor tool.

apt-get update && apt-get install -y nano

Remember to run command:

aws configure

to set up an IAM account for Jenkins, right after the script has finished running.

3. Automatically create and install a Docker server

In this example, the application that I want to automatically create and install on EC2 is Docker. Terraform will create an EC2, Ansible will install Docker on that EC2, and all the steps are done with Jenkins.

3.1 Update backend for Terraform

Take the result saved in step 1.3.2 and put the values in the file backend.tf

3.2 Initial setup with Jenkins

Some settings are needed for your Jenkins to work with this repository.

3.2.1 Jenkins credentials

For Jenkins to work, you need to create two credentials as follows:

  • Github SSH Key: Please clone or fork my sample to your github account, then add your key to the Jenkins server as image below. This key will be used so that Jenkins can connect to your repository.

02-github-ssh-key

  • EC2 SSH key:
    • Similarly, in the code Terraform will automatically attach the SSH public key located in directory modules/ec2/keyfiles/id_rsa.pub to the generated EC2. Please replace your public key in this file.
    • Next, add the SSH private key corresponding to your public key into the Jenkins server, like the image below.
    • This key will be used by Ansible to ssh into the new EC2.

03-ec2-ssh-key

3.2.2 Jenkins plugins

Next, you will need to install the Ansible plugin.

04-ansible-plugin

3.2.3 Create Jenkins Multibranch Pipeline

Now, the last step is that you create a Jenkins Multibranch Pipeline like the image below, connect to your repository using the credential that you created in step 3.2.1.

Also, remember to replace the region inside Jenkinsfile at line 58.

05-create-jenkins-pipeline

And that's it. Pipeline worked. Images for pipeline run:

06-pipeline-run-1

07-pipeline-run-2

08-pipeline-run-3

09-pipeline-run-4

10-pipeline-run-5

11-pipeline-run-6

12-pipeline-run-7

4. Process flow in this sample

  • Step 1: on your local computer, you use the Terraform s3-backend folder to create an S3 bucket, the purpose of which is to store the remote state for the main Terraform code - used to create EC2.

  • Step 2:

    • You create a Jenkins server, install on that server the packages: Docker, Docker Compose.
    • Using the file docker-compose.yml to install Jenkins.
    • In the Jenkins container, you use the install.sh script to install the necessary packages: Terraform, AWS CLI, Ansible.
  • Step 3: on admin Jenkins:

    • Add required SSH key to Jenkins credentials.
    • Install Ansible plugin.
    • Create pipeline for sample repository.
  • Step 4:

    • When Jenkins runs, it will automatically execute the main Terraform code - used to create a new EC2.
    • After creating EC2, it will print EC2's public IP address into an inventory file named aws_hosts.
    • Also check the status of EC2 ready or not.
  • Step 5: Jenkins through the Ansible plugin, will execute the Ansible Playbook via the EC2 SSH Key and the previous inventory file aws_hosts. It will automatically run the playbook named ansible/docker.yml

  • Step 6: Once all deployed, there will be a step asking if you want to delete the EC2 that was just installed. If you click OK then Jenkins will execute the job and delete it.

5. Clean everything

After you're done with the lab, you'll need to delete the resources to save money.

  • EC2 will be deleted through the Jenkins pipeline so you don't need to do anything with it anymore.

  • S3 bucket to do remote backend, you need to delete it by going to s3-backend directory and typing the command:

    terraform destroy

    13-terraform-destroy

  • Delete the Jenkins server, this you can do manually just like when you created it.

6. Software version

Software Version
Docker 20.10.14
Docker Compose 1.29.2
AWS CLI 1.19.1
Terraform 1.1.8
Ansible 2.10.8
Jenkins 2.332.2

About

Sample auto deploy an application combining Jenkins with Terraform and Ansible

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published