Enhancing Efficiency by Tailoring EC2 Instance Schedules to Non-Peak Hours.

Enhancing Efficiency by Tailoring EC2 Instance Schedules to Non-Peak Hours.

ARCHITECTURE


In the swiftly changing realm of cloud computing, the imperative to maximize resource efficiency and minimize operational costs is universal across organizations. Automating the scheduling of EC2 instances to power down during off-peak periods is a strategy that stands out for its effectiveness. Envision a setup where your AWS EC2 instances automatically enter a dormant state during the quiet hours of nights, weekends, or festive breaks, only to reawaken when their services are next in demand. This precision-driven approach is not merely aspirational; it’s a viable solution with a profound effect on your financial health. In this article, we’ll explore the sophisticated strategy of time-based EC2 instance scheduling. We’ll see how it not only slashes unnecessary expenditures but also polishes your cloud operations, granting you unprecedented command over your AWS expenses. Bid farewell to manual oversight and welcome the era of automated, cost-efficient cloud computing.

Launching EC2 instances

The following are the steps:

Create 2 ec2 instances, adding a tag key ‘env’ with value ‘dev


Create another pair of instances with tag key ‘env’ and value ‘test’


Now we have a total of 4 running instances

Creating a Role

Search for IAM on the search box and click on the service that appears


Click on ‘Policies’ on the Left panel, then ‘Create Policy’ on the top right of the page


Select the JSON Policy Editor and paste the following policy in the editor

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


Scroll down and click on ‘Next’

Give the policy a name then scroll down and click on ‘Create Policy’


Once the policy is created, click on ‘Roles’ on the Left sidebar, then click on ‘Create role’


Select the service as ‘Lambda’, then click ‘Next’


On the ‘Add Permissions’ page, search for the policy name you used in step 5 then select it when it appears and click ‘Next’

On the next page, give the role a name, then scroll to the bottom of the page and click on ‘Create role’

Creating a Lambda Function

Search for Lambda on the Search box, then click on the service that appears


On the page that appears, click on ‘Create Function’


Select ‘Author from Scratch’. On the Basic information, give the function a name and then select the Latest Python runtime .


Under the ‘Permissions’ section, expand ‘Change default execution role’ . For the execution role, select ‘Use an existing role’, then select the role we created earlier. Then, scroll to the bottom and click on ‘Create function’


On the function page that appears, select the ‘Code’ tab and paste the following code in the ‘lambda_function’ code section

# Start the instances in the dev environment:

import boto3

# AWS region and tag details
aws_region = 'us-east-1'
tag_key = 'env'
tag_value = 'dev'

# Create an EC2 client
ec2 = boto3.client('ec2', region_name=aws_region)

def lambda_handler(event, context):
    # Get instances with the specified tag and value
    instances_to_start = get_instances_with_tag(tag_key, tag_value)
    
    if instances_to_start:
        # Start the instances
        start_instances(instances_to_start)
        return {
            'statusCode': 200,
            'body': 'Started instances: {}'.format(instances_to_start)
        }
    else:
        print('No instances')
        return {
            'statusCode': 200,
            'body': 'No instances found with tag "{}" and value "{}".'.format(tag_key, tag_value)
        }

def get_instances_with_tag(key, value):
    try:
        response = ec2.describe_instances(
            Filters=[
                {
                    'Name': 'tag:' + key,
                    'Values': [value]
                }
            ]
        )
        
        instances = []
        for reservation in response['Reservations']:
            for instance in reservation['Instances']:
                instances.append(instance['InstanceId'])
        
        print(instances)
        return instances
        
    except Exception as e:
        print('Error:', e)
        return []

 
def start_instances(instance_ids):
    try:
        ec2.start_instances(InstanceIds=instance_ids)
        print('Starting instances:', instance_ids)
    except Exception as e:
        print('Error:', e)        

This code will be used to start the instances in the ‘dev’ environment. Remember to set the region in which you are running the instances in


Click on ‘Deploy’ to update the changes. The ‘Deploy’ button should be inactive on success


To test the function, click on ‘Test’ to the left of ‘Deploy’


Configure the test event by giving it a name then click on ‘Save’


To test the Lambda function. Make sure your instances are stopped.


Click on ‘Test’ on the Lambda function Page


The instances in the dev environment start.


To create the function to stop the instances, repeat from step 1–11 but use the following code for the lambda-function

#Stop instances in the dev environment

import boto3

# AWS region and tag details
aws_region = 'us-east-1'
tag_key = 'env'
tag_value = 'dev'

