PoshJosh's Blog

Automate deployment of Jenkins to AWS - Part 1 - Semi automation - Single EC2 instance

December 14, 2018

Last updated: 2020-05-06T17:25:00

Before we set off

About this Article

  • Hi, this article is one of a series of 3 articles.

  • The series is a guide to walk you through automating the deployment of

Jenkins to AWS; from semi-automation of a single EC2 instance to full automation of a Jenkins cluster. Below are the links to each of the 3 parts:

  • This article is the first part of the series.

  • You can find the source for this article here

  • The objective of this first part is to create a re-usable amazon AMI which

auto installs Jenkins, some specified plugins and omits the initial user password.

  • It is an estimated 12 minute walk through.

Prerequisites

  • For Part 1 you need to enjoy some familiarity with AWS CLI, Putty,

linux/windows shell commands and docker.

  • For Parts 2 and 3 you need to be familiar with terraform and packer in

addition to the aforementioned.

  • It is expected that you enjoy some familiarity with these tools.

Notwithstanding, if you follow the instructions, you should get by just fine.

EC2 key pair. Usually a .ppk (windows) or .pem (linux) file.

  • Have AWS-CLI installed and AWS configured with secret keys:

    • Secret access key

    • Access Key ID

Create an amaon EC2 instance

  • In order to create a re-usable amazon AMI which auto installs Jenkins, we

need to first create an amazon EC2 instance.

  • Browse to AWS Management Console -> Compute -> EC2
  • Click on Launch Instance (at the top)
  • Select the Amazon Machine Image (AMI) - Make sure to use an Amazon Linux 2

image or you won’t be able to find or install amazon-linux-extras required later in this article.

  • Select the instance type. Free tier t2.micro will not have a enough

capacity for both jenkins and sonarqube.

  • Enter other instance details
  • Click on advanced details
  • Enter the bash script below for the user data. It will be run on first launch.
#!/bin/bash
@echo off
echo "Re-start required after running this script."
echo "... Updating system"
sudo yum update -y
echo "... Installing docker"
sudo amazon-linux-extras install docker
echo "... Starting docker service"
sudo service docker start
echo "... Adding ec2 user to docker group"
sudo usermod -a -G docker ec2-user
echo "... Restarting docker"
sudo service docker restart

You can download actual file with the above contents, here

  • Wait till the EC2 instance is running and has passed status checks

Builder a jenkins image via docker

  • Next we create 3 files as follows and put them in any directory of choice.

I will be putting mine in /jenkins-auto-install.

View the 3 files through the following links:

This is the docker file that will be used to build the jenkins image

This file contains the names of plugins to install, one on each line.

  • Next we transfer the contents of the entire directory we just created to the

remote EC2 instance. We achieve this using Putty.

The following example transfers the file Sample_file.txt from the C:\ drive on a Windows computer to the ec2-user home directory on an Amazon Linux instance:

pscp -i C:\path\my-key-pair.ppk C:\path\Sample_file.txt ec2-user@public_dns:/home/ec2-user/Sample_file.txt

Open a command prompt on windows and enter the following command, replacing <path-to-ppk-file> and <public_dns> with appropriate values.

pscp -i "<path-to-ppk-file>" C:\Users\User\Documents\GitHub\jenkins-auto-install\* ec2-user@<public_dns>:/home/ec2-user/
  • Next we run the following command to build jenkins image from Dockerfile in current directory:

The docker file was one of the files we transfered to the EC2 instance

sudo docker build -t poshjosh/jenkins:lts .

Update user data of the EC2 instance

  • Next we update the user data of our EC2 instance:

    • Stop the EC2 instance
    • Click Actions
    • View/Edit user data
    • Download this file and use it to update the user data.
  • Now start the EC2 instance

  • Wait till the EC2 instance is running and has passed status checks

  • Browse to http://<ec2_public_dns>:8080 to view your jenkins installation.

  • If you encounter any problems read the observations section at the end of this article.

Create an Amazon Machin Image (AMI) from EC2 instance

Login with the password which we hard coded into the Dockerfile

ENV JENKINS_USER poshjosh
ENV JENKINS_PASS UB40-music
  • Now create a re-usable image

    • Click Actions -> Image - Create image
    • Follow the instructions to create a re-usable amazon AMI which auto installs jenkins, some specified plugins and omits the initial user password.
  • This series was designed to give you a feel of automating jenkins installation

on aws with a semi-automated exampl. The next article will involve the fully automated deployment of a single jenkins server to an AWS ec2 instance.

Observations

1. Cannot access jenkins or sonarqube via the web browser

If you can’t browse to http://<public-dns>:8080 or http://<public-dns>:9000

  • Confirm that you are using the correct EC2 instance type.
    • Browse to EC2 console -> Click on the EC2 instance
    • Click on the Monitoring tab
    • Check CPU Utilization (Percent). If it is very high, say 90% then you
    probably have the wrong instance type.
  • Check that you have selected the correct EC2 instance and thus copied

