AWS NAT gateway vs public IP for outgoing internet connections - amazon-web-services

I'm learning AWS and how to configure networking for EC2 instances and have a few questions. I'm using CentOS 7 in t2.micro instances.
The private IP is tied to the NIC in the instance as shown by ifconfig -a. The purpose seems to be a single point of contact for the server. Adding another network interface does NOT add another NIC as shown by ifconfig -a. Since the primary network interface cannot be a static IP, a secondary network interface is necessary for most configurations. For example, to connect the application to the database server, use the static IP assigned to the second network interface. Am I understanding this correctly?
The public IP is shown in the AWS console and provides a means to connect to the instance via SSH, assuming you configure the SG. The public IP also provides a means to access the internet for system updates. This seems to contradict the AWS documentation for NAT. If the public IP already provides internet access, why is a NAT (instance or gateway) needed? This is in reference to system updates which the documentation references.
You can use a NAT device to enable instances in a private subnet to connect to the Internet (for example, for software updates) or other AWS services, but prevent the Internet from initiating connections with the instances. A NAT device forwards traffic from the instances in the private subnet to the Internet or other AWS services, and then sends the response back to the instances.
Is a public IP and private IP are always assigned to each instance? I haven't walked through the process of creating an [new] AMI to verify if there's an option to not have a public IP. If the instance doesn't have a public IP, will the AWS console Connect button still allow you to connect to it to administer the server? Under what scenario will an instance NOT have a public IP? How do you connect to that instance to administer it?
I have read the NAT Gateway documentation and understand much of it. I'm having trouble understanding the pieces that state a NAT gateway or internet gateway is necessary to enable internet access, when it seems this is enabled by default. What am I missing?

I think your confusion stems from your third question. A public IP is not always assigned to an instance. Public IP is an option that you can enable or disable in public VPC subnets, and in private VPC subnets public IP isn't an option at all. For EC2 instances without a public IP a NAT gateway (or NAT instance) is required in order to access anything outside of the VPC.
You may want to place something like a database server in a private subnet so that nothing outside your VPC can ever access it. However you might want the database server to be able to access the Internet to download patches or something, or you may want it to access the AWS API in order to copy backups to S3 or something, in which case you would need a NAT Gateway to provide the server access to resources outside your VPC.

Related

Is an ETL instance supposed to be in private or public subnet?

I am currently working on a ETL tool at work (python & bash scripts managed with Airflow basically) and I am asking myself wether I should put my EC2 instance which will run the ETL in a public or private subnet. My instance should have acces to the internet to retrieve data (basically ssh through on-premises instances we have) and should also be able to be accesses through SSH.
However, I don't know if allowing outbound connection to the internet and restrict inbound connection to SSH is enough about security or if I should put the instance in a private subnet and tweaking things to be able to connect to it.
Your ETL instance should be in a private subnet behind a NAT instance.
NAT gateway will give your EC2 private network internet connectivity but still ensure that your EC2 instances are not accessible from the internet. So in order to allow access to internet it has to route traffic through public network which has a Internet gateway attached.
You should put your EC2 instance in a private subnet to prevent hackers from gaining access and stealing your data.
You can learn how to setup NAT gateway here
https://aws.amazon.com/premiumsupport/knowledge-center/nat-gateway-vpc-private-subnet/
As the main purpose of your instance is not to deliver a public service, it'd be more secure in a private subnet and go through a NAT gateway to fetch data from the Internet.
That being said a NAT gateway is expensive, so a common pattern is to use a public subnet/ through an Internet gateway with a deny rule for any incoming traffic. If you don't want your instance exposed to ddos, don't even open ssh and use AWS systems manager to ssh your instance.

How could I use a VM public static IP when subnet uses an NAT gateway?

I configured load balancer/autoscaling in AWS.
I want that my instances in the autoscaling group use the same IP for outgoing trafic. So I configured a NAT gateway.
Good, it works well.
But, now, I cannot SSH directly to the instances in the autoscaling group using their public IP address.
So, is there a way to use at the same time a NAT gateway for outgoing traffic and IP public instance address for SSH ingoing traffic ?
Thank you for your help !
If your target is to just ssh into the system then best approach is via AWS Systems Manager. Your servers remain in private network and you don't have to manage complex networking in order to achieve this.
If you use SSM then you don't have to worry about the Public IP of your system at all. No need for Elastic IP as well.
Here's a documentation which shows how to use Systems Manager to ssh into the EC2 instances. https://aws.amazon.com/premiumsupport/knowledge-center/systems-manager-ssh-vpc-resources/
So, is there a way to use at the same time a NAT gateway for outgoing traffic and IP public instance address for SSH ingoing traffic ?
Since you are using NAT, your instances are in private subnet, which means there is no direct access to them through Internet. However, you can use ssh through SSM Session Manager:
How can I use an SSH tunnel through AWS Systems Manager to access my private VPC resources?
The other way is to setup a bastion host in a public subnet, and jump through it. But SSM is the newer and more sequire way of accessing resources in private subnets.

