DEV Community

Revathi Joshi for AWS Community Builders

Posted on • Originally published at Medium

EC2 Random name Generator using Python script

Image description

Happy to show you all my latest project in python.

EC2 Random Name Generator

This python project is about generating a unique name for the “x” number of EC2 instances for a department and generating random characters and numbers that will be included in the unique name. This is especially important when in an organization, there are so many users, departments sharing an AWS environment. This ensures that they are properly named and are unique so any team member can easily tell to which department he/she belongs to.

The ONLY departments that should use this Name Generator are Marketing, Accounting and FinOps. The user then lists these departments as options and if a user puts another department, the program will print a message that they should not use this Name Generator and the program should exit. Also it checks whether an input has an incorrect upper or lowercase letters for the corresponding department.

This program also checks for any negative number input for the number of EC2 Instances that they need or no input at all.

Used a combination of the Python code — a random function, generator, range functions, loops with break statement, strings, variables and inputs.

Used a variety of courses to learn python. and most of all Practice… without which I could not have completed this project.

Practiced a lot, whether to use exit() or a sys.exit() function for the wrong user input. For exit() is considered bad to use in production code because it relies on site module and sys.exit() is good to use in production code because the sys module will always be there.

Objectives:

Several departments share an AWS environment. You need to ensure that the EC2 instances are properly named and are unique so team members can easily tell who the EC2 instances belong to.

Use Python to create your unique EC2 names that the users can then attach to the instances.

The Python Script should:

  • Generate random characters and numbers that will be included in the unique name.

  • Allow the user to input the name of their department that is used in the unique name.

  • The only departments that should use this Name Generator are the Marketing, Accounting, and FinOps Departments.

  • List these departments as options and if a user puts another department, print a message that they should not use this Name Generator.

  • Be sure to account for incorrect upper or lowercase letters in the correct department.
    Example: a user inputs accounting vs Accounting.

  • All the users input how many EC2 instances they want names for and output the same amount of unique names.

  • The number should be a positive number.

  • Prints the randomly generated unique EC2 names

Pre-requisites

  • AWS user account with admin access, not a root account.

  • Cloud9 IDE comes with Python installed.

Resources Used:

Certified Entry-Level Python Programmer Certification by ACloudGuru

I have also found these sites to be very helpful in understanding the basics of Python.

Random python library

Random python library for functions for integers

Random python library for functions for sequences

Dr. Angela Yu — 100 Days of Code: # The Complete Python Pro Bootcamp for 2022 from Udemy

www.w3schools.com

www.geeksforgeeks.org

www.hackerrank.com


Let’s get started!

Here is a link of my python project in GitHub Repository.

I have used Cloud9 as IDE to write, run, and debug the Python code with just a browser. While working on this project, I stored the files generated in my Cloud9 IDE to GitHub repository outside of this EC2 VM, so that I would not loose my code if anything happens to this VM on AWS.

Steps of Implementing the Objectives

random_name_generator.py — Generate random characters and numbers that will be included in the unique name.

import random
import string
import sys 
# variables
def string_generator(size=6, string=string.ascii_letters + string.digits):    
    return ''.join(random.choice(string) for _ in range(size))
Enter fullscreen mode Exit fullscreen mode

Allow the user to input the name of their department that is used in the unique name.

The only departments that should use this Name Generator are the Marketing, Accounting, and FinOps Departments.

List these departments as options and if a user puts another department, print a message that they should not use this Name Generator.

Be sure to account for incorrect upper or lowercase letters in the correct department.

Example: a user inputs accounting vs Accounting.

exit() is considered bad to use in production code because it relies on site module.

sys.exit() good to use in production code because the sys module will always be there.

python guides

exit while loop in python


department = input("Enter Department: Marketing, Accounting, FinOps: ")  

for _ in department:

    if department == "Marketing" or department.lower() == "marketing" :
        print("Ok to go")
        #print ("Marketing")
        break

    elif department == "Accounting" or department.lower() == "accounting" :
        print("OK to go")
        #print("Accounting")
        break

    elif department == "FinOps" or department.lower() == "finops" :
        print("OK to go")
        #print("FinOps")
        break

    else:
        print("Error: You can not use this generator, enter the name correctly.")
        raise SystemExit
        sys.exit()  



Enter fullscreen mode Exit fullscreen mode

All the users input how many EC2 instances they want names for and output the same amount of unique names.

The number should be a positive number.

# number    - how many EC2 instances they want names for
number = int(input("Enter the number of EC2 instances you need: ")) 

if number < 0:    
    print("Please enter a positive number: ") 
elif number > 0:    
    print("Good to go")
Enter fullscreen mode Exit fullscreen mode

Prints the randomly generated unique EC2 names

print()
print("--------------------------------")
print("EC2 Instance Names")
print("--------------------------------")
print() 
for _ in range(1, number + 1):    
    unique_name = department    
    EC2_unique_name = unique_name + "-" + string_generator()
    print("Your EC2 Instance's unique name is : ", EC2_unique_name)
Enter fullscreen mode Exit fullscreen mode

What we have done so far

Created a python script to generate EC2 names with unique numbers so that the team members can easily tell who the EC2 instances belong to.

Overall, it is a FUN project!!! Fun working with Python!!!

Top comments (2)

Collapse
 
rogave profile image
Robert Garcia Ventura • Edited

Hi, nice idea! :)
I would recommend to use the library uuid for generating a unique name id. Using your method with random even though the chances are really small, can be that you generate the same name twice.
Here you can learn more: en.wikipedia.org/wiki/Universally_...

Collapse
 
awsmine profile image
Revathi Joshi

Thank you, I will definitely try it out...