Skip to content

Commit cb15833

Browse files
authored
Reuse app domain for domain identity (#834)
- Use service_config.domain_name for notifications-email-domain module - DRY up usage of domain_name and hosted_zone_id in service root module by defining local variables ## Context We originally thought that the domain for the SES domain identity we create had to be the same as the root of the hosted zone domain rather than a subdomain, but we were wrong. This change causes the domain identity to just reuse the same domain as the app's configured domain, which simplifies things and also removes the chance of conflict between two apps that enable notifications.
1 parent fb1e6e6 commit cb15833

File tree

5 files changed

+21
-16
lines changed

5 files changed

+21
-16
lines changed
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
data "aws_route53_zone" "zone" {
2-
name = var.domain_name
3-
}
4-
51
resource "aws_route53_record" "dkim" {
62
count = 3
73

84
allow_overwrite = true
95
ttl = 60
106
type = "CNAME"
11-
zone_id = data.aws_route53_zone.zone.zone_id
12-
name = "${aws_sesv2_email_identity.sender_domain.dkim_signing_attributes[0].tokens[count.index]}._domainkey"
7+
zone_id = var.hosted_zone_id
8+
name = "${aws_sesv2_email_identity.sender_domain.dkim_signing_attributes[0].tokens[count.index]}._domainkey.${var.domain_name}"
139
records = ["${aws_sesv2_email_identity.sender_domain.dkim_signing_attributes[0].tokens[count.index]}.dkim.amazonses.com"]
1410

1511
depends_on = [aws_sesv2_email_identity.sender_domain]
@@ -19,7 +15,7 @@ resource "aws_route53_record" "spf_mail_from" {
1915
allow_overwrite = true
2016
ttl = "600"
2117
type = "TXT"
22-
zone_id = data.aws_route53_zone.zone.zone_id
18+
zone_id = var.hosted_zone_id
2319
name = aws_sesv2_email_identity_mail_from_attributes.sender_domain.mail_from_domain
2420
records = ["v=spf1 include:amazonses.com ~all"]
2521
}
@@ -29,6 +25,6 @@ resource "aws_route53_record" "mx_receive" {
2925
type = "MX"
3026
ttl = "600"
3127
name = local.mail_from_domain
32-
zone_id = data.aws_route53_zone.zone.zone_id
28+
zone_id = var.hosted_zone_id
3329
records = ["10 feedback-smtp.${data.aws_region.current.name}.amazonaws.com"]
3430
}

infra/modules/notifications-email-domain/resources/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@ variable "domain_name" {
22
description = "The domain name to configure SES, also used as the resource names"
33
type = string
44
}
5+
6+
variable "hosted_zone_id" {
7+
type = string
8+
description = "The Route53 hosted zone id for the domain"
9+
default = null
10+
}

infra/{{app_name}}/service/identity_provider.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module "identity_provider" {
2222
temporary_password_validity_days = local.identity_provider_config.password_policy.temporary_password_validity_days
2323
verification_email_message = local.identity_provider_config.verification_email.verification_email_message
2424
verification_email_subject = local.identity_provider_config.verification_email.verification_email_subject
25-
domain_name = local.network_config.domain_config.hosted_zone
25+
domain_name = local.domain_name
2626
domain_identity_arn = local.notifications_config == null ? null : local.domain_identity_arn
2727
sender_email = local.notifications_config == null ? null : local.notifications_config.sender_email
2828
sender_display_name = local.notifications_config == null ? null : local.notifications_config.sender_display_name

infra/{{app_name}}/service/main.tf

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ locals {
5050

5151
network_config = module.project_config.network_configs[local.environment_config.network_name]
5252

53-
service_name = "${local.prefix}${local.service_config.service_name}"
53+
service_name = "${local.prefix}${local.service_config.service_name}"
54+
domain_name = local.service_config.domain_name
55+
hosted_zone_id = local.domain_name != null ? data.aws_route53_zone.zone[0].zone_id : null
5456
}
5557

5658
terraform {
@@ -119,11 +121,11 @@ data "aws_security_groups" "aws_services" {
119121

120122
data "aws_acm_certificate" "certificate" {
121123
count = local.service_config.enable_https ? 1 : 0
122-
domain = local.service_config.domain_name
124+
domain = local.domain_name
123125
}
124126

125127
data "aws_route53_zone" "zone" {
126-
count = local.service_config.domain_name != null ? 1 : 0
128+
count = local.domain_name != null ? 1 : 0
127129
name = local.network_config.domain_config.hosted_zone
128130
}
129131

@@ -140,8 +142,8 @@ module "service" {
140142
public_subnet_ids = data.aws_subnets.public.ids
141143
private_subnet_ids = data.aws_subnets.private.ids
142144

143-
domain_name = local.service_config.domain_name
144-
hosted_zone_id = local.service_config.domain_name != null ? data.aws_route53_zone.zone[0].zone_id : null
145+
domain_name = local.domain_name
146+
hosted_zone_id = local.hosted_zone_id
145147
certificate_arn = local.service_config.enable_https ? data.aws_acm_certificate.certificate[0].arn : null
146148

147149
cpu = local.service_config.cpu

infra/{{app_name}}/service/notifications.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ module "notifications_email_domain" {
1818
count = local.notifications_config != null && !local.is_temporary ? 1 : 0
1919
source = "../../modules/notifications-email-domain/resources"
2020

21-
domain_name = local.network_config.domain_config.hosted_zone
21+
domain_name = local.domain_name
22+
hosted_zone_id = local.hosted_zone_id
2223
}
2324

2425
# If the app has `enable_notifications` set to true AND this *is* a temporary
@@ -27,7 +28,7 @@ module "existing_notifications_email_domain" {
2728
count = local.notifications_config != null && local.is_temporary ? 1 : 0
2829
source = "../../modules/notifications-email-domain/data"
2930

30-
domain_name = local.network_config.domain_config.hosted_zone
31+
domain_name = local.domain_name
3132
}
3233

3334
# If the app has `enable_notifications` set to true, create a new email notification

0 commit comments

Comments
 (0)