Skip to content

Lambda Function Cannot Connect to EC2 Instance in the Same Subnet Despite Identical Security Groups

0

I have a Lambda function and an EC2 instance in the same VPC subnet. Both use the same security group (which allows all internal traffic). An EC2 instance in the same subnet can connect to the target EC2 on port 443 without issues, but the Lambda function times out on every invocation.

My subnet route table has a more-specific route that overrides the local route for the VPC CIDR, pointing all VPC traffic to a Transit Gateway ENI for centralized inspection through a firewall. The routing, TGW route tables, and inspection VPC configuration have been verified as correct end-to-end.

The AWS documentation states: "You cannot route traffic between hosts in the same subnet through an appliance." If same-subnet traffic bypasses the route table, why does EC2-to-EC2 work but Lambda-to-EC2 fails?

1 Answer
2
Accepted Answer

The documentation statement "You cannot route traffic between hosts in the same subnet through an appliance" [1] applies to standard EC2 instances but does not apply to Lambda functions.

Why EC2-to-EC2 works (same subnet):

Traffic between two EC2 ENIs in the same subnet is delivered at Layer 2 (data link layer). The VPC fabric delivers the packet directly to the destination ENI based on its MAC address without consulting the subnet route table. This is why same-subnet EC2 traffic bypasses appliance routes entirely, the route table is never evaluated for same-subnet delivery.

Why Lambda-to-EC2 fails (same subnet):

Lambda functions do not execute inside your VPC. A Lambda function runs in a VPC owned by the Lambda service [2]. It accesses your VPC resources through a Hyperplane ENI (also called a cross-account ENI), which is a network interface attached to your subnet on behalf of the Lambda service. The Lambda execution environment establishes a network tunnel to this Hyperplane ENI to reach resources in your VPC [3].

Because the traffic arrives at your VPC via a tunnel through the Hyperplane ENI (not from a locally-resident instance), it does evaluate the subnet route table even for same-subnet destinations. If your route table has a more-specific route overriding the local route (e.g., the VPC CIDR pointing to a TGW ENI or appliance), Lambda traffic will match that route and be sent to the inspection path.

Summary:

SourceBehaviourRoute table evaluated?
EC2 instance (same subnet)Layer 2 direct deliveryNo
Lambda function (same subnet)Tunnelled via Hyperplane ENIYes

What this means for inspection architectures:

If you use a centralized inspection pattern where a more-specific route overrides the VPC local route (East-West inspection), Lambda traffic to same-subnet destinations will traverse the inspection path while EC2 traffic will not. This is expected behaviour.

Troubleshooting steps if Lambda traffic fails through the inspection path:

  1. Verify the firewall policy permits traffic from the Lambda ENI source IP to the destination. Lambda ENIs are ephemeral, the source IP may change between invocations. Use CIDR-based rules (the full subnet range) rather than specific IP-based rules.

  2. Verify appliance mode is enabled on the TGW VPC attachment for the inspection VPC. Without appliance mode, the forward path (SYN) and return path (SYN-ACK) may traverse different AZs, causing a stateful firewall to drop the SYN-ACK as it has no matching session state.

  3. Check the firewall logs for deny entries matching the Lambda source IP and destination port.

References:

AWS
SUPPORT ENGINEER

answered 17 days ago

EXPERT

reviewed 17 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.