Skip to content

About

SES email forwarding to an existing mailbox

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

terraform-aws-ses-email-forwarder

SES email forwarding to an existing mailbox

Goal

This module is intended for small, domain-owned contact addresses where running a full mail server would be unnecessary overhead.

Examples:

  • hello@cloudless.sh
  • contact@airos.sh
  • support@example.com

The goal is simple:

Own the address. Don't run a mail server.

Architecture

Internet
   ↓
Amazon SES inbound receiving
   ↓
SES receipt rule
   ↓
S3 bucket storing the raw message
   ↓
Lambda forwarder
   ↓
Existing destination mailbox

Amazon SES does not pass the full raw email body directly to a Lambda receipt action. For forwarding use cases, the usual pattern is to store the inbound message in S3 first, then let Lambda read the raw message from S3 and forward it.

Use cases

Use this module when you want to publish an email address for a project, domain, product, or internal tool, but you do not want to operate a mailbox provider for that domain.

Good examples:

  • a contact address for a static website
  • a project inbox forwarded to an existing company mailbox
  • lightweight inbound email for documentation, demos, or small services
  • domain-specific aliases that forward to one or more real inboxes

This module is not intended to be a full email platform.

What it creates

Depending on configuration, the module can create:

  • SES domain identity
  • SES DKIM DNS records
  • Route53 MX record for SES inbound receiving
  • SES receipt rule set
  • SES receipt rule for one or more recipients
  • S3 bucket for raw inbound messages
  • S3 lifecycle policy for message retention
  • Lambda function for forwarding (Node.js 22, ARM64)
  • IAM permissions for SES, S3, and Lambda
  • CloudWatch log group for the forwarder function

Usage

The forward_to addresses must be verified identities in SES within the same region. If your SES account is still in sandbox mode, both sender and recipient addresses must be verified.

module "cloudless_email" {
  source  = "ql4b/ses-email-forwarder/aws"
  version = "~> 1.0"

  namespace = "cloudless"
  name      = "email"

  domain_name = "cloudless.sh"

  recipients = [
    "hello@cloudless.sh",
    "contact@cloudless.sh",
  ]

  forward_to = [
    "carlo@example.com",
  ]

  route53_zone_id = data.aws_route53_zone.cloudless.zone_id

  create_identity  = true
  create_mx_record = true

  message_retention_days = 30
}

This creates resources named with the prefix cloudless-email:

  • cloudless-email-inbox (S3 bucket)
  • cloudless-email-forwarder (Lambda function)
  • cloudless-email-forwarder (SES receipt rule set)

Existing SES identity

If the SES identity is managed elsewhere, for example with an existing SES module, identity creation can be disabled:

module "airos_email" {
  source  = "ql4b/ses-email-forwarder/aws"
  version = "~> 1.0"

  namespace = "airos"
  name      = "email"

  domain_name = "airos.sh"

  recipients = [
    "contact@airos.sh",
  ]

  forward_to = [
    "carlo@example.com",
  ]

  route53_zone_id = data.aws_route53_zone.airos.zone_id

  create_identity  = false
  create_mx_record = true
}

Context and naming

This module uses cloudposse/label/null for consistent resource naming and tagging.

All standard context variables are supported:

module "email" {
  source  = "ql4b/ses-email-forwarder/aws"
  version = "~> 1.0"

  namespace   = "myorg"
  environment = "prod"
  name        = "email"
  attributes  = ["contact"]

  # ... module-specific variables
}

Resources will be named using the generated ID (e.g. myorg-prod-email-contact-inbox).

When composing with other modules that use the same labeling convention, pass context directly:

module "email" {
  source  = "ql4b/ses-email-forwarder/aws"
  version = "~> 1.0"

  context = module.label.context

  domain_name = "example.com"
  recipients  = ["hello@example.com"]
  forward_to  = ["team@company.com"]

  route53_zone_id = data.aws_route53_zone.example.zone_id
}

Requirements

Name Version
terraform >= 1.3
aws >= 5.0

Providers

Name Version
aws >= 5.0

Modules

Name Source Version
forwarder ql4b/lambda-function/aws ~> 1.0
this cloudposse/label/null 0.25.0

Resources

Name Type
aws_iam_role_policy.forwarder resource
aws_lambda_permission.ses resource
aws_route53_record.mx resource
aws_route53_record.ses_dkim resource
aws_route53_record.ses_verification resource
aws_s3_bucket.inbox resource
aws_s3_bucket_lifecycle_configuration.inbox resource
aws_s3_bucket_policy.inbox resource
aws_ses_active_receipt_rule_set.this resource
aws_ses_domain_dkim.this resource
aws_ses_domain_identity.this resource
aws_ses_receipt_rule.forward resource
aws_ses_receipt_rule_set.this resource
aws_caller_identity.current data source
aws_region.current data source

Inputs

