Skip to content

Commit 2dcf470

Browse files
author
Premdeep Saini
committed
add support for gitlab backup upload to S3
1 parent 595434b commit 2dcf470

File tree

5 files changed

+183
-1
lines changed

5 files changed

+183
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
external_url '${gitlab_url}'
2+
gitlab_rails['monitoring_whitelist'] = ['0.0.0.0/0','127.0.0.0/8', '::1/128']
3+
gitlab_rails['db_adapter'] = "postgresql"
4+
gitlab_rails['db_encoding'] = "unicode"
5+
gitlab_rails['db_database'] = "${gitlab_db_name}"
6+
gitlab_rails['db_username'] = "${gitlab_db_username}"
7+
gitlab_rails['db_password'] = "${gitlab_db_password}"
8+
gitlab_rails['db_host'] = "${gitlab_db_host}"
9+
gitlab_rails['redis_host'] = "${gitlab_redis_host}"
10+
gitlab_rails['redis_port'] = 6379
11+
postgresql['enable'] = false
12+
redis['enable'] = false
13+
nginx['redirect_http_to_https'] = false
14+
nginx['listen_port'] = 80
15+
nginx['listen_https'] = false
16+
letsencrypt['enable'] = false
17+
18+
################
19+
# S3 Backup
20+
################
21+
gitlab_rails['backup_upload_connection'] = {
22+
'provider' => 'AWS',
23+
'region' => '${aws_region}',
24+
# If using an IAM Profile, don't configure aws_access_key_id & aws_secret_access_key
25+
'use_iam_profile' => true
26+
}
27+
gitlab_rails['backup_upload_remote_directory'] = '${gitlab_backup_s3_bucket_name}'

main.tf

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
locals {
2-
managed_by = "Terraform"
2+
managed_by = "Terraform"
3+
gitlab_config_template_file = "${path.module}/gitlab_config_templates/gitlab.rb.tftpl"
4+
gitlab_config_generated_file = "${path.cwd}/gitlab_config/gitlab.rb"
5+
gitlab_config_playbook_file = "${path.module}/playbooks/gitlab_setup.yaml"
6+
gitlab_complete_url = join("", tolist(["https://", values(module.records.route53_record_name)[0]]))
37
}
48

