Create AWS SQS Endpoint for VPC - amazon-web-services

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).

Related

Internet access via VPC endpoint

Is there anyone who can explain me that situation below ?
I connected to Ec2 intance in private subnet via Bastion Host.Then I created a vpc gateway endpoint to reach s3. I assigned the required role to ec2 and the connection to s3 is done. My question is, how can Ec2 instance in private subnet download something from the internet?
A VPC endpoint is used to access a certain service, in this case S3, over the AWS network instead of over the internet.
If you want your instance to be able to talk to the internet, you'll need to configure a NAT gateway in a public subnet, and you'll need to configure the route table of the private subnet to forward traffic to the NAT gateway.
A simple solution would be:
Put everything in a Public Subnet
Assign a Public IP address to the Amazon EC2 instance
Use Security Groups to secure access on the Amazon EC2 instance (that is, only allow Inbound connections from the Bastion)
The effective result is the same as using a Private Subnet, but the EC2 instance will be able to establish Outbound connections to the Internet (but not Inbound, so it remains secure).

AWS VPC with internet access sometimes timeouts with outside requests

I've created a VPC (due to the RDS connectivity needs inside the lambdas) in AWS which has internet access most of the time, but some times my outside requests timeout (mostly these happen with SES as they're the majority of outside requests). I've configured my VPC the following way (sorry, not in the created order, just reading them off AWS):
VPC with 172.30.0.0/16 CIDR
3 private subnets with 172.30.0.0/24, 172.30.1.0/24, 172.30.2.0/24 and a different availability zone for each (1a, 1b, 1c) with 0.0.0.0/0 route targeting my NAT
1 public subnet with 172.30.3.0/24 to 1a availability zone with a 0.0.0.0/0 route targeting my IGW
2 route tables (private and public) with the 3 private subnets in the private route table and the public one in it's own
Security groups for lambdas directing all outbound traffic to 0.0.0.0/0
Lambdas are configured to use these subnets and the given security group.
I'm not understanding why my internet requests some times fail from inside the VPC, it's almost as if the lambda gets started at some availability zone and that specific one does not have access to the internet inside the vpc.
EDIT: Resolved! I had the public subnet listed in my lambda function which caused the timeouts
AWS Lambda functions that are connected to a VPC should always be configured to use private subnets.
If those Lambda functions also require Internet access, they can use a NAT Gateway or NAT Instance to reach the Internet. These NAT services should be configured to use the public subnet(s).
When the Lambda function is connected to a private subnet, then traffic destined for the Internet will be routed from the private subnet, through the NAT Gateway/NAT Instance, and out to the Internet. This will not work if the Lambda function is connected to a public subnet. (And a Lambda function cannot connect directly from a VPC to the Internet.)

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

I can't connect through RDP to my EC2 Windows instance

NETWORK:
I have a private subnet and a public subnet. A VPC with IG attached. There is a NAT Gateway in the public subnet. I also have 2 route table, the public route table routes everything to the IG, the private route table routes everything to the NAT Gateway.
EC2:
My EC2 Windows instance is in the private subnet with a EIP and a Public DNS. It has a security group that allows all fron internet inbound/outbound but i cant even ping neither rdp
All the machine or instances in the private subnet cannot be connected externally hence the name private subnet. If you want to connect to your instance externally you must place it in the public subnet (the subnet that is connected to the internet gateway). Eventhough, you attach an elastic ip (which is a public ip) but it is routed through a NAT. The EIP must go through the internet gateway only.
If you really want to connect you need to VPN to your VPC (using AWS VPC VPN) and then RDP to your instance. Or have a gate instance in your public subnet.
Verify that the route table for the subnet has a route that sends all traffic destined outside the VPC to the internet gateway for the VPC. For more information, see Creating a Custom Route Table (Internet Gateways) in the Amazon VPC User Guide.