From 51cdda948e82747fee8d216604b7037186aee741 Mon Sep 17 00:00:00 2001 From: Saravanan Gnanaguru Date: Mon, 19 Aug 2024 23:33:30 +0530 Subject: [PATCH] added jenkins installation script --- tf-ec2-with-modules/terraform-project/main.tf | 1 + .../terraform-project/modules/ec2/main.tf | 1 + .../terraform-project/modules/ec2/outputs.tf | 4 +++ .../modules/ec2/variables.tf | 5 ++++ .../modules/security-group/main.tf | 26 +++++++++++++------ .../terraform-project/outputs.tf | 3 +++ .../scripts/install-jenkins.sh | 26 +++++++++++++++++++ .../terraform-project/variables.tf | 5 ++++ 8 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 tf-ec2-with-modules/terraform-project/scripts/install-jenkins.sh diff --git a/tf-ec2-with-modules/terraform-project/main.tf b/tf-ec2-with-modules/terraform-project/main.tf index 5795caa..8325ea6 100644 --- a/tf-ec2-with-modules/terraform-project/main.tf +++ b/tf-ec2-with-modules/terraform-project/main.tf @@ -43,4 +43,5 @@ module "ec2" { subnet_id = module.subnet.subnet_id security_group_id = module.security_group.security_group_id key_name = var.key_name + user_data = file("${var.user_data}") } \ No newline at end of file diff --git a/tf-ec2-with-modules/terraform-project/modules/ec2/main.tf b/tf-ec2-with-modules/terraform-project/modules/ec2/main.tf index a8d5c8a..eab60cf 100644 --- a/tf-ec2-with-modules/terraform-project/modules/ec2/main.tf +++ b/tf-ec2-with-modules/terraform-project/modules/ec2/main.tf @@ -4,6 +4,7 @@ resource "aws_instance" "main" { subnet_id = var.subnet_id security_groups = [var.security_group_id] key_name = var.key_name + user_data = var.user_data tags = { Name = "MyEC2Instance" diff --git a/tf-ec2-with-modules/terraform-project/modules/ec2/outputs.tf b/tf-ec2-with-modules/terraform-project/modules/ec2/outputs.tf index 28d05f4..f6c4ba4 100644 --- a/tf-ec2-with-modules/terraform-project/modules/ec2/outputs.tf +++ b/tf-ec2-with-modules/terraform-project/modules/ec2/outputs.tf @@ -1,3 +1,7 @@ output "instance_id" { value = aws_instance.main.id +} + +output "instance_public_ip" { + value = aws_instance.main.public_ip } \ No newline at end of file diff --git a/tf-ec2-with-modules/terraform-project/modules/ec2/variables.tf b/tf-ec2-with-modules/terraform-project/modules/ec2/variables.tf index e941026..d3d44d9 100644 --- a/tf-ec2-with-modules/terraform-project/modules/ec2/variables.tf +++ b/tf-ec2-with-modules/terraform-project/modules/ec2/variables.tf @@ -21,4 +21,9 @@ variable "security_group_id" { variable "key_name" { description = "value of the key pair" type = string +} + +variable "user_data" { + description = "filename of userdata file" + type = string } \ No newline at end of file diff --git a/tf-ec2-with-modules/terraform-project/modules/security-group/main.tf b/tf-ec2-with-modules/terraform-project/modules/security-group/main.tf index 120dd8e..8c066d0 100644 --- a/tf-ec2-with-modules/terraform-project/modules/security-group/main.tf +++ b/tf-ec2-with-modules/terraform-project/modules/security-group/main.tf @@ -2,17 +2,27 @@ resource "aws_security_group" "main" { vpc_id = var.vpc_id ingress { - from_port = 22 - to_port = 22 + description = "Allow Jenkins traffic" + from_port = 8080 + to_port = 8080 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] # Allows access from anywhere + } + + ingress { + description = "Allow HTTP traffic" + from_port = 80 + to_port = 80 protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] + cidr_blocks = ["0.0.0.0/0"] # Allows access from anywhere } - egress { - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] + ingress { + description = "Allow SSH traffic" + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] # Adjust for more security if needed } tags = { diff --git a/tf-ec2-with-modules/terraform-project/outputs.tf b/tf-ec2-with-modules/terraform-project/outputs.tf index 4f01b47..ac912bb 100644 --- a/tf-ec2-with-modules/terraform-project/outputs.tf +++ b/tf-ec2-with-modules/terraform-project/outputs.tf @@ -1,3 +1,6 @@ output "instance_id" { value = module.ec2.instance_id +} +output "instance_public_ip" { + value = module.ec2.instance_public_ip } \ No newline at end of file diff --git a/tf-ec2-with-modules/terraform-project/scripts/install-jenkins.sh b/tf-ec2-with-modules/terraform-project/scripts/install-jenkins.sh new file mode 100644 index 0000000..8fd9442 --- /dev/null +++ b/tf-ec2-with-modules/terraform-project/scripts/install-jenkins.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Update the package index +sudo yum update -y + +# Install Java 11 (required for Jenkins) +sudo amazon-linux-extras install java-openjdk11 -y + +# Enable the Jenkins repository +sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo + +# Import the Jenkins package signing key +sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key + +# Install Jenkins +sudo yum install jenkins -y + +# Start the Jenkins service +sudo systemctl start jenkins + +# Enable Jenkins to start on system boot +sudo systemctl enable jenkins + +# Print the Jenkins initial admin password +echo "Jenkins initial admin password:" +sudo cat /var/lib/jenkins/secrets/initialAdminPassword diff --git a/tf-ec2-with-modules/terraform-project/variables.tf b/tf-ec2-with-modules/terraform-project/variables.tf index d308e05..40b4601 100644 --- a/tf-ec2-with-modules/terraform-project/variables.tf +++ b/tf-ec2-with-modules/terraform-project/variables.tf @@ -5,3 +5,8 @@ variable "key_name" { default = "your-pem-key-name" } +variable "user_data" { + description = "filename of userdata file" + type = string + default = "userdata.sh" +} \ No newline at end of file