the correct public-DNS.

  • Confirm that the port numbers for both jenkins and sonarqube are consistent

in setup.sh, user-data.txt and terraform.tf

2. Use Putty to execute a shell script from Windows to remote linux (AWS)

  • Linux shell - Bad interpreter error
[ec2-user@ip-172-31-16-248 ~]$ chmod +x docker_run_jenkins_and_sonarqube.sh
[ec2-user@ip-172-31-16-248 ~]$ ./docker_run_jenkins_and_sonarqube.sh
-bash: ./docker_run_jenkins_and_sonarqube.sh: /bin/bash^M: bad interpreter: No such file or directory
  • Solution

The script indicates that it must be executed by a shell located at /bin/bash^M. There is no such file: it’s called /bin/bash.

The ^M is a carriage return character. Linux uses the line feed character to mark the end of a line, whereas Windows uses the two-character sequence CR LF. Your file has Windows line endings, which is confusing Linux.

Remove the spurious CR characters. You can do it with the following command:

sed -i -e 's/\r$//' create_mgw_3shelf_6xIPNI1P.sh

3. Use Putty to send a directory from windows to remote linux server (AWS)

  • Space not allowed in file paths. In this case in .ppk file path
C:\Windows\system32>pscp -i C:\Users\User\Google Drive\IT\software\AWS\demo.ppk -r C:\Users\User\Documents\GitHub\jenkins-auto-install <public_dns>:/home
Unable to use key file "C:\Users\User\Google" (unable to open file)
FATAL ERROR: No supported authentication methods available (server sent: publickey)
  • %20 can’t be used to replace space in file paths. In this case in .ppk file path
C:\Windows\system32>pscp -i C:\Users\User\Google%20Drive\IT\software\AWS\demo.ppk -r C:\Users\User\Documents\GitHub\jenkins-auto-install <public_dns>:/home
Unable to use key file "C:\Users\User\Google%20Drive\IT\software\AWS\demo.ppk" (unable to open file)
FATAL ERROR: No supported authentication methods available (server sent: publickey)
  • Single quote will not work to enclose file paths with space(s)
C:\Windows\system32>pscp -i 'C:\Users\User\Google Drive\IT\software\AWS\demo.ppk' -r C:\Users\User\Documents\GitHub\jenkins-auto-install <public_dns>:/home
Unable to use key file "'C:\Users\User\Google" (unable to open file)
FATAL ERROR: No supported authentication methods available (server sent: publickey)
  • Missing user name (i.e ec2-user) prefix in target dir
C:\Windows\system32>pscp -i "C:\Users\User\Google Drive\IT\software\AWS\demo.ppk" -r C:\Users\User\Documents\GitHub\jenkins-auto-install <public_dns>:/home
Server refused our key
FATAL ERROR: No supported authentication methods available (server sent: publickey)
  • Adding -l user ec2-user will not help as long as there is missing user name (i.e ec2-user) prefix in target dir
C:\Windows\system32>pscp -i "C:\Users\User\Google Drive\IT\software\AWS\demo.ppk" -l user ec2-user -r C:\Users\User\Documents\GitHub\jenkins-auto-install <public_dns>:/home
Server refused our key
FATAL ERROR: No supported authentication methods available (server sent: publickey)
  • Adding -l user ec2-user, while having user name (i.e ec2-user) prefix in target dir, will lead to error
C:\Windows\system32>pscp -i "C:\Users\User\Google Drive\IT\software\AWS\demo.ppk" -l user ec2-user -r C:\Users\User\Documents\GitHub\jenkins-auto-install ec2-user@<public_dns>:/home
pscp: ec2-user: No such file or directory

pscp: -r: No such file or directory

pscp: C:\Users\User\Documents\GitHub\jenkins-auto-install: not a regular file
  • You obviously cannot copy to /home/ directory this way. Rather copy to /home/ec2-user (user home path)
C:\Windows\system32>pscp -i "C:\Users\User\Google Drive\IT\software\AWS\demo.ppk" C:\Users\User\Documents\GitHub\jenkins-auto-install\* ec2-user@<public_dns>:/home/
pscp: unable to open /home//default-user.groovy: permission denied
pscp: unable to open /home//Dockerfile: permission denied
pscp: unable to open /home//jenkins-plugins: permission denied
  • Success
C:\Windows\system32>pscp -i "C:\Users\User\Google Drive\IT\software\AWS\demo.ppk" C:\Users\User\Documents\GitHub\jenkins-auto-install\* ec2-user@<public_dns>:/home/ec2-user/
default-user.groovy       | 0 kB |   0.7 kB/s | ETA: 00:00:00 | 100%
Dockerfile                | 0 kB |   0.6 kB/s | ETA: 00:00:00 | 100%
jenkins-plugins           | 0 kB |   0.2 kB/s | ETA: 00:00:00 | 100%