can we connect AWS ec2 instance which is having only private IP with the other ec2 instance with a public IP ? from putty

I have just started my career in the IT field and currently learning new technologies like AWS.
I have a small doubt and it will be really helpful for me if anyone can help me with it.
"Can we connect ec2 instance which is having only private IP with the other ec2 instance with a public IP"
In Putty, I have logged into an EC2 instance (With public IP) and tried to ssh into other EC2 instances (which is having only private IP) by mentioning its private IP Address, but I wasn't able to login to that.
Thank you in advance :)
You can do this (providing security groups and network configuration allows).
The first method (and more prefered) is through a private to private connection, just because an instance has a public IP does not mean it is inaccessible from private inbound connections. If they share the same VPC as long as your security groups/NACLs allow inbound from the private IP range you will be able to connect to the instance via its private IP.
If the instances share a different VPC (or even account) you can connect between using either transit gateway or a single peering connection, then whitelist the IP range of the source. A caveat is that these separate networks cannot be peered if they have a crossover in network address ranges (you would need to create a secondary range to deal with this).
The alternative is to use either a NAT Gateway or NAT instance and then bind this to the route table for your instance for the 0.0.0.0/0 route. You would then need to whitelist in the public instances security group the public EIP of the NAT. This is less preferable as it requires communication across the internet.

How to make an AWS EC2 instance accessible only via a private IP address?

I have a database running on AWS EC2 and I want it to NOT be accessible via any public IPs or DNS. I only want it to be accessible via a private IP address, so that a Node.JS within the same AWS zone could connect to it via an internal IP address (not a public one). So something like localhost but an AWS version of it.
Is this at all possible? How to do that? I didn't find anything online about it.
Generally speaking an instance is private as long as a
public IPv4 address is not assigned.
Instances with public IPv4 addresses but no Internet Gateway attached as a route will not allow inbound traffic as long as an internet gateway is not attached, although this should not be used to make an instance private as it is easy to be reversed.
The best approach is to create private subnets for where the private instances should exist, ensure that the setting for auto assign public IPv4 is not enabled.
Create a separate route table for the subnets and if you want the instances to be able to connect to the internet create either a Nat gateway or Nat instance for IPv4 traffic and add a route to the internet in your route table to use these as the target.
If you want ipv6 traffic outbound too you will need perform the above with an egress only gateway.
Check out this AWS documentation for a further breakdown of public and private network configuration.

Access Internet from AWS VPC instance without public IP address

We're setting up an Amazon VPC in which we will provision (for now) a single EC2 instance and one RDS instance. This is to 'extend our data center', and should only be using private subnet(s).
So actually, we have this setup, and it is working well (insert smiley face icon). For all intents and purposes, we're mirroring the VPC scenario 4 outlined by Amazon here: http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Scenario4.html
tl;dr: A single VCP, with a VPN connecting to our corporate network. The VPN uses a Virtual Private Gateway (VPC end) and Customer Gateway (our end) to allow us access as necessary to the EC2, which contains a webserver connecting to the RDS instance as needed. Anyone on our network has access to the web server running on the EC2 via a URL. All this is working as expected.
The problem comes when the EC2 instance needs to access a resource on the Internet - The idea is for us to NOT have any public subnets, but to route all traffic from the EC2 instance through our VPN and out the 'standard' path of our corporate Internet access. However we're having trouble setting this up.
The fact that it can be done is hightlighted in Amazon's FAQ here:
https://aws.amazon.com/vpc/faqs/
Q. How do instances without public IP addresses access the Internet?
Instances without public IP addresses can access the Internet in one of two ways:
Instances without public IP addresses can route their traffic through a NAT gateway or a NAT instance to access the Internet. These instances use the public IP address of the NAT gateway or NAT instance to traverse the Internet. The NAT gateway or NAT instance allows outbound communication but doesn’t allow machines on the Internet to initiate a connection to the privately addressed instances.
For VPCs with a hardware VPN connection or Direct Connect connection, instances can route their Internet traffic down the virtual private gateway to your existing datacenter. From there, it can access the Internet via your existing egress points and network security/monitoring devices.
We are trying to avoid option #1 as there is a cost involved (along with complexity and security issues). #2 is the perfect resolution for us, but understanding the process to set it up has been eluding us for a while.
Can anyone walk us through what we need to do (or point us to the correct resources) to ensure the EC2 instance* can access the Internet by routing the traffic down the VPN, through our corporate datacenter, and our our existing Internet access point?
* and anything within the private subnet for that matter
If you are using scenario #2, then all there is to do on the AWS end is to ensure that traffic destined for the internet, 0.0.0.0/0 is routed to your Virtual Private Gateway.
Once traffic heads there, it will go to your Customer Gateway, and into your corporate datacenter. It's up to your local IT guys on that end to get Internet-destined traffic heading out, if it's even possible. But at that point, it's no longer an AWS issue.