Thursday, March 28, 2024
HomeProgrammingExploring an Object-Oriented Jenkins Pipeline for Terraform | by Krishnadutt Panchagnula |...

Exploring an Object-Oriented Jenkins Pipeline for Terraform | by Krishnadutt Panchagnula | Sep, 2022


A novel structure design in Jenkins’ multi-stage Terraform CD pipeline to enhance CI/CD granularity

picture from TopTal

Normally, after we carry out terraform plan, terraform destroy, or terraform apply, we apply these actions to all of the assets in our goal information, typically fundamental.tf (you need to use any title for the file, however this title is simply used as a conference).

Within the age of CI/CD, when we have now every part as pipelines proper from knowledge, and utility code to infrastructure code, it’s normally troublesome to this granularity. Normally, no less than in Terraform, to attain these three totally different actions, we have now three totally different pipelines to carry out terraform plan: terraform apply and terraform destroy. And after we choose a sure motion (for instance terraform plan), this motion is carried out on all of the levels and on all assets throughout the pipeline.

However after we observe all these pipelines, there’s a commonality that may be abstracted out to create a generality, on which the dynamic nature may be inherited. Simply as we create a category, utilizing totally different objects with totally different attribute values may be constructed, is it potential to create an identical base class (learn pipelines) which when instantiated can create totally different pipeline objects?

###One Pipeline to create all of them###

To be able to construct this class-based pipeline, we first must create a terraform script. This script developed ought to be loosely coupled and ought to be modular in nature. For this, we have now created this modular script, which has three modules named “Networking,” “Compute,” and “Notifications.” The parts that every of those modules create is as follows:

  1. Networking: 1 VPC and 1 subnet
  2. Compute : 1 IAM position, 1 Lambda, 1 EC2 t2.micro occasion
  3. Notifications: 1 SNS matter and 1 electronic mail subscription

And the file construction is as follows:

As soon as we have now this prepared, let’s create a fab script in declarative model in a Jenkins file.

To create this class-based structure model to flexibly create pipeline objects on the motion and useful resource degree, we’re going to make the most of a characteristic known as “parameters” in Jenkins. This characteristic helps us create a number of objects utilizing a single base class Jenkins pipeline. On this instance, let’s create three actions specifically:

  • terraform plan: This creates and prints out a plan of the assets that we’re going to create within the respective supplier ( may be AWS, Kubernetes, GCP, Azure, and so on.)
  • terraform apply: This command creates the assets within the respective supplier and creates a state-file that saves the present state of assets in it.
  • terraform destroy: This removes all of the assets which are listed throughout the state-file.

These actions are carried out on three modules/assets specifically “Networking,” “Compute,” and “Notifications.”

The above parameters create a UI for the tip consumer, as proven beneath, which might assist the tip consumer to create objects of the bottom pipeline on the fly.

the build requires parameters: actions = plan, networking, compute, and notification unchecked. button that says build at bottom

Based mostly on the actions chosen and the assets on which these actions should be achieved, Jenkins will create a dynamic pipeline in line with your requirement. Within the image beneath, we see that we have now utilized terraform for the networking and compute assets in #24, and run terraform apply on networking and notification in run #25. To scrub the infrastructure, we ran terraform destroy on run #26.

The current method carried out is extra consistent with Steady supply ideas than steady deployment.

For the Jenkins file and Terraform code, seek advice from this hyperlink.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments