EC2 Random name Generator using Python script

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 exi t. 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))

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 produc tion code because it relies on site module.

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

# https://pythonguides.com/python-exit-command/

# https://stackoverflow.com/questions/16656313/exit-while-loop-in-python

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 proj ect!!! Fun working with Python!!!


EC2 Random name Generator using Python script was originally published in Towards AWS on Medium, where people are continuing the conversation by highlighting and responding to this story.

Namaste Devops is a one stop solution view, read and learn Devops Articles selected from worlds Top Devops content publishers inclusing AWS, Azure and others. All the credit/appreciations/issues apart from the Clean UI and faster loading time goes to original author.

Comments

Did you find the article or blog useful? Please share this among your dev friends or network.

An android app or website on your mind?

We build blazing fast Rest APIs and web-apps and love to discuss and develop on great product ideas over a Google meet call. Let's connect for a free consultation or project development.

Contact Us

Trending DevOps Articles

Working with System.Random and threads safely in .NET Core and .NET Framework

Popular DevOps Categories

Docker aws cdk application load balancer AWS CDK Application security AWS CDK application Application Load Balancers with DevOps Guru Auto scale group Automation Autoscale EC2 Autoscale VPC Autoscaling AWS Azure DevOps Big Data BigQuery CAMS DevOps Containers Data Observability Frequently Asked Devops Questions in Interviews GCP Large Table Export GCP Serverless Dataproc DB Export GTmetrix Page Speed 100% Google Page Speed 100% Healthy CI/CD Pipelines How to use AWS Developer Tools IDL web services Infrastructure as code Istio App Deploy Istio Gateways Istio Installation Istio Official Docs Istio Service Istio Traffic Management Java Database Export with GCP Jenkin K8 Kubernetes Large DB Export GCP Linux MSSQL March announcement MySQL Networking Popular DevOps Tools PostgreSQL Puppet Python Database Export with GCP Python GCP Large Table Export Python GCP Serverless Dataproc DB Export Python Postgres DB Export to BigQuery Sprint Top 100 Devops Questions TypeScript Client Generator anti-patterns of DevOps application performance monitoring (APM) aws amplify deploy blazor webassembly aws cdk application load balancer security group aws cdk construct example aws cdk l2 constructs aws cdk web application firewall aws codeguru reviewer cli command aws devops guru performance management aws service catalog best practices aws service catalog ci/cd aws service catalog examples azure Devops use cases azure devops whitepaper codeguru aws cli deploy asp.net core blazor webassembly devops guru for rds devops guru rds performance devops project explanation devops project ideas devops real time examples devops real time scenarios devops whitepaper aws docker-compose.yml health aware ci/cd pipeline example host and deploy asp.net core blazor webassembly on AWS scalable and secure CI/CD pipelines security vulnerabilities ci cd pipeline security vulnerabilities ci cd pipeline aws smithy code generation smithy server generator
Show more