Skip to content

Commit 0d69e4d

Browse files
authored
Merge pull request #4 from infraspecdev/refactor-module
Refactor module
2 parents ae92f12 + c9bb7ce commit 0d69e4d

File tree

18 files changed

+514
-477
lines changed

18 files changed

+514
-477
lines changed

backup.tf

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* Resources for setting up Gitlab remote backup on Amazon S3 */
2+
locals {
3+
gitlab_backup_iam_policy_name = "${local.environment_prefix}-gitlab-backup"
4+
gitlab_backup_iam_role_name = "${local.environment_prefix}-gitlab-backup"
5+
}
6+
resource "aws_s3_bucket" "gitlab_backup" {
7+
count = var.enable_gitlab_backup_to_s3 ? 1 : 0
8+
bucket = var.gitlab_backup_bucket_name
9+
10+
tags = merge(local.default_tags, var.additional_tags)
11+
12+
lifecycle {
13+
precondition {
14+
condition = anytrue([
15+
(var.enable_gitlab_backup_to_s3 == false),
16+
(var.enable_gitlab_backup_to_s3 == true && var.gitlab_backup_bucket_name != null)
17+
])
18+
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."
19+
}
20+
}
21+
}
22+
23+
resource "aws_s3_bucket_acl" "gitlab_backup" {
24+
count = var.enable_gitlab_backup_to_s3 ? 1 : 0
25+
bucket = aws_s3_bucket.gitlab_backup[0].id
26+
acl = "private"
27+
}
28+
29+
data "aws_iam_policy_document" "gitlab_s3_backup" {
30+
count = var.enable_gitlab_backup_to_s3 ? 1 : 0
31+
statement {
32+
effect = "Allow"
33+
actions = [
34+
"s3:AbortMultipartUpload",
35+
"s3:GetBucketAcl",
36+
"s3:GetBucketLocation",
37+
"s3:GetObject",
38+
"s3:GetObjectAcl",
39+
"s3:ListBucketMultipartUploads",
40+
"s3:PutObject",
41+
"s3:PutObjectAcl"
42+
]
43+
resources = [
44+
"arn:aws:s3:::${aws_s3_bucket.gitlab_backup[0].bucket}/*"
45+
]
46+
}
47+
statement {
48+
effect = "Allow"
49+
actions = [
50+
"s3:GetBucketLocation",
51+
"s3:ListAllMyBuckets"
52+
]
53+
resources = [
54+
"*"
55+
]
56+
}
57+
statement {
58+
effect = "Allow"
59+
actions = [
60+
"s3:ListBucket"
61+
]
62+
resources = [
63+
"arn:aws:s3:::${aws_s3_bucket.gitlab_backup[0].bucket}"
64+
]
65+
}
66+
}
67+
68+
resource "aws_iam_policy" "gitlab_backup" {
69+
count = var.enable_gitlab_backup_to_s3 ? 1 : 0
70+
name = local.gitlab_backup_iam_policy_name
71+
policy = data.aws_iam_policy_document.gitlab_s3_backup[0].json
72+
tags = merge({
73+
Name = local.gitlab_backup_iam_policy_name
74+
}, local.default_tags, var.additional_tags)
75+
}
76+
77+
resource "aws_iam_role" "gitlab_backup" {
78+
name = local.gitlab_backup_iam_role_name
79+
assume_role_policy = <<EOF
80+
{
81+
"Version": "2012-10-17",
82+
"Statement": [
83+
{
84+
"Action": "sts:AssumeRole",
85+
"Principal": {
86+
"Service": "ec2.amazonaws.com"
87+
},
88+
"Effect": "Allow",
89+
"Sid": ""
90+
}
91+
]
92+
}
93+
EOF
94+
managed_policy_arns = var.enable_gitlab_backup_to_s3 ? [aws_iam_policy.gitlab_backup[0].arn] : []
95+
tags = merge({
96+
Name = local.gitlab_backup_iam_role_name
97+
}, local.default_tags, var.additional_tags)
98+
}

