From 324d051f465e5a88cf2b8593ace46a49d3be1f75 Mon Sep 17 00:00:00 2001 From: Saravanan Gnanaguru Date: Sun, 1 Sep 2024 20:37:38 +0530 Subject: [PATCH] updated ubuntu scripts --- tf-ec2-with-modules/terraform-project/ami.tf | 32 ++++++++++++++++ tf-ec2-with-modules/terraform-project/main.tf | 19 +--------- .../terraform-project/modules/ec2/ami.tf | 32 ++++++++++++++++ .../terraform-project/modules/ec2/main.tf | 2 +- ...jenkins.sh => install-jenkins-az-linux.sh} | 0 .../scripts/install-jenkins-ubuntu-linux.sh | 38 +++++++++++++++++++ 6 files changed, 104 insertions(+), 19 deletions(-) create mode 100644 tf-ec2-with-modules/terraform-project/ami.tf create mode 100644 tf-ec2-with-modules/terraform-project/modules/ec2/ami.tf rename tf-ec2-with-modules/terraform-project/scripts/{install-jenkins.sh => install-jenkins-az-linux.sh} (100%) create mode 100644 tf-ec2-with-modules/terraform-project/scripts/install-jenkins-ubuntu-linux.sh diff --git a/tf-ec2-with-modules/terraform-project/ami.tf b/tf-ec2-with-modules/terraform-project/ami.tf new file mode 100644 index 0000000..b5311c6 --- /dev/null +++ b/tf-ec2-with-modules/terraform-project/ami.tf @@ -0,0 +1,32 @@ +data "aws_ami" "amazon_linux" { + most_recent = true + + filter { + name = "name" + values = ["amzn2-ami-hvm-*-x86_64-gp2"] + + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + + owners = ["amazon"] +} + +data "aws_ami" "ubuntu_linux" { + most_recent = true + + filter { + name = "name" + values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*", "ubuntu/images/hvm-ssd/ubuntu-lunar-24.04-amd64-server-*"] + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + + owners = ["amazon"] +} \ No newline at end of file diff --git a/tf-ec2-with-modules/terraform-project/main.tf b/tf-ec2-with-modules/terraform-project/main.tf index 8325ea6..bed75af 100644 --- a/tf-ec2-with-modules/terraform-project/main.tf +++ b/tf-ec2-with-modules/terraform-project/main.tf @@ -19,26 +19,9 @@ module "security_group" { vpc_id = module.vpc.vpc_id } - -data "aws_ami" "example" { - most_recent = true - - filter { - name = "name" - values = ["amzn2-ami-hvm-*-x86_64-gp2"] - } - - filter { - name = "virtualization-type" - values = ["hvm"] - } - - owners = ["amazon"] -} - module "ec2" { source = "./modules/ec2" - ami = data.aws_ami.example.id + ami = data.aws_ami.ubuntu_linux.id instance_type = "t2.micro" subnet_id = module.subnet.subnet_id security_group_id = module.security_group.security_group_id diff --git a/tf-ec2-with-modules/terraform-project/modules/ec2/ami.tf b/tf-ec2-with-modules/terraform-project/modules/ec2/ami.tf new file mode 100644 index 0000000..b5311c6 --- /dev/null +++ b/tf-ec2-with-modules/terraform-project/modules/ec2/ami.tf @@ -0,0 +1,32 @@ +data "aws_ami" "amazon_linux" { + most_recent = true + + filter { + name = "name" + values = ["amzn2-ami-hvm-*-x86_64-gp2"] + + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + + owners = ["amazon"] +} + +data "aws_ami" "ubuntu_linux" { + most_recent = true + + filter { + name = "name" + values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*", "ubuntu/images/hvm-ssd/ubuntu-lunar-24.04-amd64-server-*"] + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + + owners = ["amazon"] +} \ 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 eab60cf..7e5d20c 100644 --- a/tf-ec2-with-modules/terraform-project/modules/ec2/main.tf +++ b/tf-ec2-with-modules/terraform-project/modules/ec2/main.tf @@ -1,5 +1,5 @@ resource "aws_instance" "main" { - ami = var.ami + ami = data.aws_ami.ubuntu_linux.id instance_type = var.instance_type subnet_id = var.subnet_id security_groups = [var.security_group_id] diff --git a/tf-ec2-with-modules/terraform-project/scripts/install-jenkins.sh b/tf-ec2-with-modules/terraform-project/scripts/install-jenkins-az-linux.sh similarity index 100% rename from tf-ec2-with-modules/terraform-project/scripts/install-jenkins.sh rename to tf-ec2-with-modules/terraform-project/scripts/install-jenkins-az-linux.sh diff --git a/tf-ec2-with-modules/terraform-project/scripts/install-jenkins-ubuntu-linux.sh b/tf-ec2-with-modules/terraform-project/scripts/install-jenkins-ubuntu-linux.sh new file mode 100644 index 0000000..2dc6e18 --- /dev/null +++ b/tf-ec2-with-modules/terraform-project/scripts/install-jenkins-ubuntu-linux.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +# Update the package index +sudo apt-get update -y + +# Install Java 17 (latest stable version required for Jenkins) +sudo apt-get install openjdk-17-jdk -y + +# Set JAVA_HOME environment variable +echo "export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64" | sudo tee -a /etc/profile +echo "export PATH=\$JAVA_HOME/bin:\$PATH" | sudo tee -a /etc/profile +source /etc/profile + +# Enable the Jenkins repository +wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add - +sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list' + +# Update the package index again +sudo apt-get update -y + +# Install Jenkins +sudo apt-get install jenkins -y + +# Start the Jenkins service +sudo systemctl start jenkins + +# Enable Jenkins to start on system boot +sudo systemctl enable jenkins + +# Wait for Jenkins to complete setup +echo "Waiting for Jenkins to complete setup..." +while [ ! -f /var/lib/jenkins/secrets/initialAdminPassword ]; do + sleep 10 +done + +# Print the Jenkins initial admin password +echo "Jenkins initial admin password:" +sudo cat /var/lib/jenkins/secrets/initialAdminPassword \ No newline at end of file