Provisioning cloud infrastructure is made easy with Terraform & Jenkins Pipeline integration. Terraform is ‘Infrastructure as Code’ method to provision infrastructure.
Jenkins Pipeline to run the sequence of Terraform commands to initialize the project, create plan and apply resources.
This article explains how to integrate Terraform with Jenkins and deploy infrastructure resources in AWS cloud.
Before start writing Jenkins Pipeline Code, you need to plan Jenkins resources to prepare them to integrate in the Pipeline Code. In this project below resources are integrated.
Pipeline: AWS steps This plugin to provide Pipeline-compatible steps for authenticating AWS APIs and manage resources.
Terraform This plugin to Install and configure Terraform Binary.
Git This plugin to integrate with SCM tools to clone the code repository.
Credentials This plugin to store credentials in the Jenkins Credentials Store.
Terraform tool must be configured to install the Terraform automatically and use the binary to run Terraform commands in the Pipeline code.
Bit Bucket repository credentials must be stored in the Jenkins credentials store.
Create IAM Role and attach IAM role to the Jenkins EC2 Instance. Ensure that you have added correct IAM policies to the IAM role based on the resources that you want to deploy using Terraform IaC.
Terraform Code Terraform code used in this project committed to the bit bucket repository.
Jenkins Pipeline Code Jenkinsfile is committed to the bit bucket repository.
Create Pipeline Job
Login to Jenkins and create Job of type Pipeline and provide repository details in the pipeline section to clone the Jenkinsfile.
Once Pipeline Job is ready, Just click on Build to run the Pipeline. Jenkins run the Pipeline code and each stage will be executed in sequence to run ‘Terraform init’, ‘Terraform Plan’ & ‘Terraform Apply’ commands.
Below is the Jenkins Pipeline Job status showing all stages are executed successfully.