-: Now let's talk about the shutdown behavior
of your EC2 instances.
So the question is, how should the instance react when
the shutdown is done from within the operating system?
So not within the console, but within the OS.
So by default, it is stop.
That means that if you stop the operating system,
then the EC2 instance will stop as well.
But you have a second option, named terminate.
That means that if you have an EC2 instance running
and you initiate a shutdown command from within,
then two options.
First, you stop your EC2 instance,
which is what we've seen so far.
Number two is to terminate the instance.
That means that it will be gone.
So this is not applicable when shutting
down the instance from the AWS console.
So, if you stop the instance by doing right-click,
stop instance, it's not going to terminate it.
It's only when you initiate the shutdown
from within the EC2 instance.
And the CLI attribute for this is
InstanceInitiatedShutdownBehaviour
which makes sense
because the shutdown is initiated from within the instance.
The second thing to know is
that there is a termination protection
on your EC2 instance that you can enable.
This is to protect against accidental termination
within the console or the CLI.
And so, an exam tip is that
if you have an instance where the shutdown behavior
is terminate and you still enable termination protection
then if you shutdown the EC2 instance from the OS,
what will happen?
Well, the instance will still be terminated
because you haven't been doing this from the console,
but you've done this from within the OS.
Okay? So let's have a look at this behavior in the console.
So let's launch an instance
and we're going to play
with the termination protection setting.
So let's choose Amazon Linux 2.
We choose the 64-bit.
We choose t2.micro
I will use whatever key pair, it doesn't really matter.
For network settings I will use
the same AWSSSH security group as before
I'm going to-
don't change the storage
and for advanced details,
this is where things get interesting.
So there is the shutdown behavior,
and what happens when you shut down the instance
either stop or terminate, stop is the default,
but then let's choose Terminate.
And I'm also going to enable termination protection
to show you that even though we have
enabled termination protection
when we shut down from within the instance,
then the instance itself is going to terminate, okay?
So let's verify this.
I'm going to go ahead and launch this instance.
And now it is successfully initiating. So let's find it.
This is the one I wanna look at, okay.
So if you look at this instance
and I do instance state and then terminate
It'll say, "would you like to terminate this instance?"
I say, "yes, please terminate."
You're going to get an error because, well,
this disableAPITermination instance attribute is not
at the correct value.
So that means that we have enabled termination protection.
And therefore, what you can do
is that you could change from the instance setting
the termination protection to disable it.
And once you disable the termination protection
then the instance can be terminated using the console
or an API code.
But, we don't want to disable it, okay?
We want keep it like this.
So we want to keep it enabled.
And what I show you is that even though
we have the termination protection currently enabled
on this instance, we can still terminate it by shutting it
down because of the shutdown behavior we set from before.
So to do so I'm going to use EC2 Instance Connect
to connect into my instance.
And then I will type "sudo shutdown".
And this-
this command actually shuts down the instance from within.
So this says an operating system type of shutdown.
Okay, it happens from within the operating system
of your instance.
And, what's going to happen now
is that if I close this and wait a little bit,
so let me wait for it to happen.
This is my instance right here.
I'm going to wait until the shutdown command is issued
and then we'll see it stopping.
And then because of the shutdown behavior
that has set before, in case a shutdown actually,
it will terminate the instance right away.
This is a setting I just enabled.
So let's observe this behavior.
And as we can see now, this was very quick,
but now the instance state is in the terminated phase.
So it actually got terminated, even
though what I did was issuing a shutdown command.
And that's because of the shutdown behavior we just set.
Okay. So that's it for this lecture.
I hope you liked it.
And I will see you in the next lecture.