Put lambda in VPC and subnet - amazon-web-services

I have lambda function which needs to access RDS in isolated private subnet.
And lambda function also need to access internet endpoint and be invoked from internet.
In this case,
put lambda in private subnet (with Nat gateway)
lambda can access internet from nat gateway
However ,,, is it possible to invoke lambda function in private subnet from internet?
(I can set API gateway to the lambda in private subnet?)

"However ,,, is it possible to invoke lambda function in private
subnet from internet?"
Yes, you always invoke Lambda from the AWS API, which is on the public Internet. Then AWS creates an instance of your Lambda function to handle the invocation. You never make a direct network connection to a Lambda function. It doesn't matter if your Lambda function is configured to run inside your VPC, you still invoke it the same way.

Related

How can I troubleshoot connectivity issues between AWS resource types that are not included in the Reachability Analyzer tool? (e.g. Lambda functions)

I have a Lambda function deployed into a public subnet in a VPC and I’m trying to connect to a Lambda function outside of a VPC and I’m running into connectivity issues.
I believe the security group settings and IAM policies will permit the connection, but I'm not sure if there's an issue with connecting to an out-of-VPC Lambda from an in-VPC one.
Is there a tool in AWS Console, AWS CLI or anywhere else that I can use to troubleshoot where the connection is failing? I’ve used the Reachability Analyzer before but it only works on a handful of resource types like EC2 instances.
I've tried invoking the out-of-VPC Lambda from inside my in-VPC Lambda, but the request doesn't work and I don't see any helpful information about what happened. I tried running the Reachability Analyzer, but it doesn't allow you to test if Lambda functions are reachable.
I was expecting the request to work, but I'm not sure if I need to configure a VPC interface endpoint because I'm connecting from an in-VPC Lambda to an out-of-VPC Lambda.
I’m new to networking and would appreciate any help.
I have a Lambda function deployed into a public subnet in a VPC and I’m trying to connect to a Lambda function outside of a VPC and I’m running into connectivity issues.
The Lambda function in the VPC never gets a public IP assigned to it. So it can't connect to anything outside of the VPC. It can't use the Internet Gateway attached to the public subnet because it doesn't have a public IP.
By "connect to a Lambda function outside of a VPC" what you are really doing is connecting to the AWS API outside of the VPC. You never "connect" to a Lambda function, because Lambda functions aren't running and just sitting around idle waiting for your request. Lambda functions don't really exist until a request comes in to the AWS Lambda Invoke API, at which point AWS spins up an instance of the Lambda function and passes it the invocation payload.
To fix this connectivity issue, you either need to create an AWS Lambda VPC Endpoint in your VPC, to handle requests to the Lambda API originating in your VPC. Or you need to move the VPC Lambda function to a private subnet, with a route to a NAT Gateway. Lambda functions in private subnets can access things outside the VPC by having their requests routed through the NAT Gateway.
I was expecting the request to work, but I'm not sure if I need to configure a VPC interface endpoint because I'm connecting from an in-VPC Lambda to an out-of-VPC Lambda.
That's not how VPC Interface Endpoints work. The entire purpose of VPC Interface Endpoints is to allow a resource inside your VPC to access part of the AWS API that exists outside the VPC. A VPC Interface Endpoint will absolutely allow your Lambda function running in the VPC to access the Lambda Invoke API, in order to trigger an execution of your out-of-VPC Lambda function.

Is there a way that invoke a AWS Lambda function between different VPC?

I has two Lambda functions,
Function A access private subnet database so set in private VPC.
Function B with no VPC setting, because it needs public network.
Is there a way that can invoke function B by function A? Function A and B are synchronized.
Or if both function can be synchronized, is there any design that can solve this question?
Such as function A publish event to SNS, function B subcribe SNS?
You always invoke a Lambda function via the public AWS API. The Lambda function's aren't running constantly waiting for network connections. An instance of the Lambda function doesn't even exist until AWS creates an instance in order to handle an invocation request that it has received. Configuring them both to be in the same VPC wouldn't even help anything here.
Your Lambda function A either needs to be in a private subnet with a route to a NAT Gateway, or it needs to be in a VPC with an AWS Lambda VPC Endpoint.

Access AWS Batch from a lambda instance

I'm getting timeout errors when trying to create AWS Batch jobs using the AWS Javascript SDK from a Lambda.
How can I connect to AWS Batch from a Lambda instance besides creating public/private subnets with a NAT gateway?
The only other way is to not put your lambda in VPC. If you really need your function to be in VPC, the only way is to place it in private subnet and then use NAT in public subnet. This is because there is no VPC interface endpoint for AWS Batch.
You could also have second lambda function, not in VPC. So the main function in the VPC, invokes the second one which operates on AWS Batch. This will work, because AWS Lambda has interface endpoint which allows you to invoke functions from VPC without internet access.

Access a Lambda function inside a private subnet using API gateway

I have a VPC containing 2 Lambda functions A & B:
A is on 2 public subnets
B is on 2 private subnets with a RDS Database instance
The VPC itself has Internet access via the NAT instance.
I need a 3rd party API to communicate with B, but B is in a private subnet. Now I was wondering whether API Gateway solves this problem or whether it requires more work.
Thanks in advance
If you want the 3rd-party to invoke the Lambda function and pass data to it, then AWS API Gateway is correct for the task. That is exactly what API Gateway is designed to do.
See: Using AWS Lambda with Amazon API Gateway - AWS Lambda
Alternatively, you could use an Elastic Load Balancer and have it invoke the Lambda function.
From: Using AWS Lambda with an Application Load Balancer - AWS Lambda
You also mention that you have an AWS Lambda "on 2 public subnets". AWS Lambda functions should not be configured to connect to public subnets. They should either be configured to use "No VPC" (in which case they can directly access the Internet), or they should be connected to private subnets (and can use a NAT Gateway or NAT Instance to access the Internet if required).
See: Configuring a Lambda function to access resources in a VPC - AWS Lambda:
Connect your function to private subnets to access private resources. If your function needs internet access, use network address translation (NAT). Connecting a function to a public subnet doesn't give it internet access or a public IP address.

Why can I call an on-VPC aws lambda from an off-VPC aws lambda but not vice versa?

So if I have two lambdas, one inside a private VPC, and one not on a VPC, calling the private lambda from inside the public lambda works but I cant call the public from the private lambda.
There's no NAT setup.
Why is this? It seems that I should not be able to call the private from the public theoretically.
In order to trigger a Lambda function, all that's required is for the caller to have outbound access to the Invoke AWS API. It is not necessary for the invoked Lambda function to have any open inbound ports, or any public Internet access.
So a public (non-VPC, has Internet access) Lambda function can call the Invoke API to trigger the private Lambda function, but the private VPC (no Internet access) Lambda function cannot access the Invoke API to trigger any Lambda function.
Inside VPC you need an AWS VPC interface endpoint (https://docs.aws.amazon.com/vpc/latest/userguide/vpce-interface.html#create-interface-endpoint). It allows access to AWS Lambda service without going through the Internet.
To solve this issue you may configure the lambda inside the VPC to have internet access. This article explains how to set up this configuration.