Skip to content

Commit ae92f12

Browse files
authored
Merge pull request #3 from infraspecdev/ses-smtp-support
add support for amazon SES as smtp service
2 parents fa031dd + a6c2b80 commit ae92f12

File tree

7 files changed

+129
-4
lines changed

7 files changed

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

gitlab_config_templates/smtp.tftpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
gitlab_rails['smtp_enable'] = true
2+
gitlab_rails['smtp_address'] = "${smtp_address}"
3+
gitlab_rails['smtp_port'] = 587
4+
gitlab_rails['smtp_user_name'] = "${smtp_username}"
5+
gitlab_rails['smtp_password'] = "${smtp_password}"
6+
gitlab_rails['smtp_domain'] = "${smtp_domain}"
7+
gitlab_rails['smtp_authentication'] = "login"
8+
gitlab_rails['smtp_enable_starttls_auto'] = true

main.tf

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,18 +456,27 @@ resource "aws_iam_instance_profile" "gitlab" {
456456

457457
data "template_file" "gitlab_config_template" {
458458
template = join("\n", [
459-
for fn in fileset(".", "${local.gitlab_config_template_file_path}/**") : file(fn)
459+
file("${local.gitlab_config_template_file_path}/postgres.tftpl"),
460+
file("${local.gitlab_config_template_file_path}/redis.tftpl"),
461+
file("${local.gitlab_config_template_file_path}/nginx.tftpl"),
462+
file("${local.gitlab_config_template_file_path}/rails.tftpl"),
463+
var.create_ses_identity ? file("${local.gitlab_config_template_file_path}/smtp.tftpl") : "",
460464
])
461-
vars = {
465+
vars = merge({
462466
gitlab_url = local.gitlab_complete_url,
463467
gitlab_db_name = module.gitlab_pg.db_instance_name,
464468
gitlab_db_username = module.gitlab_pg.db_instance_username,
465469
gitlab_db_password = module.gitlab_pg.db_instance_password,
466470
gitlab_db_host = module.gitlab_pg.db_instance_address,
467471
gitlab_redis_host = aws_elasticache_cluster.gitlab_redis.cache_nodes[0].address,
468-
aws_region = aws_s3_bucket.gitlab_backup[0].region
472+
aws_region = aws_s3_bucket.gitlab_backup[0].region,
469473
gitlab_backup_s3_bucket_name = aws_s3_bucket.gitlab_backup[0].bucket
470-
}
474+
}, var.create_ses_identity ? {
475+
smtp_address = "email-smtp.${var.aws_region}.amazonaws.com",
476+
smtp_username = aws_iam_access_key.gitlab_smtp_user[0].id,
477+
smtp_password = aws_iam_access_key.gitlab_smtp_user[0].ses_smtp_password_v4,
478+
smtp_domain = data.aws_route53_zone.email_domain[0].name
479+
} : {})
471480
}
472481

473482
resource "local_sensitive_file" "rendered_gitlab_config_file" {
@@ -496,3 +505,60 @@ resource "null_resource" "gitlab_reconfigure" {
496505
command = "ansible-playbook -u ubuntu -i '${aws_instance.gitlab[0].private_ip},' --private-key ${var.private_key} -e 'instance_ip_address=${aws_instance.gitlab[0].private_ip} workdir=${local.gitlab_config_tmp_path} config_file=${local_sensitive_file.gitlab_config_file.filename}' ${local.gitlab_config_playbook_file}"
497506
}
498507
}
508+
509+
data "aws_route53_zone" "email_domain" {
510+
count = var.create_ses_identity ? 1 : 0
511+
name = var.ses_domain != null ? var.ses_domain : var.hosted_zone
512+
}
513+
514+
resource "aws_ses_domain_identity" "email_domain" {
515+
count = var.create_ses_identity ? 1 : 0
516+
domain = data.aws_route53_zone.email_domain[0].name
517+
}
518+
519+
resource "aws_route53_record" "email_domain_amazonses_verification_record" {
520+
count = var.create_ses_identity ? 1 : 0
521+
zone_id = data.aws_route53_zone.email_domain[0].zone_id
522+
name = "_amazonses.${aws_ses_domain_identity.email_domain[0].id}"
523+
type = "TXT"
524+
ttl = "600"
525+
records = [aws_ses_domain_identity.email_domain[0].verification_token]
526+
}
527+
528+
resource "aws_ses_domain_identity_verification" "email_domain_verification" {
529+
count = var.create_ses_identity ? 1 : 0
530+
domain = aws_ses_domain_identity.email_domain[0].id
531+
532+
depends_on = [aws_route53_record.email_domain_amazonses_verification_record[0]]
533+
}
534+
535+
resource "aws_iam_user" "gitlab_smtp_user" {
536+
count = var.create_ses_identity ? 1 : 0
537+
name = var.ses_username
538+
}
539+
540+
resource "aws_iam_access_key" "gitlab_smtp_user" {
541+
count = var.create_ses_identity ? 1 : 0
542+
user = aws_iam_user.gitlab_smtp_user[0].name
543+
}
544+
545+
data "aws_iam_policy_document" "gitlab_ses_sender" {
546+
count = var.create_ses_identity ? 1 : 0
547+
statement {
548+
actions = ["ses:SendRawEmail"]
549+
resources = [aws_ses_domain_identity.email_domain[0].arn]
550+
}
551+
}
552+
553+
resource "aws_iam_policy" "gitlab_ses_sender" {
554+
count = var.create_ses_identity ? 1 : 0
555+
name = "gitlab_ses_sender"
556+
description = "Allows sending of e-mails via Simple Email Service"
557+
policy = data.aws_iam_policy_document.gitlab_ses_sender[0].json
558+
}
559+
560+
resource "aws_iam_user_policy_attachment" "gitlab_ses_sender" {
561+
count = var.create_ses_identity ? 1 : 0
562+
user = aws_iam_user.gitlab_smtp_user[0].name
563+
policy_arn = aws_iam_policy.gitlab_ses_sender[0].arn
564+
}

variables.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,27 @@ variable "private_key" {
273273
type = string
274274
description = "Private key to execute ansible playbook on Gitlab instance."
275275
}
276+
277+
variable "create_ses_identity" {
278+
type = bool
279+
description = "Create a Amazon SES domain identity for Gitlab SMTP service. The domain should be hosted on Route53."
280+
default = false
281+
}
282+
283+
variable "ses_domain" {
284+
type = string
285+
description = "Route53 hosted domain name for Amazon SES. If no value provided, value of Gitlab hosted zone will be assumed as default."
286+
default = null
287+
}
288+
289+
variable "aws_region" {
290+
type = string
291+
description = "AWS region code. Eg: ap-south-1"
292+
default = "ap-south-1"
293+
}
294+
295+
variable "ses_username" {
296+
type = string
297+
description = "Username for Gitlab SMTP user"
298+
default = "gitlab_smtp_user"
299+
}

0 commit comments

Comments
 (0)