How to install Jenkins on AMI using Packer

packer
Reading Time: 3 minutes

Hello Readers, In this blog, we’ll see how we can install Jenkins on an AMI for EC2 instance in AWS using packer.  Before starting you must have Packer, and AWS CLI installed on your machine and they both must be configured. 

What is Packer?

Packer is an open-source tool that allows you to create the same machine image for multiple platforms from a single source template. A common use case is to create a golden image that teams can use across an organization on a cloud infrastructure.

Why Packer?

This is because it supports cross-platform. You can create multiple machine images from a single source file.
To achieve this, Packer uses a JSON template file to describe the configuration needed to create the image.

What is AMI?

Amazon Machine Image (AMI) is support and managed image provided by AWS that provides the information needed to launch an instance. You must specify the AMI when launching the instance. If you need multiple instances with the same configuration, you can launch multiple instances from a single AMI.

To build an AMI, firstly we have to install Packer on our machine. For installation, you can refer to this URL

Now, we’ll create a directory named Packer in which we’ll create template.json, vars.json, and script.sh file Create a template file template.json for AMI :

In the template file we are using three components:

  • variables: The variables the section is a key/value mapping of the user variable name to a default value. A default value can be an empty string.
  • builders: Builders are responsible for creating machines and generating images from them for various platforms.
  • provisioners: Provisioners use built-in and third-party software to install and configure the machine image after booting.
{
  "variables": {
    "aws_access_key": "",
    "aws_secret_key": ""
  },
  "builders": [
    {
      "type": "amazon-ebs",
      "access_key": "{{user `aws_access_key`}}",
      "secret_key": "{{user `aws_secret_key`}}",
      "region": "us-east-1",
      "source_ami": "ami-04505e74c0741db8d",
      "instance_type": "t2.micro",
      "ssh_username": "{{user `ssh_username`}}",
      "ami_name": "packer-example {{timestamp}}",
	  "tags":{
		"Name": "packer_jenkins - {{timestamp}}"
	  }
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "script": "script.sh" 
    }
  ]
}

Create a Variable file vars.json

You have to provide your AWS_ACCESS_KEY and AWS_SECRET_KEY in the vars.json file

{
    "aws_access_key": "<AWS_ACCESS_KEY>",
    "aws_secret_key": "<AWS_SECRET_KEY>",
   "ssh_username": "ubuntu"
}

Now we will create a script to install docker and docker-compose.

#!/bin/bash

echo $PWD

sudo apt-get update && sudo apt install -y docker.io
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
 sudo chmod +x /usr/local/bin/docker-compose
 sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
 
 sudo docker-compose --version
sudo systemctl restart  startup.service 
 sleep 10
sudo systemctl enable  startup.service

sudo systemctl stop  startup.service
sudo systemctl restart  startup.service
sleep 20
 sudo docker-compose up -d
 
# sudo docker-compose logs -f

Now create a docker-compose file

version: '3.8'
services:
  jenkins:
    image: jenkins/jenkins:lts
    privileged: true
    user: root
    ports:
     - 8080:8080
     - 50000:50000
  container_name: jenkins
  volumes:
    - /home/${myname}/jenkins_compose/jenkins_configuration:/var/jenkins_home
    - /var/run/docker.sock:/var/run/docker.sock

To run the JSON file for building AMI

packer build -var-file=vars.json template.json
By using the dns name you can access the jenkins and to get the initial admin password you can check the docker logs.

Now, In this blog we have created an AMI in which we have installed jenkins.

Reference:

https://www.packer.io/docs

knoldus

Written by 

Ashi Dubey is a Software Intern at Knoldus Inc Software. She has a keen interest toward learning new technologies. Her practice area is Devops. When not working, you will find her with a Book.

Leave a Reply