How to Stop and Start Ec2 instance using Jenkins


In this tutorial you will learn how to Stop and Start AWS Ec2 instance from Jenkins

Steps:1 Create an IAM User with Acess and Secret Key

Steps:2 Create an IAM policy


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


Step: 3 Assing IAM policy to IAM user

Steps: 4 Configure IAM Access and Secret in Jenkins Server
Install package on Ubuntu Server
$ sudo apt-get install awscli
# sudo su – jenkins
$ aws configure

Steps: 5 Create Jenkins Job to start and Stop Ec2 instance
Create a parameterized build to pass the Ec2 Instance id and Ec2 State name in Start or Stop



Step: 6 Create Write Shell script in Build Step


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

Steps: 7 Now Start your Jenkins Build to Stop and Start Ec2 Instance 



Share on Google Plus

About Penguin Technology

I am a passionate cloud and DevOps professional specializing in Linux and open-source solutions. Through this blog, I share my knowledge and experience with the community, offering tips and insights on cloud technologies and DevOps practices.
    Blogger Comment

0 comments:

Post a Comment