11def IaC_template_generator_ec2 (input ) -> str :
22
3- ec2 = ['aws_key_pair' , 'aws_security_group' ]
3+ ec2 = ['aws_key_pair' , 'aws_security_group' , 'aws_instance' ]
44
55 aws_ec2_create_key_pair = 'true' if input .key_pair else 'false'
66 aws_ec2_create_security_group = 'true' if input .security_group else 'false'
7+ aws_ec2_create_instance = 'true' if input .aws_instance else 'false'
78
89
9- key_path = "${path.module}/terraform.pub"
10-
1110 prompt = f"""
1211 Generate a Python code to generate a Terraform project (project name is app/media/MyTerraform)
1312 that dynamically provisions { ec2 } resources ensuring a modular, flexible structure to enable users
@@ -32,6 +31,8 @@ def IaC_template_generator_ec2(input) -> str:
3231 key_pair_create(bool), key_pair_name(string)
3332 - Sets these variables names for aws_security_group resource:
3433 security_group_create(bool), security_group_name(string), security_group_ingress_rules(map(object)), security_group_egress_rule(object())
34+ - Sets these variables names for aws_instance resource:
35+ instance_create(bool), instance_type(string)
3536 - terraform.tfvars:
3637 - Structure as follows:
3738 key_pair_create = { aws_ec2_create_key_pair }
@@ -61,6 +62,9 @@ def IaC_template_generator_ec2(input) -> str:
6162 protocol = "-1"
6263 cidr_blocks = ["0.0.0.0/0"]
6364 }}
65+
66+ instance_create = { aws_ec2_create_instance }
67+ instance_type = "t2.micro"
6468 - versions.tf:
6569 - Structure as follows:
6670 terraform {{
@@ -76,6 +80,28 @@ def IaC_template_generator_ec2(input) -> str:
7680 2. Module Directory Structure (modules/ec2):
7781 - create an empty file called "terraform.pub" to store the public key for key_pair resource
7882 - main.tf:
83+ - Create the below data block:
84+ ```
85+ data "aws_ami" "linux" {{
86+ most_recent = true
87+ owners = ["amazon"]
88+
89+ filter {{
90+ name = "name"
91+ values = ["al2023-ami-2023*kernel-6.1-x86_64"]
92+ }}
93+
94+ filter {{
95+ name = "root-device-type"
96+ values = ["ebs"]
97+ }}
98+
99+ filter {{
100+ name = "virtualization-type"
101+ values = ["hvm"]
102+ }}
103+ }}
104+ ```
79105 - Set the following parameters for aws_key_pair resource (name its terraform resource to "key_pair") and avoid using any other parameters:
80106 - 1. count (type: number): follow the below syntax for count:
81107 ```
@@ -85,9 +111,9 @@ def IaC_template_generator_ec2(input) -> str:
85111 ```
86112 key_name = var.key_pair_name
87113 ```
88- - 3. public_key (type: string): follow the below syntax for public_key:
114+ - 3. public_key (type: string): follow the below syntax for public_key, avoid generating double brackets {{}} for path.module in the below syntax :
89115 ```
90- public_key = file("{ key_path } ")
116+ public_key = file("${{path.module}}/terraform.pub ")
91117 ```
92118 - Set the following parameters for aws_security_group resource (name its terraform resource to "security_group") and avoid using any other parameters:
93119 - 1. count (type: number): follow the below syntax for count:
@@ -120,11 +146,34 @@ def IaC_template_generator_ec2(input) -> str:
120146 cidr_blocks = var.security_group_egress_rule["cidr_blocks"]
121147 }}
122148 ```
149+ - Set the following parameters for aws_instance resource (name its terraform resource to "instance") and avoid using any other parameters:
150+ - 1. count (type: number): follow the below syntax for count:
151+ ```
152+ count = var.instance_create ? 1 : 0
153+ ```
154+ - 2. ami (type: string): follow the below syntax for ami, it uses the data block:
155+ ```
156+ ami = data.aws_ami.linux.id
157+ ```
158+ - 3. instance_type (type: string): follow the below syntax for instance_type:
159+ ```
160+ instance_type = var.instance_type
161+ ```
162+ - 4. key_name: follow the below syntax for key_name:
163+ ```
164+ key_name = var.key_pair_create ? aws_key_pair.key_pair[0].key_name : null
165+ ```
166+ - 5. vpc_security_group_ids: follow the below syntax for vpc_security_group_ids:
167+ ```
168+ vpc_security_group_ids = var.security_group_create ? [aws_security_group.security_group[0].id] : null
169+ ```
123170 - variables.tf:
124171 - Sets these variables names for aws_key_pair resource:
125172 key_pair_create(bool), key_pair_name(string)
126173 - Sets these variables names for aws_security_group resource:
127174 security_group_create(bool), security_group_name(string), security_group_ingress_rules(map(object)), security_group_egress_rule(object())
175+ - Sets these variables names for aws_instance resource:
176+ instance_create(bool), instance_type(string)
128177 - terraform.tfvars:
129178 - Structure as follows:
130179 key_pair_create = { aws_ec2_create_key_pair }
@@ -154,6 +203,9 @@ def IaC_template_generator_ec2(input) -> str:
154203 protocol = "-1"
155204 cidr_blocks = ["0.0.0.0/0"]
156205 }}
206+
207+ instance_create = { aws_ec2_create_instance }
208+ instance_type = "t2.micro"
157209 - versions.tf:
158210 - Structure as follows:
159211 terraform {{
0 commit comments