Skip to content

Commit 03ae095

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

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

main.tf

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ resource "aws_instance" "gitlab" {
1010
associate_public_ip_address = false
1111
vpc_security_group_ids = [aws_security_group.gitlab.id]
1212
key_name = var.gitlab_ssh_public_key != null ? aws_key_pair.gitlab_ssh[0].key_name : null
13+
iam_instance_profile = aws_iam_instance_profile.gitlab.name
1314
root_block_device {
1415
volume_type = var.volume_type
1516
volume_size = var.volume_size
@@ -357,3 +358,94 @@ resource "aws_security_group" "gitlab_redis" {
357358
ManagedBy = local.managed_by
358359
}
359360
}
361+
362+
resource "aws_s3_bucket" "gitlab_backup" {
363+
count = var.enable_gitlab_backup_to_s3 ? 1 : 0
364+
bucket = var.gitlab_backup_bucket_name
365+
lifecycle {
366+
precondition {
367+
condition = anytrue([
368+
(var.enable_gitlab_backup_to_s3 == false),
369+
(var.enable_gitlab_backup_to_s3 == true && var.gitlab_backup_bucket_name != null)
370+
])
371+
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."
372+
}
373+
374+
}
375+
}
376+
377+
resource "aws_s3_bucket_acl" "gitlab_backup" {
378+
count = var.enable_gitlab_backup_to_s3 ? 1 : 0
379+
bucket = aws_s3_bucket.gitlab_backup[0].id
380+
acl = "private"
381+
}
382+
383+
data "aws_iam_policy_document" "gitlab_s3_backup" {
384+
count = var.enable_gitlab_backup_to_s3 ? 1 : 0
385+
statement {
386+
effect = "Allow"
387+
actions = [
388+
"s3:AbortMultipartUpload",
389+
"s3:GetBucketAcl",
390+
"s3:GetBucketLocation",
391+
"s3:GetObject",
392+
"s3:GetObjectAcl",
393+
"s3:ListBucketMultipartUploads",
394+
"s3:PutObject",
395+
"s3:PutObjectAcl"
396+
]
397+
resources = [
398+
"arn:aws:s3:::${aws_s3_bucket.gitlab_backup[0].bucket}/*"
399+
]
400+
}
401+
statement {
402+
effect = "Allow"
403+
actions = [
404+
"s3:GetBucketLocation",
405+
"s3:ListAllMyBuckets"
406+
]
407+
resources = [
408+
"*"
409+
]
410+
}
411+
statement {
412+
effect = "Allow"
413+
actions = [
414+
"s3:ListBucket"
415+
]
416+
resources = [
417+
"arn:aws:s3:::${aws_s3_bucket.gitlab_backup[0].bucket}"
418+
]
419+
}
420+
}
421+
422+
resource "aws_iam_policy" "gitlab_backup" {
423+
count = var.enable_gitlab_backup_to_s3 ? 1 : 0
424+
name = "gitlab-backup"
425+
policy = data.aws_iam_policy_document.gitlab_s3_backup[0].json
426+
}
427+
428+
resource "aws_iam_role" "gitlab_backup" {
429+
name = "gitlab-backup"
430+
assume_role_policy = <<EOF
431+
{
432+
"Version": "2012-10-17",
433+
"Statement": [
434+
{
435+
"Action": "sts:AssumeRole",
436+
"Principal": {
437+
"Service": "ec2.amazonaws.com"
438+
},
439+
"Effect": "Allow",
440+
"Sid": ""
441+
}
442+
]
443+
}
444+
EOF
445+
managed_policy_arns = var.enable_gitlab_backup_to_s3 ? [aws_iam_policy.gitlab_backup[0].arn] : []
446+
}
447+
448+
resource "aws_iam_instance_profile" "gitlab" {
449+
name = "gitlab"
450+
role = aws_iam_role.gitlab_backup.name
451+
}

variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,15 @@ 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+
}

0 commit comments

Comments
 (0)