Peering AWS VPC private to NAT - amazon-web-services

I have two VPC's in VPC1 in public subnet there is NAT GAteway for private subnet to go to internet... in VPC2 private subnet I have EC2 instance that needs to go to internet but i don't want to put NAT gateway or NAT instance in the public subnet of VPC2 if there is a way to sent traffic through peering and use NAT gateway in VPC1?

Unfortunately there is not supported as VPC peering is not transitive as explained here:
If either VPC in a peering relationship has one of the following connections, you cannot extend the peering relationship to that connection:
An internet connection in a private subnet through a NAT device
Also:
Similarly, if VPC A has a NAT device that provides internet access to instances in private subnets in VPC A, instances in VPC B cannot use the NAT device to access the internet.

Related

AWS VPC with both internet gateway and NAT

I am lost on how to provide outbound internet access to AWS Lambda in our VPC while also having internet gateway to support inbound access (from Internet) to certain resources in our VPC.
From the documentation provided (below), I understand we need to create a private and public subnet (with NAT), and have one route table pointing to IGW, and another to the NAT.
https://aws.amazon.com/premiumsupport/knowledge-center/internet-access-lambda-function/
Our setup is as follows.
VPC
Private subnet
Public subnet
Route Table
Table1
Public subnet
0.0.0.0/0 - IGW
Table2
Private subnet
0.0.0.0/0 - NAT
Lambda
VPC
Private subnet
RDS (Need access from outside of VPC)
Under VPC
With this setup, Lambda can access internet but the setup stops external inbound access to our resources in the VPC.
If we reroute our 0.0.0.0/0 in our private subnet to IGW, we can access our resources in VPC from external network but the Lambda loses connectivity to Internet.
Any one has clarity on how to set this up?
Appreciate any views on this.
Just move the resources that need to be publicly accessible into a public subnet (a subnet with a route to the Internet Gateway). The Lambda function has to remain in a private subnet (a subnet with a route to a NAT Gateway).
So in your case the RDS instance should be in the public subnet, and the Lambda function should be in the private subnet.

Is a NAT instance required in every vpc?

I have a hub and spoke model for infrastructure with a management vpc and 4-5 different environment vpc, peered to the management vpc.
I already have it set up but currently I only have 1 nat instance and 1 bastion host. Both of which are in the management vpc public subnet.
I wish to connect an instance in an environment vpc in a private subnet to this NAT instance to be able to reach the internet. Is that possible?
Note: I have the NAT/Bastion working correctly in the management vpc. I can reach all servers in both vpc with the Bastion host. I just can't get the above working.
My question is... is it possible? or do I need to set up an NAT in every vpc?
Edge to edge routing through a gateway (including the VPC Internet Gateway or IGW) is an unsupported VPC configuration with VPC peering.
Example: Edge to Edge Routing Through an Internet Gateway
Let's say you have a VPC peering connection between VPC A and VPC B (pcx-abababab). VPC A has an internet gateway; VPC B does not. Edge to edge routing is not supported; you cannot use VPC A to extend the peering relationship to exist between VPC B and the internet. For example, traffic from the internet can’t directly access VPC B by using the internet gateway connection to VPC A.
Similarly, if VPC A has a NAT device that provides internet access to instances in private subnets in VPC A, instances in VPC B cannot use the NAT device to access the internet.
Transit Gateway seems to fit for the case.
Transit Gateway Example: Centralized Router
The following diagram shows the key components of the configuration for this scenario. In this scenario, there are three VPC attachments and one Site-to-Site VPN attachment to the transit gateway. Packets from the subnets in VPC, A, VPC B, and VPC C that have the internet as a destination, route first through the transit gateway and then route to the VPN.

AWS VPC - Private subnet added to the public route table attached to internet gateway

