Public Subnet is not created in one VPC - amazon-web-services

I have created a VPC with CIDR 10.0.0.0/16. Then in that VPC, I wanted to create two public subnets. So I have created two subnets with CIDR 10.0.1.0/24 and 10.0.2.0/24.
Now I have launched two EC2 instances on these subnets but I am not getting public IP of both the Instances. Please help me understand why I am not getting public IP of these instances and what should I do to get the public IP of these instances.

To get a public IP address you either
specify a flag on the subnet (in cli it's MapPublicIpOnLaunch, in the console I don't recall where) you want a public address by default on the EC2 instances
explicitly specify you want a public IP when launching an EC2 instance (on the screen you select the vpc and subnets)
assign an elastic IP address to an instance
and do not forget to create a default routing on the subnet to the internet gateway

Related

Creating two EC2 Instances with the same public IP

I am trying to emulate an environment where two computers are connected to the same network using EC2 instances.
I tried creating a VPC with a public and private subnets. I connected a NAT gateway to the private subnet and then created an instance in private subnet and it doesn't seem to have public ip. Is there another way to approach to create this environment
private subnet and it doesn't seem to have public ip.
That's correct. Instances in private subnets do not have public IP. However, they do access the internet through NAT which as a single public IP. This means that for any external API/service, the two instance will seem to have same IP address.
You can use a Load Balancer as the public interface and a auto scaling to scale to 2 instances of ec2

How can i connect to a EC2 machine which is within a VPC being ELB

I am trying my hands on VPC and ELB.
my question is .
when we create a EC2 instance in default VPC we have public ip attached to it and we can ssh into it.
BUT
When i create an instance with in a VPC there i don't see any public ip to ssh into .
so where can i find the public ip of the instance.so that i can ssh into
Also note what happens when: instance in a part of security group attached to ELB.
setting an elastic ip to the instance is one part . What if auto scaling spins up another instance what happens then ?
if i need to deploy into the EC2 . how would i do it? how do i access the public ip
When you create an instance under public subnet it does not mean the instance will have public access automatically, you need to enable public IP during launch process if you want to do that after:
Select EC2 instance> Actions>Networking> Manage Ip Addresses. Then use the option "To add or edit an IPv4 public IP Allocate an Elastic IP to this instance or network interface".
Create an elastic IP
Navigate to Elastic IP address link> click Associate IP
Select the instance to associate IP and save.
Now you will have EC2 instance with public IP without restarting/deleting EC2 instance.

EC2 instance in public subnet has no public IP

I have created a VPC and within I created a Subnet and a Internet Gateway (attached to the VPC). In that subnet, I created a Route Table in which I set route 0.0.0.0/0 to target the Internet Gateway.
After that I launched an EC2 instance in my subnet but it has no public IP so I can't connect to it using RDP.
I tried to enable 'DNS hostnames' and 'DNS resolution' but it didn't add a public IP to my instance (even after restarting it). In my subnet, I enabled 'Auto-assign IPv4' but still no public IP.
Any clue what I'm missing here please?
Your instances won't automatically get public IPs attached to their ENIs after you enable 'Auto-assign IPv4' in your subnet. One possible solution without spinning up a new EC2 is to attach an Elastic IP (as #Oleksii Donoha suggested in the comment) to this instance. You can follow the aws docs to allocate an Elastic IP address and then associate it with your running instance.
Side note: It's not possible to attach an ephemeral ip to an already-created ENI or EC2 instance. See discussions here.
Though your instance sits in public subnet, you have to make sure that 'Auto-assign Public IP' is either set to 'Enable' or 'Use subnet setting (Enable)'on Configure Instance Details page. Sometimes folks forget to check/reverify this setting while creating EC2 instances.

AWS : SSH to private subnet EC2 instance from public subnet EC2 instance via NAT GATEWAY is not happening

I have set up below VPC configuration but the SSH to the instance is not happening at the moment:
Created a new VPC
Created a public and private subnet
Launched an ec2 instance to the public subnet updated route tables for internet gateway
Launched ec2 instance to private subnet.
Associated a natgateway to the public subnet with in EIP
Route table updated for private subnet with natgateway
SSH from public instance to private instance is not happening with keypair.
Can you let me know what have I missed here.
First things first:
NAT Gateway in Public Subnet allows Instances from Private Subnet to reach internet for software updates etc via Internet Gateway.
NAT Gateway doesn't play any role in SSHing into an Instance.
Try this to test:
Attach Default NACL(allows all inbound and outbound) on Public and Private Subnet where your EC2 Instances resides.
Create 2 security groups for public(lets say Pub-SG) and private subnets(Prv-SG).
Allow SSH from everywhere/specific ip on Pub-SG.
On Prv-SG allow SSH from Pub-SG as source for better security reasons.
If both instances are launched using same key pair then with SSH-Agent Forwarding You can connect Private Instance through Public Instance.
I suspect you are missing a security group that permits SSH traffic between the instances on the two subnets.
AWS is secure by default - you need to explicitly permit traffic, roles, etc.
So in this case, the easiest thing to do would be to create a security group that spans the CIDR block of the VPC, and assign this to both instances.
Edit: I just noticed you say you added your NAT gateway to the public subnet - I presume you mean private.
This guide covers a similar scenario - public web subnet and private db subnet, and discusses all the routes + ACLs you need.
Also, when you say ssh with the keypair isn't happening - are you connecting at all, or does the connection time out?
When it comes to one EC2 instance communicating with another EC2 instance within a VPC, NAT Gateway has no role.
Make sure that the Security Group to which the private subnet instance belongs, allows SSH protocol from the Security Group to which the public subnet instance belongs.
Also, the NACL associated with the private subnet should allow inbound and outbound SSH traffic from CIDR block to which the public subnet instance belongs.
copy th ssh public key of the private instance to the public instance. open the ssh key material using vscode then use nano or vim to paste the content of the public key the save and exit. Now you can ssh into your private instance given the SG of the private instance allows traffic from the public instance.

My EC2 instance has a Public IP but don't have a Public DNS

I created a new VPC called testglobal with 2 subnets: opennet, closednet
So I created EC2 instances on those subnets and now I can't ping them.
In EC2 panel it shows the Public IP but don't shows Public DNS.
They are all associated to ElasticIPs and added to a group with All Traffic IN/OUT.
Am I doing something wrong?
EC2 instances in your public subnet (what you've called opennet) will have a public DNS and a public IP address. You will only be able to ping them if they are in a security group that allows ICMP echo requests from your client-side IP address.
Instances in your private subnet (closednet) will not have public IP addresses or public DNS names. They will only have private addresses within the range of your subnets. That is the intent of a private subnet - to not allow direct public Internet traffic. Access systems in the private subnets only from the instance in public subnets, or alternatively via VPN.
More specific advice would require further information, like screenshots of the instance, subnet, route table and security group details.