First: Dynamically executing code submitted by users is a massive security risk. So think very carefully before you do that.
Given that the Lambda function is your code the easiest way to do this is to have a list of domains that are allowed to be connected to and parse the user code so that only those domains are allowed. That's not actually "easy" but it's probably the best solution.
If your Lambda function is not connected to a VPC then there is no way to filter the traffic from it. But if it is connected to a VPC then you could send traffic from the Lambda function via a firewall of some sort. However, in today's world most traffic is going to be encrypted; you can filter based on the SNI in the HTTPS connection header but doing this introduces a bunch of costs - firewalls, NAT Gateways, etc. So it's less expensive to go with the first option - parse the code.
First, Lambda doesn't have ways to limit the outbound traffic. You may be able to do that by attaching the functions to a VPC and then routing all traffic via some proxy that checks and limits the communication.
Lambda usage reportAccepted Answerasked 10 months ago
Why is AWS::Lambda::EventInvokeConfig's limit for MaximumRetryAttempts 2?asked 5 months ago
Cannot set reserve concurrency for a lambda functionasked 2 months ago
AWS Service that can act like Lambda without a time limit?asked 8 months ago
How to set rate limit and burst in a usage plan in api gateway?asked 2 months ago
AWS S3 - File access only allowed when coming from a specific domainasked 6 months ago
How to get the enable / disable of the trigger set in lambda with boto3.asked 3 years ago
Large number of unaccounted for SQS requestsAccepted Answerasked 3 years ago
How to limit requests made from lambda? For example, I want to set that only the specified domain name can be requested in lambdaAccepted Answerasked 4 months ago
Make Lambda Function Urls to be accessible within the VPC only.asked 7 months ago