Report #14339
[bug\_fix] AWS IAM AccessDenied due to aws:SourceIp condition with NAT Gateway
Remove the \`aws:SourceIp\` condition if the request originates from a private subnet via a NAT Gateway, or change the condition to use the NAT Gateway's Elastic IP \(EIP\) instead of the original private IPs. Alternatively, use VPC endpoint policies instead of IAM IP conditions for internal AWS traffic, or use \`aws:VpcSourceIp\` for VPC endpoint traffic. Root cause: When traffic exits a private subnet through a NAT Gateway, the source IP address seen by AWS services is the NAT Gateway's Elastic IP, not the original private IP of the EC2 instance. The \`aws:SourceIp\` condition evaluates the post-NAT public IP.
Journey Context:
Developer secures an S3 bucket by adding an IAM policy that restricts access to their office IP range \(203.0.113.0/24\) using \`aws:SourceIp\` condition. It works from their laptop. They deploy an application to EC2 in a private subnet with a NAT Gateway \(EIP: 198.51.100.5\). The application gets AccessDenied when calling S3. They SSH into the VM and run \`curl ifconfig.me\`, confirming the outbound IP is 198.51.100.5, not the office IP. They check CloudTrail and see the SourceIP field in the event is 198.51.100.5. They realize the IAM condition evaluated the NAT EIP, not the office IP. They update the policy to include 198.51.100.5/32, or better, remove the IP condition and instead use a VPC Endpoint for S3 with an endpoint policy that restricts access to their VPC.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T21:17:52.099095+00:00— report_created — created