2

I'm trying to invoke a lambda from an ec2. They are both in the same self-referencing security group, the same vpc, even the same subnet.

When I invoke the lambda from my rails server on the ec2 I get /home/ubuntu/.rbenv/versions/3.1.3/lib/ruby/3.1.0/net/http.rb:1018:in initialize': Failed to open TCP connection to lambda.us-east-1.amazonaws.com:443 (execution expired) (Seahorse::Client::NetworkingError)\ If add a fully open security group (all traffic from 0.0.0.0/0 for both inbound and outbound) the it works as expected and I get a response from the lambda. I can't figure out what is wrong with the sg. Anyone have any advice?

2
  • This is a common misapprehension. It would be good if AWS documented the invocation process (via the AWS Lambda service control plane) more visibly. Commented May 1 at 22:15
  • 2
    Is there a particular reason why you have configured the Lambda function to use a VPC (eg to connect to a database in that VPC)? If such access is not required then it is better to NOT attach the Lambda function to a VPC. It will then have full access to the Internet. Commented May 1 at 23:57

1 Answer 1

4

It doesn't matter that the function is configured to run in the same VPC as the EC2 instance and use the same security group. Lambda functions aren't sitting there running 100% of the time and listening for incoming requests. When you invoke a Lambda function, you connect to the public AWS Lambda service API to request that the AWS Lambda service create a new invocation of your Lambda function for you. That is why the error message is saying that it is trying to connect to lambda.us-east-1.amazonaws.com, which is obviously not an address that exists inside your VPC.

It sounds like your EC2 instance has Internet access already, since opening up the outbound rules in the security group appear to solve the issue. You will have to leave all outbound traffic open in the EC2 instance's security group in order to allow it to connect to the Internet in order for it to communicate with the AWS Lambda service and trigger new invocations of the Lambda function.

Alternatively, you could add an AWS Lambda Interface VPC Endpoint to your VPC, which would allow your EC2 instance to connect to the AWS Lambda service as if it existed inside the VPC. Then you would only need to open up the EC2 instance's security group to allow it to connect to the VPC Endpoint's Security group.

I highly recommend using different security groups for different resource types (EC2, Lambda, Endpoints, etc.) since each one has different inbound and outbound network requirements, and the only way to properly secure them all is to have different security groups with different rules for each one.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This all makes sense. I'll give it a try.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.