-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawsregionfinder.py
49 lines (38 loc) · 1.48 KB
/
awsregionfinder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import json
import os
# Get the directory of the script
script_directory = os.path.dirname(os.path.realpath(__file__))
# Relative path to JSON file containing state to AWS region mapping
mapping_file = os.path.join(script_directory, "state_regions.json")
while True:
# Prompt user for state input
state = input("Enter state (type 'q' to quit): ")
# Check if user wants to quit
if state.lower() == 'q':
print("Exiting the script. Goodbye!")
break
try:
# Read the mapping file
with open(mapping_file, 'r') as file:
state_mapping = json.load(file)
# Fetch AWS region from the mapping based on the provided state
region = state_mapping.get(state)
# Check if the state is valid
if region is None:
print("Invalid state. Please enter a valid state from the mapping file.")
else:
# Output the result in JSON format
result = {
"state": state,
"region": region
}
print(json.dumps(result, indent=2))
# Alternatively, you can output in YAML-like format
# print(f"state: {state}\nregion: {region}")
print("\n")
except FileNotFoundError:
print(f"Mapping file '{mapping_file}' not found. Please check the file path.")
break
except json.JSONDecodeError:
print(f"Error decoding JSON in file '{mapping_file}'. Please check the file format.")
break