Skip to content

Commit

Permalink
WIP Jenkins on AWS with Terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolman-Freecss committed Oct 16, 2024
1 parent f951496 commit ce3eddd
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
108 changes: 108 additions & 0 deletions .infra/terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Create a VPC
resource "aws_vpc" "main_vpc" {
cidr_block = "10.0.0.0/16"

tags = {
Name = "Main VPC"
}
}

# Create an Internet Gateway for the VPC
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main_vpc.id
}

# Create a Route Table
resource "aws_route_table" "public_rt" {
vpc_id = aws_vpc.main_vpc.id

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}

tags = {
Name = "Public Route Table"
}
}

# Associate the route table with the subnet
resource "aws_route_table_association" "public_rt_assoc" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public_rt.id
}

resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.main_vpc.id
cidr_block = "10.0.1.0/24" # Range of IP addresses for the subnet
map_public_ip_on_launch = true

tags = {
Name = "Public Subnet"
}
}



# Create a group of security rules for Jenkins
resource "aws_security_group" "jenkins_sg" {
name = "jenkins-sg"
description = "Security group for Jenkins instance"
vpc_id = aws_vpc.main_vpc.id

# Permit incoming traffic on ports 80, 22, and 8080
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

# Permit outgoing traffic to all destinations
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}

# Create an EC2 instance for Jenkins
resource "aws_instance" "jenkins_server" {
ami = "ami-0e54eba7c51c234f6" # Amazon Linux 2 AMI or the AMI of your choice
instance_type = var.instance_type
key_name = var.key_name
subnet_id = aws_subnet.public_subnet.id
vpc_security_group_ids = [aws_security_group.jenkins_sg.id] # Use the security group created above

# Connect the initialization script to the instance
user_data = <<-EOF
#!/bin/bash
sudo yum update -y
sudo yum install -y java-11-amazon-corretto
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-2023.key
sudo yum install -y jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins
EOF

tags = {
Name = "Jenkins Server"
}
}

9 changes: 9 additions & 0 deletions .infra/terraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "jenkins_public_ip" {
description = "Public IP address of the Jenkins instance"
value = aws_instance.jenkins_server.public_ip
}

output "jenkins_url" {
description = "URL of the Jenkins instance"
value = "http://${aws_instance.jenkins_server.public_ip}:8080"
}
3 changes: 3 additions & 0 deletions .infra/terraform/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
provider "aws" {
region = "us-east-1" # Specify the region where the resources will be created
}
13 changes: 13 additions & 0 deletions .infra/terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
variable "instance_type" {
default = "t2.micro"
}

variable "key_name" {
description = "SSH key name"
default = "my-ssh-key"
}

variable "subnet_id" {
description = "Subnet ID where the instance will be created"
default = ""
}

0 comments on commit ce3eddd

Please sign in to comment.