# Create an EC2 client
ec2 = boto3.client('ec2', region_name=aws_region)

def lambda_handler(event, context):
    # Get instances with the specified tag and value
    instances_to_stop = get_instances_with_tag(tag_key, tag_value)
    
    if instances_to_stop:
        # Stop the instances
        stop_instances(instances_to_stop)
        return {
            'statusCode': 200,
            'body': 'Stopped instances: {}'.format(instances_to_stop)
        }
    else:
        print('No instances')
        return {
            'statusCode': 200,
            'body': 'No instances found with tag "{}" and value "{}".'.format(tag_key, tag_value)
        }

def get_instances_with_tag(key, value):
    try:
        response = ec2.describe_instances(
            Filters=[
                {
                    'Name': 'tag:' + key,
                    'Values': [value]
                }
            ]
        )
        
        instances = []
        for reservation in response['Reservations']:
            for instance in reservation['Instances']:
                instances.append(instance['InstanceId'])
        
        return instances
        
    except Exception as e:
        print('Error:', e)
        return []

def stop_instances(instance_ids):
    try:
        ec2.stop_instances(InstanceIds=instance_ids)
        print('Stopping instances:', instance_ids)
    except Exception as e:
        print('Error:', e)        

Scheduling the Lambda Function

Search for ‘cloudwatch’ on the services search tab and select the service


On the Left panel, expand ‘Events’ then click on ‘Rules’


On the ‘Rules’ page that appears, click on ‘Create rule’


Give the Rule a name, a description and select ‘Schedule’ for the Rule type, then click on ‘Continue in EventBridge Scheduler’ at the bottom of the page


On the ‘Create schedule’ page, scroll down to ‘Schedule pattern’ and set the following configurations:

a. Occurrence: Recurring schedule

b. Schedule type: Cron-based schedule

c. Cron expression: Input the cron for when you would like to run the function. Use crontab to develop your cron expression.


The section at the bottom shows the next 10 triggers

Set the Flexible time window to ‘Off’ and scroll to the bottom of the page and click on ‘Next’


On the ‘Select target’ page, select ‘AWS Lambda’ as the Target API and select the function to start the EC2s from the dropdown


Scroll to the bottom and click on ‘Skip review and create schedule’


On the Review and ‘create schedule’ page, scroll to the bottom and click on ‘Create schedule’

This creates the schedule

Repeat steps 1–10 to schedule the Lambda function to stop the instances. The only thing we will be changing is the name of the schedule and the cron schedule

After creating the schedule, you should have both schedules on your ‘schedules’ dashboard

Summary

To sum up, leveraging Lambda functions to automate the scheduling of EC2 instances is a savvy approach to cost management. This method ensures that instances are operational only when necessary, avoiding needless charges. By implementing such schedules, businesses can enjoy a reduction in their AWS bills, reflecting a more efficient use of resources. The automation of these processes not only saves money but also frees up valuable time for IT teams. Ultimately, this strategy is a smart investment in both financial and operational terms, streamlining cloud services in a cost-effective manner.


Soundariya B

Cloud and Data intern in Perfdel Technologies llp|??1X Certified AWS cloud practitioner | ??1X Azure AZ-900 Certified | Docker ??| Terraform??| Kubernetes??| Linux | Aveva OSI soft PI??|Pre-final | Speaker |

10 个月

Congratulations for ur success! Ur dedication and hardwork ! ????Sakshi Ravi

Dr. KARTHI M

Associate Professor| AWS Community Builder| Head, AWS Cloud Club| WIPRO Advanced Technology Certified Faculty-Cloud Computing | Certified AWS Academy Educator | Programming enthusiast|

10 个月

Fantastic work, Sakshi Ravi . Successfully finishing the #30DaysCloudChallenge highlights your commitment and perseverance. Excited to see how you leverage these new skills in your career.

Dheeraj Choudhary

Senior Cloud & Devops Engineer | AI-ML Enthusiast | Infra-As-Code Expert | DevOps - Build and Release Engineering | 5x AWS Certified | AWS Hero | Technical Blogger | Hashicorp Ambassador

10 个月

Keep up the spirit ??

Sabiha Ashik

10X AWS Certified | AWS Hero |UG Dubai Leader|??Solutions Architect| Amazon Connect Ambassador

10 个月

Way to go Sakshi Ravi ????

Parna Mehta

AWS Cloud specialist - Helping you embark on your Cloud Journey

10 个月

Kudos to you Sakshi Ravi for achieving your goal ?? keep going

要查看或添加评论,请登录

Sakshi Ravi的更多文章

社区洞察

其他会员也浏览了