12 November 2026 · DMARC Engine · 10 min read
- Use centralised key management
- Rotate keys regularly
- Ensure key distribution
- Monitor aggregate reports
DKIM Key Management Challenges with Dynamic IP Addresses
A common pattern is for load balancers and autoscaling groups to utilise dynamic IP addresses, which can pose significant challenges for DomainKeys Identified Mail (DKIM) key management. Receivers typically expect the DKIM signature to be aligned with the domain name, and the IP address of the sending server. When the IP address is dynamic, it can be difficult to maintain a valid DKIM signature.
default._domainkey.example.com. IN TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCq4j2jKx3+HcWb9yFTocjX1+YtGKu8H8YrZU9XKfbwHqZnEzf8vP4UtJgNnZz4Ql5v8+1zj5Dm6pZ3c9Uvz4W3K0Xt6QIDAQAB"The aggregate reports will show a high percentage of DKIM failures when the IP address of the sending server changes, as the receiver will not be able to verify the DKIM signature. This can lead to a decrease in deliverability, as receivers may view the email as unauthenticated.
In a hosted or managed setup, the provider may handle DKIM key management, including rotation and updates, which can simplify the process. However, in a self-managed setup, it is essential to implement a centralised key management system to ensure that all sending servers, including load balancers and autoscaling groups, are using the correct DKIM key.
import dns.resolver
answers = dns.resolver.resolve('default._domainkey.example.com', 'TXT')
for answer in answers:
print(answer)A list of considerations for DKIM key management with dynamic IP addresses includes:
* ensuring all sending servers are using the correct DKIM key
* implementing a centralised key management system
* regularly rotating DKIM keys to maintain security
* monitoring aggregate reports for DKIM failures
> Warning: Failing to manage DKIM keys correctly can lead to a decrease in deliverability, as receivers may view the email as unauthenticated.
Understanding DKIM Key Rotation and Expiration
DKIM key rotation is crucial for maintaining the security and authenticity of emails. A common pattern is to rotate DKIM keys every 6-12 months, depending on the organisation's security policies. The aggregate reports will show a decrease in DKIM pass rates if the key is not rotated or updated correctly.
default._domainkey.example.com. IN TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCq4j6hNtG4Rz4FtjNo0wL9wX2J3kRw8w9n5hJm9Ww9hJm8w9n5hJm9Ww9hJ"When rotating DKIM keys, it is essential to consider the expiration of the previous key. Receivers typically cache DKIM keys for a short period, usually 2-5 days, before expiring them. The new key should be published in DNS before the old key expires to prevent any email delivery issues.
default._domainkey.example.com. IN TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCq4j6hNtG4Rz4FtjNo0wL9wX2J3kRw8w9n5hJm9Ww9hJm8w9n5hJm9Ww9hJ"
selector2._domainkey.example.com. IN TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCq4j6hNtG4Rz4FtjNo0wL9wX2J3kRw8w9n5hJm9Ww9hJm8w9n5hJm9Ww9hJ"A hosted or managed DKIM setup may handle key rotation and expiration automatically, but it is still crucial to understand the underlying process to troubleshoot any issues that may arise.
> Tip: when rotating DKIM keys, use a new selector to publish the new key, allowing for a gradual rollout and minimising the impact on email delivery.
Configuring DKIM for Load Balancers: Selector Records and Key Storage
When configuring DKIM for load balancers, the primary concern is ensuring that each node behind the load balancer uses the same DKIM key for signing emails. This is achieved through the use of selector records, which allow multiple keys to be published under a single domain. A common pattern is to use a unique selector for each key, such as selector1._domainkey.example.com or lb1._domainkey.example.com, to differentiate between keys used by different load balancers or nodes.
selector1._domainkey.example.com. IN TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCq3NLG6c6Qqj6w4XxY8JNQ8kK8x8XT8x8x8x8x8x8x8x8x8x8x8x8x8x"
The key storage mechanism is also crucial, as it must be accessible by all nodes behind the load balancer. A hosted or managed setup may provide a centralised key management system, which simplifies the process of storing and distributing keys. However, in a self-managed environment, keys can be stored on a shared file system or in a database, and then distributed to each node.
openssl genrsa -out private.key 2048
openssl rsa -in private.key -pubout -out public.key
A list of best practices for configuring DKIM for load balancers includes:
* Using a unique selector for each key to avoid conflicts
* Storing keys in a centralised location, accessible by all nodes
* Ensuring that all nodes behind the load balancer use the same DKIM key for signing emails
* Rotating keys regularly to maintain security
selector2._domainkey.example.com. IN TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCq3NLG6c6Qqj6w4XxY8JNQ8kK8x8XT8x8x8x8x8x8x8x8x8x8x8x8x"
Note: When using a load balancer, it is essential to ensure that the DKIM key is properly distributed to all nodes, to avoid email signing errors. This can be achieved through automation scripts or by using a centralised key management system.
Autoscaling Considerations: Centralised Key Management and Distribution
When implementing DKIM in an autoscaling environment, centralised key management and distribution are crucial to ensure that all mail servers, regardless of their IP addresses, use the same DKIM key. A common pattern is to use a single DKIM key for all mail servers, and store it in a central location, such as a key management service or a configuration management system. This approach allows for easy rotation and management of DKIM keys, and ensures that all mail servers are using the correct key.
default._domainkey.example.com. IN TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+yt4wH77Q6y2NBd8Hb9JH4aoZ7xH1X1Ql0n5rEgUv8VfQk1jHJxCMg4wH+K0k0mTb3U4Z1HJ0l0l0QwIDAQAB"
In a hosted or managed setup, such as Amazon SES or Google Cloud Mail, the DKIM key management is typically handled by the provider, and the user is only required to add a CNAME record to their domain. However, in a self-hosted environment, the user is responsible for managing the DKIM keys, and must ensure that all mail servers are using the correct key.
- use a centralised key management system to store and distribute DKIM keys
- use a configuration management system to ensure that all mail servers are using the correct DKIM key
- rotate DKIM keys regularly to maintain security
import dns.resolver
def get_dkim_key(domain):
answers = dns.resolver.resolve(f"default._domainkey.{domain}", "TXT")
for answer in answers:
for txt_string in answer.strings:
if txt_string.startswith("v=DKIM1;"):
return txt_string
return None
Warning: failing to manage DKIM keys correctly can lead to mail delivery issues, as receivers may reject mail that is not signed with a valid DKIM key. It is essential to ensure that all mail servers are using the correct DKIM key, and that the key is rotated regularly to maintain security.
Practical Example: Implementing DKIM with AWS Autoscaling and ELB
Implementing DKIM with Amazon Web Services (AWS) Autoscaling and Elastic Load Balancer (ELB) requires careful consideration of key management and distribution. A common pattern is to use a centralised key management system to generate and distribute DKIM keys to autoscaling instances.
default._domainkey.example.com. IN TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCq3gZxKzZr6KU7m+MzG9l1xRb9X9KQH4R1Z4K8K5MzG9l1xRb9X9KQH4R1Z4K8K"The default._domainkey.example.com selector record above is a typical example of a DKIM key record, where default is the selector and example.com is the domain. The p parameter contains the public key.
When using AWS Autoscaling, it is essential to ensure that all instances have access to the same DKIM key. One approach is to store the DKIM key in Amazon Simple Storage Service (S3) and have instances retrieve the key during bootstrapping. This can be achieved using an AWS CloudFormation script or a user data script that runs during instance initialisation.
#!/bin/bash
aws s3 cp s3://example-bucket/dkim.key /etc/dkim/dkim.keyThe script above retrieves the DKIM key from an S3 bucket and stores it on the instance. The key can then be used by the mail server to sign outgoing emails.
A hosted or managed setup, such as Amazon SES, handles DKIM key management differently. In Amazon SES, DKIM keys are automatically generated and rotated, eliminating the need for manual key management. However, when using a load balancer or autoscaling, it is still necessary to ensure that all instances have access to the same DKIM key.
Tip: When implementing DKIM with AWS Autoscaling and ELB, consider using a centralised key management system to simplify key distribution and rotation. This can help reduce the complexity of managing multiple DKIM keys across different instances.
In addition to storing the DKIM key, it is also necessary to configure the mail server to use the key. This typically involves updating the mail server configuration to include the DKIM key and selector. For example, in OpenDKIM, the configuration file would include the following:
Domain example.com
Selector default
PrivateKey /etc/dkim/dkim.keyThe configuration above specifies the domain, selector, and private key file for OpenDKIM. The mail server will use this configuration to sign outgoing emails with the DKIM key.
When using a load balancer, such as ELB, it is essential to ensure that the DKIM key is distributed to all instances behind the load balancer. This can be achieved using a combination of AWS Autoscaling and a centralised key management system. By storing the DKIM key in a central location, such as S3, and having instances retrieve the key during bootstrapping, it is possible to ensure that all instances have access to the same DKIM key.
Monitoring and Troubleshooting DKIM Issues with Aggregate Reports
A common pattern is for receivers to send aggregate reports to the address specified in the rua tag of a DMARC record, which can help identify DKIM issues. The aggregate reports will show the number of messages that passed or failed DKIM verification, along with the IP addresses and selectors used. For example, a DMARC record might include the following rua tag:
_dmarc.example.com. IN TXT "v=DMARC1; p=none; rua=mailto:aggregate@example.com"Receivers typically send these reports in XML format, which can be parsed to extract relevant information. A hosted or managed setup may handle this parsing and provide a user-friendly interface to view the reports.
<feedback>
<version>1</version>
<report_metadata>
<org_name>example.com</org_name>
<email>aggregate@example.com</email>
<extra_contact_info></extra_contact_info>
<report_id>1234567890</report_id>
<date_range>
<begin>1643723400</begin>
<end>1646315200</end>
</date_range>
</report_metadata>
<policy_published>
<domain>example.com</domain>
<adkim>r</adkim>
<aspf>r</aspf>
<p>none</p>
<sp>none</sp>
<pct>100</pct>
</policy_published>
<record>
<row>
<source_ip>192.0.2.1</source_ip>
<count>10</count>
<policy_evaluated>
<disposition>none</disposition>
<dkim>fail</dkim>
<spf>pass</spf>
</policy_evaluated>
</row>
</record>
</feedback>
The aggregate reports can help identify issues such as DKIM key mismatches or expired keys. For instance, if the reports show a high number of DKIM failures from a specific IP address, it may indicate that the DKIM key is not properly configured or has expired. In a load-balanced environment, it is essential to ensure that all nodes are using the same DKIM key and selector to avoid mismatches.
Warning: Failure to properly manage DKIM keys and selectors can lead to deliverability issues, as receivers may reject messages with invalid or missing DKIM signatures.
Best Practices for DKIM Key Management in Dynamic Environments
When managing DKIM keys in dynamic environments, such as those with load balancers and autoscaling, it is crucial to consider the trade-offs between key rotation, selector record management, and centralised key distribution. A common pattern is to use a combination of short-term and long-term DKIM keys, with the short-term keys being used for signing emails and the long-term keys being used for verification.
default._domainkey.example.com. IN TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUpwmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ51s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQAB"
A key consideration is the management of selector records, as these can become outdated or inconsistent across different nodes in the environment. Receivers typically cache DKIM public keys for a period of time, so it is essential to ensure that the selector records are updated correctly and consistently across all nodes.
Tip:
> When using a hosted or managed DKIM setup, ensure that the provider's key management and rotation policies align with your organisation's security requirements, and that you have visibility into the key rotation and expiration process.
To mitigate the risks associated with DKIM key management in dynamic environments, it is recommended to implement a centralised key management system, which can distribute and rotate keys consistently across all nodes. This can be achieved through a combination of automated scripts and DNS record updates. The aggregate reports will show any issues with DKIM key verification, allowing for prompt identification and resolution of problems.