EC2 Random name Generator using Python script
Tags: aws, python, github
Published: 2022-10-02 18:12:28


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gya772jcphvt6xim5irk.png)


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](https://auth.acloud.guru/login?state=hKFo2SBTRFNTb2tOX1NfNE1QMTZSUk04WmtOVmM3SDdubHdmaqFupWxvZ2luo3RpZNkgTWE1N3ZJZXMtRXNoNTJ3R2I5LW9mSjRkSU5aaGNOWUejY2lk2SBPS0ZnOXVvUE1DTmVsdmx2S1ZEZlBLNzY2RFJkQVlENQ&client=OKFg9uoPMCNelvlvKVDfPK766DRdAYD5&protocol=oauth2&response_type=code%20token%20id_token&redirect_uri=https%3A%2F%2Flearn.acloud.guru%2Flogin%2Fcallback&audience=https%3A%2F%2Facloudguru.auth0.com%2Fapi%2Fv2%2F&nonce=iUFLQIO9UpuGhM_CHEqCwepI7IjVRZnG&scope=openid%20profile%20email&auth0Client=eyJuYW1lIjoiYXV0aDAuanMiLCJ2ZXJzaW9uIjoiOS4xMy40In0%3D)

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

[Random python library](https://docs.python.org/3/library/random.html)

[Random python library for functions for integers](https://docs.python.org/3/library/random.html#functions-for-integers)

[Random python library for functions for sequences](https://docs.python.org/3/library/random.html#functions-for-sequences)

Dr. Angela Yu — [100 Days of Code: # The Complete Python Pro Bootcamp for 2022 from Udemy](https://www.udemy.com/course/100-days-of-code/)

[www.w3schools.com](https://www.w3schools.com/python/module_random.asp#)

[www.geeksforgeeks.org](https://www.geeksforgeeks.org/python-programming-language/?ref=ghm#)

[www.hackerrank.com](https://www.hackerrank.com/domains/python)


---


_**Let’s get started!**_

Here is a link of my python project in [GitHub Repository](https://github.com/awsmine/Python_projects/blob/main/random_name_generator.py).

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))
```

**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](https://pythonguides.com/python-exit-command/)

[exit while loop in python](https://stackoverflow.com/questions/16656313/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()  



```

**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")
```

**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)
```

## 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!!!