Last active
April 20, 2024 09:36
-
-
Save sheikhnavezz/f11eb7f88bc89549e2a8ecd7d7047771 to your computer and use it in GitHub Desktop.
Install jenkins on AWS instance via terraform and automate the manual process of taking ssh and performing the steps of script installation for jenkins on your server.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Topic: How to install jenkins via terraform without manually taking ssh of the created instance? | |
Steps : | |
1. write code for instance (here I have choosen ubuntu ami-id), default vpc and subnet. | |
2. write code for security_group | |
3. import manual key in your aws accout where you will get public and private key by using ssh-keygen cmd | |
4. Vs code: | |
create a dir. named as : Jenkins | |
create files in the jenkins folder: | |
main.tf, variables.tf, terraform.tfvars, keys, provider.tf, output.tf, jenkins.sh | |
5. provider.tf : | |
provider "aws" { | |
region = "us-east-2" # Change this to your desired AWS region | |
profile = "tf-navez" | |
} | |
#--------------- | |
6. main.tf: | |
resource "aws_instance" "jenkins_instance" { | |
ami = var.ami_value | |
instance_type = var.instance_type_value | |
key_name = var.key_pair | |
tags = { | |
Name = "Jenkins Server" | |
} | |
} | |
# -------------------------------- | |
# create empty resource block for userdata without taking ssh for instance | |
resource "null_resource" "name" { | |
connection { | |
type = "ssh" | |
user = "ubuntu" | |
private_key = file("./key/id_rsa") | |
host = aws_instance.jenkins_instance.public_ip | |
} | |
# here copy the jenkin.sh file from your dir. to the instance | |
provisioner "file" { | |
source = "jenkins.sh" | |
destination = "/tmp/jenkins.sh" # --- this will create a directory in our ec2 instance from where we will run the file. | |
} | |
# set the permissions to run the shell script file | |
provisioner "remote-exec" { | |
inline = [ | |
"sudo chmod +x /tmp/jenkins.sh", #--- this will give execute permission to the folder | |
"sh /tmp/jenkins.sh", #--- this will run the shell script | |
] | |
} | |
# wait for ec2 instance to be created before executing all the jenkins process | |
depends_on = [aws_instance.jenkins_instance] | |
# this depend_on will create the instance, take ssh into it, and run the following above cmds. | |
} | |
# -------------------------------- | |
resource "aws_default_vpc" "default_vpc" { | |
tags = { | |
Name = "deafult vpc" | |
} | |
} | |
resource "aws_key_pair" "id_rsa" { | |
key_name = var.key_pair | |
public_key = file("./key/id_rsa.pub") | |
} | |
resource "aws_default_subnet" "default_subnet" { | |
availability_zone = var.availability_zone | |
tags = { | |
Name = "default_subnet" | |
} | |
} | |
resource "aws_security_group" "security_group" { | |
# name = var.security_group_rules | |
vpc_id = aws_default_vpc.default_vpc.id | |
ingress { | |
description = "allow access to ports 8080 " | |
from_port = 8080 | |
to_port = 8080 | |
protocol = "tcp" | |
cidr_blocks = ["0.0.0.0/0"] | |
} | |
ingress { | |
description = "ssh access" | |
from_port = 22 | |
to_port = 22 | |
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 server security", | |
} | |
} | |
# ------------ | |
7. variables.tf: | |
# Defining ami value | |
variable "ami_value" { | |
description = "value for the ami" | |
} | |
# Defining instance type | |
variable "instance_type_value" { | |
description = "value for instance type" | |
} | |
# defining key pair | |
variable "key_pair" { | |
description = "key pair value" | |
} | |
# Defining AZ of instance | |
variable "availability_zone" { | |
type = string | |
description = "AWS availability zone for resources" | |
} | |
# Defining securtiy group | |
variable "security_group_rules" { | |
description = "allow access to ports 8080 and 22 " | |
} | |
#-------------- | |
8. output.tf: | |
output "jenkins_url" { | |
value = "http://${aws_instance.jenkins_instance.public_ip}:8080" | |
} | |
output "public_ip" { | |
value = aws_instance.jenkins_instance.public_ip | |
} | |
#-------------- | |
9. terraform.tfvars: | |
ami_value = "ami-0e83be366243f524a" | |
instance_type_value = "t2.medium" | |
key_pair = "./key/id_rsa.pub" | |
availability_zone = "us-east-2a" | |
security_group_rules = [ | |
{ | |
from_port = 22 | |
to_port = 22 | |
protocol = "tcp" | |
cidr_blocks = ["0.0.0.0/0"] | |
}, | |
{ | |
from_port = 8080 | |
to_port = 8080 | |
protocol = "tcp" | |
cidr_blocks = ["0.0.0.0/0"] | |
} | |
] | |
10. jenkins.sh: | |
#!/bin/bash | |
sudo apt update -y | |
sudo apt install -y fontconfig openjdk-17-jre | |
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \ | |
https://pkg.jenkins.io/debian/jenkins.io-2023.key | |
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \ | |
https://pkg.jenkins.io/debian binary/ | sudo tee \ | |
/etc/apt/sources.list.d/jenkins.list > /dev/null | |
sudo apt-get update -y | |
sudo apt-get install jenkins -y | |
sudo systemctl start jenkins | |
sudo systemctl enable jenkins | |
terraform init | |
terraform fmt | |
terraform plan | |
terraform apply | |
terraform destroy | |
Hence, you have successfully installed jenkins via terraform |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment