Skip to content

Commit 7aec562

Browse files
authored
Merge pull request #73 from mohammadll/terraform
Add EC2 Prompt
2 parents 6a66477 + 8ec8c5f commit 7aec562

1 file changed

Lines changed: 265 additions & 2 deletions

File tree

  • app/template_generators/terraform/aws
Lines changed: 265 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,269 @@
11
def IaC_template_generator_ec2(input) -> str:
2-
32

3+
ec2 = ['aws_key_pair', 'aws_security_group', 'aws_instance', 'aws_ami_from_instance']
4+
5+
aws_ec2_create_key_pair = 'true' if input.key_pair else 'false'
6+
aws_ec2_create_security_group = 'true' if input.security_group else 'false'
7+
aws_ec2_create_instance = 'true' if input.aws_instance else 'false'
8+
aws_ec2_create_ami_from_instance = 'true' if input.ami_from_instance else 'false'
9+
10+
11+
prompt = f"""
12+
Generate a Python code to generate a Terraform project (project name is app/media/MyTerraform)
13+
that dynamically provisions {ec2} resources ensuring a modular, flexible structure to enable users
14+
to configure all essential settings at the root level. Only provide Python code, no explanations or
15+
markdown formatting. The project should be organized as follows:
16+
1. Root Directory Structure:
17+
- main.tf:
18+
- Define the provider block as follows:
19+
```
20+
provider "aws" {{
21+
region = "us-east-1"
22+
}}
23+
```
24+
- Defines a module block that references "ec2" from a subdirectory within modules.
25+
Don't forget to use source parameter to call ec2 module. this is so important.
26+
This module block should expose all variables that {ec2} resources require, allowing
27+
configuration at the root level rather than directly within the module.
28+
- Every variable defined in {ec2} resources should be passed through the module block,
29+
ensuring that users can adjust all critical parameters of {ec2} resources by modifying
30+
root main.tf. Avoid using any other parameters. just use the parameters of {ec2} resources with the same keys
31+
- variables.tf:
32+
- Sets these variables names for aws_key_pair resource:
33+
key_pair_create(bool), key_pair_name(string)
34+
- Sets these variables names for aws_security_group resource:
35+
security_group_create(bool), security_group_name(string), security_group_ingress_rules(map(object)), security_group_egress_rule(object())
36+
- Sets these variables names for aws_instance resource:
37+
instance_create(bool), instance_type(string)
38+
- Sets these variables names for aws_ami_from_instance resource:
39+
ami_from_instance_create(bool), ami_name(string)
40+
- terraform.tfvars:
41+
- Structure as follows:
42+
key_pair_create = {aws_ec2_create_key_pair}
43+
key_pair_name = "ec2"
44+
45+
security_group_create = {aws_ec2_create_security_group}
46+
security_group_name = "my_rules"
47+
security_group_ingress_rules = {{
48+
ssh_rule = {{
49+
description = "SSH Ingress"
50+
from_port = 22
51+
to_port = 22
52+
protocol = "tcp"
53+
cidr_blocks = ["0.0.0.0/0"]
54+
}},
55+
http_rule = {{
56+
description = "HTTP Ingress"
57+
from_port = 80
58+
to_port = 80
59+
protocol = "tcp"
60+
cidr_blocks = ["0.0.0.0/0"]
61+
}}
62+
}}
63+
security_group_egress_rule = {{
64+
from_port = 0
65+
to_port = 0
66+
protocol = "-1"
67+
cidr_blocks = ["0.0.0.0/0"]
68+
}}
69+
70+
instance_create = {aws_ec2_create_instance}
71+
instance_type = "t2.micro"
72+
73+
ami_from_instance_create = {aws_ec2_create_ami_from_instance}
74+
ami_name = "my-own-ami"
75+
- versions.tf:
76+
- Structure as follows:
77+
terraform {{
78+
required_version = ">= 1.0"
79+
80+
required_providers {{
81+
aws = {{
82+
source = "hashicorp/aws"
83+
version = ">= 5.20"
84+
}}
85+
}}
86+
}}
87+
2. Module Directory Structure (modules/ec2):
88+
- create an empty file called "terraform.pub" to store the public key for key_pair resource
89+
- main.tf:
90+
- Create the below data block:
91+
```
92+
data "aws_ami" "linux" {{
93+
most_recent = true
94+
owners = ["amazon"]
95+
96+
filter {{
97+
name = "name"
98+
values = ["al2023-ami-2023*kernel-6.1-x86_64"]
99+
}}
100+
101+
filter {{
102+
name = "root-device-type"
103+
values = ["ebs"]
104+
}}
105+
106+
filter {{
107+
name = "virtualization-type"
108+
values = ["hvm"]
109+
}}
110+
}}
111+
```
112+
- Set the following parameters for aws_key_pair resource (name its terraform resource to "key_pair") and avoid using any other parameters:
113+
- 1. count (type: number): follow the below syntax for count:
114+
```
115+
count = var.key_pair_create ? 1 : 0
116+
```
117+
- 2. key_name (type: string): follow the below syntax for key_name:
118+
```
119+
key_name = var.key_pair_name
120+
```
121+
- 3. public_key (type: string): follow the below syntax for public_key, avoid generating double brackets {{}} for path.module in the below syntax:
122+
```
123+
public_key = file("${{path.module}}/terraform.pub")
124+
```
125+
- Set the following parameters for aws_security_group resource (name its terraform resource to "security_group") and avoid using any other parameters:
126+
- 1. count (type: number): follow the below syntax for count:
127+
```
128+
count = var.security_group_create ? 1 : 0
129+
```
130+
- 2. name: follow the below syntax for name:
131+
```
132+
name = var.security_group_name
133+
```
134+
- 3. create a dynamic block for ingress rules as follows:
135+
```
136+
dynamic "ingress" {{
137+
for_each = var.security_group_ingress_rules
138+
content {{
139+
description = ingress.value["description"]
140+
from_port = ingress.value["from_port"]
141+
to_port = ingress.value["to_port"]
142+
protocol = ingress.value["protocol"]
143+
cidr_blocks = ingress.value["cidr_blocks"]
144+
}}
145+
}}
146+
```
147+
- 4. create a block for egress rule as follows:
148+
```
149+
egress {{
150+
from_port = var.security_group_egress_rule["from_port"]
151+
to_port = var.security_group_egress_rule["to_port"]
152+
protocol = var.security_group_egress_rule["protocol"]
153+
cidr_blocks = var.security_group_egress_rule["cidr_blocks"]
154+
}}
155+
```
156+
- Set the following parameters for aws_instance resource (name its terraform resource to "instance") and avoid using any other parameters:
157+
- 1. count (type: number): follow the below syntax for count:
158+
```
159+
count = var.instance_create ? 1 : 0
160+
```
161+
- 2. ami (type: string): follow the below syntax for ami, it uses the data block:
162+
```
163+
ami = data.aws_ami.linux.id
164+
```
165+
- 3. instance_type (type: string): follow the below syntax for instance_type:
166+
```
167+
instance_type = var.instance_type
168+
```
169+
- 4. key_name: follow the below syntax for key_name:
170+
```
171+
key_name = var.key_pair_create ? aws_key_pair.key_pair[0].key_name : null
172+
```
173+
- 5. vpc_security_group_ids: follow the below syntax for vpc_security_group_ids:
174+
```
175+
vpc_security_group_ids = var.security_group_create ? [aws_security_group.security_group[0].id] : null
176+
```
177+
- Set the following parameters for aws_ami_from_instance resource (name its terraform resource to "ami") and avoid using any other parameters:
178+
- 1. count (type: number): follow the below syntax for count:
179+
```
180+
count = var.instance_create && var.ami_from_instance_create ? 1 : 0
181+
```
182+
- 2. name (type: string): follow the below syntax for name:
183+
```
184+
name = var.ami_name
185+
```
186+
- 3. source_instance_id: follow the below syntax for source_instance_id:
187+
```
188+
source_instance_id = aws_instance.instance[0].id
189+
```
190+
- variables.tf:
191+
- Sets these variables names for aws_key_pair resource:
192+
key_pair_create(bool), key_pair_name(string)
193+
- Sets these variables names for aws_security_group resource:
194+
security_group_create(bool), security_group_name(string), security_group_ingress_rules(map(object)), security_group_egress_rule(object())
195+
- Sets these variables names for aws_instance resource:
196+
instance_create(bool), instance_type(string)
197+
- Sets these variables names for aws_ami_from_instance resource:
198+
ami_from_instance_create(bool), ami_name(string)
199+
- terraform.tfvars:
200+
- Structure as follows:
201+
key_pair_create = {aws_ec2_create_key_pair}
202+
key_pair_name = "ec2"
203+
204+
security_group_create = {aws_ec2_create_security_group}
205+
security_group_name = "my_rules"
206+
security_group_ingress_rules = {{
207+
ssh_rule = {{
208+
description = "SSH Ingress"
209+
from_port = 22
210+
to_port = 22
211+
protocol = "tcp"
212+
cidr_blocks = ["0.0.0.0/0"]
213+
}},
214+
http_rule = {{
215+
description = "HTTP Ingress"
216+
from_port = 80
217+
to_port = 80
218+
protocol = "tcp"
219+
cidr_blocks = ["0.0.0.0/0"]
220+
}}
221+
}}
222+
security_group_egress_rule = {{
223+
from_port = 0
224+
to_port = 0
225+
protocol = "-1"
226+
cidr_blocks = ["0.0.0.0/0"]
227+
}}
228+
229+
instance_create = {aws_ec2_create_instance}
230+
instance_type = "t2.micro"
231+
232+
ami_from_instance_create = {aws_ec2_create_ami_from_instance}
233+
ami_name = "my-own-ami"
234+
- versions.tf:
235+
- Structure as follows:
236+
terraform {{
237+
required_version = ">= 1.0"
238+
239+
required_providers {{
240+
aws = {{
241+
source = "hashicorp/aws"
242+
version = ">= 5.20"
243+
}}
244+
}}
245+
}}
246+
Ensure this project structure supports {ec2}’s configurability, extensibility, and
247+
reusability across diverse Terraform providers, empowering users to manage their resources through a
248+
single, customizable root configuration while keeping module internals robustly modular.
249+
250+
finally just give me a python code without any note that can generate a project folder with the given
251+
schema without ```python entry. and we dont need any base directory in the python code. the final
252+
terraform template must work very well without any error!
253+
254+
Python code you give me, must have structure like that:
255+
256+
import os
257+
project_name = "app/media/MyTerraform"
258+
modules_dir = os.path.join(project_name, "modules")
259+
ec2_dir = os.path.join(modules_dir, "ec2")
260+
261+
# Create project directories
262+
os.makedirs(ec2_dir, exist_ok=True)
263+
264+
# Create main.tf
265+
with open(os.path.join(project_name, "main.tf"), "w") as main_file:
266+
# any thing you need
4267
5-
prompt = f""" """
268+
"""
6269
return prompt

0 commit comments

Comments
 (0)