Setup Jenkins on Ubuntu Linux virtual machine and configure for AWS
Let us connect to Linux machine with root user and start to install Jenkins. I don’t like bother myself with sudo commands so I do everything with root user. You may do with your user, it is entirely up to you. 😊 You can also find Jenkins installation steps on Linux server in Jenkins official website. Here is its url.
https://www.jenkins.io/doc/book/installing/linux/#debianubuntu
curl -fsSL https://pkg.jenkins.io/debian-stable/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-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
apt-get update
apt install openjdk-17-jre -y
java -version
apt-get install Jenkins -y
systemctl enable jenkins
systemctl start jenkins
systemctl status jenkins
Now I will change Jenkins http port from default to 8081 one for security purposes. It is always good to change port numbers from default to different numbers like above 52000 because most of security scanners scans only most known ports until 5000 to detect a security breach in your application. So we will be saved at least from basic scanners. It is something. I have used 8081 because it is test but in production environment I suggest you to use a number above 52000. Plus don’t expose your port to out of your internal network. Let only local or vpn users can connect to your Jenkins server.
systemctl stop jenkins
sed -i ‘s|JENKINS_PORT=8080|JENKINS_PORT=8081|’ /lib/systemd/system/jenkins.service
cat /lib/systemd/system/jenkins.service | grep “JENKINS_PORT”
echo “jenkins ALL=(ALL:ALL) NOPASSWD: ALL” >> “/etc/sudoers”
systemctl daemon-reload
systemctl start jenkins
systemctl status jenkins
Jenkins is ready to use. Get Jenkins admin password from linux console to Unlock Jenkins for first login and then open your internet browser and write Jenkins URL with your linux ip address to login Jenkins. Example : http://LinuxIpAddress:YourJenkinsPort
cat /var/lib/jenkins/secrets/initialAdminPassword
Choose “Install Suggested Plugins” at next step or “Select Plugins” if you exactly know what you need to install. Then create your admin user to manage Jenkins. Then Write a domain name to login Jenkins by using https and ssl certificate if you don’t want to bother yourself with ip address. 😊 This is not our topic now that s why I didn’t get into it. You can search google. It is very basic steps. You just need to configure Jenkins.service file as we did for port number and create a certificate file to use.
We are ready to use Jenkins. Now we will install plugins to be able to use in Jenkins ci/cd pipeline stages. For example if you don’t install Docker plugin, you can not run docker commands in pipeline stages. In most of environments, to deploy an application on AWS cloud resources or on-premise Kubernetes cluster , we will need Publish Over SSH, CloudBees AWS Credentials, Docker Pipeline and GitHub Integration. Go to Manage Jenkins and Plugins. Click on Available Plugins and write plugins name to search box in plugins page to install plugins what we need. After installation finished click on Restart Jenkins Check Box under the page.
Now we need to install terraform, aws cli and docker in Jenkins server to be able to run them in pipeline stages. Connect to your linux server that you have installed Jenkins. Besides, if you plan to add other agent nodes to your Jenkins environment then you need to install them on all your agent nodes as well.
apt-get update && apt-get install -y gnupg software-properties-common curl
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add –
apt-add-repository “deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main”
apt-get update && apt-get install terraform -y
terraform -install-autocomplete
terraform –version
apt install awscli -y
apt install docker.io -y
Now let us configure aws cloud connection for Jenkins server and so that pipelines can connect to aws account. I will create a Jenkins user with administrator rights to use in pipelines with terraform but you can change your user name and its administrator rights according to your needs in iam.tf file. As first we need to use our admin user access and secret key to create a user with terraform then we will use new user access and secret key to configure aws cli connection.
You can configure aws cli either exporting keys on ssh console or copy paste them manually. It is up to you and please use your admin user access and secret keys. 😊
With Console
export AWS_ACCESS_KEY_ID=AKIACSYW3XK
export AWS_SECRET_ACCESS_KEY=Nxz3iDcp7DoXQbm0QOY
export AWS_DEFAULT_REGION=eu-west-2
echo | aws configure
With Manual Copy Paste
Let us download iam.tf from my git repository and create Jenkins user then configure aws cli with its keys. After terraform created Jenkins user then I have used its keys to configure aws cli for Jenkins pipelines. Please don’t mind about my Jenkins user access key id and secret keys due to I remove and recreate again so they will be different than you see in the screenshots. 😊
mkdir aws
cd aws
wget https://raw.githubusercontent.com/semiharsan/terraform/main/AWS/IAM/iam_user.tf
wget https://raw.githubusercontent.com/semiharsan/terraform/main/AWS/IAM/variables.tf
terraform init
terraform apply -auto-approve
you can use commands below for automatic Aws Cli configuration with new access and secret keys belongs to Jenkins user created by terraform code
export AWS_ACCESS_KEY_ID=$(cat access_key.txt)
export AWS_SECRET_ACCESS_KEY=$(cat secret_key.txt)
export AWS_DEFAULT_REGION=”eu-west-2″
echo | aws configure
Now we need to define this aws jenkins user in Jenkins Server so that pipelines can use it to connect to aws cloud. Go to Manage Jenkins – Credentials and click on (global) to add a user account. Click on Add Credentials on next page and then choose AWS Credentials on the Kind menu, then write your aws account name along with its access key id and secret key.
Well done guys. We have installed Jenkins server and prepared it to be able to deploy applications on AWS or on-premise Kubernetes Cluster. Now you can go to one of the below articles to learn ci/cd pipelines with Jenkins.
<< GO TO DEPLOY PYTHON APP ON AWS ECS SERVICE BY JENKINS CI/CD PIPELINE >>
<< GO TO HOW TO DEPLOY PYTHON APP MAIN POST >>