boto3

import boto3
from botocore.config import Config


class EC2Utils:

    def __init__(self):

        o_config = Config(
            region_name='us-west-2',
            signature_version="v4",
            retries={
                'max_attempts': 10,
                'mode': 'standard'
            }
        )

        s_access_key = "YOUACCESSKEYHERE"
        s_secret_key = "YourSecretKeyHereedgfkjkgkdnndgfln"

        # , aws_session_token=SESSION_TOKEN
        self.o_ec2 = boto3.client('ec2', config=o_config, aws_access_key_id=s_access_key, aws_secret_access_key=s_secret_key)

    def get_all_instances(self):
        """
        Will show status of all the Instances.
        If there are no instances in that region_name you'll get an exception.
        :return:
        """

        h_instances = {}

        response = self.o_ec2.describe_instances()
        print("Debug", response)

        for o_instance in self.o_ec2.instances.all():
            h_instances["x"] = o_instance.state['Name']

        return h_instances

    def do_start_instances_stopped(self):
        """
        Will start all the instances that are stopped.
        :return:
        """

        for o_instance in self.o_ec2.instances.all():

            if o_instance.state['Name'] == 'stopped':
                o_instance.start()
Views: 8 views

Rules for writing a Comment


  1. Comments are moderated
  2. I don't publish Spam
  3. Comments with a fake email are not published
  4. Disrespectful comment are not published, even if they have a valid point
  5. Please try to read all the article before asking, as in many cases questions are already responded