Skip to content

Commit 5099a17

Browse files
authored
Merge pull request #94 from mohammadll/terraform
Add aws_efs resources to its prompt
2 parents 4547167 + 149af8e commit 5099a17

File tree

1 file changed

+348
-1
lines changed
  • app/template_generators/terraform/aws

1 file changed

+348
-1
lines changed
Lines changed: 348 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,349 @@
11
def IaC_template_generator_efs(input) -> str:
2-
pass
2+
3+
efs = ['aws_security_group', 'aws_efs_file_system', 'aws_efs_mount_target', 'aws_efs_backup_policy']
4+
5+
aws_efs_create_file_system = 'true' if input.efs_file_system else 'false'
6+
aws_efs_create_mount_target = 'true' if input.efs_mount_target else 'false'
7+
aws_efs_create_backup_policy = 'true' if input.efs_backup_policy else 'false'
8+
9+
10+
prompt = f"""
11+
Generate a Python code to generate a Terraform project (project name is app/media/MyTerraform)
12+
that dynamically provisions {efs} resources ensuring a modular, flexible structure to enable users
13+
to configure all essential settings at the root level. Only provide Python code, no explanations or
14+
markdown formatting. The project should be organized as follows:
15+
1. Root Directory Structure:
16+
- main.tf:
17+
- Define the provider block as follows:
18+
```
19+
provider "aws" {{
20+
region = "us-east-1"
21+
}}
22+
```
23+
- Defines a module block that references "efs" from a subdirectory within modules.
24+
Don't forget to use source parameter to call efs module as follows:
25+
```
26+
source = "./modules/efs"
27+
```
28+
This module block should expose all variables that {efs} resources require, allowing
29+
configuration at the root level rather than directly within the module.
30+
- Every variable defined in {efs} resources should be passed through the module block,
31+
ensuring that users can adjust all critical parameters of {efs} resources by modifying
32+
root main.tf. Avoid using any other parameters. just use the parameters of {efs} resources with the same keys
33+
- variables.tf:
34+
- Sets these variables names for aws_efs_file_system resource:
35+
file_system_create(bool), efs(object)
36+
- Sets these variables names for aws_efs_mount_target resource:
37+
mount_target_create(bool)
38+
- Sets these variables names for aws_efs_backup_policy resource:
39+
backup_policy_create(bool)
40+
- Sets these variables names for aws_security_group resource:
41+
security_group_name(string), security_group_ingress_rules(map(object)), security_group_egress_rule(object())
42+
Sets security_group_ingress_rules as follows:
43+
```
44+
type = map(object({{
45+
description = string
46+
from_port = number
47+
to_port = number
48+
protocol = string
49+
cidr_blocks = list(string)
50+
}}))
51+
```
52+
Sets security_group_egress_rule as follows:
53+
```
54+
type = object({{
55+
from_port = number
56+
to_port = number
57+
protocol = string
58+
cidr_blocks = list(string)
59+
}})
60+
```
61+
Sets efs as follows:
62+
```
63+
type = object({{
64+
creation_token = string
65+
encrypted = bool
66+
performance_mode = string
67+
throughput_mode = string
68+
backup_policy = string
69+
}})
70+
```
71+
- terraform.tfvars:
72+
- Structure as follows:
73+
security_group_name = "efs_rule"
74+
security_group_ingress_rules = {{
75+
efs_rule = {{
76+
description = "EFS Ingress"
77+
from_port = 2049
78+
to_port = 2049
79+
protocol = "tcp"
80+
cidr_blocks = ["0.0.0.0/0"]
81+
}}
82+
}}
83+
security_group_egress_rule = {{
84+
from_port = 0
85+
to_port = 0
86+
protocol = "-1"
87+
cidr_blocks = ["0.0.0.0/0"]
88+
}}
89+
90+
file_system_create = {aws_efs_create_file_system}
91+
efs = {{
92+
creation_token = "terraform"
93+
encrypted = true
94+
performance_mode = "generalPurpose"
95+
throughput_mode = "elastic"
96+
backup_policy = "ENABLED"
97+
}}
98+
99+
mount_target_create = {aws_efs_create_mount_target}
100+
backup_policy_create = {aws_efs_create_backup_policy}
101+
- versions.tf:
102+
- Structure as follows:
103+
terraform {{
104+
required_version = ">= 1.0"
105+
106+
required_providers {{
107+
aws = {{
108+
source = "hashicorp/aws"
109+
version = ">= 5.20"
110+
}}
111+
}}
112+
}}
113+
2. Module Directory Structure (modules/efs):
114+
- main.tf:
115+
- Create a locals block as follows:
116+
```
117+
locals {{
118+
default_efs_lifecycle_policies = {{
119+
transition_to_ia = "AFTER_14_DAYS",
120+
transition_to_primary_storage_class = "AFTER_1_ACCESS",
121+
}}
122+
}}
123+
```
124+
- Create these data blocks as follows:
125+
```
126+
data "aws_availability_zones" "available_zones" {{
127+
state = "available"
128+
}}
129+
130+
data "aws_vpc" "default_vpc" {{
131+
default = true
132+
}}
133+
134+
data "aws_subnets" "subnets_ids" {{
135+
filter {{
136+
name = "vpc-id"
137+
values = [data.aws_vpc.default_vpc.id]
138+
}}
139+
}}
140+
```
141+
- Set the following parameters for aws_security_group resource (name its terraform resource to "security_group") and avoid using any other parameters:
142+
- 1. count (type: number): follow the below syntax for count:
143+
```
144+
count = var.file_system_create && var.mount_target_create ? 1 : 0
145+
```
146+
- 2. name: follow the below syntax for name:
147+
```
148+
name = var.security_group_name
149+
```
150+
- 3. description: follow the below syntax for description:
151+
```
152+
description = "Security group for EFS mount targets"
153+
```
154+
- 4. vpc_id: follow the below syntax for vpc_id:
155+
```
156+
vpc_id = data.aws_vpc.default_vpc.id
157+
```
158+
- 5. create a dynamic block for ingress rules as follows:
159+
```
160+
dynamic "ingress" {{
161+
for_each = var.security_group_ingress_rules
162+
content {{
163+
description = ingress.value["description"]
164+
from_port = ingress.value["from_port"]
165+
to_port = ingress.value["to_port"]
166+
protocol = ingress.value["protocol"]
167+
cidr_blocks = ingress.value["cidr_blocks"]
168+
}}
169+
}}
170+
```
171+
- 6. create a block for egress rule as follows:
172+
```
173+
egress {{
174+
from_port = var.security_group_egress_rule["from_port"]
175+
to_port = var.security_group_egress_rule["to_port"]
176+
protocol = var.security_group_egress_rule["protocol"]
177+
cidr_blocks = var.security_group_egress_rule["cidr_blocks"]
178+
}}
179+
```
180+
- Set the following parameters for aws_efs_file_system resource (name its terraform resource to "filesystem") and avoid using any other parameters:
181+
- 1. count (type: number): follow the below syntax for count:
182+
```
183+
count = var.file_system_create ? 1 : 0
184+
```
185+
- 2. creation_token (type: string): follow the below syntax for creation_token:
186+
```
187+
creation_token = var.efs["creation_token"]
188+
```
189+
- 3. encrypted (type: string): follow the below syntax for encrypted:
190+
```
191+
encrypted = var.efs["encrypted"]
192+
```
193+
- 4. performance_mode: follow the below syntax for performance_mode:
194+
```
195+
performance_mode = var.efs["performance_mode"]
196+
```
197+
- 5. throughput_mode: follow the below syntax for throughput_mode:
198+
```
199+
throughput_mode = var.efs["throughput_mode"]
200+
```
201+
- 6. create the below blocks as follows:
202+
```
203+
lifecycle_policy {{
204+
transition_to_ia = lookup(local.default_efs_lifecycle_policies, "transition_to_ia", null)
205+
}}
206+
207+
lifecycle_policy {{
208+
transition_to_primary_storage_class = lookup(local.default_efs_lifecycle_policies, "transition_to_primary_storage_class", null)
209+
}}
210+
211+
tags = {{
212+
Name = "terraform-efs"
213+
}}
214+
```
215+
- Set the following parameters for aws_efs_mount_target resource (name its terraform resource to "mount_target") and avoid using any other parameters:
216+
- 1. count (type: number): follow the below syntax for count:
217+
```
218+
count = var.file_system_create && var.mount_target_create ? length(data.aws_availability_zones.available_zones.names) : 0
219+
```
220+
- 2. file_system_id (type: string): follow the below syntax for file_system_id:
221+
```
222+
file_system_id = aws_efs_file_system.filesystem[0].id
223+
```
224+
- 3. subnet_id: follow the below syntax for subnet_id:
225+
```
226+
subnet_id = data.aws_subnets.subnets_ids.ids[count.index]
227+
```
228+
- 4. security_groups: follow the below syntax for security_groups:
229+
```
230+
security_groups = [aws_security_group.security_group[0].id]
231+
```
232+
- Set the following parameters for aws_efs_backup_policy resource (name its terraform resource to "backup_policy") and avoid using any other parameters:
233+
- 1. count (type: number): follow the below syntax for count:
234+
```
235+
count = var.file_system_create && var.backup_policy_create ? 1 : 0
236+
```
237+
- 2. file_system_id: follow the below syntax for file_system_id:
238+
```
239+
file_system_id = aws_efs_file_system.filesystem[0].id
240+
```
241+
- 3. Create the below block as follows:
242+
```
243+
backup_policy {{
244+
status = var.efs["backup_policy"]
245+
}}
246+
```
247+
- variables.tf:
248+
- Sets these variables names for aws_efs_file_system resource:
249+
file_system_create(bool), efs(object)
250+
- Sets these variables names for aws_efs_mount_target resource:
251+
mount_target_create(bool)
252+
- Sets these variables names for aws_efs_backup_policy resource:
253+
backup_policy_create(bool)
254+
- Sets these variables names for aws_security_group resource:
255+
security_group_name(string), security_group_ingress_rules(map(object)), security_group_egress_rule(object())
256+
Sets security_group_ingress_rules as follows:
257+
```
258+
type = map(object({{
259+
description = string
260+
from_port = number
261+
to_port = number
262+
protocol = string
263+
cidr_blocks = list(string)
264+
}}))
265+
```
266+
Sets security_group_egress_rule as follows:
267+
```
268+
type = object({{
269+
from_port = number
270+
to_port = number
271+
protocol = string
272+
cidr_blocks = list(string)
273+
}})
274+
```
275+
Sets efs as follows:
276+
```
277+
type = object({{
278+
creation_token = string
279+
encrypted = bool
280+
performance_mode = string
281+
throughput_mode = string
282+
backup_policy = string
283+
}})
284+
- terraform.tfvars:
285+
- Structure as follows:
286+
security_group_name = "efs_rule"
287+
security_group_ingress_rules = {{
288+
efs_rule = {{
289+
description = "EFS Ingress"
290+
from_port = 2049
291+
to_port = 2049
292+
protocol = "tcp"
293+
cidr_blocks = ["0.0.0.0/0"]
294+
}}
295+
}}
296+
security_group_egress_rule = {{
297+
from_port = 0
298+
to_port = 0
299+
protocol = "-1"
300+
cidr_blocks = ["0.0.0.0/0"]
301+
}}
302+
303+
file_system_create = {aws_efs_create_file_system}
304+
efs = {{
305+
creation_token = "terraform"
306+
encrypted = true
307+
performance_mode = "generalPurpose"
308+
throughput_mode = "elastic"
309+
backup_policy = "ENABLED"
310+
}}
311+
312+
mount_target_create = {aws_efs_create_mount_target}
313+
backup_policy_create = {aws_efs_create_backup_policy}
314+
- versions.tf:
315+
- Structure as follows:
316+
terraform {{
317+
required_version = ">= 1.0"
318+
319+
required_providers {{
320+
aws = {{
321+
source = "hashicorp/aws"
322+
version = ">= 5.20"
323+
}}
324+
}}
325+
}}
326+
Ensure this project structure supports {efs}’s configurability, extensibility, and
327+
reusability across diverse Terraform providers, empowering users to manage their resources through a
328+
single, customizable root configuration while keeping module internals robustly modular.
329+
330+
finally just give me a python code without any note that can generate a project folder with the given
331+
schema without ```python entry. and we dont need any base directory in the python code. the final
332+
terraform template must work very well without any error!
333+
334+
Python code you give me, must have structure like that:
335+
336+
import os
337+
project_name = "app/media/MyTerraform"
338+
modules_dir = os.path.join(project_name, "modules")
339+
efs_dir = os.path.join(modules_dir, "efs")
340+
341+
# Create project directories
342+
os.makedirs(efs_dir, exist_ok=True)
343+
344+
# Create main.tf
345+
with open(os.path.join(project_name, "main.tf"), "w") as main_file:
346+
# any thing you need
347+
348+
"""
349+
return prompt

0 commit comments

Comments
 (0)