aws ec2 public ip from vpc timeout - amazon-web-services

Is it possible to configure aws vpc/subnet so that the public ip4 addresses are reachable from within the VPC?
At the moment, the ec2 machine's ip4 addresses are reachable from the internet, but timeout when connecting from within the same machine or from another machine on the same subnet.

Quite embarrasing. But maybe this will help someone in the future.
We had a security group assigned to the instances which only allowed specific ips to access http ports. The sec groups also apply to instances within the same subnet, so an access rule from instances with the same security group was added which resolved the problem. Alternatively you can also allow access by private ip, for instance.
So if it ever happens that you need to access localhost via the public ip, access must be allowed by the active security group.

Related

EC2 open port for only private IP address

My Linux based EC2 instances have both public and private IPs. I am hosting two servers on it:
S3 server, which will be accessible over both internet/private network and
NFS server, which should be accessed only via private IPs/internal network.
I want to open NFS port in the Security Group but only allow the access over the EC2 instances's private IP. I don't think this can be achieved by using only security groups as when the request reaches EC2 node, the Internet Gateway has already replaced the public IP with the private IP of the EC2 instance and the instance is not even aware that there is an extra public IP associated with it.
Instead what can be done is to choose the sources and selectively allow NFS access only to the VPCs and on-premises network (in case there is a VPN connectivity) but that would also mean that every time a new VPC is created OR a new VPN endpoint is established, someone has to modify the Security Group to accommodate new IP addresses.
What I ideally need is a way to apply port and IP based rules on the Internet Gateway which is currently not available/exported. Looking for alternatives.
The simplest method would be to add an Inbound rule to the Security Group where the Source is the CIDR range of the VPC.
For example, if your VPC is 172.31.0.0/16, then use this as the Source in the security group. It would permit any inbound connection from that IP address range.

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.

EC2 instance unable to reach itself via hostname

I have created an EC2 instance behind an ELB and the hostname is
mysubdomain.domain.com
The instance is reachable via the internet from my local workstation (have opened all connections from My IP --> to the instance in the security group it belongs to);
However, when performing
curl mysubdomain.domain.com
from within the instance, it times out;
Do I need to assign a public (I assume I want it to be elastic so that I don't have to change it every now and then in my security group inbound rules) and add an allow rule in my security group (that the instance belongs to) from that specific IP?
Is there another way to go about it, given that I have reached the limit of Elastic IPs?
For an Amazon EC2 instance to access the Internet, it must either be:
In a public subnet with a public IP address, or
In a private subnet with a NAT Gateway or similar NAT service
By default, security groups allow all outbound traffic, so you will not need to modify the security groups.
Even if you have reached the limit of your Elastic IP addresses, you can launch the EC2 instance with a Public IP address via Auto-assign Public IP — this is different to an Elastic IP address, in that it is assigned when the instance is Started and might change when the instance is Stopped/Started. However, it will work perfectly fine to obtain Internet access.
If an instance is behind a load balancer, there is no reason to want to reach that instance directly from the Internet. Thus, there is no need for an Elastic IP address. In fact, in best-practice architectures, the instances should be in a Private Subnet so that they are better protected from the Internet. This will then require a NAT Gateway or NAT Instance to enable the instance to access the Internet.

Private subnets options in AWS

I started to mess around with aws a couple days ago and I have a question about VPCs.
I'm considering two options to make "private" a subnet and I want to know your opinion:
OPTION 1:
A subnet with an ig attached (a public subnet really) with an ACL to only allow traffic from the VPC.
Pros: cheaper!
Cons: public ip for each instance (is this a real problem?), different public Ip for each instance (a kind of messy stuff for ip's whitelists)
OPTION 2:
A subnet without an ig attached using a Nat gateway.
Pros: more secure?, same public Ip for each instance.
Cons: more expensive.
I consider the Option 2 the best technical solution, only a bit more expensive. But, do you consider the option 1 a bad practice? Has more pitfalls that I'm not seeing?
It appears that your requirements are:
Run some Amazon EC2 instances
Keep them "private" from the Internet (no direct access from the Internet to the instances)
Allow the instances to access the Internet (eg to download updates, or to communicate with AWS API endpoints)
Your Option 1 is a public subnet, made private by modifying the Network ACLs to allow traffic only within the subnet. You are giving public IP addresses to the instances, presumably so they can access the Internet. This will not work. The Network ACLs that limit access to the VPC will also block traffic from the instances to the Internet.
Your Option 2 is a private subnet, with instances accessing the Internet via a NAT gateway. There is no need to assign public IP addresses to these instances, since they are in a private subnet. Yes, this will work but, as you note, there is the extra expense of the NAT Gateway (or a NAT instance).
An alternative is to use a Public Subnet with Security Groups:
Use a public subnet and give instances auto-assigned public IP addresses. This will allow them to access the Internet. (Note: This is different to an Elastic IP Address, which is limited to 5 per region per AWS account.)
Use a Security Group on each instance to block Inbound access. The instances will still be able to initiate outbound access to the Internet (and responses will come back because Security Groups are stateful)
See documentation: Amazon EC2 Security Groups for Linux Instances
Of course, since the instances now block inbound traffic, you will need a way to connect to them. This is typically done by launching a Bastion Server (Jump Box) in your public subnet. You can connect to the Bastion Server via public IP address, then connect to the private servers via their private IP address. To allow this, configure the Security Group on the private instances to permit inbound traffic from the IP range of the VPC, or from the Security Group associated with the Bastion Server.

Communication between AWS VPC instances via public IP

We have two AWS instances (Instance A and Instance B) which are running in the same VPC. There is an internet facing service on Instance A which is restricted (via security group) to a subset of IP addresses. Instance A has a DNS entry so the service can be accessed via someservice.example.org.
When trying to access the service from Instance B it works correctly if we used the VPC internal IP address however we cannot seem to get the correct security group configuration to allow this instance access via the public DNS.
We have added the 'default' VPC security group to Instance A but we're still unable to access this service directly. We also have the same problem trying to configure access to Instance A from other VPCs.
I know that we can create a private DNS for the VPC which could solve the problem when we are in the same VPC but this doesn't get around the problem when running in another VPC.
This sounds like a DNS resolution issue. If you are using Route53 for DNS the easiest way to fix this is to create a private Route53 DNS zone for your VPC and add something like:
CNAME 'someservice.example.org' that points to the instance's internal IP address.
Note that you really want to use the internal private IP address whenever possible. It will keep the network traffic within your VPC, which will be much faster and more secure. It may also be cheaper for you, at least if the instances are also within the same availability zone. You can read more about that on the EC2 pricing page in the Data Transfer section.
Also note that you can't open up the security group to allow only instances from your VPC/security group to access something via the public IP. This is because the traffic hitting the public IP is seen as coming "from the internet", not from your VPC. You would have to grant access to the servers via their public IP addresses instead of their security groups.
You mention also using a second VPC, but that would be a separate problem that could be addressed via VPC Peering.