config.tf

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* Resources for management of gitlab.rb from within the terraform module itself using Ansible playbooks */
2+
3+
locals {
4+
gitlab_config_file_name = "gitlab.rb"
5+
rendered_gitlab_config_file_name = "gitlab_rendered.rb"
6+
gitlab_additional_config_file_name = "gitlab_additional.rb"
7+
gitlab_config_tmp_path = "/tmp/gitlab/gitlab_config"
8+
gitlab_config_template_file_path = "${path.module}/templates"
9+
gitlab_config_file_path = "${path.cwd}/gitlab_config"
10+
gitlab_config_playbook_file = "${path.module}/playbooks/gitlab_setup.yaml"
11+
gitlab_complete_url = join("", tolist(["https://", values(module.records.route53_record_name)[0]]))
12+
}
13+
14+
data "template_file" "gitlab_config_template" {
15+
template = join("\n", [
16+
file("${local.gitlab_config_template_file_path}/postgres.tftpl"),
17+
file("${local.gitlab_config_template_file_path}/redis.tftpl"),
18+
file("${local.gitlab_config_template_file_path}/nginx.tftpl"),
19+
file("${local.gitlab_config_template_file_path}/rails.tftpl"),
20+
var.create_ses_identity ? file("${local.gitlab_config_template_file_path}/smtp.tftpl") : "",
21+
])
22+
vars = merge({
23+
gitlab_url = local.gitlab_complete_url,
24+
gitlab_db_name = module.gitlab_pg.db_instance_name,
25+
gitlab_db_username = module.gitlab_pg.db_instance_username,
26+
gitlab_db_password = module.gitlab_pg.db_instance_password,
27+
gitlab_db_host = module.gitlab_pg.db_instance_address,
28+
gitlab_redis_host = aws_elasticache_cluster.gitlab_redis.cache_nodes[0].address,
29+
aws_region = aws_s3_bucket.gitlab_backup[0].region,
30+
gitlab_backup_s3_bucket_name = aws_s3_bucket.gitlab_backup[0].bucket
31+
}, var.create_ses_identity ? {
32+
smtp_address = "email-smtp.${var.aws_region}.amazonaws.com",
33+
smtp_username = aws_iam_access_key.gitlab_smtp_user[0].id,
34+
smtp_password = aws_iam_access_key.gitlab_smtp_user[0].ses_smtp_password_v4,
35+
smtp_domain = data.aws_route53_zone.email_domain[0].name
36+
} : {})
37+
}
38+
39+
resource "local_sensitive_file" "rendered_gitlab_config_file" {
40+
filename = "${local.gitlab_config_tmp_path}/${local.rendered_gitlab_config_file_name}"
41+
content = data.template_file.gitlab_config_template.rendered
42+
}
43+
44+
data "local_sensitive_file" "gitlab_additional_config" {
45+
count = fileexists("${local.gitlab_config_file_path}/${local.gitlab_additional_config_file_name}") ? 1 : 0
46+
filename = "${local.gitlab_config_file_path}/${local.gitlab_additional_config_file_name}"
47+
}
48+
49+
resource "local_sensitive_file" "gitlab_config_file" {
50+
filename = "${local.gitlab_config_tmp_path}/${local.gitlab_config_file_name}"
51+
content = join("\n", tolist([
52+
data.template_file.gitlab_config_template.rendered,
53+
data.local_sensitive_file.gitlab_additional_config != [] ? data.local_sensitive_file.gitlab_additional_config[0].content : ""
54+
]))
55+
}
56+
57+
/*
58+
Adding null_resource trigger on timestamp is a hack to always check the diff in the
59+
config if any and apply the config changes to Gitlab.
60+
*/
61+
resource "null_resource" "gitlab_reconfigure" {
62+
triggers = {
63+
timestamp = timestamp()
64+
}
65+
provisioner "local-exec" {
66+
command = "ansible-playbook -u ubuntu -i '${aws_instance.gitlab.private_ip},' --private-key ${var.private_key} -e 'instance_ip_address=${aws_instance.gitlab.private_ip} workdir=${local.gitlab_config_tmp_path} config_file=${local_sensitive_file.gitlab_config_file.filename}' ${local.gitlab_config_playbook_file}"
67+
}
68+
}

gitlab_config_templates/gitlab-nginx.rb.tftpl

