Integrate Terraform with Jenkins Pipeline

2021年06月13日


In this article, we are talking about Jenkins Integration with Terraform. How we can execute the Terraform Script using Jenkins.
As we all know Jenkins is a well-known open-source continuous integration and continuous development automation tool, I talked a lot about Jenkins and today also I’m talking about Jenkins.

The final architecture after complete.



What is Terraform?
Terraform is the infrastructure as a code offering from HashiCorp. It is a tool for building, changing and managing infrastructure in a safe, repeatable way. Operators and Infrastructure teams can use Terraform to manage environments with a configuration language called the HashiCorp Configuration Language (HCL) for human-readable, automated deployments.

Infrastructure as Code
If you are new to infrastructure as code as a concept, it is the process of managing infrastructure in a file or files rather than manually configuring resources in a user interface. A resource in this instance is any piece of infrastructure in a given environment, such as a virtual machine, security group, network interface, etc.

At a high level, Terraform allows operators to use HCL to author files containing definitions of their desired resources on almost any provider (AWS, GCP, GitHub, Docker, etc) and automates the creation of those resources at the time of apply.

In this track, we will cover the basic functions of Terraform to create infrastructure on AWS.

First, install Jenkins. Refer to the URL at the bottom of this page to install Jenkins.

Make sure the EC2 instance has an IAM role attached to it. The role should have sufficient privileges to provision AWS resources. Or, preferably, associate an AK/SK to the "jenkins" user.
# su - jenkins

~]$ aws configure
...


Install Terraform plugin
In Jenkins console, go to Manage Jenkins > Manage Plugins > Available > search Terraform.



PS
The version of Terraform plugin used in this post is 1.0.10.


After Terraform plugin is installed on your Jenkins, it will display in the Installed tab.

Now we can see the Terraform option in the Build Environment tab, now we need to configure Terraform for Jenkins.




Configure Terraform

Go to Manage Jenkins > Global Tool Configuration
Terraform displays on the list.

Manually install Terraform on the same server as Jenkins. Installing Terraform is extremely easy, just two commands:

~]# wget -O terraform_1.0.0_linux_amd64.zip https://releases.hashicorp.com/terraform/1.0.0/terraform_1.0.0_linux_amd64.zip

~]# unzip terraform_*_linux_amd64.zip -d /usr/local/bind

Archive:  terraform_1.0.0_linux_amd64.zip
  inflating: /usr/local/bin/terraform  

~]# terraform --version
Terraform v1.0.0
on linux_amd64


From Manage Jenkins > Global Tool Configuration > Terraform,
Add Terraform
Uncheck the "Install automatically" checkbox.

Name: Terraform1.0.0
Install directory: /usr/local/bin/



Let’s create new project to execute Terraform from Jenkins.


Create a repo in SCM. Here I use CodeCommit as the SCM.

~]$ aws codecommit create-repository --repository-name TerraformJenkins --repository-description "Terraform in Jenkins"
{
    "repositoryMetadata": {
        "accountId": "123456789012",
        "repositoryId": "91**-**-**-**-**1c",
        "repositoryName": "TerraformJenkins",
        "repositoryDescription": "Terraform in Jenkins",
        "lastModifiedDate": "2021-06-13T06:16:58.799000+00:00",
        "creationDate": "2021-06-13T06:16:58.799000+00:00",
        "cloneUrlHttp": "https://git-codecommit.us-west-2.amazonaws.com/v1/repos/TerraformJenkins",
        "cloneUrlSsh": "ssh://git-codecommit.us-west-2.amazonaws.com/v1/repos/TerraformJenkins",
        "Arn": "arn:aws:codecommit:us-west-2:1234567890:TerraformJenkins"
    }
}

~]$ git clone https://git-codecommit.us-west-2.amazonaws.com/v1/repos/TerraformJenkins

~]$ cd TerraformJenkins/

TerraformJenkins]$ vim production.tf
module "ec2" {
  source = "./modules/ec2"

  region = "${var.region}"
}

TerraformJenkins]$ vim provider.tf
provider "aws" {
  region = "${var.region}"
  profile = "default"
}

TerraformJenkins]$ vim variables.tf
variable "region" {
  description = "region"
  default = "us-west-2"
}

TerraformJenkins]$ mkdir -p modules/ec2/

TerraformJenkins]$ vim modules/ec2/main.tf
data "aws_caller_identity" "current" {}

data "aws_ami" "ec2_instance" {
  most_recent = true

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["${data.aws_caller_identity.current.account_id}"]
}

resource "aws_instance" "web" {
  ami           = "${data.aws_ami.ec2_instance.id}"
  instance_type = "t2.micro"

  tags = {
    Name = "EC2 Demo Instance"
  }
}

TerraformJenkins]$ vim modules/ec2/variables.tf
variable "region" {
  description = "region"
}

TerraformJenkins]$ tree
.
├── modules
│   └── ec2
│       ├── main.tf
│       └── variables.tf
├── production.tf
├── provider.tf
└── variables.tf

2 directories, 5 files

TerraformJenkins]$ git add .

TerraformJenkins]$ git commit -m "Add Terraform templates"

TerraformJenkins]$ git push origin master
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 587 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://git-codecommit.us-west-2.amazonaws.com/v1/repos/TerraformJenkins
 * [new branch]      master -> master


Create a New Project

Jenkins -> New Item
Enter an item name: Terraform-pipeline
Choose Pipeline

Pipeline
There are 2 options for Jenkinsfile, i.e., Pipeline script and Pipeline script From SCM. (Pipeline Script: You can write your Pipeline code directly on Jenkins job. This will be demostrated in a later post. Refer to the bottom URL to build a more complex pipeline using Jenkins and Terraform.)



In this post, we will demostrate a relatively simple configuration using the inline editor.

Pipeline
Defininition: Pipeline script
Script:
pipeline {
  agent any
  tools {
      terraform "Terraform1.0.0"
  }

  stages {
    stage('Git Checkout') {
      steps {
        git credentialsId: '16**-**-**-**-**cb', url: 'https://git-codecommit.us-west-2.amazonaws.com/v1/repos/TerraformJenkins'
      }
    }

    stage('Terraform Init') {
      steps {
        sh label: '', script: 'terraform init'
      }
    }
    
    stage('Terraform apply') {
      steps {
        sh label: '', script: 'terraform apply --auto-approve'
      }
    }
  }
}

PS
You can use https://codebeautify.org/ to beautify the format of Groovy template.

Credentials ID specified here could be retrieved from Jenkins credential management page.

Or, more convienient, using the built-in syntax generator.


We configure our first Terraform pipeline on Jenkins.

Execute (Build Now) the Jenkins pipeline.

Once pipeline is executed completely, you can see a new EC2 instance is created on your AWS account.


Cleanup

$ sudo su -

~]# cd /var/lib/jenkins/workspace/Terraform-pipeline

Note
The default workspace directory is /var/lib/jenkins/workspace.

Terraform-pipeline]# terraform destroy


References

Terraform with Jenkins pipeline



Category: Jenkins Tags: public

Upvote


Downvote