Skip to main content

Building a CI/CD Pipeline Using Terraform

Hands-On Lab

 

Photo of Travis Thomsen

Travis Thomsen

Course Development Director in Content

Length

01:00:00

Difficulty

Intermediate

Our development team is updating their CI/CD processes to start using Terraform to deploy out infrastructure. We have been tasked with creating a new Jenkins Pipeline that will utilize Terraform scripts created by another team-member.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Building a CI/CD Pipeline Using Terraform

The Scenario

Our development team is updating their CI/CD processes to start using Terraform to deploy out infrastructure. We have been tasked with creating a new Jenkins Pipeline that will utilize Terraform scripts created by another team-member.

Logging In

Use the IP address and credentials provided on the hands-on lab overview page, and log in with SSH to the server.

There's a fair bit of creating and editing files in this lab. All of the examples here involve using Vim, but use whichever text editor is most comfortable.

Complete the Setup of Our Docker Swarm

On the manager node get the join token:

docker swarm join-token worker

On the worker node run the join command (pasting the join token in the appropriate spot):

docker swarm join --token [JOIN_TOKEN] [IP]:2377

Get Jenkins Running

We need to get the Jenkins password:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Copy the output from that command. Now let's get into a browser, and go to the public IP of the swarm manager, on port 8080, like this: http://< Swarm_Manager_Public_IP >:8080/. In the web form we're greeted with, we can paste that password we got from the cat command.

One we click Continue, we'll land on another page. We'll click Install suggested plugins here. This may take a few minutes.

On the following screen, we've got to create a user. Plug appropriate values into the web form, then click Save and Continue in the lower right of the screen. Click Save and Finish on the next screen, then Staru using Jenkins on the one after that.

If there are any problems with installing updates, click on Manage Jenkins in the dashboard, and fix things there. Most problems are with dependencies, and there will be enough on the screen to get us going in the right direction if there's trouble.

Set up a Pipeline

Once we're up and running, we can click New Item in the left-hand menu. Select Pipeline from the list of choices, and name it MyDeployment. Click the OK button to create the pipeline.

On the next screen, check the box next to This project is parameterized, click the Add Parameter dropdown, and select Choice Parameter.

In the web form that appears, give a Name of image_name, Choices of ghost:latest and ghost:alpine (with each choice on its own line).

Click the Add Parameter dropdown again, and select Choice Parameter. Give this one a Name of ghost_ext_port and set 80 and 8081 (again, with each on it's own line) as Choices.

Now for our pipeline.

Create the Build Pipeline

Down in the Pipeline section of the screen (way down), in the Script box, paste this code in:

node {
  git 'https://github.com/linuxacademy/content-terraform-docker-service.git'
  stage('init') {
    sh label: 'Initialize Terraform', script: "terraform init"
  }
  stage('plan') {
    sh label: 'Plan Terraform', script: "terraform plan -out=tfplan -input=false -var image_name=${image_name} -var ghost_ext_port=${ghost_ext_port}"
    script {
        timeout(time: 10, unit: 'MINUTES') {
          input(id: "Deploy Gate", message: "Deploy environment?", ok: 'Deploy')
        }
    }
  }
  stage('apply') {
    sh label: 'Deploy Infrastructure', script: "terraform apply -lock=false -input=false tfplan"
  }
}

Once that's in, click the blue Save button in the lower left.

Deploy Ghost Blog

From the Pipeline MyDeployment screen, click Build with Parameters on the left. Select ghost:alpine in the image_name dropdown, and 8081 as the ghost_ext_port.

Click the Build button.

To watch things fire up, click the little down arrow (next to #1 in the Build History section) and select Console Output. Near the bottom of the screen we land on, click the Deploy hyperlink. We should get a Finished: Success message at the end of the output when things are done.

Conclusion

We did it. We've used Terraform to build out a new CI/CD pipeline (Jenkins). We can test this by visiting, in a web browser, http://< LAB_PUBLIC_IP >:8081 and watching Ghost blog come up. Congratulations!