Inter VPC internet access between lambdas - amazon-web-services

I am working on a project where my main lambda function is in a VPC in private subnet and some sister lambda functions in a different VPC which are in their own private subnets. How can I go about calling these sister lambdas's from the main lambda across VPC without giving internet access to each of them via a NAT gateway linking to a public subnet which has an internet gateway attached to it.
Other AWS services that my main lambda invokes are:
1. S3
2. Dynamodb
3. Autoscaling
4. ECS
5. RDS

This can be done, but there are some complex steps involved.
First of all, when you use aws-sdk, the calls are made through the internet. To avoid this situation and access the services within the AWS network, The AWS has introduced some private VPC endpoints. I have only used S3 and API gateway private endpoints to date. But there is more type of VPC endpoints.
This is how I would do today,
setup a private API gateway API to invoke lambda - The private API's are only accessible through a private VPC endpoint for API gateway.
create a private VPC endpoint for API gateway.
setup VPC peering between the VPCs
(from the sister lambda on other VPC) invoke the API through the VPC endpoints public DNS URL
The drawback of adding an API in front of the lambda is, the API has a hard timeout of 29 seconds.
hope this helps.

Related

How can I connect an AWS lambda inside a VPC to connect to a cloudformation stack?

current situation:
I'm developing an AWS lambda that would launch an EC2 instance through a cloud formation stack.
I've deployed it inside a VPC, and thus had created endpoints to give it access to ressources such as S3/DynamoDB. However I cannot find any endpoints for the cloud formation, and as a result my function gets stucked at:
Starting new HTTPS connection (1): cloudformation.ap-south-1.amazonaws.com:443
update 1
Here is the snippet of code I'm using to connect to cloudformation:
self.cfn = session.resource('cloudformation')
stackdata = self.cfn.create_stack(
StackName="STACK-{}".format(instance_name),
DisableRollback=True,
TemplateURL=constants.TEMPLATE_TYPE[instance_type],
Parameters=params,
Capabilities=['CAPABILITY_IAM', 'CAPABILITY_AUTO_EXPAND','CAPABILITY_NAMED_IAM']
)
Please be noted that my code works just fine in a none-VPC setup (if I deploy my lambda outside of a VPC)
Could anyone help me try to figure out what I'm missing here?
Lambda function that is deployed to the VPC doesn't have access to the internet. That means that it's not able to access any of the AWS services endpoints unless you do one of two things:
create a VPC endpoint for that service
Add NAT Gateway so Lambda function can use it to access internet
You add NAT gateway to the public subnet.
After that, you need to edit route tables for private subnets to point to the NAT gateway. When you add a Lambda function to the VPC, you choose in which subnets it can be deployed. It's necessary to associate all of those subnets with the NAT gateway, so you're sure that the Lambda function will always have access to the NAT gateway.
If your Lambda function really needs to be in VPC (it needs access to some other resources inside of VPC), this is ok, but if it's not really necessary, I'd suggest you just move it outside of VPC (NAT gateway is $35/month + traffic).
You can see the details here as well: AWS Knowledgebase

One VPC Endpoint for multiple services in AWS

I have VPC with two private subnets in two different AZ and
I am trying to create a VPC Endpoint for connecting lambda to my database in private one of subnet
But I need SNS also for sending message to users based on my lambda function
Do I need to create another endpoint for SNS in that subnet or I can attach multiple services to the same endpoint
I know NAT gateway is an option but it is costly than endpoints
Please suggest best way to do this
Thanks,
Monika
No. A VPC Endpoint is always for a specific service.
By the way, you do not require a VPC Endpoint for Lambda in your situation. Simply configure the AWS Lambda function to connect to the private subnets in the VPC. This is an in-built capability of Lambda functions and the do not require a VPC Endpoint to access the subnets.

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.

AWS API Gateway Access Private Subnet

