Okay, so let's have a look
at the behaviors of IP in AWS.
So we have a public IPv4 for our instance
and this is the IPv4 we can use through SSH
into our machine.
And it works great, okay?
So once we're logged in, remember, we see
that there is a private IP attached
as the host name of my instance.
So once we are in, we can start using the private IP,
but if we disconnect from our instance
and we try to do the SSH command
but this time using the private IPv4 address,
as we can see, it will not work.
Why? Well, let's have a look.
So I'm going to press Enter.
As you can see, this does not work, and why?
Well, private IPv4s belong to a private network
but right now I'm not connected
to the private network of AWS.
I can only connect to AWS for the internet.
And this is why private IPs do not work.
And this is why you have to use a public IP
because the public IP allows you to access AWS
from a public network,
and therefore the public network being the internet.
Okay.
So now let's have a look at the behavior of our instance,
just to observe it once when we stop it and restart.
So we're going to stop our instance.
And the instance is now stopping.
And let's just save the IPv4 address right here
just to have it on my screen.
So we're going to stop our instance.
So my instance is now stopped
and I'm going to start my instance,
which is different, by the way, from rebooting an instance.
So you need to stop and start the instance
to observe the behavior of the public IPv4 changing.
So now that I've stopped it and started it,
in a few seconds, we're going to obtain a new public IPv4.
So here we go.
This one right here, I'm going to copy it and paste it here.
As you can see, yes, indeed, in this console,
the two IPs are different.
So if I use the previous IP through SSH,
this is not going to go anywhere
because my instance has changed its IPv4.
But if I use the new IP to do my SSH command,
then I can SSH again in my instance,
but the private IP has not changed
because it doesn't need to.
Okay, so how do we solve this problem?
Because maybe we want to stop and start our instance,
but we don't want the public IPv4 to change.
In this case, what we have to use is an Elastic IP.
So right now you see Elastic IP addresses is zero.
So on the left-hand side, you can find Elastic IPs.
I'm going to open this in a new tab
and we're going to allocate a new IP address
from Amazon's pool of IPv4 address.
And what this will do is that it will create an IPv4 address
that we own now, at least for the time we rent it.
And this can be allocated to a specific EC2 instance
and it will keep the Elastic IP as long as it's attached.
So, with Elastic IP addresses,
you are charged as long as you don't use them.
So we need to attach it as soon as possible,
otherwise we will be charged.
So for this, we can right-click or we can do Action
and Associate Elastic IP address.
So if we do so,
then we need to say what we want to attach it to.
So, is it an instance or a network interface?
So it's going to be an instance.
And I'm going to choose the instance that is running
and then the private IP
to which to associate the Elastic IP address.
So there's only one private IP for my instance.
So we're good to go.
Okay.
So we associate it.
And now let's have a look at what happens.
So if you go back to our EC2 instance,
I'm going to refresh this window and close this.
So as you can see now, the public IPv4 address is now equal
to the value of the elastic IP address.
And if I scroll down and refresh this fully,
as we can see now, the Elastic IP addresses field
also has an Elastic IP address right here,
which is a public address.
And now we can click on this Elastic IP.
And this takes us directly
into the Elastic IP console from EC2
where we can get some more information
around this Elastic IP.
So this is good.
So now let's have a look and see if that works.
So we're going to copy the address right here
and I'm going to run the SSH command.
And this one will not work, so we have to use a new one.
And yes, indeed, we can do an SSH right now
in case we stop the instance.
So let's stop this instance and see what happens.
So as we can see now, the instance is in stopped state
but it still has a public IPv4
because the Elastic IP is still attached to it.
So that means that if we start our instance again,
then our instance is back in the running state
and the Elastic IP address is still attached
and the public IPv4 has not changed.
So we can also check this
by doing one more time the SSH command.
And yes, it works using the same command as before.
So if you finish this, we can go into this Elastic IP
and we can disassociate it to remove it from our instance.
And then, do not leave it running.
Please make sure to release this Elastic IP address.
It's not build for it.
And then back into our EC2 instance, if we refresh it,
it has obtained a new public IPv4 address
because it has been released from the Elastic IP.
And to finish this hands-on,
I can just terminate my instance and be good to go.
So that's it for this lecture.
I hope you liked it.
And I will see you in the next lecture.