From 0166b67f74479d30f1203a49b79269a07c1cd60a Mon Sep 17 00:00:00 2001 From: Jorge Falcon <22119513+BCTBB@users.noreply.github.com> Date: Thu, 7 Aug 2025 17:13:18 -0400 Subject: [PATCH] Add custom domain support to SES module (#86) - Added variable to drive custom mail from configuration - Added resource for MX record to a specific route 53 zone, if custom mail value from is enabled - Add SPF record to local.spf_records list, if custom mail value from is enabled. Current spf resource takes care of - Creating the record if the value is present - Destroying the record if the value is not present (when disabled or variable is not set) --- addons/ses/README.md | 7 ++++++- addons/ses/main.tf | 27 +++++++++++++++++++++++---- addons/ses/variables.tf | 12 ++++++++++++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/addons/ses/README.md b/addons/ses/README.md index 5b2f1ca..cacbdc3 100644 --- a/addons/ses/README.md +++ b/addons/ses/README.md @@ -9,7 +9,7 @@ No requirements. | Name | Version | |------|---------| -| [aws](#provider\_aws) | 4.62.2 | +| [aws](#provider\_aws) | 6.7.0 | ## Modules @@ -21,15 +21,20 @@ No modules. |------|------| | [aws_iam_policy.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource | | [aws_route53_record.amazonses_dkim_record](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource | +| [aws_route53_record.dmarc_domain](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource | +| [aws_route53_record.mx_record](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource | | [aws_route53_record.spf_domain](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource | | [aws_ses_domain_dkim.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ses_domain_dkim) | resource | | [aws_ses_domain_identity.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ses_domain_identity) | resource | +| [aws_ses_domain_mail_from.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ses_domain_mail_from) | resource | | [aws_iam_policy_document.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | +| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source | ## Inputs | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| +| [custom\_mail\_from](#input\_custom\_mail\_from) | Custom MAIL FROM domain settings |
object({
enabled = optional(bool, false)
domain_prefix = optional(string, "")
}) | {
"domain_prefix": "",
"enabled": false
} | no |
| [domain](#input\_domain) | Domain to use for SES. | `string` | n/a | yes |
| [extra\_txt\_records](#input\_extra\_txt\_records) | Extra TXT records that have to match the same name as the Fleet instance | `list(string)` | `[]` | no |
| [zone\_id](#input\_zone\_id) | Route53 Zone ID | `string` | n/a | yes |
diff --git a/addons/ses/main.tf b/addons/ses/main.tf
index 2f8cd5a..b1f8966 100644
--- a/addons/ses/main.tf
+++ b/addons/ses/main.tf
@@ -1,11 +1,14 @@
locals {
- spf_domains = [
+ spf_domains = compact(concat([
aws_ses_domain_identity.default.domain,
- "_amazonses.${aws_ses_domain_identity.default.domain}"
- ]
+ "_amazonses.${aws_ses_domain_identity.default.domain}",
+ var.custom_mail_from.enabled == true ? "${var.custom_mail_from.domain_prefix}.${aws_ses_domain_identity.default.domain}" : null
+ ]))
dmarc_domain = "_dmarc.${aws_ses_domain_identity.default.domain}"
}
+data "aws_region" "current" {}
+
resource "aws_ses_domain_identity" "default" {
domain = var.domain
}
@@ -14,6 +17,23 @@ resource "aws_ses_domain_dkim" "default" {
domain = aws_ses_domain_identity.default.domain
}
+### CUSTOM MAIL FROM SETTINGS ###
+
+resource "aws_ses_domain_mail_from" "default" {
+ count = var.custom_mail_from.enabled == true ? 1 : 0
+ domain = aws_ses_domain_identity.default.domain
+ mail_from_domain = "${var.custom_mail_from.domain_prefix}.${aws_ses_domain_identity.default.domain}"
+}
+
+resource "aws_route53_record" "mx_record" {
+ count = var.custom_mail_from.enabled == true ? 1 : 0
+ zone_id = var.zone_id
+ name = aws_ses_domain_mail_from.default[count.index].mail_from_domain
+ type = "MX"
+ ttl = "600"
+ records = ["10 feedback-smtp.${data.aws_region.current.region}.amazonses.com"]
+}
+
###DKIM VERIFICATION#######
resource "aws_route53_record" "amazonses_dkim_record" {
@@ -25,7 +45,6 @@ resource "aws_route53_record" "amazonses_dkim_record" {
records = ["${element(aws_ses_domain_dkim.default.dkim_tokens, count.index)}.dkim.amazonses.com"]
}
-
resource "aws_route53_record" "spf_domain" {
for_each = toset(local.spf_domains)
zone_id = var.zone_id
diff --git a/addons/ses/variables.tf b/addons/ses/variables.tf
index 3620621..6a13b13 100644
--- a/addons/ses/variables.tf
+++ b/addons/ses/variables.tf
@@ -13,3 +13,15 @@ variable "extra_txt_records" {
description = "Extra TXT records that have to match the same name as the Fleet instance"
default = []
}
+
+variable "custom_mail_from" {
+ type = object({
+ enabled = optional(bool, false)
+ domain_prefix = optional(string, "")
+ })
+ default = {
+ enabled = false
+ domain_prefix = ""
+ }
+ description = "Custom MAIL FROM domain settings"
+}