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