I have Public and Pvt Subnets in my VPC. I have some services running on EC2 in Pvt subnet, that needs to be accessed by external/mobile resources. How do I do this- is VPCLink and NLB the way to do it, or any other way, create some access point in Public subnet (??). Lambda seems to be the answer (for almost everything in AWS now) - not sure even how that access works for resources in Pvt Subnet.
Also the same Pvt Subnet has access external resources (outside of AWS) - how do I do this using the API Gateway?
Not quite understanding how the API-Gateway (and Lambda) is situated vis-a-vis - VPC and subnets- and how the network access control functions- can they access Pvt subnets directly or not. The documentation is somewhat silent on this, only talks about IAM - if someone can explain this. Found this on Lambda: AWS Lambda: How to setup a NAT gateway for a lambda function with VPC access.
The documentation says "API Gateway allows you to securely connect ... publicly addressable web services hosted inside or outside of AWS". My resources in Pvt subnet are not publicly addressable - I suppose.
Thanks
Are the services you have running on EC2 offering an API? API Gateway is meant to proxy API requests. It's commonly used in conjunction with Lambda to allow Lambda functions to process HTTP requests. An API Gateway is not necessary for your service. You can simply use an Application Load Balancer (ALB) or an Elastic Load Balancer (ELB). They can reside on a public subnet while your service remains in the private subnet. You can use security groups and VPC routing tables to allow communication from your public ALB/ELB to your private EC2 service.
With Ben’s help here is the answer
Introducing Amazon API Gateway Private Endpoints
Inbound: Accessing services hosted in Private Subnet via API Gateway
Endpoint integrations inside a private VPC. With this capability, you can now have your backend running on EC2 be private inside your VPC without the need for a publicly accessible IP address or load balancer.
So essentially API Gateway can access published endpoints, even in Private Subnets.
OutBound: Accessing externally hosted services from Private Subnet via API Gateway
API Gateway private endpoints are made possible via AWS PrivateLink interface VPC endpoints. Interface endpoints work by creating elastic network interfaces in subnets that you define inside your VPC. Those network interfaces then provide access to services running in other VPCs, or to AWS services such as API Gateway. When configuring your interface endpoints, you specify which service traffic should go through them. When using private DNS, all traffic to that service is directed to the interface endpoint instead of through a default route, such as through a NAT gateway or public IP address.
So you simply need to create a VPC endpoint in the Pvt Subnet for the API Gateway.

Putting both Lambda and RDS in default VPC behind an API Gateway?

I am trying to have an architecture with:
Route53 <-> API gateway <-> Lambda <-> RDS and DynamoDB.
I am confused about some networking aspects here!
From most of the documentation, what I understand is that Lambda is by default launched in default VPC and can access internet from there but no resources inside a "VPC". And this 2nd VPC (in quotes) refers to non-default VPCs in most discussions. But what is not clear is what if I placed the Lambda and RDS both in default VPC, lambda in a public subnet with --vpc-config info and RDS in a private subnet, will my Lambda have the internet connection?
Even when everything is in default subnet, should I put my lambda function in to a private subnet with Internet access through an Amazon VPC NAT gateway?
I know it is a theoretical question - documents are confusing me by not explicitly mentioning what cannot be done!
From most of the documentation, what I understand is that Lambda is by
default launched in default VPC and can access internet from there but
no resources inside a "VPC".
That is incorrect. By default Lambda is not launched in a VPC at all. Or if it is in a VPC it is in one that you cannot see because it doesn't exist in your AWS account.
what if I placed the Lambda and RDS both in default VPC, lambda in a
public subnet with --vpc-config info and RDS in a private subnet, will
my Lambda have the internet connection?
No, your Lambda function will not have internet access, even in a public subnet. This is because it is never assigned a public IP address. Once you place a Lambda function inside a VPC you have to have a NAT gateway in order to for the Lambda function to access anything outside the VPC.
Even when everything is in default subnet, should I put my lambda
function in to a private subnet with Internet access through an Amazon
VPC NAT gateway?
Yes, that is the correct way to provide a Lambda function with access to both a VPC and resources that exist outside the VPC.
Also note that DynamoDB (and the AWS API) does not run in your VPC. So if you place a Lambda function inside your VPC that needs to access DynamoDB, or anything else that is accessed via the AWS API, you will have to add a NAT gateway to the VPC.
Note that the "Default VPC" is the term for a the VPC that is setup for you when you first create your AWS account. You can see this VPC in your account in the VPC service console. Aside from it being created for you with default settings, you should just think of this as another VPC in your account. The Default VPC is not used by Lambda when you don't specify a VPC, and it is not used by other services like DynamoDB that exist outside your VPC network.