GithubHelp home page GithubHelp logo

brandonoakes / boto3_ec2_instance_menu Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 7 KB

Simple Boto3 script that allows the user to start or stop their IAM EC2 instance from the command line

Python 100.00%

boto3_ec2_instance_menu's Introduction

Boto3 EC2 Menu

Creates custom session based on user's IAM credientials, allowing the user to review and start/stop their specified EC2 Instance.

See requirements below before running the EC2 Menu script


Requirements

1. From the command line make sure you have Python 3

    python3 --version

Click this link https://docs.brew.sh/Installation if you need help downloading Python 3

2. Set up your virtual environment

    python3 -m venv myvenv

You can activate your virtual environment by running:

    source myvenv/bin/activate

Enter the command deactivate when you want to exit your virtual environment

3. Install AWS CLI:

https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html

4. Now that you have the AWS CLI you will want to configure your device to store your user's IAM AWS Access Key ID and Secret Access Key. This Access Key and Secret Key are given to you as a download when you initially create you IAM user on AWS. Following the link below for more information concerning IAM set up.

https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html

Once you have your Access Key and Secret Key run the following:

    aws configure --profile <Your_IAM_User>

You will be given the following options, fill them out accordingly:

    AWS Access Key ID:
    AWS Secret Access Key:
    Default region name:
    Default output format:

5. Install script requirements (make sure your virtual environment is activated):

pip3 install -r requirements.txt

6. Now run python3 app.py and you are good to go.

Disfrutar

boto3_ec2_instance_menu's People

Contributors

brandonoakes avatar

Watchers

James Cloos avatar  avatar

boto3_ec2_instance_menu's Issues

code review

/instance_session

good overall function purpose commenting and function naming. By reading the first 2 lines of any of the functions I already have a good idea what is about to go down.

length

Your code density is pretty sparse. user_instance could be rewritten as

def user_instance():
    '''Creates boto3 session and  ec2 instance object  from user input'''
   # get user input <-
    iam_user = input("Enter your Identity Access Management user profile: ")
    clear()
    ec2_id = input("Enter the EC2 instance Id you would like to look up: ")
    clear()

    # get ec2 instance <-
    return boto3.session.Session(profile_name=iam_user).resource('ec2').Instance(id=ec2_id)

In your code this function stretches out for nearly 30 lines. This can cause a couple issues.

  1. the code is so commented it goes all the way around the bend and becomes difficult to read again.
    • the actual functionality has been spread so thinly the code no longer makes too much sense on its own. For example, why do we grab ec2_resource? We have to go down 8 lines to see it used again and then only to return a child.
  2. the code being spread this thin makes the code longer.
    • The more you can see on a single screen, the better. Scrolling around and losing place is an annoyance and will drive you to split out a file.
    • Clutter gets pushed up the ladder. Splitting out files leads to adding subfolders sooner which can lead to a messy project.
  3. White space loses meaning
    • Here you place about 1 empty line between each line and then 2 or 3 empty lines between functions. The presence of whitespace when you are used to its absence is more dramatic than this. I will bet you noticed the next line before getting to this segment.
    • I would suggest no empty lines between related lines of code, 1 empty line between segments in code, and then 1-2 empty lines between functions. Functions also give notice to themselves because they have colored key words and are all the way left.
  4. It is technically wasting memory and processing power
    • probably the least important reason, as the effect is negligible in most circumstances and because compiled languages will optimize the issues out anways.
    • each one of the variables you define that are then never used are actually being generated in memory. Because python is interpretted, it has to read in 78 lines when this file could maybe be compressed down to 30 lines.
  5. Even the comments I pointed out with arrows are technically not necessary since the code is pretty self explanatory.
    • I still like to write little headers for code segments though, so I would leave them in, but thats a style thing.

string extraction

This is not an important suggestion, but something to consider when thinking of things to do when scaling an application.
The basic idea is to remove strings from your code and instead use constants.
For user facing strings you would place all user facing strings into a single file. The purpose is so that in editor mode you can ignore code. And additionally, (if you are ever releasing in multiple languages) so you can have this one file translated and you're done.
For internal strings such as running on line 58. These would not go into the strings file previously mentioned. They would go somewhere depending on their scope; likely at the top of the file, or in a globals file. The purpose is to take advantage of your IDE's autocomplete and not misspell things. Also, if ever it turns out the spec changes, you can change this single string and all of your code begins working again. No diving through your code.

/ec2_instance_menu

You have chosen good variables to define. Every variable is used and important.

major tradition ouch

This is your main file. It should look special. When I opened your repo, it was not clear to me which file was the main. It does not need to be named Main but generally the main is called out with a distinctly casual name. Some I have seen are

  • main
  • program
  • index (generally only web projects)
  • [project name] ( this is probably the choice you were going for, but your project name is rather technical sounding, so it doesn't really stand out compared to the other files. One project I follow is pokemon showdown, their main is pokemon-showdown which does distinguish itself)

meaning split between files

I am forced to determine the meaning of choice 1,2, 3 from the response you have written for it. This means likely I would have to open menu_options in order to properly develop this file. This means we need to either, recollapse menu_options back into this file or find a way to make the meaning more clear. I would honestly suggest at this size, to just collapse menu_options back into this file, but since we are practicing for larger projects I will suggest defining an enum.
in menu_options define a user options enum

from enum import Enum
class User_Options(Enum):
   START_INSTANCE = 1
   STOP_INSTANCE = 2
   EXIT_PROGRAM = 3

Now in ec2_instance_menu, you can import this enum along with main_menu and refer to things in comparison with this enum. Now your code can look like

if user_choice == User_Options.EXIT_PROGRAM:
   quit()

Additionally you can now reorder and renumber the meanings of enum and every file referencing it will automatically be updated, so maintenance and growth will be simpler.

/menu_options

not much to say about this one. Hopefully this file grows into a strong file someday.

/helper_functions

the term helper function is a very common term, but having a file devoted to them is pretty uncommon. Generally only this case in informal and/or small projects. It implies that functions of unrelated use will be dropped into it and thats generally not a good idea for something you hope to scale. So a name more targetted at a unifying reason is suggested. Maybe user_input.

maximize laziness.

one of my core goals while programming is to be as actively lazy as possible. Some parts of that are

  • Never copy code
  • Maximize code reuse

Your function clear is pretty good, I notice that you use it a lot throughout you code. You've probably saved something like 20 lines for it.
I also notice that you almost always use it directly after asking the user for input. So, you can go even further with your laziness.

def custom_input( question ):
   output = input(question)
   os.system('clear')
   return output

Now you can replace every time you ask for user input and then clear the screen with this new function. Additionally, this allows you to standardize how your user interacts with your program in one place. You can change this as much as you want, but the user experience will always be consistent since there will not be an outlier where you forgot to put clear() after asking for input.
Now that we've got standard interface we can start doing fancier stuff. Like adding interesting information to the input line such as aws console path and status (idk how aws console works, just making stuff up) and then maybe replace clear with a couple new lines.

def custom_input(question ,instance):
  prelude = ''
  if instance != nil:
      prelude += instance.path + '::' + instance.status
  output = input(prelude + '>>' + question)
  print('\n\n')
  return output

the end.

I tried my best to roast this. I think its actually pretty good.

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.