Lines changed: 0 additions & 3 deletions
This file was deleted.

gitlab_config_templates/gitlab-postgres.tftpl

Lines changed: 0 additions & 1 deletion
This file was deleted.

gitlab_config_templates/gitlab-rails.tftpl

Lines changed: 0 additions & 22 deletions
This file was deleted.

gitlab_config_templates/gitlab-redis.tftpl

Lines changed: 0 additions & 1 deletion
This file was deleted.

load_balancers.tf

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* Resources for Gitlab classic load balancer */
2+
locals {
3+
gitlab_lb_sg_name = "${local.environment_prefix}-gitlab-lb"
4+
gitlab_lb_name = "${local.environment_prefix}-gitlab"
5+
}
6+
resource "aws_security_group" "gitlab_lb" {
7+
name = local.gitlab_lb_sg_name
8+
vpc_id = data.aws_vpc.vpc.id
9+
description = "Security group for Gitlab load balancer"
10+
ingress = [
11+
{
12+
from_port = 80
13+
protocol = "tcp"
14+
to_port = 80
15+
cidr_blocks = ["0.0.0.0/0"]
16+
ipv6_cidr_blocks = ["::/0"]
17+
prefix_list_ids = []
18+
security_groups = []
19+
self = false
20+
description = "allow http ingress from anywhere"
21+
},
22+
{
23+
from_port = 443
24+
protocol = "tcp"
25+
to_port = 443
26+
cidr_blocks = ["0.0.0.0/0"]
27+
ipv6_cidr_blocks = ["::/0"]
28+
prefix_list_ids = []
29+
security_groups = []
30+
self = false
31+
description = "allow https ingress from anywhere"
32+
},
33+
{
34+
from_port = 22
35+
protocol = "tcp"
36+
to_port = 22
37+
cidr_blocks = ["0.0.0.0/0"]
38+
ipv6_cidr_blocks = ["::/0"]
39+
prefix_list_ids = []
40+
security_groups = []
41+
self = false
42+
description = "allow SSH ingress from anywhere"
43+
}
44+
]
45+
egress = [
46+
{
47+
from_port = 0
48+
protocol = "-1"
49+
to_port = 0
50+
cidr_blocks = ["0.0.0.0/0"]
51+
ipv6_cidr_blocks = ["::/0"]
52+
prefix_list_ids = []
53+
security_groups = []
54+
self = false
55+
description = "allow all egress"
56+
}
57+
]
58+
tags = merge({
59+
Name = local.gitlab_lb_sg_name
60+
}, local.default_tags, var.additional_tags)
61+
}
62+
63+
module "elb" {
64+
source = "terraform-aws-modules/elb/aws"
65+
version = "~> 2.0"
66+
name = local.gitlab_lb_name
67+
subnets = var.public_subnet_ids
68+
security_groups = [aws_security_group.gitlab_lb.id]
69+
internal = false
70+
listener = [
71+
{
72+
instance_port = 80
73+
instance_protocol = "HTTP"
74+
lb_port = 80
75+
lb_protocol = "HTTP"
76+
},
77+
{
78+
instance_port = 80
79+
instance_protocol = "HTTP"
80+
lb_port = 443
81+
lb_protocol = "HTTPS"
82+
ssl_certificate_id = var.create_acm_certificate ? module.acm.acm_certificate_arn : var.acm_certificate_arn
83+
},
84+
{
85+
instance_port = 22
86+
instance_protocol = "TCP"
87+
lb_port = 22
88+
lb_protocol = "TCP"
89+
},
90+
]
91+
health_check = {
92+
target = "${var.healthcheck_protocol}:${var.healthcheck_port}${var.healthcheck_path}"
93+
interval = var.healthcheck_interval
94+
healthy_threshold = var.healthcheck_healthy_threshold
95+
unhealthy_threshold = var.healthcheck_unhealthy_threshold
96+
timeout = var.healthcheck_timeout
97+
}
98+
number_of_instances = 1
99+
instances = tolist([aws_instance.gitlab.id])
100+
tags = merge({
101+
Name = local.gitlab_lb_name
102+
}, local.default_tags, var.additional_tags)
103+
}

0 commit comments

Comments
 (0)