References


Written byChinomso IkwuagwuExcélsior

Limited conversations with distributed systems.

Modifying legacy applications using domain driven design (DDD)

Gherkin Best Practices

Code Review Best Practices

Hacking Cypress in 9 minutes

Some common mistakes when developing java web applications

How to make a Spring Boot application production ready

SQL JOINS - A Refresher

Add Elasticsearch to Spring Boot Application

Add entities/tables to an existing Jhipster based project

CSS 3 Media Queries - All over again

Maven Dependency Convergence - quick reference

Amazon SNS Quick Reference

AWS API Gateway Quick Reference

Amazon SQS Quick Reference

AWS API Gateway Quick Reference

AWS Lambda Quick Reference

Amazon DynamoDB - Quick Reference

Amazon Aurora

Amazon Relational Database Service

AWS Database Services

AWS Security Essentials

Amazon Virtual Private Cloud Connectivity Options

Summary of AWS Services

AWS Certified Solutions Architect - Quick Reference

AWS CloudFront FAQs - Curated

AWS VPC FAQs - Curated

AWS EC2 FAQs - Curated

AWS Achritect 5 - Architecting for Cost Optimization

AWS Achritect 4 - Architecting for Performance Efficiency

AWS Achritect - 6 - Passing the Certification Exam

AWS Achitect 3 - Architecting for Operational Excellence

AWS Achitect 2 - Architecting for Security

AWS Achitect 1 - Architecting for Reliability

Amazon DynamoDB Accelerator (DAX)

Questions and Answers - AWS Certified Cloud Architect Associate

Questions and Answers - AWS Certified Cloud Architect Associate

AWS Connectivity - PrivateLink, VPC-Peering, Transit-gateway and Direct-connect

AWS - VPC peering vs PrivateLink

Designing Low Latency Systems

AWS EFS vs FSx

AWS Regions, Availability Zones and Local Zones

AWS VPC Endpoints and VPC Endpoint Services (AWS Private Link)

AWS - IP Addresses

AWS Elastic Network Interfaces

AWS Titbits

Jenkins on AWS - Automation

Jenkins on AWS - Setup

Jenkins on AWS - Best practices

Introduction to CIDR Blocks

AWS Lamda - Limitations and Use Cases

AWS Certified Solutions Architect Associate - Part 10 - Services and design scenarios

AWS Certified Solutions Architect Associate - Part 9 - Databases

AWS Certified Solutions Architect Associate - Part - 8 Application deployment

AWS Certified Solutions Architect Associate - Part 7 - Autoscaling and virtual network services

AWS Certified Solutions Architect Associate - Part 6 - Identity and access management

AWS Certified Solutions Architect Associate - Part 5 - Compute services design

AWS Certified Solutions Architect Associate - Part 4 - Virtual Private Cloud

AWS Certified Solutions Architect Associate - Part 3 - Storage services

AWS Certified Solutions Architect Associate - Part 2 - Introduction to Security

AWS Certified Solutions Architect Associate - Part 1 - Key services relating to the Exam

AWS Certifications - Part 1 - Certified solutions architect associate

AWS Virtual Private Cloud (VPC) Examples

Curated info on AWS Virtual Private Cloud (VPC)

Notes on Amazon Web Services 8 - Command Line Interface (CLI)

Notes on Amazon Web Services 7 - Elastic Beanstalk

Notes on Amazon Web Services 6 - Developer, Media, Migration, Productivity, IoT and Gaming

Notes on Amazon Web Services 5 - Security, Identity and Compliance

Notes on Amazon Web Services 4 - Analytics and Machine Learning

Notes on Amazon Web Services 3 - Managment Tools, App Integration and Customer Engagement

Notes on Amazon Web Services 2 - Storages databases compute and content delivery

Notes on Amazon Web Services 1 - Introduction

AWS Auto Scaling - All you need to know

AWS Load Balancers - How they work and differences between them

AWS EC2 Instance Types - Curated

Amazon Web Services - Identity and Access Management Primer

Amazon Web Services - Create IAM User

Preparing Jenkins after Installation

Jenkins titbits, and then some

Docker Titbits

How to Add Chat Functionality to a Maven Java Web App

Packer - an introduction

Terraform - an introduction

Versioning REST Resources with Spring Data REST

Installing and running Jenkins in Docker

Automate deployment of Jenkins to AWS - Part 2 - Full automation - Single EC2 instance

Automate deployment of Jenkins to AWS - Part 1 - Semi automation - Single EC2 instance

Introduction to Jenkins

Software Engineers Reference - Dictionary, Encyclopedia or Wiki - For Software Engineers