-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
276 lines (244 loc) · 7.56 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
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
locals {
swap_size = var.swap_volume_size == var.gitlab_volume_size ? var.swap_volume_size + 1 : var.swap_volume_size
}
resource "aws_route53_record" "this" {
zone_id = var.zone_id
name = var.host_domain
type = "A"
ttl = "300"
records = [aws_instance.this.private_ip]
}
resource "aws_key_pair" "this" {
key_name = "${var.environment}-gitlab"
public_key = base64decode(aws_ssm_parameter.public_key.value)
}
resource "tls_private_key" "this" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_ssm_parameter" "public_key" {
name = "${var.environment}-gitlab-public-ssh-key"
type = "SecureString"
value = base64encode(tls_private_key.this.public_key_openssh)
}
resource "aws_ssm_parameter" "private_key" {
name = "${var.environment}-gitlab-private-ssh-key"
type = "SecureString"
tier = "Advanced"
value = base64encode(tls_private_key.this.private_key_pem)
}
module "security_group_gitlab" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 4.0"
description = "Security group for the gitlab EC2"
name = "${var.environment}-gitlab"
vpc_id = var.vpc_id
ingress_cidr_blocks = var.ingress_cidr_blocks
ingress_rules = ["https-443-tcp", "ssh-tcp"]
ingress_with_cidr_blocks = [ for block in var.ingress_cidr_blocks:
{
from_port = 2222
to_port = 2222
protocol = "tcp"
description = "SSH port for Gitlab container port-forward"
cidr_blocks = block
}
]
egress_rules = ["all-all"]
}
resource "aws_backup_vault" "gitlab" {
count = var.backups_enabled ? 1 : 0
name = "${var.environment}-gitlab"
}
resource "aws_iam_instance_profile" "this" {
name = "${var.environment}-gitlab"
role = aws_iam_role.this.name
}
resource "aws_iam_role" "this" {
name = "${var.environment}-gitlab"
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
POLICY
}
resource "aws_iam_role_policy" "certbot_r53" {
name = "certbot-r53"
role = aws_iam_role.this.id
policy = <<-EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"route53:ListHostedZones",
"route53:GetChange"
],
"Resource": [
"*"
]
},
{
"Effect" : "Allow",
"Action" : [
"route53:ChangeResourceRecordSets"
],
"Resource" : [
"arn:aws:route53:::hostedzone/${var.zone_id}"
]
}
]
}
EOF
}
resource "aws_iam_role_policy" "gitlab_backup" {
count = var.backups_enabled ? 1 : 0
name = "backup"
role = aws_iam_role.this.id
policy = <<-EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "StartBackup",
"Effect": "Allow",
"Action": [
"iam:PassRole",
"backup:StartBackupJob"
],
"Resource": [
"${aws_backup_vault.gitlab[0].arn}",
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/service-role/AWSBackupDefaultServiceRole"
]
},
{
"Sid": "DescribeJob",
"Effect": "Allow",
"Action": "backup:DescribeBackupJob",
"Resource": "*"
}
]
}
EOF
}
resource "aws_launch_template" "gitlab" {
name = "${var.environment}-gitlab"
image_id = data.aws_ami.ubuntu.id
instance_type = var.instance_type
key_name = aws_key_pair.this.key_name
ebs_optimized = true
user_data = base64encode(templatefile("${path.module}/resources/templates/user_data.tpl",
{
docker_compose_yml = base64encode(templatefile("${path.module}/resources/templates/docker-compose.yml.tpl",
{
host_domain = var.host_domain
gitlab_container_name = var.gitlab_container_name
})),
install_script = base64encode(templatefile("${path.module}/resources/scripts/install.sh",
{
certbot_email = var.certbot_email
host_domain = var.host_domain
make_fs = var.gitlab_snapshot_id == null ? true : false
swap = local.swap_size
backups_enabled = var.backups_enabled
})),
backup_script = base64encode(templatefile("${path.module}/resources/scripts/backup.sh",
{
vol_arn = var.gitlab_snapshot_id != null ? aws_ebs_volume.gitlab_snapshot[0].arn : aws_ebs_volume.gitlab.arn
backup_role_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/service-role/AWSBackupDefaultServiceRole"
vault_name = aws_backup_vault.gitlab[0].id
aws_region = data.aws_region.current.name
backups_enabled = var.backups_enabled
retention_days = var.retention_days
})),
renew_script = base64encode(templatefile("${path.module}/resources/scripts/renew.sh",
{
certbot_email = var.certbot_email
host_domain = var.host_domain
gitlab_container_name = var.gitlab_container_name
}))
}
))
network_interfaces {
subnet_id = var.subnet_id
security_groups = [module.security_group_gitlab.security_group_id]
associate_public_ip_address = false
delete_on_termination = true
}
block_device_mappings {
device_name = "/dev/sda1"
ebs {
volume_size = 30
volume_type = "gp3"
delete_on_termination = true
}
}
iam_instance_profile {
name = aws_iam_instance_profile.this.name
}
monitoring {
enabled = true
}
tag_specifications {
resource_type = "instance"
tags = {
Name = "${var.environment}-gitlab"
}
}
lifecycle {
create_before_destroy = true
}
depends_on = [aws_ebs_volume.gitlab]
}
resource "aws_ebs_volume" "gitlab" {
availability_zone = data.aws_subnet.selected.availability_zone
size = var.gitlab_volume_size
tags = {
Name = "${var.environment}-gitlab"
}
}
resource "aws_ebs_volume" "gitlab_snapshot" {
count = var.gitlab_snapshot_id != null ? 1 : 0
availability_zone = data.aws_subnet.selected.availability_zone
size = var.gitlab_volume_size
snapshot_id = var.gitlab_snapshot_id
tags = {
Name = "${var.environment}-gitlab-snapshot"
snapshot_id = var.gitlab_snapshot_id
}
}
resource "aws_instance" "this" {
iam_instance_profile = aws_iam_instance_profile.this.name
launch_template {
id = aws_launch_template.gitlab.id
version = "$Latest"
}
tags = {
Name = "${var.environment}-gitlab"
snapshot_id = var.gitlab_snapshot_id
}
}
resource "aws_volume_attachment" "gitlab" {
device_name = "/dev/sdh"
volume_id = var.gitlab_snapshot_id != null ? aws_ebs_volume.gitlab_snapshot[0].id : aws_ebs_volume.gitlab.id
instance_id = aws_instance.this.id
}
resource "aws_ebs_volume" "swap" {
availability_zone = data.aws_availability_zones.available.names[0]
size = local.swap_size
}
resource "aws_volume_attachment" "swap" {
device_name = "/dev/sdj"
volume_id = aws_ebs_volume.swap.id
instance_id = aws_instance.this.id
}