59
resource "aws_instance" "gitlab" {
@@ -10,16 +14,22 @@ resource "aws_instance" "gitlab" {
1014
associate_public_ip_address = false
1115
vpc_security_group_ids = [aws_security_group.gitlab.id]
1216
key_name = var.gitlab_ssh_public_key != null ? aws_key_pair.gitlab_ssh[0].key_name : null
17+
iam_instance_profile = aws_iam_instance_profile.gitlab.name
1318
root_block_device {
1419
volume_type = var.volume_type
1520
volume_size = var.volume_size
1621
delete_on_termination = false
1722
}
23+
24+
provisioner "local-exec" {
25+
command = "ansible-playbook -u ubuntu -i '${self.private_ip},' --private-key ${var.private_key} -e 'instance_ip_address=${self.private_ip} file_path=${local_file.gitlab_config_file.filename}' ${local.gitlab_config_playbook_file}"
26+
}
1827
tags = {
1928
Name = "${var.environment_prefix}-gitlab"
2029
Environment = var.environment_prefix
2130
ManagedBy = local.managed_by
2231
}
32+
depends_on = [local_file.gitlab_config_file]
2333
}
2434

2535
resource "aws_key_pair" "gitlab_ssh" {
@@ -357,3 +367,108 @@ resource "aws_security_group" "gitlab_redis" {
357367
ManagedBy = local.managed_by
358368
}
359369
}
370+
371+
resource "aws_s3_bucket" "gitlab_backup" {
372+
count = var.enable_gitlab_backup_to_s3 ? 1 : 0
373+
bucket = var.gitlab_backup_bucket_name
374+
lifecycle {
375+
precondition {
376+
condition = anytrue([
377+
(var.enable_gitlab_backup_to_s3 == false),
378+
(var.enable_gitlab_backup_to_s3 == true && var.gitlab_backup_bucket_name != null)
379+
])
380+
error_message = "Gitlab backup to S3 is set to ${var.enable_gitlab_backup_to_s3}. gitlab_backup_bucket_name is mandatory to create S3 bucket."
381+
}
382+
383+
}
384+
}
385+
386+
resource "aws_s3_bucket_acl" "gitlab_backup" {
387+
count = var.enable_gitlab_backup_to_s3 ? 1 : 0
388+
bucket = aws_s3_bucket.gitlab_backup[0].id
389+
acl = "private"
390+
}
391+
392+
data "aws_iam_policy_document" "gitlab_s3_backup" {
393+
count = var.enable_gitlab_backup_to_s3 ? 1 : 0
394+
statement {
395+
effect = "Allow"
396+
actions = [
397+
"s3:AbortMultipartUpload",
398+
"s3:GetBucketAcl",
399+
"s3:GetBucketLocation",
400+
"s3:GetObject",
401+
"s3:GetObjectAcl",
402+
"s3:ListBucketMultipartUploads",
403+
"s3:PutObject",
404+
"s3:PutObjectAcl"
405+
]
406+
resources = [
407+
"arn:aws:s3:::${aws_s3_bucket.gitlab_backup[0].bucket}/*"
408+
]
409+
}
410+
statement {
411+
effect = "Allow"
412+
actions = [
413+
"s3:GetBucketLocation",
414+
"s3:ListAllMyBuckets"
415+
]
416+
resources = [
417+
"*"
418+
]
419+
}
420+
statement {
421+
effect = "Allow"
422+
actions = [
423+
"s3:ListBucket"
424+
]
425+
resources = [
426+
"arn:aws:s3:::${aws_s3_bucket.gitlab_backup[0].bucket}"
427+
]
428+
}
429+
}
430+
431+
resource "aws_iam_policy" "gitlab_backup" {
432+
count = var.enable_gitlab_backup_to_s3 ? 1 : 0
433+
name = "gitlab-backup"
434+
policy = data.aws_iam_policy_document.gitlab_s3_backup[0].json
435+
}
436+
437+
resource "aws_iam_role" "gitlab_backup" {
438+
name = "gitlab-backup"
439+
assume_role_policy = <<EOF
440+
{
441+
"Version": "2012-10-17",
442+
"Statement": [
443+
{
444+
"Action": "sts:AssumeRole",
445+
"Principal": {
446+
"Service": "ec2.amazonaws.com"
447+
},
448+
"Effect": "Allow",
449+
"Sid": ""
450+
}
451+
]
452+
}
453+
EOF
454+
managed_policy_arns = var.enable_gitlab_backup_to_s3 ? [aws_iam_policy.gitlab_backup[0].arn] : []
455+
}
456+
457+
resource "aws_iam_instance_profile" "gitlab" {
458+
name = "gitlab"
459+
role = aws_iam_role.gitlab_backup.name
460+
}
461+
462+
resource "local_file" "gitlab_config_file" {
463+
filename = local.gitlab_config_generated_file
464+
content = templatefile(local.gitlab_config_template_file, {
465+
gitlab_url = local.gitlab_complete_url,
466+
gitlab_db_name = module.gitlab_pg.db_instance_name,
467+
gitlab_db_username = module.gitlab_pg.db_instance_username,
468+
gitlab_db_password = module.gitlab_pg.db_instance_password,
469+
gitlab_db_host = module.gitlab_pg.db_instance_address,
470+
gitlab_redis_host = aws_elasticache_cluster.gitlab_redis.cache_nodes[0].address,
471+
aws_region = aws_s3_bucket.gitlab_backup[0].region
472+
gitlab_backup_s3_bucket_name = aws_s3_bucket.gitlab_backup[0].bucket
473+
})
474+
}

outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ output "gitlab_redis_address" {
3737
value = aws_elasticache_cluster.gitlab_redis.cache_nodes[0].address
3838
description = "Gitlab Redis cluster address"
3939
}
40+
41+
output "gitlab_complete_url" {
42+
value = local.gitlab_complete_url
43+
}

playbooks/gitlab_setup.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
- name: Configure Gitlab
3+
hosts: "{{ instance_ip_address }}"
4+
gather_facts: no
5+
vars:
6+
ansible_host_key_checking: false
7+
tasks:
8+
- local_action: wait_for port=22 host="{{ instance_ip_address }}" delay=10 timeout=300
9+
- name: copy gitlab.rb to /etc/gitlab/
10+
become: true
11+
copy:
12+
src: "{{ file_path }}"
13+
dest: "/etc/gitlab/gitlab.rb"
14+
owner: "root"
15+
group: "root"
16+
mode: 0600
17+
- name: reconfigure Gitlab
18+
become: true
19+
command: gitlab-ctl reconfigure

variables.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,20 @@ variable "gitlab_redis_parameter_group" {
256256
family = null
257257
}
258258
}
259+
260+
variable "enable_gitlab_backup_to_s3" {
261+
type = bool
262+
default = false
263+
description = "Enable Gitlab backup on S3 bucket"
264+
}
265+
266+
variable "gitlab_backup_bucket_name" {
267+
type = string
268+
default = null
269+
description = "Name of S3 bucket to be used for Gitlab backup"
270+
}
271+
272+
variable "private_key" {
273+
type = string
274+
description = "Private key to execute ansible playbook on Gitlab instance."
275+
}

0 commit comments

Comments
 (0)