-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor module #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
24b5ef3
remove count 1 from gitlab EC2 instance
b808416
add managed_by and environment to default tags. add environment prefi…
bb20397
add support for additional tags
8fe083d
add configurable EBS volume IOPS with default to 3000
562cefa
add tags for ses resources
89134cd
cleanup main.tf by separating resources logically by functionality
c9bb7ce
move resource name evaluation to locals
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* Resources for setting up Gitlab remote backup on Amazon S3 */ | ||
| locals { | ||
| gitlab_backup_iam_policy_name = "${local.environment_prefix}-gitlab-backup" | ||
| gitlab_backup_iam_role_name = "${local.environment_prefix}-gitlab-backup" | ||
| } | ||
| resource "aws_s3_bucket" "gitlab_backup" { | ||
| count = var.enable_gitlab_backup_to_s3 ? 1 : 0 | ||
| bucket = var.gitlab_backup_bucket_name | ||
|
|
||
| tags = merge(local.default_tags, var.additional_tags) | ||
|
|
||
| lifecycle { | ||
| precondition { | ||
| condition = anytrue([ | ||
| (var.enable_gitlab_backup_to_s3 == false), | ||
| (var.enable_gitlab_backup_to_s3 == true && var.gitlab_backup_bucket_name != null) | ||
| ]) | ||
| 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." | ||
| } | ||
| } | ||
| } | ||
|
|
||
| resource "aws_s3_bucket_acl" "gitlab_backup" { | ||
| count = var.enable_gitlab_backup_to_s3 ? 1 : 0 | ||
| bucket = aws_s3_bucket.gitlab_backup[0].id | ||
| acl = "private" | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "gitlab_s3_backup" { | ||
| count = var.enable_gitlab_backup_to_s3 ? 1 : 0 | ||
| statement { | ||
| effect = "Allow" | ||
| actions = [ | ||
| "s3:AbortMultipartUpload", | ||
| "s3:GetBucketAcl", | ||
| "s3:GetBucketLocation", | ||
| "s3:GetObject", | ||
| "s3:GetObjectAcl", | ||
| "s3:ListBucketMultipartUploads", | ||
| "s3:PutObject", | ||
| "s3:PutObjectAcl" | ||
| ] | ||
| resources = [ | ||
| "arn:aws:s3:::${aws_s3_bucket.gitlab_backup[0].bucket}/*" | ||
| ] | ||
| } | ||
| statement { | ||
| effect = "Allow" | ||
| actions = [ | ||
| "s3:GetBucketLocation", | ||
| "s3:ListAllMyBuckets" | ||
| ] | ||
| resources = [ | ||
| "*" | ||
| ] | ||
| } | ||
| statement { | ||
| effect = "Allow" | ||
| actions = [ | ||
| "s3:ListBucket" | ||
| ] | ||
| resources = [ | ||
| "arn:aws:s3:::${aws_s3_bucket.gitlab_backup[0].bucket}" | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| resource "aws_iam_policy" "gitlab_backup" { | ||
| count = var.enable_gitlab_backup_to_s3 ? 1 : 0 | ||
| name = local.gitlab_backup_iam_policy_name | ||
| policy = data.aws_iam_policy_document.gitlab_s3_backup[0].json | ||
| tags = merge({ | ||
| Name = local.gitlab_backup_iam_policy_name | ||
| }, local.default_tags, var.additional_tags) | ||
| } | ||
|
|
||
| resource "aws_iam_role" "gitlab_backup" { | ||
| name = local.gitlab_backup_iam_role_name | ||
| assume_role_policy = <<EOF | ||
| { | ||
| "Version": "2012-10-17", | ||
| "Statement": [ | ||
| { | ||
| "Action": "sts:AssumeRole", | ||
| "Principal": { | ||
| "Service": "ec2.amazonaws.com" | ||
| }, | ||
| "Effect": "Allow", | ||
| "Sid": "" | ||
| } | ||
| ] | ||
| } | ||
| EOF | ||
| managed_policy_arns = var.enable_gitlab_backup_to_s3 ? [aws_iam_policy.gitlab_backup[0].arn] : [] | ||
| tags = merge({ | ||
| Name = local.gitlab_backup_iam_role_name | ||
| }, local.default_tags, var.additional_tags) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* Resources for management of gitlab.rb from within the terraform module itself using Ansible playbooks */ | ||
|
|
||
| locals { | ||
| gitlab_config_file_name = "gitlab.rb" | ||
| rendered_gitlab_config_file_name = "gitlab_rendered.rb" | ||
| gitlab_additional_config_file_name = "gitlab_additional.rb" | ||
| gitlab_config_tmp_path = "/tmp/gitlab/gitlab_config" | ||
| gitlab_config_template_file_path = "${path.module}/templates" | ||
| gitlab_config_file_path = "${path.cwd}/gitlab_config" | ||
| gitlab_config_playbook_file = "${path.module}/playbooks/gitlab_setup.yaml" | ||
| gitlab_complete_url = join("", tolist(["https://", values(module.records.route53_record_name)[0]])) | ||
| } | ||
|
|
||
| data "template_file" "gitlab_config_template" { | ||
| template = join("\n", [ | ||
| file("${local.gitlab_config_template_file_path}/postgres.tftpl"), | ||
| file("${local.gitlab_config_template_file_path}/redis.tftpl"), | ||
| file("${local.gitlab_config_template_file_path}/nginx.tftpl"), | ||
| file("${local.gitlab_config_template_file_path}/rails.tftpl"), | ||
| var.create_ses_identity ? file("${local.gitlab_config_template_file_path}/smtp.tftpl") : "", | ||
| ]) | ||
| vars = merge({ | ||
| gitlab_url = local.gitlab_complete_url, | ||
| gitlab_db_name = module.gitlab_pg.db_instance_name, | ||
| gitlab_db_username = module.gitlab_pg.db_instance_username, | ||
| gitlab_db_password = module.gitlab_pg.db_instance_password, | ||
| gitlab_db_host = module.gitlab_pg.db_instance_address, | ||
| gitlab_redis_host = aws_elasticache_cluster.gitlab_redis.cache_nodes[0].address, | ||
| aws_region = aws_s3_bucket.gitlab_backup[0].region, | ||
| gitlab_backup_s3_bucket_name = aws_s3_bucket.gitlab_backup[0].bucket | ||
| }, var.create_ses_identity ? { | ||
| smtp_address = "email-smtp.${var.aws_region}.amazonaws.com", | ||
| smtp_username = aws_iam_access_key.gitlab_smtp_user[0].id, | ||
| smtp_password = aws_iam_access_key.gitlab_smtp_user[0].ses_smtp_password_v4, | ||
| smtp_domain = data.aws_route53_zone.email_domain[0].name | ||
| } : {}) | ||
| } | ||
|
|
||
| resource "local_sensitive_file" "rendered_gitlab_config_file" { | ||
| filename = "${local.gitlab_config_tmp_path}/${local.rendered_gitlab_config_file_name}" | ||
| content = data.template_file.gitlab_config_template.rendered | ||
| } | ||
|
|
||
| data "local_sensitive_file" "gitlab_additional_config" { | ||
| count = fileexists("${local.gitlab_config_file_path}/${local.gitlab_additional_config_file_name}") ? 1 : 0 | ||
| filename = "${local.gitlab_config_file_path}/${local.gitlab_additional_config_file_name}" | ||
| } | ||
|
|
||
| resource "local_sensitive_file" "gitlab_config_file" { | ||
| filename = "${local.gitlab_config_tmp_path}/${local.gitlab_config_file_name}" | ||
| content = join("\n", tolist([ | ||
| data.template_file.gitlab_config_template.rendered, | ||
| data.local_sensitive_file.gitlab_additional_config != [] ? data.local_sensitive_file.gitlab_additional_config[0].content : "" | ||
| ])) | ||
| } | ||
|
|
||
| /* | ||
| Adding null_resource trigger on timestamp is a hack to always check the diff in the | ||
| config if any and apply the config changes to Gitlab. | ||
| */ | ||
| resource "null_resource" "gitlab_reconfigure" { | ||
| triggers = { | ||
| timestamp = timestamp() | ||
| } | ||
| provisioner "local-exec" { | ||
| 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}" | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* Resources for Gitlab classic load balancer */ | ||
| locals { | ||
| gitlab_lb_sg_name = "${local.environment_prefix}-gitlab-lb" | ||
| gitlab_lb_name = "${local.environment_prefix}-gitlab" | ||
| } | ||
| resource "aws_security_group" "gitlab_lb" { | ||
| name = local.gitlab_lb_sg_name | ||
| vpc_id = data.aws_vpc.vpc.id | ||
| description = "Security group for Gitlab load balancer" | ||
| ingress = [ | ||
| { | ||
| from_port = 80 | ||
| protocol = "tcp" | ||
| to_port = 80 | ||
| cidr_blocks = ["0.0.0.0/0"] | ||
| ipv6_cidr_blocks = ["::/0"] | ||
| prefix_list_ids = [] | ||
| security_groups = [] | ||
| self = false | ||
| description = "allow http ingress from anywhere" | ||
| }, | ||
| { | ||
| from_port = 443 | ||
| protocol = "tcp" | ||
| to_port = 443 | ||
| cidr_blocks = ["0.0.0.0/0"] | ||
| ipv6_cidr_blocks = ["::/0"] | ||
| prefix_list_ids = [] | ||
| security_groups = [] | ||
| self = false | ||
| description = "allow https ingress from anywhere" | ||
| }, | ||
| { | ||
| from_port = 22 | ||
| protocol = "tcp" | ||
| to_port = 22 | ||
| cidr_blocks = ["0.0.0.0/0"] | ||
| ipv6_cidr_blocks = ["::/0"] | ||
| prefix_list_ids = [] | ||
| security_groups = [] | ||
| self = false | ||
| description = "allow SSH ingress from anywhere" | ||
| } | ||
| ] | ||
| egress = [ | ||
| { | ||
| from_port = 0 | ||
| protocol = "-1" | ||
| to_port = 0 | ||
| cidr_blocks = ["0.0.0.0/0"] | ||
| ipv6_cidr_blocks = ["::/0"] | ||
| prefix_list_ids = [] | ||
| security_groups = [] | ||
| self = false | ||
| description = "allow all egress" | ||
| } | ||
| ] | ||
| tags = merge({ | ||
| Name = local.gitlab_lb_sg_name | ||
| }, local.default_tags, var.additional_tags) | ||
| } | ||
|
|
||
| module "elb" { | ||
| source = "terraform-aws-modules/elb/aws" | ||
| version = "~> 2.0" | ||
| name = local.gitlab_lb_name | ||
| subnets = var.public_subnet_ids | ||
| security_groups = [aws_security_group.gitlab_lb.id] | ||
| internal = false | ||
| listener = [ | ||
| { | ||
| instance_port = 80 | ||
| instance_protocol = "HTTP" | ||
| lb_port = 80 | ||
| lb_protocol = "HTTP" | ||
| }, | ||
| { | ||
| instance_port = 80 | ||
| instance_protocol = "HTTP" | ||
| lb_port = 443 | ||
| lb_protocol = "HTTPS" | ||
| ssl_certificate_id = var.create_acm_certificate ? module.acm.acm_certificate_arn : var.acm_certificate_arn | ||
| }, | ||
| { | ||
| instance_port = 22 | ||
| instance_protocol = "TCP" | ||
| lb_port = 22 | ||
| lb_protocol = "TCP" | ||
| }, | ||
| ] | ||
| health_check = { | ||
| target = "${var.healthcheck_protocol}:${var.healthcheck_port}${var.healthcheck_path}" | ||
| interval = var.healthcheck_interval | ||
| healthy_threshold = var.healthcheck_healthy_threshold | ||
| unhealthy_threshold = var.healthcheck_unhealthy_threshold | ||
| timeout = var.healthcheck_timeout | ||
| } | ||
| number_of_instances = 1 | ||
| instances = tolist([aws_instance.gitlab.id]) | ||
| tags = merge({ | ||
| Name = local.gitlab_lb_name | ||
| }, local.default_tags, var.additional_tags) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.