Name Description Type Default Required
additional_tag_map Additional key-value pairs to add to each map in tags_as_list_of_maps. Not added to tags or id.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration.
map(string) {} no
attributes ID element. Additional attributes (e.g. workers or cluster) to add to id,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the delimiter
and treated as a single ID element.
list(string) [] no
bucket_name Optional custom S3 bucket name for raw messages string null no
context Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as null to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional_tag_map, which are merged.
any
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
no
create_identity Whether to create the SES domain identity and DKIM records bool true no
create_mx_record Whether to create the Route53 MX record for SES inbound receiving bool true no
delimiter Delimiter to be used between ID elements.
Defaults to - (hyphen). Set to "" to use no delimiter at all.
string null no
descriptor_formats Describe additional descriptors to be output in the descriptors output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
{<br/> format = string<br/> labels = list(string)<br/>}
(Type is any so the map values can later be enhanced to provide additional options.)
format is a Terraform format string to be passed to the format() function.
labels is a list of labels, in order, to pass to format() function.
Label values will be normalized before being passed to format() so they will be
identical to how they appear in id.
Default is {} (descriptors output will be empty).
any {} no
domain_name Domain used for inbound email receiving string n/a yes
enabled Set to false to prevent the module from creating any resources bool null no
environment ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' string null no
forward_to Destination email addresses where messages are forwarded list(string) n/a yes
id_length_limit Limit id to this many characters (minimum 6).
Set to 0 for unlimited length.
Set to null for keep the existing setting, which defaults to 0.
Does not affect id_full.
number null no
label_key_case Controls the letter case of the tags keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the tags input.
Possible values: lower, title, upper.
Default value: title.
string null no
label_order The order in which the labels (ID elements) appear in the id.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present.
list(string) null no
label_value_case Controls the letter case of ID elements (labels) as included in id,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the tags input.
Possible values: lower, title, upper and none (no transformation).
Set this to title and set delimiter to "" to yield Pascal Case IDs.
Default value: lower.
string null no
labels_as_tags Set of labels (ID elements) to include as tags in the tags output.
Default is to include all labels.
Tags with empty values will not be included in the tags output.
Set to [] to suppress all generated tags.
Notes:
The value of the name tag, if included, will be the id, not the name.
Unlike other null-label inputs, the initial setting of labels_as_tags cannot be
changed in later chained modules. Attempts to change it will be silently ignored.
set(string)
[
"default"
]
no
message_retention_days Number of days to keep raw inbound messages in S3 number 30 no
name ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a tag.
The "name" tag is set to the full id string. There is no tag with the value of the name input.
string null no
namespace ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique string null no
recipients Email addresses handled by the SES receipt rule list(string) n/a yes
regex_replace_chars Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, "/[^a-zA-Z0-9-]/" is used to remove all characters other than hyphens, letters and digits.
string null no
route53_zone_id Route53 hosted zone ID used to create DNS records string null no
rule_set_name Optional SES receipt rule set name. If null, one is created string null no
stage ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' string null no
tags Additional tags (e.g. {'BusinessUnit': 'XYZ'}).
Neither the tag keys nor the tag values will be modified by this module.
map(string) {} no
tenant ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for string null no

Outputs

Name Description
bucket_arn ARN of the S3 bucket storing raw inbound messages
bucket_name Name of the S3 bucket storing raw inbound messages
lambda_function_arn ARN of the Lambda forwarder function
lambda_function_name Name of the Lambda forwarder function
ses_domain_identity_arn ARN of the SES domain identity, when created by this module
ses_rule_set_name Name of the SES receipt rule set

Deployment

terraform apply

The Lambda binary is pre-compiled and shipped with the module. No build step required.

To rebuild from source (requires Go 1.23+):

make build

SES receiving region

Amazon SES inbound email receiving is only available in selected AWS regions. Deploy this module in a region that supports SES receiving and make sure the MX record points to the corresponding inbound endpoint.

Example MX value:

10 inbound-smtp.eu-west-1.amazonaws.com

DNS requirements

For receiving email through SES, the domain needs an MX record pointing to the SES inbound endpoint.

If create_mx_record = true, the module can create that record in Route53.

If DNS is managed elsewhere, set create_mx_record = false and create the MX record manually.

SPF record

This module does not create an SPF TXT record because domains often have existing TXT records that would conflict.

For improved deliverability, add include:amazonses.com to your domain's SPF record manually:

v=spf1 include:amazonses.com -all

If the domain already has a TXT record (e.g. Google site verification), merge the SPF value into the existing record set:

"v=spf1 include:amazonses.com -all"
"google-site-verification=..."

SPF is not strictly required for forwarding — DKIM (created by this module when create_identity = true) is sufficient for most providers to accept the forwarded message.

Message retention

Inbound messages are stored as raw email objects in S3 before they are forwarded.

By default, raw messages are retained for 30 days:

message_retention_days = 30

Set this value according to the operational needs of the project. Short retention is usually enough for simple forwarding use cases.

Dependencies

Design philosophy

This module is intentionally small.

It does not try to provide a complete mail hosting solution. It only covers the narrow case of receiving email for a verified domain and forwarding it to an existing inbox.

That makes it useful for static websites, small products, documentation sites, internal tools, and lightweight project domains.

License

MIT

About

SES email forwarding to an existing mailbox

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages