-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
315 lines (266 loc) · 7.33 KB
/
main.tf
File metadata and controls
315 lines (266 loc) · 7.33 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
provider "aws" {
region = "us-east-1"
}
# Crée le repository ECR
resource "aws_ecr_repository" "hello_world" {
name = "hello-world"
}
# Groupe de sécurité pour le front-end
resource "aws_security_group" "frontend_sg" {
name = "frontend-sg"
description = "Allow HTTP and SSH traffic"
vpc_id = var.vpc_id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# Groupe de sécurité pour le back-end
resource "aws_security_group" "backend_sg" {
name = "backend-sg"
description = "Security group for backend instances"
vpc_id = var.vpc_id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# Rôle IAM pour les instances EC2
resource "aws_iam_role" "ec2_role" {
name = "ec2-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ec2.amazonaws.com"
}
}
]
})
}
# Attache la politique ECR à ce rôle
resource "aws_iam_role_policy_attachment" "ec2_role_policy" {
role = aws_iam_role.ec2_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryFullAccess"
}
# Profil d'instance IAM pour associer le rôle IAM aux instances EC2
resource "aws_iam_instance_profile" "ec2_instance_profile" {
name = "ec2-instance-profile"
role = aws_iam_role.ec2_role.name
}
# Groupe de sécurité pour l'instance RDS
resource "aws_security_group" "rds_sg" {
name = "rds-sg"
description = "Allow MySQL traffic"
vpc_id = var.vpc_id
ingress {
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# Paire de clés SSH
resource "aws_key_pair" "deployer_key" {
key_name = var.key_name
public_key = file("~/.ssh/${var.key_name}.pub")
}
# Instances EC2 pour le front-end
resource "aws_instance" "frontend_instance" {
count = 2
ami = var.ami_id
instance_type = "t2.micro"
key_name = aws_key_pair.deployer_key.key_name
security_groups = [aws_security_group.frontend_sg.name]
user_data = <<-EOF
#!/bin/bash
sudo yum update -y
sudo yum install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx
# Configure Nginx to serve the React application
sudo cat > /etc/nginx/conf.d/default.conf <<EOL
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
index index.html;
location / {
try_files \$uri \$uri/ /index.html;
}
}
EOL
sudo systemctl restart nginx
EOF
tags = {
Name = "frontend-instance-${count.index}"
}
}
# Instances EC2 pour le back-end
resource "aws_instance" "backend" {
count = 2
ami = var.ami_id
instance_type = "t2.micro"
key_name = aws_key_pair.deployer_key.key_name
iam_instance_profile = aws_iam_instance_profile.ec2_instance_profile.name
security_groups = [aws_security_group.backend_sg.name]
associate_public_ip_address = true
user_data = <<-EOF
#!/bin/bash
sudo yum update -y
sudo yum install -y docker
sudo systemctl start docker
sudo systemctl enable docker
EOF
tags = {
Name = "backend-instance-${count.index}"
}
}
output "backend_instance_ips" {
description = "The public IPs of the backend instances"
value = aws_instance.backend[*].public_ip
}
output "ecr_repository_url" {
description = "The URL of the ECR repository"
value = aws_ecr_repository.hello_world.repository_url
}
# Load Balancer pour le front-end
resource "aws_elb" "frontend_elb" {
name = "frontend-elb"
availability_zones = ["us-east-1d"]
security_groups = [aws_security_group.frontend_sg.id]
listener {
instance_port = 80
instance_protocol = "HTTP"
lb_port = 80
lb_protocol = "HTTP"
}
health_check {
target = "HTTP:80/"
interval = 30
timeout = 5
healthy_threshold = 2
unhealthy_threshold = 2
}
instances = aws_instance.frontend_instance[*].id
}
# Load Balancer pour le back-end
resource "aws_elb" "backend_elb" {
name = "backend-elb"
availability_zones = ["us-east-1d"]
security_groups = [aws_security_group.backend_sg.id]
listener {
instance_port = 80
instance_protocol = "HTTP"
lb_port = 80
lb_protocol = "HTTP"
}
health_check {
target = "HTTP:80/"
interval = 30
timeout = 5
healthy_threshold = 2
unhealthy_threshold = 2
}
instances = aws_instance.backend[*].id
}
# Instance RDS
resource "aws_db_instance" "default" {
allocated_storage = 5
storage_type = "gp2"
engine = "mysql"
engine_version = "8.0"
instance_class = "db.t3.micro"
identifier = "mydb-instance"
username = var.db_username
password = var.db_password
parameter_group_name = "default.mysql8.0"
skip_final_snapshot = true
publicly_accessible = true
vpc_security_group_ids = [aws_security_group.rds_sg.id]
tags = {
Name = "mydb"
}
}
# AWS Backup Vault
resource "aws_backup_vault" "rds_backup_vault" {
name = "rds-backup-vault"
}
# IAM Role for AWS Backup
resource "aws_iam_role" "backup_role" {
name = "backup-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "backup.amazonaws.com"
}
}
]
})
}
resource "aws_iam_role_policy_attachment" "backup_role_policy" {
role = aws_iam_role.backup_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForBackup"
}
# AWS Backup Plan
resource "aws_backup_plan" "rds_backup_plan" {
name = "rds-backup-plan"
rule {
rule_name = "rds-12hour-backup"
target_vault_name = aws_backup_vault.rds_backup_vault.name
schedule = "cron(0 */12 * * ? *)" # Cron expression for every 12 hours
lifecycle {
delete_after = 30 # Number of days to retain the backup
}
}
}
# AWS Backup Selection
resource "aws_backup_selection" "rds_backup_selection" {
name = "rds-backup-selection"
iam_role_arn = aws_iam_role.backup_role.arn
plan_id = aws_backup_plan.rds_backup_plan.id
resources = [
aws_db_instance.default.arn
]
}