Thursday, July 1, 2021

Jenkins Terraform Integration | How do you integrate Terraform with Jenkins | Automate Infrastructure setup using Terraform and Jenkins Pipeline

We will be learning how to execute Terraform scripts automatically using Jenkins pipeline. We will learn how to create EC2 instance using Terraform and Jenkins in AWS cloud.

Watch the steps in YouTube channel:



Pre-requisites:
  • Jenkins is up and running
  • Terraform is installed in Jenkins
  • Terraform files already created in your SCM
  • Make sure you have necessary IAM role created with right policy and attached to Jenkins EC2 instance. see below for the steps to create IAM role.
I have provided my public repo as an example which you can use.

Create IAM role to provision EC2 instance in AWS 



Select AWS service, EC2, Click on Next Permissions


Type EC2 and choose AmazonEC2FullAccess as policy


Click on Next tags, Next Review
give some role name and click on Create role.



Assign IAM role to EC2 instance

Go back to Jenkins EC2 instance, click on EC2 instance, Security, Modify IAM role


Type your IAM role name my-ec2-terraform-role and Save to attach that role to EC2 instance.



Create a new Jenkins Pipeline

Give a name to the pipeline you are creating.



Add parameters to the pipeline

Click checkbox - This project is parameterized, choose Choice Parameter


Enter name as action
type apply and enter and type destroy as choices as it is shown below(it should be in two lines)


Go to Pipeline section

Add below pipeline code and modify per your GitHub repo configuration.

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
            checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/mydevopscoach/my-tf-iac-aws-repo']]])            

          }
        }
        
        stage ("terraform init") {
            steps {
                sh ('terraform init') 
            }
        }
        
        stage ("terraform Action") {
            steps {
                echo "Terraform action is --> ${action}"
                sh ('terraform ${action} --auto-approve') 
           }
        }
    }
}

Click on Build with Parameters and choose apply to build the infrastructure or choose destroy if you like to destroy the infrastructure you have built. 



Click on Build
Now you should see the console output if you choose apply.


Pipeline will look like below:


Login to AWS console, you should see the new EC2 instance created.

2 comments:

  1. Hi...How to do the same for Azure VM?

    ReplyDelete
  2. Hi,i am trying to destroy action. but getting the error Destroy complete! Resources: 0 destroyed.

    ..............job status.....................
    Terraform action is --> destroy
    [Pipeline] sh
    + terraform destroy --auto-approve

    [0m [1m [32mNo changes. [0m [1m No objects need to be destroyed. [0m

    [0mEither you have not created any objects yet or the existing objects were
    already deleted outside of Terraform.
    [0m [1m [32m
    Destroy complete! Resources: 0 destroyed.
    [0m
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Finished: SUCCESS

    ReplyDelete

GitHub Actions CICD Pipeline to Deploy Java WebApp into Azure App Service | Integration GitHub Actions with Azure App Service

Pre-requisites: Make sure Java web app is setup in GitHub Create WebApp in Azure Cloud. Please click here for the steps. What are we going ...