Optimizing Terraform with Terragrunt: Deploying Jenkins on AWS EC2
This tutorial demonstrates how to use Terragrunt to streamline Terraform configurations for provisioning an AWS EC2 instance, installing Jenkins, configuring security groups and a private S3 bucket, and creating a simple Jenkins pipeline, including step‑by‑step commands and full code examples.
Welcome to the DevOps Cloud Classroom. Follow the live session on March 14 at 20:00 to learn how to use Terragrunt to optimize Terraform code.
Terraform, created by HashiCorp, is an open‑source infrastructure‑as‑code tool that uses HCL or JSON to define cloud resources such as AWS EC2 instances and S3 buckets. In this project you will learn how to deploy an EC2 instance, bootstrap it with Jenkins, create a Jenkins security group, set up a private S3 bucket for Jenkins artifacts, and build a simple Jenkins pipeline.
Prerequisites
You need to have the following tools installed:
AWS CLI (installed and configured)
Terraform (installed and configured)
An IDE (VS Code is used in the example)
Project Initialization
Create a new folder in your IDE, cd into it, and add main.tf , variable.tf , providers.tf and outputs.tf files.
main.tf contains the main configuration:
resource "aws_instance" "instance" {
ami = var.ami
instance_type = var.instance
user_data = var.ec2_user_data
vpc_security_group_ids = [aws_security_group.security_group.id]
tags = {
Name = "Jenkins Instance"
}
}
resource "aws_security_group" "security_group" {
vpc_id = var.vpc
ingress {
description = "Allow SSH from my Public IP"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["-.-.-.-/32"]
}
ingress {
description = "Allows Access to the Jenkins Server"
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "Allows Access to the Jenkins Server"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "Jenkins Security Group"
}
}
resource "aws_s3_bucket" "jojenkinsbucket" {
bucket = "jojenkinsbucket"
}
resource "aws_s3_bucket_acl" "jenkinsbucketacl" {
bucket = aws_s3_bucket.jojenkinsbucket.id
acl = "private"
}variable.tf defines the variables:
variable "vpc" {
description = "The Default VPC of EC2"
type = string
default = "vpc-0be40a17d234455e3"
}
variable "ami" {
description = "The AMI ID of the Instance"
type = string
default = "ami-0dfcb1ef8550277af"
}
variable "instance" {
description = "The Instance Type of EC2"
type = string
default = "t2.micro"
}
variable "ec2_user_data" {
description = "User Data for Jenkins EC2"
type = string
default = <<-EOF
#!/bin/bash
sudo yum update -y
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
sudo yum upgrade
sudo amazon-linux-extras install java-openjdk11 -y
sudo yum install -y jenkins
sudo systemctl enable jenkins
sudo systemctl start jenkins
EOF
}providers.tf configures the AWS provider:
provider "aws" {
region = "us-east-1"
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}outputs.tf outputs the public IP after deployment:
output "public_ip" {
value = aws_instance.instance.public_ip
}Infrastructure Deployment
Run terraform init to initialize the working directory and download required plugins.
Run terraform validate to verify the configuration files.
Run terraform plan to preview the actions Terraform will take.
Run terraform apply to create, update, or destroy resources as defined.
After applying, verify the EC2 instance creation in the AWS console.
Testing Jenkins
Jenkins Pipeline is a set of plugins that enable continuous delivery pipelines. Access the Jenkins UI via EC2_PUBLIC_IP:8080 , create a new pipeline project, and use the following script:
pipeline {
agent any
stages {
stage("build") {
steps {
echo 'Building the application...'
}
}
stage("test") {
steps {
echo 'Testing the application...'
}
}
stage("deploy") {
steps {
echo 'Deploying the application...'
}
}
}
}Trigger an immediate build; a green view indicates success.
Destroying Resources
When the experiment is finished, run terraform destroy to delete all created resources and avoid unnecessary charges.
By completing this tutorial you have learned how to deploy an EC2 instance, bootstrap it with Jenkins, configure security groups, create a private S3 bucket for Jenkins artifacts, and build a simple Jenkins pipeline.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.