Coder Social home page Coder Social logo

jomyg / python-script-that-shuts-down-and-starts-an-aws-rds Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 6 KB

Python script that shuts down and starts an AWS RDS instance according to the schedule you specified, you can use the AWS SDK for Python (Boto3). Make sure you have Boto3 installed and configured with your AWS credentials.

aws cli devops python rds sheduler

python-script-that-shuts-down-and-starts-an-aws-rds's Introduction

Python-script-that-shuts-down-and-starts-an-AWS-RDS

Python script that shuts down and starts an AWS RDS instance according to the schedule you specified, you can use the AWS SDK for Python (Boto3). Make sure you have Boto3 installed and configured with your AWS credentials.

Here provided time is like: Start & Stop for monday to friday is 08:00 AM IST and 07:00 PM IST, For Weekend shutdown at 07:00 PM IST and start at 08:00 AM IST

You can install it using pip if you haven't already:

pip install boto3

Here's a Python script to achieve

import boto3
import datetime
import time

# RDS instance information
rds_instance_id = 'rds-instance-id'
region_name = 'aws-region'

# Create an RDS client
client = boto3.client('rds', region_name=region_name)

# Define the schedule for shutdown and start
shutdown_time_weekday = datetime.time(19, 0)  # 7:00 PM IST
start_time_weekday = datetime.time(8, 0)     # 8:00 AM IST
shutdown_time_weekend = datetime.time(19, 0)  # 7:00 PM IST
start_time_monday = datetime.time(8, 0)      # 8:00 AM IST

def is_weekend():
    # Check if today is Friday (4) or Saturday (5) or Sunday (6)
    return datetime.datetime.today().weekday() in [4, 5, 6]

while True:
    current_time = datetime.datetime.now().time()
    
    # Check if it's a weekday or weekend
    if is_weekend():
        if current_time >= shutdown_time_weekend:
            # Shutdown the RDS instance
            print(f"Shutting down RDS instance {rds_instance_id}")
            client.stop_db_instance(DBInstanceIdentifier=rds_instance_id)
        elif current_time >= start_time_monday:
            # Start the RDS instance
            print(f"Starting RDS instance {rds_instance_id}")
            client.start_db_instance(DBInstanceIdentifier=rds_instance_id)
    else:
        if current_time >= shutdown_time_weekday:
            # Shutdown the RDS instance
            print(f"Shutting down RDS instance {rds_instance_id}")
            client.stop_db_instance(DBInstanceIdentifier=rds_instance_id)
        elif current_time >= start_time_weekday:
            # Start the RDS instance
            print(f"Starting RDS instance {rds_instance_id}")
            client.start_db_instance(DBInstanceIdentifier=rds_instance_id)
    
    # Sleep for a minute before checking the time again
    time.sleep(60)

python-script-that-shuts-down-and-starts-an-aws-rds's People

Contributors

jomyg avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.