Skip to content

Commit 41d95af

Browse files
committed
update generation script and add notes about duplicates
1 parent ea5c043 commit 41d95af

File tree

5 files changed

+423
-15
lines changed

5 files changed

+423
-15
lines changed

codegen/quotas/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
This Python script is used to generate Terraform files for managing AWS service quota requests. It interacts with the AWS Service Quotas API and fetches information about the quotas for different services. The script then generates Terraform code based on this information and writes it to (`main.tf` and `variables.tf`) files.
44

5-
Note that generating the quotas could be time consuming as the script honors the API limits for the used AWS APIs.
5+
## Gotchas
6+
7+
- Generating the quotas could be time consuming as the script honors the API limits for the used AWS APIs.
8+
- Certain services have duplicate quotas - same description but different code. Those are handled by appending the quota code to the input variable name.
69

710
## Requirements
811
- Python 3.6+

codegen/quotas/generate_quotas.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,31 @@ def list_quotas_for_service(service_code):
6060

6161

6262
def generate_terraform(services):
63+
"""
64+
Generate Terraform code for the given AWS services.
65+
66+
This function iterates over the provided services, fetches the quotas for each service,
67+
and generates Terraform code for each adjustable quota. If a quota with the same variable name
68+
already exists, it appends the quota code to the quota name to make it unique, and stores the
69+
duplicate variable in a separate list.
70+
71+
Parameters:
72+
services (list): A list of AWS services. Each service is a dictionary that contains the service details.
73+
74+
Returns:
75+
tuple: A tuple containing two strings. The first string is the Terraform code for the main.tf file,
76+
and the second string is the Terraform code for the variables.tf file.
77+
78+
Prints:
79+
For each duplicate variable, it prints a message in the format "Duplicate Variable: {variable_name}: {quota_code}".
80+
"""
6381
terraform_variables = ""
6482
terraform_maps = ""
6583
unique_variables = set()
6684
duplicate_variables = []
6785
for service in services:
68-
time.sleep(
69-
0.3
70-
) # Adjust this based on your rate limit analysis and AWS documentation
86+
# Adjust this based on your rate limit analysis and AWS documentation
87+
time.sleep(0.3)
7188
quotas = list_quotas_for_service(service["ServiceCode"])
7289
for quota in quotas:
7390
if quota["Adjustable"]:
@@ -76,14 +93,15 @@ def generate_terraform(services):
7693
)
7794
if variable_name in unique_variables:
7895
duplicate_variables.append(f"{variable_name}: {quota['QuotaCode']}")
96+
quota["QuotaName"] = f"{quota['QuotaName']}_{quota['QuotaCode']}"
7997
else:
8098
unique_variables.add(variable_name)
81-
terraform_variables += terraform_variable_template(
82-
service["ServiceCode"], quota["QuotaName"], quota["QuotaCode"]
83-
)
84-
terraform_maps += terraform_locals_template(
85-
service["ServiceCode"], quota["QuotaName"], quota["QuotaCode"]
86-
)
99+
terraform_variables += terraform_variable_template(
100+
service["ServiceCode"], quota["QuotaName"], quota["QuotaCode"]
101+
)
102+
terraform_maps += terraform_locals_template(
103+
service["ServiceCode"], quota["QuotaName"], quota["QuotaCode"]
104+
)
87105
main_tf = terraform_main(terraform_maps)
88106
vars_tf = terraform_vars(terraform_variables)
89107
for variable in duplicate_variables:
@@ -95,20 +113,20 @@ def generate_terraform(services):
95113
# Fetch all services
96114
services = list_all_services()
97115

98-
# Generate the markdown document
99-
# markdown_document = generate_markdown_document(services)
116+
# Generate the Terraform code
100117
tf_main, tf_vars = generate_terraform(services)
101118

102119
# Ensure the output directory exists
103120
output_dir = args.outdir
104121
if not os.path.exists(output_dir):
105122
os.makedirs(output_dir)
106123

107-
# Write the main.tf and variables.tf to the specified output directory
124+
# Write the main.tf to the specified output directory
108125
main_tf_path = os.path.join(output_dir, "main.tf")
109126
with open(main_tf_path, "w") as file:
110127
file.write(tf_main)
111128

129+
# Write the variables.tf to the specified output directory
112130
variables_tf_path = os.path.join(output_dir, "variables.tf")
113131
with open(variables_tf_path, "w") as file:
114132
file.write(tf_vars)
@@ -117,6 +135,7 @@ def generate_terraform(services):
117135
subprocess.run(["terraform", "fmt", main_tf_path], check=True)
118136
subprocess.run(["terraform", "fmt", variables_tf_path], check=True)
119137

138+
# Print the success message
120139
print(
121140
f"Terraform files have been written to {output_dir} and formatted with terraform fmt"
122141
)

modules/request-quota-increase/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Request AWS Quota Increase
22

3-
This module can be used to request a quota increase for an AWS Resource.
3+
This module can be used to request a quota increase for AWS Resources. The module is [generated](../../codegen/quotas/) using [AWS Service Quotas API](https://docs.aws.amazon.com/servicequotas/2019-06-24/apireference/Welcome.html), and inputs for each adjustable quota for different services are added to the module.
4+
5+
**NOTE:** The service quotas for certain services have duplicate items. Those duplicate quotas have been named differently in the [input variables](./variables.tf) by appending the service quota code at the end of the variable name, e.g. `networkmonitor_number_of_probes_per_monitor` and `networkmonitor_number_of_probes_per_monitor_l_f192a8d6`.
46

57
## Features
68

@@ -11,7 +13,6 @@ This module can be used to request a quota increase for an AWS Resource.
1113
### Core Concepts
1214

1315
- [AWS Service Quotas Documentation](https://docs.aws.amazon.com/servicequotas/?id=docs_gateway)
14-
- [Generated AWS Service Quotas](../../docs/quotas.md)
1516
- [AWS Service Quotas Generator](../../codegen/quotas/)
1617

1718

0 commit comments

Comments
 (0)