1- Hello ! It looks like your message might have been empty . How can I assist you today ?
1+ import os
2+ project_name = "app/media/MyTerraform"
3+ modules_dir = os .path .join (project_name , "modules" )
4+ ec2_dir = os .path .join (modules_dir , "ec2" )
5+
6+ # Create project directories
7+ os .makedirs (ec2_dir , exist_ok = True )
8+
9+ # Create main.tf
10+ with open (os .path .join (project_name , "main.tf" ), "w" ) as main_file :
11+ main_file .write ('''
12+ provider "aws" {
13+ region = "us-east-1"
14+ }
15+
16+ module "ec2" {
17+ source = "./modules/ec2"
18+
19+ key_pair_create = var.key_pair_create
20+ key_pair_name = var.key_pair_name
21+
22+ security_group_create = var.security_group_create
23+ security_group_name = var.security_group_name
24+ security_group_ingress_rules = var.security_group_ingress_rules
25+ security_group_egress_rule = var.security_group_egress_rule
26+
27+ instance_create = var.instance_create
28+ instance_type = var.instance_type
29+
30+ ami_from_instance_create = var.ami_from_instance_create
31+ ami_name = var.ami_name
32+ }
33+ ''' )
34+
35+ # Create variables.tf
36+ with open (os .path .join (project_name , "variables.tf" ), "w" ) as variables_file :
37+ variables_file .write ('''
38+ variable "key_pair_create" {
39+ type = bool
40+ }
41+
42+ variable "key_pair_name" {
43+ type = string
44+ }
45+
46+ variable "security_group_create" {
47+ type = bool
48+ }
49+
50+ variable "security_group_name" {
51+ type = string
52+ }
53+
54+ variable "security_group_ingress_rules" {
55+ type = map(object({
56+ description = string
57+ from_port = number
58+ to_port = number
59+ protocol = string
60+ cidr_blocks = list(string)
61+ }))
62+ }
63+
64+ variable "security_group_egress_rule" {
65+ type = object({
66+ from_port = number
67+ to_port = number
68+ protocol = string
69+ cidr_blocks = list(string)
70+ })
71+ }
72+
73+ variable "instance_create" {
74+ type = bool
75+ }
76+
77+ variable "instance_type" {
78+ type = string
79+ }
80+
81+ variable "ami_from_instance_create" {
82+ type = bool
83+ }
84+
85+ variable "ami_name" {
86+ type = string
87+ }
88+ ''' )
89+
90+ # Create terraform.tfvars
91+ with open (os .path .join (project_name , "terraform.tfvars" ), "w" ) as tfvars_file :
92+ tfvars_file .write ('''
93+ key_pair_create = true
94+ key_pair_name = "ec2"
95+
96+ security_group_create = true
97+ security_group_name = "my_rules"
98+ security_group_ingress_rules = {
99+ ssh_rule = {
100+ description = "SSH Ingress"
101+ from_port = 22
102+ to_port = 22
103+ protocol = "tcp"
104+ cidr_blocks = ["0.0.0.0/0"]
105+ },
106+ http_rule = {
107+ description = "HTTP Ingress"
108+ from_port = 80
109+ to_port = 80
110+ protocol = "tcp"
111+ cidr_blocks = ["0.0.0.0/0"]
112+ }
113+ }
114+ security_group_egress_rule = {
115+ from_port = 0
116+ to_port = 0
117+ protocol = "-1"
118+ cidr_blocks = ["0.0.0.0/0"]
119+ }
120+
121+ instance_create = false
122+ instance_type = "t2.micro"
123+
124+ ami_from_instance_create = true
125+ ami_name = "my-own-ami"
126+ ''' )
127+
128+ # Create versions.tf
129+ with open (os .path .join (project_name , "versions.tf" ), "w" ) as versions_file :
130+ versions_file .write ('''
131+ terraform {
132+ required_version = ">= 1.0"
133+
134+ required_providers {
135+ aws = {
136+ source = "hashicorp/aws"
137+ version = ">= 5.20"
138+ }
139+ }
140+ }
141+ ''' )
142+
143+ # Create ec2 module files
144+ with open (os .path .join (ec2_dir , "terraform.pub" ), "w" ) as pub_file :
145+ pass
146+
147+ with open (os .path .join (ec2_dir , "main.tf" ), "w" ) as ec2_main_file :
148+ ec2_main_file .write ('''
149+ data "aws_ami" "linux" {
150+ most_recent = true
151+ owners = ["amazon"]
152+
153+ filter {
154+ name = "name"
155+ values = ["al2023-ami-2023*kernel-6.1-x86_64"]
156+ }
157+
158+ filter {
159+ name = "root-device-type"
160+ values = ["ebs"]
161+ }
162+
163+ filter {
164+ name = "virtualization-type"
165+ values = ["hvm"]
166+ }
167+ }
168+
169+ resource "aws_key_pair" "key_pair" {
170+ count = var.key_pair_create ? 1 : 0
171+ key_name = var.key_pair_name
172+ public_key = file("${path.module}/terraform.pub")
173+ }
174+
175+ resource "aws_security_group" "security_group" {
176+ count = var.security_group_create ? 1 : 0
177+ name = var.security_group_name
178+
179+ dynamic "ingress" {
180+ for_each = var.security_group_ingress_rules
181+ content {
182+ description = ingress.value["description"]
183+ from_port = ingress.value["from_port"]
184+ to_port = ingress.value["to_port"]
185+ protocol = ingress.value["protocol"]
186+ cidr_blocks = ingress.value["cidr_blocks"]
187+ }
188+ }
189+
190+ egress {
191+ from_port = var.security_group_egress_rule["from_port"]
192+ to_port = var.security_group_egress_rule["to_port"]
193+ protocol = var.security_group_egress_rule["protocol"]
194+ cidr_blocks = var.security_group_egress_rule["cidr_blocks"]
195+ }
196+ }
197+
198+ resource "aws_instance" "instance" {
199+ count = var.instance_create ? 1 : 0
200+ ami = data.aws_ami.linux.id
201+ instance_type = var.instance_type
202+ key_name = var.key_pair_create ? aws_key_pair.key_pair[0].key_name : null
203+ vpc_security_group_ids = var.security_group_create ? [aws_security_group.security_group[0].id] : null
204+ }
205+
206+ resource "aws_ami_from_instance" "ami" {
207+ count = var.instance_create && var.ami_from_instance_create ? 1 : 0
208+ name = var.ami_name
209+ source_instance_id = aws_instance.instance[0].id
210+ }
211+ ''' )
212+
213+ with open (os .path .join (ec2_dir , "variables.tf" ), "w" ) as ec2_variables_file :
214+ ec2_variables_file .write ('''
215+ variable "key_pair_create" {
216+ type = bool
217+ }
218+
219+ variable "key_pair_name" {
220+ type = string
221+ }
222+
223+ variable "security_group_create" {
224+ type = bool
225+ }
226+
227+ variable "security_group_name" {
228+ type = string
229+ }
230+
231+ variable "security_group_ingress_rules" {
232+ type = map(object({
233+ description = string
234+ from_port = number
235+ to_port = number
236+ protocol = string
237+ cidr_blocks = list(string)
238+ }))
239+ }
240+
241+ variable "security_group_egress_rule" {
242+ type = object({
243+ from_port = number
244+ to_port = number
245+ protocol = string
246+ cidr_blocks = list(string)
247+ })
248+ }
249+
250+ variable "instance_create" {
251+ type = bool
252+ }
253+
254+ variable "instance_type" {
255+ type = string
256+ }
257+
258+ variable "ami_from_instance_create" {
259+ type = bool
260+ }
261+
262+ variable "ami_name" {
263+ type = string
264+ }
265+ ''' )
266+
267+ with open (os .path .join (ec2_dir , "terraform.tfvars" ), "w" ) as ec2_tfvars_file :
268+ ec2_tfvars_file .write ('''
269+ key_pair_create = true
270+ key_pair_name = "ec2"
271+
272+ security_group_create = true
273+ security_group_name = "my_rules"
274+ security_group_ingress_rules = {
275+ ssh_rule = {
276+ description = "SSH Ingress"
277+ from_port = 22
278+ to_port = 22
279+ protocol = "tcp"
280+ cidr_blocks = ["0.0.0.0/0"]
281+ },
282+ http_rule = {
283+ description = "HTTP Ingress"
284+ from_port = 80
285+ to_port = 80
286+ protocol = "tcp"
287+ cidr_blocks = ["0.0.0.0/0"]
288+ }
289+ }
290+ security_group_egress_rule = {
291+ from_port = 0
292+ to_port = 0
293+ protocol = "-1"
294+ cidr_blocks = ["0.0.0.0/0"]
295+ }
296+
297+ instance_create = false
298+ instance_type = "t2.micro"
299+
300+ ami_from_instance_create = true
301+ ami_name = "my-own-ami"
302+ ''' )
303+
304+ with open (os .path .join (ec2_dir , "versions.tf" ), "w" ) as ec2_versions_file :
305+ ec2_versions_file .write ('''
306+ terraform {
307+ required_version = ">= 1.0"
308+
309+ required_providers {
310+ aws = {
311+ source = "hashicorp/aws"
312+ version = ">= 5.20"
313+ }
314+ }
315+ }
316+ ''' )
0 commit comments