-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
111 lines (86 loc) · 2.19 KB
/
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
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
resource "tls_private_key" "public_key" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "local_file" "private_key" {
content = tls_private_key.public_key.private_key_openssh
filename = "${path.module}/key.pem"
file_permission = "0600"
}
resource "aws_key_pair" "key_pair" {
key_name = "${var.name}-key"
public_key = tls_private_key.public_key.public_key_openssh
}
resource "aws_vpc" "vpc" {
cidr_block = "10.0.0.0/24"
tags = {
Name = "${var.name}-vpc"
}
}
resource "aws_internet_gateway" "internet_gateway" {
vpc_id = aws_vpc.vpc.id
tags = {
Name = "${var.name}-internet_gateway"
}
}
resource "aws_route_table" "route_table" {
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.internet_gateway.id
}
tags = {
Name = "${var.name}-route_table"
}
}
resource "aws_subnet" "subnet" {
vpc_id = aws_vpc.vpc.id
cidr_block = "10.0.0.0/24"
availability_zone = "us-east-1a"
tags = {
Name = "${var.name}-subnet"
}
}
resource "aws_route_table_association" "aws_route_table_association" {
for_each = aws_subnet.subnet
route_table_id = aws_route_table.route_table.id
subnet_id = aws_subnet.subnet.id
}
resource "aws_security_group" "security_group" {
name = "${var.name}-security_group"
description = "The ${var.name} security group generated by Terraform"
vpc_id = aws_vpc.vpc.id
dynamic "ingress" {
for_each = var.ports
content {
from_port = ingress.value
to_port = ingress.value
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"]
}
}
resource "aws_eip" "elastic_ip" {
instance = aws_instance.ec2_instance.id
}
resource "aws_instance" "ec2_instance" {
# Ubuntu 22.04 ami
ami = var.ami
instance_type = var.instance_type
key_name = aws_key_pair.key_pair.key_name
subnet_id = aws_subnet.subnet.id
vpc_security_group_ids = [aws_security_group.security_group.id]
root_block_device {
volume_size = 150
volume_type = "gp3"
}
tags = {
Name = "${var.name}-ec2"
}
}