-
Notifications
You must be signed in to change notification settings - Fork 2
/
linux-vm-main.tf
78 lines (67 loc) · 2.3 KB
/
linux-vm-main.tf
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
###################################
## Virtual Machine Module - Main ##
###################################
locals {
ec2_subnet_list = [aws_subnet.public-subnet-1.id, aws_subnet.public-subnet-2.id, aws_subnet.public-subnet-3.id]
}
# Create EC2 Instances
resource "aws_instance" "linux-server" {
count = var.ec2_count
ami = data.aws_ami.amazon-linux-2022.id
instance_type = var.linux_instance_type
subnet_id = element(local.ec2_subnet_list, count.index)
vpc_security_group_ids = [aws_security_group.aws-linux-sg.id]
associate_public_ip_address = var.linux_associate_public_ip_address
source_dest_check = false
key_name = aws_key_pair.key_pair.key_name
user_data = file("aws-user-data.sh")
# root disk
root_block_device {
volume_size = var.linux_root_volume_size
volume_type = var.linux_root_volume_type
delete_on_termination = true
encrypted = true
}
# extra disk
ebs_block_device {
device_name = "/dev/xvda"
volume_size = var.linux_data_volume_size
volume_type = var.linux_data_volume_type
encrypted = true
delete_on_termination = true
}
tags = {
Name = "${lower(var.app_name)}-${var.app_environment}-linux-server-${count.index+1}"
Environment = var.app_environment
}
}
# Define the security group for the Linux server
resource "aws_security_group" "aws-linux-sg" {
name = "${lower(var.app_name)}-${var.app_environment}-linux-sg"
description = "Allow incoming HTTP connections"
vpc_id = aws_vpc.vpc.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow incoming HTTP connections"
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow incoming SSH connections"
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "${lower(var.app_name)}-${var.app_environment}-linux-sg"
Environment = var.app_environment
}
}