*** EC2 Random Name Generator ***

Morgan Clark
4 min readJul 17, 2022

Hello everyone, I decided to go back over some of my previous projects and expand on what was presented. Below you will find a small Python script which can be utilized to randomly generate a name for an AWS ec2 instance.

Scenario:

Several departments share an AWS environment. You need to ensure that the ec2s 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:

1. Allow the user to input how many ec2 instances they want names for and output the same amount of unique names.
2. Allow the user to input the name of their department that is used in the unique name.
3. Generate random characters and numbers that will be included in the unique name.

ADVANCED
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.

COMPLEX
Turn the above into a Function and execute the Function to verify it works.

Tools for the task:

  1. Python 3.7
  2. AWS CLI (AWS Command Line Interface)
  3. Github account
  4. AWS account
  5. IDE (Integrated Development Environment) I selected AWS Cloud9 with ec2 instance with Ubuntu Linux/Unix for the platform

Python 3.7 code:

#!/usr/bin/env python3.7

import random #Importing the Random module which will generate numbers based on a range provided by the developer/programmer

def ec2_name_chars(num_chars): #definition of function that the value assigned to num_char to generate several characters
return “”.join(chr(random.randint(33,125)) for i in range(num_chars)) #Takes all items in an iterable and joins then into one string

ec2_count = int(input(“Enter the number of AWS EC2 instances “)) # Counter that will be incremented through each iteration

#List of authorized departments who may utilize this name generating script, also those are the recognized department names to be entered by the user

depts = [“Accounting”, “FinOps”, “Marketing”]
count = 0 # Counter used to determine the number of requested random names enter by the user

while count != ec2_count: # Counter that will be incremented through each iteration
if count < ec2_count:
print(“Please select from one of these departments: Accounting, FinOps or Marketing”)
ec2_name = str(input(“\nTo be included in the name of your new AWS ec2 instance: “)) #Prompts the user for the desired named of the ec2 instance
if ec2_name in depts:
print(ec2_name + ec2_name_chars(15)) #Print statement presents both the requested name and the generated characters
print(“\n”)
count += 1
elif not ec2_name in depts: # The user did not enter the correct spelling or upper case character(s)
ec2_name = str(input(“\nPlease try again, please select from one of these departments: Accounting, FinOps or Marketing!\n”))
if ec2_name in depts: # Check to see if the new entry is correct
print(ec2_name + ec2_name_chars(15)) #Print statement presents both the requested name and the generated characters
count += 1
else:
print(“\nYou are not authorized to use this program!”) # User still didn’t enter the correct name so they are told they are not authorized
print(“\nPlease obtain the required authorization and rerun the program once again, Thank You!”)
break
# Notifies the user that their request has been completed
print(‘\nYour request has been completed and ‘ + str(count) + ‘ randomly generated name(s) were created!’)
print(‘Thank you!’)

Output:

Successful usage of the Python script

In the above screen shot, the Python script RandomNameGenerator_Adv.py was executed. The user was prompted to enter how many names were needed for AWS ec2 instances and two (2) was the desired amount.

The user was prompted to enter a name by selecting it from the list of departments: Accounting, FinOps or Marketing. The user successfully entered Accounting for the first name and the script randomly generated a new name containing Accounting plus several random characters.

Once again and based on the number of the random names the user had requested, the user was prompted for the second and final department name and they entered FinOps for the first name and the script randomly generated a new name containing FinOps plus several random characters.

The script notified the user that their request had been completed and the script ended.

In the scenario, there was also a need and requirement to notify the user when/if they entered an incorrect department name or if their entry did not match the list of departments exactly, including the case of the letters: Upper case or Lower case.

Failed initial attempt for the Accounting department’s name

In the above example, the user entered accounting instead of Accounting, so the script prompted them to try again and to make sure their selection matched the list. Upon the second attempt, the script continued with the user’s request and presented them with the two new randomly generated names for their AWS ec2 instances.

Thanks for taking a few minutes to review this particular task.

--

--

Morgan Clark

Sr. Telecom Engineer, pursuing new skills and career opportunities in DevOps.