Hope you are all doing good.
AWS allows us to attach a private subnet (created in a Customer VPC) with a route table having route to the internet via internet gateway. Does it make any difference to the instance launched in the private sub-net? or it is as good as having the private subnet not linked to the route table having route to the internet
Regards
AJ
A subnet is not private if it has routes to/from the internet. The instances launched in a private subnet cannot access the internet and cannot be reached from the internet.
For e.g if you have a database instance and you don't want anyone but the instances to access the instance, you can launch the instance in the private subnets.
Also if you want the instances in the private subnet to access internet, you need to setup a Nat Gateway (or a Nat Instance)
Hope this helps
First, we need to know what's the difference between a public subnet and a private subnet.
Public Subnet means this subnet has an Internet Gateway attached to it.
Private Subnet means this subnet has a NAT Gateway attached to it.
And the Internet Gateway can have both inbound and outbound but the NAT only can have outbound to the Internet.
And A NAT will be built in a public subnet but attach to a private subnet.
You can have lots of public/private subnets at the same time and attached NAT or IGW for them, but the IGW only can have one in each VPC.
In some use cases, we will not attach any kinds of NAT and IGW for a subnet. that's because it might be a database subnet and won't expect it to communicate with the Internet.
Normally, we will use a subnet with NAT attached for our application such as EC2 or ECS, Internal Load Balancers.
On the other hand, we will use a subnet with IGW attached for Internet-facing Load balancers, Nginx, Apache.
AWS VPC Design:
https://aws.amazon.com/tw/answers/networking/aws-single-vpc-design/
I think the hidden magic here is that the instance in the private subnet needs a public IP to communicate with outside, we never ever think this an issue in our lives. When our computer connect to the router, it get access to internet without any issue, why it works so smoothly? All because the router handles everything for us, including allocating a private IP for us, making recursively DNS query for us, and the most important, translating the private IP using the router's public IP so that our computer can communicate with the outside bidirectionally.
Let's go back to this AWS VPC scenario, you attached a route table with a default route to the Internet Gateway to your private subnet, which makes it looks like a public subnet. However, the instances still have no public IPs, and, not like our router, the Internet Gateway of AWS VPC doesn't do NAT works! So how can an instance with no public IP and no NAT access the internet? It's impossible.

NAT Gateway with Internet Gateway and lambda

We use elastic beanstalk to run our main application out of EC2, we also have an RDS instance in that VPC. Those instances have public IPs so it can use a standard internet gateway to access the internet. No problems there.
Now I have created a lambda function, associated it with the 3 subnets corresponding to the 3 AZs the EC2 instances live in. Everything is still good. My lambda can connect to those resources just fine.
My problem is I need my lambda to reach the internet. Normally I'd route the subnets it's in to 0.0.0.0/0 and route it out through a NAT gateway. However, because the EC2 and RDS instances in the subnets of the VPC my lambda is associated with have public IPs putting a NAT gateway in breaks their internet connectivity. How should I go about giving my lambda internet access, without breaking the IGW for the other Ec2 instances?
I was thinking of maybe creating 3 new subnets within the 3 AZs, associating that with my lambda function, create a NAT gateway in each AZ subnet, make the corresponding routes for each subnet. If I did that would my lambda still be able to access the EC2\RDS instances within the other subnets? I have a lambda sg and an ec2 sg and the lambda sg is permitted access to the ec2 sg. Hopefully this makes sense!
As it is not possible to attach public IP addresses to Lambda functions, you have to launch them in private subnets and forward internet traffic to a NAT gateway/instance to let your functions access the Internet.
It looks like you have only created public subnets in your VPC. As you have already suggested, you need to create private subnets that hosts your lambda functions.
Create 1 public and 1 private subnet per AZ.
Launch NAT Gateways in public subnets.
Update the routing table of the private subnets and forward all internet traffic to the NAT GW.
Private subnet RT
0.0.0.0/0 --> NAT GW
Public subnet RT
0.0.0.0/0 --> IGW

Create AWS SQS Endpoint for VPC

I have two subnets in a VPC. One is public subnet and other is a private subnet.
All EC2 instances in the public subnet and private subnet should access SQS, even if the internet is not reachable.
Is there a way like DNS filtering/SQS IP filtering on the outbound traffic so that the EC2 instances can access only AWS SQS service (like SQS) within VPC, without assigning public-IP to the instances?
(In short, I want to establish VPC endpoint like functionality for SQS, such that SQS is available within VPC but the internet is not necessarily reachable)
For example :
"ping sqs.us-east-1.amazonaws.com" should work
"ping google.com" should not work
You could create a NAT Gateway in the public subnet, which will allow the Amazon EC2 instances in the private subnet to connect to the Internet.
This provides only outbound connectivity, so the EC2 instances in the private subnet are not reachable from the Internet. The instances in the private subnet will not have a Public IP address (only the NAT Gateway will have one).