• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to Stop and Start Ec2 instance using Jenkins

by admin

Jenkins is an open-source Continuous Integration tool. However, it’s not limited to Continuous Integration alone. Jenkins is supported by a large number of plugins that enhance its capability. The Jenkins tool is written in Java and so are its plugins. The tool has a minimalistic GUI that can be improved using specific plugins if required.

In this post, you will learn how to Stop and Start the AWS Ec2 instance from Jenkins.

Creating IAM user and Policy

1. Create an IAM User with Access and Secret Key.

2. Create an IAM policy:

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"ec2:StartInstances",
"ec2:StopInstances"
],
"Resource": "*"
}
]
}

3. Assing IAM policy to IAM user.

Configure IAM access and secrete in Jenkins server

1. Configure IAM Access and Secret in Jenkins Server.

2. Install package on Ubuntu Server:

$ sudo apt-get install awscli
# sudo su – jenkins
$ aws configure

Create Jenkins Job

At a higher level, a typical Jenkins job contains a unique name, a description, parameters, build steps, and post-build actions.

1. Create Jenkins Job to start and Stop Ec2 instance.

2. Create a parameterized build to pass the EC2 Instance ID and EC2 State name in Start or Stop.

Jenkins EC2 Parameter

3. Create a Shell script in Build Step.

Jenkins Shell Script

Write Shell script in Shell Excute

if [ "$State" = "Start" ]
then
  aws ec2 start-instances --instance-ids $InstanceID
  echo Instance $InstanceID Started
elif [ "$State" = "Stop" ]
then
  aws ec2 stop-instances --instance-ids $InstanceID
  echo Instance $InstanceID Stopped
fi

Start Jenkins Build

A Jenkins build (not to be confused with a software build) can be anything from a simple Windows batch command to a complex Perl script.

Now Start your Jenkins Build to Stop and Start Ec2 Instance.

Jenkins Start Build

Filed Under: DevOps

Some more articles you might also be interested in …

  1. How to schedule master node running pod/service as a worker node
  2. How to write Bubble sort in Bash
  3. The Ultimate Beginner’s Guide to Static Website Hosting With Google Firebase (for FREE)
  4. “docker dead but subsys locked” – error while starting docker
  5. How to Download Ext JS GPL
  6. How to backup and restore Docker containers
  7. Puppet Server’s Resources Cheat Sheet with Examples
  8. Examples of “shift” Command in Shell Scripts
  9. How to Find and Delete Empty Directories and Files in Linux
  10. Understanding Positional Parameters (Passing Parameters/Arguments to Shell script)

You May Also Like

Primary Sidebar

Recent Posts

  • powertop Command Examples in Linux
  • powertop: command not found
  • powerstat: command not found
  • powerstat Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright