Instructor: So now let's talk about Elastic IP.
So when you stop and start an EC2 instance,
it will change its public IP.
And if you need to have a fixed public IP
then you need Elastic IP.
So this is for the use cases where you need to have an IP
that's going to be constant
across your usage, obviously, to instances.
So it's an IPv4 Elastic IP that you own
as long as you don't delete it.
So it's a reservation.
And so the idea is that you attach this Elastic IP
to one EC2 instance at a time
and that EC2 instance will inherit the IPv4 from it, okay?
You can remap this IP across instances
and we'll see this in the hands-on,
and you don't pay for the Elastic IP
if it's attached to a server,
which is a good thing,
but if you just reserve the Elastic IP
without attaching it to a server,
then you're going to be billed for it.
So Elastic IP, why do we use them?
Well, the use case is that you can mask the failure
of an instance or software by rapidly remapping the address
that your instance is accessible at
to another instance in your account
that is also running the software.
And by default, you can have
up to five Elastic IPs in your account.
You can ask AWS to increase that,
but the idea is that you should avoid using Elastic IPs,
at least that's my personal opinion,
unless you really, really, really have to,
this is why you can only have five Elastic IPs
in your accounts.
So other ways, an alternative for you to pass that is
that you can use a random public IP
and instead register a DNS name to it,
maybe using Route 53,
or use a load balancer,
which will have a static host name
and the load balancer is smart enough to redirect
to your correct EC2 instance.
Now let's see Elastic IPs in action.
So I have my first instance here
and I need to make sure
that I'm going to have a security group
that's going to be open.
So I'm going to find a security group
and make sure from the rules
that SSH such is going to be open
to all, to Anywhere and click on save Rule.
Here we go, we're go to go.
So I have my first instance
but I'm going to create a second instance.
I will create my second instance.
So the name is MySecondInstance.
I will choose Amazon Linux 2 on t2.micro.
For the key pair I will choose a demo key pair.
For the network settings,
I will select the existing AWS SSH security group.
And then I'm good to go.
I'm going to launch my instance and open it.
So now we have two EC2 instances being run.
And what I'm going to do is I'm going to first set them up.
So I'm gonna go
into EC2 Instance Connect, connect to my instance.
This is EC2 instance number one.
I'm going to make sure that I remove my hello.txt,
and so I'm going to,
so there's no files in here,
now I'm going to just create a new file.
I'll do it touch hello-instance-1.txt.
So we can see
now we have a file named hello-instance-1.txt available
to us, which is enough.
So I've set up my first instance.
And then for my second instance,
I'm going to do the exact same thing.
I'm going to connect to it using EC2 Instance Connect
and I'm going
to touch a new file named hello-instance-2.txt,
excuse me, which shows that yes,
we are on instance number two, okay?
So now that my two instances are available
as we can see, they have different IPv4 addresses.
And so what I'm going to do is first attach an Elastic IP
to my first instance.
So to do so on the left hand side,
I'm gonna go into Elastic IPs
and allocate a new Elastic IP address
from Amazon's pool of IPv4 address
but you could bring your own
if you wanted to.
Let's allocate this IP.
And as we can see now,
this IPv4 right here is accessible to me.
And so what I can do is
that I can attach it to my EC2 instance, so Action.
And then I can associate this Elastic IP address
with an instance
and I'm going to attach it with my first instance.
So, good, now it is associated with my first instance.
So if we go into my instances and look at my first instance,
we can see that the public IPv4 right here is 18.198,
which is the same as my IPv4 address
that is Elastic IP.
So what I mean is that if I do an SSH command now
and then I replaced the public IP
by the one I have from the Elastic IP and connect to it
and we do ls,
as we can see, I am on my instance one
because it says hello-instance-1.txt.
So, okay, I can disconnect.
And what I will do is I will launch the exact same command
but after remapping my Elastic IPv4,
so what I can do is go back to my Elastic IP, Action.
I will disassociate this Elastic IP.
So this is going to remove it
from my current EC2 instance.
And then I will associate this Elastic IP
but this time with my second instance,
and then Associate.
So now it has been associated
with the second EC2 instance.
And so if I rerun this command right here
so we are in, okay, and we get us some warning
because the remote host identification has changed.
So I'm on the different EC2 instance.
And this is the security that my SSH command has,
which is saying that,
hey, it looks like you're connected to a different host
even though you're using the same command.
So this is expected for us,
but this is a nice warning
to know in case you're being attacked.
And so if I do clear my screen and do ls,
this time I see hello-instance-2.txt,
which shows that I am connected to my second instance.
So this is really the power of Elastic IP here.
You can use the same command and be redirected
to two different instances.
And so this is how we can mask the failure of instance one,
for example, by remaking the Elastic IP
to the second instance.
So to finish this, please make sure
to terminate your second instance
and go to your Elastic IPs.
And what you can do is just disassociate this Elastic IP.
And then when this is done
you can release the Elastic IP address
in order not to be billed for it,
because it will be unused.
Okay, so that's it for this lecture.
I hope you liked it.
And I will see you in the next lecture.