11locals {
2- managed_by = " Terraform"
2+ default_tags = {
3+ managed_by = " Terraform"
4+ environment = var.environment
5+ }
6+ environment_prefix = substr (var. environment , 0 , 1 )
37 gitlab_config_file_name = " gitlab.rb"
48 rendered_gitlab_config_file_name = " gitlab_rendered.rb"
59 gitlab_additional_config_file_name = " gitlab_additional.rb"
@@ -24,18 +28,19 @@ resource "aws_instance" "gitlab" {
2428 delete_on_termination = false
2529 }
2630
27- tags = {
28- Name = " ${ var . environment_prefix } -gitlab"
29- Environment = var.environment_prefix
30- ManagedBy = local.managed_by
31- }
31+ tags = merge ({
32+ Name = " ${ local . environment_prefix } -gitlab"
33+ }, local. default_tags )
3234
3335}
3436
3537resource "aws_key_pair" "gitlab_ssh" {
3638 count = var. gitlab_ssh_public_key != null ? 1 : 0
37- key_name = " ${ var . environment_prefix } -gitlab-key-pair"
39+ key_name = " ${ local . environment_prefix } -gitlab-key-pair"
3840 public_key = var. gitlab_ssh_public_key
41+ tags = merge ({
42+ Name = " ${ local . environment_prefix } -gitlab-key-pair"
43+ }, local. default_tags )
3944}
4045
4146data "aws_vpc" "vpc" {
@@ -47,7 +52,7 @@ data "aws_route53_zone" "zone" {
4752}
4853
4954resource "aws_security_group" "gitlab" {
50- name = " ${ var . environment_prefix } -gitlab"
55+ name = " ${ local . environment_prefix } -gitlab"
5156 vpc_id = data. aws_vpc . vpc . id
5257 description = " Security group for Gitlab instance"
5358 ingress = [
@@ -98,14 +103,13 @@ resource "aws_security_group" "gitlab" {
98103 description = " allow all egress"
99104 }
100105 ]
101- tags = {
102- Environment = var.environment_prefix
103- ManagedBy = local.managed_by
104- }
106+ tags = merge ({
107+ Name = " ${ local . environment_prefix } -gitlab"
108+ }, local. default_tags )
105109}
106110
107111resource "aws_security_group" "gitlab_lb" {
108- name = " ${ var . environment_prefix } -gitlab-lb"
112+ name = " ${ local . environment_prefix } -gitlab-lb"
109113 vpc_id = data. aws_vpc . vpc . id
110114 description = " Security group for Gitlab load balancer"
111115 ingress = [
@@ -156,10 +160,9 @@ resource "aws_security_group" "gitlab_lb" {
156160 description = " allow all egress"
157161 }
158162 ]
159- tags = {
160- Environment = var.environment_prefix
161- ManagedBy = local.managed_by
162- }
163+ tags = merge ({
164+ Name = " ${ local . environment_prefix } -gitlab-lb"
165+ }, local. default_tags )
163166}
164167
165168module "records" {
@@ -189,16 +192,16 @@ module "acm" {
189192
190193 wait_for_validation = true
191194
192- tags = {
195+ tags = merge ( {
193196 Name = var.gitlab_domain
194- }
197+ }, local . default_tags )
195198}
196199
197200module "elb" {
198201 source = " terraform-aws-modules/elb/aws"
199202 version = " ~> 2.0"
200203
201- name = " ${ var . environment_prefix } -gitlab"
204+ name = " ${ local . environment_prefix } -gitlab"
202205
203206 subnets = var. public_subnet_ids
204207 security_groups = [aws_security_group . gitlab_lb . id ]
@@ -236,20 +239,20 @@ module "elb" {
236239 number_of_instances = 1
237240 instances = tolist ([aws_instance . gitlab . id ])
238241
239- tags = {
240- Environment = var .environment_prefix
241- }
242+ tags = merge ( {
243+ Name = " ${ local . environment_prefix } -gitlab "
244+ }, local . default_tags )
242245}
243246
244247module "gitlab_pg" {
245248 source = " terraform-aws-modules/rds/aws"
246- identifier = " ${ var . environment_prefix } -gitlab-pg"
249+ identifier = " ${ local . environment_prefix } -gitlab-pg"
247250 create_db_instance = true
248251 create_db_subnet_group = true
249252 create_db_parameter_group = var. gitlab_pg_create_db_parameter_group
250253 parameter_group_name = var. gitlab_pg_parameter_group_name
251254 parameters = var. gitlab_pg_parameters
252- db_subnet_group_name = " ${ var . environment_prefix } -gitlab-pg"
255+ db_subnet_group_name = " ${ var . environment } -gitlab-pg"
253256 subnet_ids = var. gitlab_pg_subnet_ids
254257 allocated_storage = var. gitlab_pg_allocated_storage
255258 storage_type = var. gitlab_pg_storage_type
@@ -263,10 +266,13 @@ module "gitlab_pg" {
263266 create_random_password = false
264267 publicly_accessible = var. gitlab_pg_publicly_accessible
265268 vpc_security_group_ids = [aws_security_group . gitlab_rds . id ]
269+ tags = merge ({
270+ Name = " ${ local . environment_prefix } -gitlab-pg"
271+ }, local. default_tags )
266272}
267273
268274resource "aws_security_group" "gitlab_rds" {
269- name = " ${ var . environment_prefix } -gitlab-rds"
275+ name = " ${ local . environment_prefix } -gitlab-rds"
270276 vpc_id = data. aws_vpc . vpc . id
271277 description = " Security group for Gitlab RDS"
272278 ingress = [
@@ -282,14 +288,13 @@ resource "aws_security_group" "gitlab_rds" {
282288 description = " allow TCP access from Gitlab instance"
283289 }
284290 ]
285- tags = {
286- Environment = var.environment_prefix
287- ManagedBy = local.managed_by
288- }
291+ tags = merge ({
292+ Name = " ${ local . environment_prefix } -gitlab-rds"
293+ }, local. default_tags )
289294}
290295
291296resource "aws_elasticache_cluster" "gitlab_redis" {
292- cluster_id = " ${ var . environment_prefix } -gitlab-redis"
297+ cluster_id = " ${ local . environment_prefix } -gitlab-redis"
293298 engine = " redis"
294299 node_type = var. gitlab_redis_node_type
295300 num_cache_nodes = var. gitlab_redis_num_cache_nodes
@@ -299,6 +304,10 @@ resource "aws_elasticache_cluster" "gitlab_redis" {
299304 security_group_ids = [aws_security_group . gitlab_redis . id ]
300305 subnet_group_name = var. gitlab_redis_create_subnet_group == true ? aws_elasticache_subnet_group. gitlab_redis [0 ]. name : var. gitlab_redis_subnet_group_name
301306
307+ tags = merge ({
308+ Name = " ${ local . environment_prefix } -gitlab-redis"
309+ }, local. default_tags )
310+
302311 lifecycle {
303312 precondition {
304313 condition = anytrue ([
@@ -325,12 +334,13 @@ resource "aws_elasticache_parameter_group" "gitlab_redis" {
325334
326335resource "aws_elasticache_subnet_group" "gitlab_redis" {
327336 count = var. gitlab_redis_create_subnet_group == true ? 1 : 0
328- name = " ${ var . environment_prefix } -gitlab-redis"
337+ name = " ${ local . environment_prefix } -gitlab-redis"
329338 subnet_ids = var. gitlab_redis_subnet_ids
330- tags = {
331- Name = " ${ var . environment_prefix } -gitlab-redis"
332- ManagedBy = local.managed_by
333- }
339+
340+ tags = merge ({
341+ Name = " ${ local . environment_prefix } -gitlab-redis"
342+ }, local. default_tags )
343+
334344 lifecycle {
335345 precondition {
336346 condition = var. gitlab_redis_create_subnet_group && length (var. gitlab_redis_subnet_ids ) != 0
@@ -340,7 +350,7 @@ resource "aws_elasticache_subnet_group" "gitlab_redis" {
340350}
341351
342352resource "aws_security_group" "gitlab_redis" {
343- name = " ${ var . environment_prefix } -gitlab-redis"
353+ name = " ${ local . environment_prefix } -gitlab-redis"
344354 vpc_id = data. aws_vpc . vpc . id
345355 description = " Security group for Gitlab Redis"
346356 ingress = [
@@ -356,15 +366,19 @@ resource "aws_security_group" "gitlab_redis" {
356366 description = " allow TCP access from Gitlab instance"
357367 }
358368 ]
359- tags = {
360- Environment = var.environment_prefix
361- ManagedBy = local.managed_by
362- }
369+ tags = merge ({
370+ Name = " ${ local . environment_prefix } -gitlab-redis"
371+ }, local. default_tags )
363372}
364373
365374resource "aws_s3_bucket" "gitlab_backup" {
366375 count = var. enable_gitlab_backup_to_s3 ? 1 : 0
367- bucket = var. gitlab_backup_bucket_name
376+ bucket = " ${ local . environment_prefix } -${ var . gitlab_backup_bucket_name } "
377+
378+ tags = merge ({
379+ Name = " ${ local . environment_prefix } -${ var . gitlab_backup_bucket_name } "
380+ }, local. default_tags )
381+
368382 lifecycle {
369383 precondition {
370384 condition = anytrue ([
@@ -373,7 +387,6 @@ resource "aws_s3_bucket" "gitlab_backup" {
373387 ])
374388 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."
375389 }
376-
377390 }
378391}
379392
@@ -424,12 +437,15 @@ data "aws_iam_policy_document" "gitlab_s3_backup" {
424437
425438resource "aws_iam_policy" "gitlab_backup" {
426439 count = var. enable_gitlab_backup_to_s3 ? 1 : 0
427- name = " gitlab-backup"
440+ name = " ${ local . environment_prefix } - gitlab-backup"
428441 policy = data. aws_iam_policy_document . gitlab_s3_backup [0 ]. json
442+ tags = merge ({
443+ Name = " ${ local . environment_prefix } -gitlab-backup"
444+ }, local. default_tags )
429445}
430446
431447resource "aws_iam_role" "gitlab_backup" {
432- name = " gitlab-backup"
448+ name = " ${ local . environment_prefix } - gitlab-backup"
433449 assume_role_policy = << EOF
434450{
435451 "Version": "2012-10-17",
@@ -446,11 +462,17 @@ resource "aws_iam_role" "gitlab_backup" {
446462}
447463EOF
448464 managed_policy_arns = var. enable_gitlab_backup_to_s3 ? [aws_iam_policy . gitlab_backup [0 ]. arn ] : []
465+ tags = merge ({
466+ Name = " ${ local . environment_prefix } -gitlab-backup"
467+ }, local. default_tags )
449468}
450469
451470resource "aws_iam_instance_profile" "gitlab" {
452- name = " gitlab"
471+ name = " ${ local . environment_prefix } - gitlab"
453472 role = aws_iam_role. gitlab_backup . name
473+ tags = merge ({
474+ Name = " ${ local . environment_prefix } -gitlab"
475+ }, local. default_tags )
454476}
455477
456478data "template_file" "gitlab_config_template" {
0 commit comments