AWS: Make an EC2 User Data Script to Download Jenkins on an Ubuntu Instance

AWS: Make an EC2 User Data Script to Download Jenkins on an Ubuntu Instance

Objective

  • Launch EC2 instance with already installed Jenkins on it. Once server shows up in console, hit the IP address in browser and you Jenkins page should be visible.

  • Take screenshot of Userdata and Jenkins page, this will verify the task completion.

Let's Start

Login to your AWS Console and select EC2 Service. On the right corner of the window, Click on Launch Instances

Give Name to the server and Select OS Image as Ubuntu

Select Instance t2.micro (Free) Type and Keypair - Create it if not done already.

Create New Key Pair - Give name and select .ppk to login using putty.

Click on Create key pair and make sure to keep it safe and handy to use when login to this instance.

Select the keypair and scroll down.

Network settings. VPC as Default and Update Security group as follows.

Reason for Updating Security Group - SSh Port 22 was already added by default to connect to it. Also, add a rule for Custom TCP protocol and 8080 port number. Jenkins by default runs on port 8080

Scroll Down and keep storage settings as default

Extend Advanced details. Add the following script in user data under Advanced Details to install Jenkins on the EC2 instance.

#!/bin/bash
sudo apt update -y
sudo apt install openjdk-11-jre-headless -y
java -version 2>/home/ubuntu/output.txt

curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
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 enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins >>/home/ubuntu/output.txt

Review and Launch Instance.

Instance Got Created, You can also see the logs.

Now when you see instance status it is initializing and hence we are unable to connect. That means the User Data scripts are running in backend.

Now after around 5 mins of you see the status turns as below and you are able to connect.

Login to EC2 Instance and hit below commands

ls
cat /home/ubuntu/output.txt

User Data in AWS is a versatile tool in the hands of developers and system administrators alike. Whether you're looking to simplify the initial setup, automate configurations, or improve the repeatability of instance launches, User Data can prove invaluable. As with all powerful tools, however, caution and best practices should be employed to ensure efficient and safe operations.


Thanks for spending your valuable time in learning to enhance your career!๐Ÿ˜ƒ๐Ÿ™


Follow me on

Hashnode: kshitijaa.hashnode.dev

LinkedIn: linkedin.com/in/kshitija-bartakke-malwade-3..


ย