How to Start and Stop an EC2 Instance on a Schedule Using Jenkins

Tharun kumarTharun kumar
3 min read

Introduction

Amazon EC2 instances provide scalable computing resources, but running them continuously can lead to unnecessary costs. Automating the start and stop of EC2 instances on a predefined schedule helps optimize costs while ensuring availability when needed. Jenkins, a widely used automation tool, can be configured to achieve this. In this blog post, we'll walk through setting up Jenkins to schedule the start and stop of an EC2 instance.

Prerequisites

Before proceeding, ensure you have the following:

  1. An AWS account with EC2 instances.

  2. IAM User with appropriate permissions to start and stop EC2 instances.

  3. Jenkins installed on a system with access to AWS.

  4. AWS CLI installed and configured on the Jenkins server.

  5. Jenkins Job with Scheduled Execution setup.

Step 1: Configure AWS CLI

Jenkins will use AWS CLI to start and stop EC2 instances. To configure AWS CLI:

aws configure

Provide your AWS Access Key, Secret Key, region, and output format when prompted.

Step 2: Create an IAM User and Assign Permissions

To allow Jenkins to control EC2 instances, create an IAM user with the following policy:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ec2:StartInstances",
                "ec2:StopInstances"
            ],
            "Resource": "arn:aws:ec2:*:*:instance/*"
        }
    ]
}

Attach this policy to the IAM user and use its credentials in the AWS CLI configuration.

Step 3: Create Shell Scripts to Start and Stop EC2 Instances

Create two shell scripts in the Jenkins workspace:

Script to Start EC2 Instance (start_ec2.sh)

#!/bin/bash
INSTANCE_ID="i-xxxxxxxxxxxxxxxxx"
aws ec2 start-instances --instance-ids $INSTANCE_ID

Script to Stop EC2 Instance (stop_ec2.sh)

#!/bin/bash
INSTANCE_ID="i-xxxxxxxxxxxxxxxxx"
aws ec2 stop-instances --instance-ids $INSTANCE_ID

Make sure both scripts have execution permissions:

chmod +x start_ec2.sh stop_ec2.sh

Step 4: Create Jenkins Jobs

Job 1: Start EC2 Instance

  1. Open Jenkins and create a New Freestyle Project.

  2. Under Build Steps, choose Execute Shell and add the following command:

     /path/to/start_ec2.sh
    
  3. Save and configure a Build Trigger using Build periodically (e.g., 0 6 * * * for 6 AM daily).

Job 2: Stop EC2 Instance

  1. Create another Freestyle Project.

  2. Under Build Steps, choose Execute Shell and add the following command:

     /path/to/stop_ec2.sh
    
  3. Configure a Build Trigger (e.g., 0 22 * * * for 10 PM daily).

Step 5: Verify Automation

  • Run the jobs manually in Jenkins to check if EC2 instances start and stop correctly.

  • Monitor AWS Console to see the instance state changes.

  • Check the Jenkins logs for any errors and troubleshoot accordingly.

Conclusion

By leveraging Jenkins and AWS CLI, we can efficiently automate EC2 instance scheduling, reducing costs while ensuring availability. This method can be extended further with additional scripting, notifications, or integrations with other automation tools.

Would you like to enhance this setup with Slack notifications or further security measures? Let us know in the comments!

0
Subscribe to my newsletter

Read articles from Tharun kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tharun kumar
Tharun kumar

Software Engineer