-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
172 lines (143 loc) · 3.62 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
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
provider "aws" {
region = var.aws_region
}
data "aws_caller_identity" "current" {}
data "aws_ami" "latest_ami" {
most_recent = true
filter {
name = "tag:Name"
values = ["UbuntuImage"]
}
filter {
name = "tag:Version"
values = ["0.0.7"]
}
owners = ["self"]
}
resource "aws_eip" "static_ip" {
tags = {
Name = "RailsAppEIP"
}
}
resource "aws_eip_association" "static_ip_association" {
instance_id = aws_instance.rails_instance.id
allocation_id = aws_eip.static_ip.id
}
resource "tls_private_key" "ror_key" {
algorithm = "RSA"
rsa_bits = 2048
}
resource "random_string" "ror_key_suffix" {
length = 8
special = false
upper = false
}
resource "aws_secretsmanager_secret" "ror_key_secret" {
name = "ror_key_secret-${random_string.ror_key_suffix.result}"
}
resource "aws_secretsmanager_secret_version" "ror_key_version" {
secret_id = aws_secretsmanager_secret.ror_key_secret.id
secret_string = tls_private_key.ror_key.private_key_pem
}
resource "aws_key_pair" "deployer" {
key_name = var.key_name
public_key = tls_private_key.ror_key.public_key_openssh
}
resource "aws_vpc" "rails_vpc" {
cidr_block = var.vpc_cidr_block
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "rails-vpc"
}
}
resource "aws_subnet" "rails_public_subnet" {
vpc_id = aws_vpc.rails_vpc.id
cidr_block = "10.0.10.0/24"
map_public_ip_on_launch = true
availability_zone = "us-west-2a"
tags = {
Name = "rails-public-subnet"
}
}
resource "aws_internet_gateway" "rails_igw" {
vpc_id = aws_vpc.rails_vpc.id
tags = {
Name = "rails-igw"
}
}
resource "aws_route_table" "rails_public_rt" {
vpc_id = aws_vpc.rails_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.rails_igw.id
}
tags = {
Name = "rails-public-rt"
}
}
resource "aws_route_table_association" "rails_public_rt_association" {
subnet_id = aws_subnet.rails_public_subnet.id
route_table_id = aws_route_table.rails_public_rt.id
}
resource "aws_security_group" "rails_sg" {
name = var.security_group_name
description = "Allow inbound traffic for PostgreSQL, Rails, Redis, and SSH"
vpc_id = aws_vpc.rails_vpc.id
ingress {
description = "SSH"
from_port = 2222
to_port = 2222
protocol = "tcp"
cidr_blocks = var.ssh_cidr_blocks
}
ingress {
description = "Rails HTTP"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = var.http_cidr_blocks
}
ingress {
description = "Rails HTTPS"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = var.https_cidr_blocks
}
# Only allow PostgreSQL and Redis access within the VPC
ingress {
description = "PostgreSQL"
from_port = 5432
to_port = 5432
protocol = "tcp"
cidr_blocks = var.postgres_cidr_blocks
}
ingress {
description = "Redis"
from_port = 6379
to_port = 6379
protocol = "tcp"
cidr_blocks = var.redis_cidr_blocks
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = var.security_group_name
}
}
resource "aws_instance" "rails_instance" {
ami = data.aws_ami.latest_ami.id
instance_type = var.instance_type
subnet_id = aws_subnet.rails_public_subnet.id
vpc_security_group_ids = [aws_security_group.rails_sg.id]
key_name = var.key_name
associate_public_ip_address = true
tags = {
Name = "RailsApplication"
}
}