Using Jenkins to create AWS Infrastructure with Terraform.

Search for a command to run...

No comments yet. Be the first to comment.
Maintaining the inventory file while working with Ansible with AWS can be a difficult process owing to frequent changes in dynamic components of the infrastructure. Fortunately, there is a convenient solution called Ansible Dynamic Inventory. Dynamic...

In this article, you will learn how to use Ingress-NGINX Controller for Kubernetes to route traffic to Grafana and Prometheus respectively. What is an Ingress? In Kubernetes, an Ingress is an API object that manages external access to services within...

In this article, you will learn how to create a Jenkins AMI using Hashicorp Packer and Ansible Playbook. What is Packer? Packer is an open-source tool for creating identical machine images for multiple platforms from a single source configuration. Pa...

Protecting your AWS account access keys is critical for protecting your company's cloud infrastructure. Managing several accounts, on the other hand, can be complicated and time-consuming. aws-vault secures the storage and management of access keys f...

The finished product of this project is terraform code that builds an AWS environment with three EC2 instances. The Jenkins CI/CD Pipeline will be triggered when the code is pushed to a GitHub repository via GitHub Webhook.
Jenkins is an open-source automation server that helps facilitate the continuous integration and delivery (CI/CD) of software projects. It allows developers to automate various stages of the software development lifecycle, including building, testing, and deploying applications.
Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp. It enables you to define and manage your infrastructure resources, such as virtual machines, networks, storage, and services, in a declarative manner. With Terraform, you can provision and manage your infrastructure across various cloud providers and on-premises environments.
GitHub Account
Install Terraform
Jenkins Installed
Terraform Cloud Account
Install Git
Terraform Cloud is an application that helps teams use Terraform together. It manages Terraform runs in a consistent and reliable environment, and includes easy access to shared state and secret data, access controls for approving changes to infrastructure, a private registry for sharing Terraform modules, detailed policy controls for governing the contents of Terraform configurations, and more.

2. Select CLI-driven workflow.

Name your workspace then Create workspace.
Copy the provided example code and add it to backend.tf to replace the current backend.
terraform {
cloud {
organization = "YOUR ORGANIZATION NAME"
workspaces {
name = "YOUR WORKSPACES NAME"
}
}
}
Set the Execution Mode by clicking Remote and selecting Local.

Run terraform login in CLI and Enter the value yes.

Terraform will open a web browser to the tokens page for app.terraform.io.

Enter a Description and click Generate token.
Copy down the provided token and keep it in a safe place. (You can always generate a new token if you lose the previous one)
Copy and paste the Terraform Cloud token

Run terraform init
This ensures our Terraform code is connected to Terraform cloud, uses Terraform cloud workspaces and stores our state file remotely.
Create the following terraform files
backend.tf
terraform {
cloud {
organization = " YOUR ORGANIZATION NAME "
workspaces {
name = " YOUR WORKSPACES NAME "
}
}
}
provider.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
variables.tf
variable "instance_type" {
type = string
default = "t2.micro"
}
variable "monitoring" {
default = true
}
variable "tags" {
type = map(any)
default = {
Terraform = "true"
Environment = "dev"
}
}
data "aws_vpc" "selected" {
default = true
}
data "aws_security_groups" "default" {
filter {
name = "group-name"
values = ["default"]
}
}
ec2.tf
module "ec2_instance" {
source = "terraform-aws-modules/ec2-instance/aws"
for_each = toset(["one", "two", "three"])
name = "instance-${each.key}"
instance_type = var.instance_type
monitoring = var.monitoring
vpc_security_group_ids = data.aws_security_groups.default.ids
tags = var.tags
}
Create the Jenkins Security Group with the following inbound rules:
Outbound Rules:
Enter [Public IP]:8080 or [Public DNS]:8080 in your browser to see Jenkins setup.

retrieve your administrative password by running sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Copy and then paste the result into the Unlock Jenkins field and click Continue.
Click Install suggested plugins.
Enter the required information. Save and continue to Jenkins.

Click Manage Plugins > Select Available > Search Pipeline: AWS Steps, Terraform and install without restart.

Click Manage Jenkins > Global Tool Configuration then scroll to Terraform and click Add Terraform

Enter any name of your choice and select Terraform version Terraform 30504 linux (386) and click save

To get the Terraform Cloud credentials, in the terminal run cat /home/<YOUR-USERNAME>/.terraform.d/credentials.tfrc.json for Linux or open the the file credentials.tfrc.json with the path C:\Users\<YOUR-USERNAME>\AppData\Roaming\terraform.d\credentials.tfrc.json in windows.
Copy the output. Create a local txt file and paste the output and save.
Go back to Jenkins and add a new credential.

Kind = Secret file
File = (Choose the file you just saved)
ID = tf-creds
Description = terraform cloud credentials

Go to the project GitHub repo and click Settings.

Click Webhooks then Add Webhook

Enter the following:
Payload URL = [jenkins url]/github-webhook/
Please Note: Ensure the / is at the end.
Content type = application/json
Which events would you like to trigger this webhook? = Just the push event
Click Add WebHook

Go to the Jenkins Dashboard and click New Item.
Name the pipeline terraform-jenkins and select Pipeline. Then click OK.

Configure the pipeline
1. Build Triggers = GitHub hook trigger for GITScm polling
2. Pipeline Definition = Pipeline script from SCM
3. SCM = Git
4. Repository HTTPS URL = [repo name].git
5. Branches to build = */main
6. Script Path = jenkinsfile



Jenkins requires a Jenkinsfile in the root of our code directory to create a Jenkins Pipeline.
pipeline {
agent any
environment {
TF_IN_AUTOMATION = 'true'
TF_CLI_CONFIG_FILE = credentials('tf-creds')
}
tools {
terraform 'terraform'
}
stages {
stage('Init') {
steps {
sh 'terraform init -no-color'
}
}
stage('Plan') {
steps {
sh 'terraform plan -no-color'
}
}
stage('Apply') {
steps {
sh 'terraform apply -auto-approve -no-color'
}
}
}
}
Push your code to GitHub and the build process will be triggered because of the GitHub webhook we created earlier.
The image below shows a successful build along with the instances running in AWS.


Our intention with build parameters is to provide an option for the users to select which “Action” they wish to perform. Depending on the selection, the pipeline will either run the “apply” command or “destroy” the infrastructure.
Go to: Dashboard >terraform-jenkins> Configuration. In the general section, click on the checkbox stating, “This project is parameterized.”

Select the Choice Parameter and fill in the form as shown below. The name represents the “variable” name we will use in our pipeline script to replace the variable with the selected value.
Each choice is listed on the next line; here, we have two choices – apply and destroy. These are potential values.

Edit the jenkinsfile to use the action variable in the “ Apply” stage.

Click on Build with Parameters then Select destroy and click on Build

The image below shows the resources being destroyed along with confirmation of the instances terminated in AWS.

