Skip to content

Commit bb9bcc5

Browse files
authored
Merge pull request #71 from mohammadll/terraform
feat(IAM_Prompt): Add IAM prompt for creating iam_users and iam_groups
2 parents 5857c1f + 25f66c1 commit bb9bcc5

File tree

1 file changed

+143
-3
lines changed
  • app/template_generators/terraform/aws

1 file changed

+143
-3
lines changed
Lines changed: 143 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,146 @@
11
def IaC_template_generator_iam(input) -> str:
22

3-
3+
iam = ['aws_iam_user', 'aws_iam_group']
44

5-
prompt = f""" """
6-
return prompt
5+
aws_iam_create_user = 'true' if input.iam_user else 'false'
6+
aws_iam_create_group = 'true' if input.iam_group else 'false'
7+
8+
prompt = f"""
9+
Generate a Python code to generate a Terraform project (project name is app/media/MyTerraform)
10+
that dynamically provisions {iam} resources ensuring a modular, flexible structure to enable users
11+
to configure all essential settings at the root level. Only provide Python code, no explanations or
12+
markdown formatting. The project should be organized as follows:
13+
1. Root Directory Structure:
14+
- main.tf:
15+
- Define the provider block as follows:
16+
```
17+
provider "aws" {{
18+
region = "us-east-1"
19+
}}
20+
```
21+
- Defines a module block that references "iam" from a subdirectory within modules.
22+
This module block should expose all variables that {iam} resources require, allowing
23+
configuration at the root level rather than directly within the module.
24+
- Every variable defined in {iam} resources should be passed through the module block,
25+
ensuring that users can adjust all critical parameters of {iam} resources by modifying
26+
root main.tf. Avoid using any other parameters. just use the parameters of {iam} resources with the same keys
27+
- variables.tf:
28+
- Sets these variables names for aws_iam_user resource:
29+
iam_create_user(bool), iam_users(list(map(string)))
30+
- Sets these variables names for aws_iam_group resource:
31+
iam_create_group(bool), iam_groups(list(map(string)))
32+
- terraform.tfvars:
33+
- Structure as follows:
34+
iam_create_user = {aws_iam_create_user}
35+
iam_users = [
36+
{{
37+
name = "devopshobbies"
38+
path = "/"
39+
}}
40+
]
41+
42+
iam_create_group = {aws_iam_create_group}
43+
iam_groups = [
44+
{{
45+
name = "developers"
46+
path = "/"
47+
}}
48+
]
49+
- versions.tf:
50+
- Structure as follows:
51+
terraform {{
52+
required_version = ">= 1.0"
53+
54+
required_providers {{
55+
aws = {{
56+
source = "hashicorp/aws"
57+
version = ">= 5.20"
58+
}}
59+
}}
60+
}}
61+
2. Module Directory Structure (modules/iam):
62+
- main.tf:
63+
- Set the following parameters for aws_iam_user resource (name its terraform resource to "users") and avoid using any other parameters:
64+
- 1. count (type: number): follow the below syntax for count:
65+
```
66+
count = var.iam_create_user ? length(var.iam_users) : 0
67+
```
68+
- 2. name (type: string): follow the below syntax for name parameter:
69+
```
70+
name = var.iam_users[count.index]["name"]
71+
```
72+
- 3. path (type: string): follow the below syntax for path parameter:
73+
```
74+
path = var.iam_users[count.index]["path"]
75+
```
76+
- Set the following parameters for aws_iam_group resource (name its terraform resource to "groups") and avoid using any other parameters:
77+
- 1. count (type: number): follow the below syntax for count:
78+
```
79+
count = var.iam_create_group ? length(var.iam_groups) : 0
80+
```
81+
- 2. name (type: string): follow the below syntax for name parameter:
82+
```
83+
name = var.iam_groups[count.index]["name"]
84+
```
85+
- 3. path (type: string): follow the below syntax for path parameter:
86+
```
87+
path = var.iam_groups[count.index]["path"]
88+
```
89+
- variables.tf:
90+
- Sets these variables names for aws_iam_user resource:
91+
iam_create_user(bool), iam_users(list(map(string)))
92+
- Sets these variables names for aws_iam_group resource:
93+
iam_create_group(bool), iam_groups(list(map(string)))
94+
- terraform.tfvars:
95+
- Structure as follows:
96+
iam_create_user = {aws_iam_create_user}
97+
iam_users = [
98+
{{
99+
name = "devopshobbies"
100+
path = "/"
101+
}}
102+
]
103+
104+
iam_create_group = {aws_iam_create_group}
105+
iam_groups = [
106+
{{
107+
name = "developers"
108+
path = "/"
109+
}}
110+
]
111+
- versions.tf:
112+
- Structure as follows:
113+
terraform {{
114+
required_version = ">= 1.0"
115+
116+
required_providers {{
117+
aws = {{
118+
source = "hashicorp/aws"
119+
version = ">= 5.20"
120+
}}
121+
}}
122+
}}
123+
Ensure this project structure supports {iam}’s configurability, extensibility, and
124+
reusability across diverse Terraform providers, empowering users to manage their resources through a
125+
single, customizable root configuration while keeping module internals robustly modular.
126+
127+
finally just give me a python code without any note that can generate a project folder with the given
128+
schema without ```python entry. and we dont need any base directory in the python code. the final
129+
terraform template must work very well without any error!
130+
131+
Python code you give me, must have structure like that:
132+
133+
import os
134+
project_name = "app/media/MyTerraform"
135+
modules_dir = os.path.join(project_name, "modules")
136+
iam_dir = os.path.join(modules_dir, "iam")
137+
138+
# Create project directories
139+
os.makedirs(iam_dir, exist_ok=True)
140+
141+
# Create main.tf
142+
with open(os.path.join(project_name, "main.tf"), "w") as main_file:
143+
# any thing you need
144+
145+
"""
146+
return prompt

0 commit comments

Comments
 (0)