You can lock down Amazon SES SMTP credentials with an IAM policy on the IAM user that owns them. A Deny on aws:SourceIp limits which IP addresses can send, and an Allow with the ses:FromAddress condition limits which sender addresses can be used. This page explains the example policy above, fixes the problems in it, and walks through creating, checking and testing a restricted SMTP user.
- Email must be sent and allowed from certain IP Address
- Email Must be sent and allowed from specific email ids only
How the example policy works
SES SMTP credentials belong to an IAM user. The SMTP password is derived from that user's secret access key. Each time an application logs in to email-smtp.<region>.amazonaws.com and sends a message, SES checks the user's IAM policies for the ses:SendRawEmail action. That means the usual IAM conditions apply to SMTP traffic too, so you can restrict it by IP address and by sender.
| Policy element | What it does |
|---|---|
Deny + Action: * + NotIpAddress aws:SourceIp | Blocks every request from this user unless it comes from one of the listed public IP ranges. An explicit Deny always overrides any Allow. |
Bool aws:ViaAWSService: false | Applies the Deny only to calls the user makes directly, not to calls an AWS service makes on the user's behalf. |
Allow ses:SendEmail, ses:SendRawEmail | Grants sending permission. SMTP uses ses:SendRawEmail. ses:SendEmail matters only if the same user also calls the API. |
Resource: ...identity/contact@... | Limits sending to one verified SES identity. |
StringEquals ses:FromAddress | Allows sending only when the From address exactly matches the listed value. |
Note: Without an Allow, IAM denies by default. So the Deny statement only adds the IP restriction. The Allow statement is what controls which senders are permitted.
Problems in the original policy and a corrected version
The idea behind the example above is sound, but the policy as written has several problems that can block all mail or quietly fail to protect anything:
- Private IP in
aws:SourceIp.172.21.20.81/30is a private RFC 1918 address. Over the internet,aws:SourceIpholds the public IP that AWS sees, usually your NAT gateway or firewall egress address. A private range will never match, so requests from that host are denied. - Misaligned CIDR blocks.
172.21.20.81/30and13.137.35.31/30are host addresses, not network boundaries. The /30 networks are actually.80/30and.28/30, which may not be the hosts you meant. Use/32for a single IP. - Resource too narrow. If you verified the domain (for example
example.com) rather than the individual address, sending is checked against the domain identity ARN. A resource that names only the email-address identity then fails with Access denied. List both ARNs. - Invalid account ID.
68444444is not a 12-digit AWS account ID. Replace it with your real one. - Single sender only.
ses:FromAddresstakes a list, so you can add more approved senders without adding statements. - No Sid on the Deny statement. This is optional, but a Sid makes logs and reviews easier to read.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyOutsideApprovedPublicIPs",
"Effect": "Deny",
"Action": "*",
"Resource": "*",
"Condition": {
"NotIpAddress": {
"aws:SourceIp": [
"203.0.113.10/32",
"198.51.100.28/30"
]
},
"Bool": {
"aws:ViaAWSService": "false"
}
}
},
{
"Sid": "AllowSendFromApprovedSenders",
"Effect": "Allow",
"Action": [
"ses:SendRawEmail",
"ses:SendEmail"
],
"Resource": [
"arn:aws:ses:ap-south-1:111122223333:identity/example.com",
"arn:aws:ses:ap-south-1:111122223333:identity/contact@example.com"
],
"Condition": {
"StringEquals": {
"ses:FromAddress": [
"contact@example.com",
"alerts@example.com"
]
}
}
}
]
}Code language: JSON / JSON with Comments (json)
Replace the documentation IPs (203.0.113.10, 198.51.100.28/30), the account ID, the Region and the addresses with your own. To find the public IP a server actually uses, run curl -s https://checkip.amazonaws.com on that server.
Create a restricted SMTP user
The SES console's SMTP settings > Create SMTP credentials button creates an IAM user with a broad send policy. For a restricted user, create the user yourself, attach your own policy, and turn its access key into an SMTP password.
aws iam create-user --user-name ses-smtp-app
aws iam put-user-policy \
--user-name ses-smtp-app \
--policy-name ses-smtp-restricted \
--policy-document file://ses-smtp.json
aws iam create-access-key --user-name ses-smtp-appCode language: JavaScript (javascript)
The SMTP username is the AccessKeyId. The SMTP password is not the secret key itself. It is derived from the secret key for a specific Region, using the algorithm AWS publishes:
#!/usr/bin/env python3
# Usage: python3 smtp_password.py <SECRET_ACCESS_KEY> <REGION>
import base64, hmac, hashlib, sys
def sign(key, msg):
return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()
secret, region = sys.argv[1], sys.argv[2]
sig = sign(("AWS4" + secret).encode("utf-8"), "11111111")
for part in (region, "ses", "aws4_request", "SendRawEmail"):
sig = sign(sig, part)
print(base64.b64encode(bytes([0x04]) + sig).decode("utf-8"))Code language: PHP (php)
Note: Derive the password for the same Region as the SMTP endpoint you connect to. A password derived for us-east-1 will not authenticate against email-smtp.ap-south-1.amazonaws.com.
Validate and simulate before you deploy
IAM Access Analyzer catches syntax problems and flags policy issues, including malformed IP ranges:
aws accessanalyzer validate-policy \
--policy-type IDENTITY_POLICY \
--policy-document file://ses-smtp.jsonCode language: JavaScript (javascript)
The policy simulator shows whether a given IP and sender would be allowed. Run it once with an approved IP and once with an unapproved one:
aws iam simulate-principal-policy \
--policy-source-arn arn:aws:iam::111122223333:user/ses-smtp-app \
--action-names ses:SendRawEmail \
--resource-arns arn:aws:ses:ap-south-1:111122223333:identity/example.com \
--context-entries \
ContextKeyName=aws:SourceIp,ContextKeyValues=203.0.113.10,ContextKeyType=ip \
ContextKeyName=ses:FromAddress,ContextKeyValues=contact@example.com,ContextKeyType=string \
--query 'EvaluationResults[].EvalDecision'Code language: JavaScript (javascript)
The expected result is "allowed" for the approved IP and sender. You should see "explicitDeny" when the IP is outside the list, and "implicitDeny" when the From address is not in the list.
Test over SMTP
swaks is a convenient way to send a test message. Run it from an approved host, then again from a host that is not approved:
swaks --server email-smtp.ap-south-1.amazonaws.com:587 --tls \
--auth LOGIN \
--auth-user AKIAXXXXXXXXXXXXXXXX \
--auth-password 'DERIVED_SMTP_PASSWORD' \
--from contact@example.com \
--to verified-recipient@example.com \
--header 'Subject: SES SMTP policy test'Code language: JavaScript (javascript)
When the policy allows the message, the session ends with 250 Ok followed by a message ID. When it does not, SES returns 554 Access denied with the IAM user, the action ses:SendRawEmail and the resource it checked. That resource ARN tells you which identity to add to the policy if the denial was unexpected. Ports 25, 587 and 2587 use STARTTLS. Ports 465 and 2465 use TLS from the start of the connection.
Common errors and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
535 Authentication Credentials Invalid | You used the secret key as the password, derived it for the wrong Region, or the key is inactive | Derive the password for the endpoint's Region and confirm the access key is active |
554 Access denied from a host that should be approved | aws:SourceIp lists a private IP, or traffic leaves through a different NAT or proxy IP | Use the public egress IP shown by checkip.amazonaws.com |
554 Access denied naming a domain identity ARN | The resource lists only the email-address identity | Add identity/example.com to Resource |
554 Access denied naming a configuration set ARN | The message uses a configuration set that the policy does not cover | Add the configuration-set/NAME ARN to Resource |
| Everything denied after you switch to an SES SMTP VPC endpoint | Over a VPC endpoint aws:SourceIp is absent, so NotIpAddress matches and the Deny applies | Base the restriction on a key that is present for endpoint traffic, such as aws:VpcSourceIp, and test with the simulator and a real send |
554 Message rejected: Email address is not verified | The account is in the SES sandbox, or the From identity is not verified | Verify the identity or recipient, or request production access |
Best practices
- Use one IAM user per application. Then a leaked password can only send as that application, from its approved IPs.
- Give the user no other permissions. A Deny on
Action: *is acceptable for an SMTP-only user, but on any broader user scope it toses:SendRawEmailandses:SendEmail. - Store SMTP credentials in a secrets manager rather than in application config files. Rotate them by creating a new access key, deriving a new password, deploying it, and then deleting the old key.
- Changing the policy takes effect for existing credentials. You do not need to regenerate the SMTP password after editing the IP list or sender list.
- Watch sending activity and authentication failures with CloudTrail and SES metrics, and set alarms on sudden volume spikes.
- Across several accounts, enforce the same guardrails centrally with service control policies. See the AWS Organizations guide.
Frequently asked questions
Which IAM action does SES SMTP require?
The SMTP interface needs ses:SendRawEmail on the sending identity. ses:SendEmail is only needed if the same credentials also call the SES API with simple (non-raw) messages.
Can I restrict SES SMTP credentials to specific IP addresses?
Yes. Attach a Deny statement with NotIpAddress on aws:SourceIp to the SMTP IAM user. List the public egress IPs of your servers, not their private VPC or LAN addresses.
How do I allow several sender addresses in one policy?
Give ses:FromAddress a JSON array of addresses under StringEquals. You can also use StringLike with a pattern such as *@example.com to allow every address on a domain.
Do I need new SMTP credentials after changing the IAM policy?
No. The SMTP password is derived from the access key, and policies are checked on every request, so policy changes apply to the existing credentials.
Why does SES say access denied on a domain ARN when I only listed an email address?
If the domain is verified, SES may check the send against the domain identity rather than the individual address. Include the domain identity ARN, the email identity ARN, or both, in the Resource element.
Related tutorials
- More AWS tutorials
- AWS Tutoriaols: A Comprehensive Guide to AWS VPC Endpoint Services
- Complete Guide of AWS Organization and AWS access portal
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals