So let's talk about cloud formation nested stacks
because these can happen at the exam.
Nested stacks are stacks in other stacks,
hence the name nested.
They basically allow you to isolate a pattern
that you need to repeat or maybe common components
in two separate stacks and just call them from other stacks.
If you know, for example, how to configure a load balancer
really well and you want to re-use the configuration
all the across the board for your company,
then instead of copying and pasting,
you would isolate that load balancer into a nested stack
and reference that from within your stack.
Maybe it's a security group maybe, the SSH security group
that you want to have a specific configuration for it
maybe you want to isolate this into a nested stack.
So they're considered best practice
and we'll see how to use them in a second.
So by the way, if you wanted to update a nested stack
then you will always update the parent first,
the root stack, and then everything
will get updated from there.
So that's the idea.
They could have very small questions at the exam
so let's go have a look at how it works in practice.
So, let's create a stack and this one will
be a template file.
And that will be the seven nested stack .yml.
So before we do this, let's have a look
at what's inside of that file obviously
So in this one, we have an SSHKey.
This is the keypad we're going to use to
SHH into our instance
and now, the cool thing though, is that we have
a cloud formation stack as a type.
We have myStack within resources and the type of which
is an AWS: Cloudformation: Stack.
That is enough for you to declare a nested stack.
So the properties is that you have to define a template url.
So I used this template url right here which is the
LAMP Single Instance dot Template.
So we could follow this link.
So I'll just copy this link just so you can see what it is.
Let's go have a look in there.
This is just a template file and this is in json form,
but it doesn't really matter.
So you can see this has parameters.
There's Key Name, DBName, DBUser, and all these things
are parameters that we have to pass.
Then there is a mapping that was defined
and then at the very bottom, resources.
It will create a web server instance of EZ2 Instance
and on there it would install using, actually,
CloudFormation::Init.
So here we see again, CloudFormation::Init.
It will install mysql, httpd, php, so a lot more.
Then in the files it will basically correct
an entire html file in there which is quite nice.
So as you can see, CloudFormation::Init can be used
to do some pretty crazy stuff and install goes
on and on and on.
At the very end we have still services running
and some commands to configure everything.
The cool thing is that now this stack,
we won't copy and paste it into our stack,
we're just going to call it as a nested stack.
So let's go have a look here.
What you see is the TemplateURL and this will
automatically pull in that stack for us.
We'll pass in the parameters KeyName, DBName, DBUser,
DBPassword, DBRootPassword, InstanceType,
and finally where we launch SSH from.
The cool thing is that we can reference outputs
of this stack directly to our output values.
So we can, for example, get an output from a nested stack
by doing mystack.output.websiteurl.
So let's go have a look, because I think
when we actually use it it will make more sense.
So here we go, we're going to click next.
I'll call it nested-stack-example.
SSHKey will be the same as before.
Click on next and click on create stack.
So here the important thing is that within capabilities now
it's saying, because you're using AWS::CloudFormation::stack
you need to acknowledge that you might create IAM resources
and also that you need to have this IM
Capability_Auto_Expand that you need,
because you need to expand the nested stack
we just referenced.
So we'll just tick these two boxes
and click on create stack.
Here we go.
So now our thing is being initiated.
The cool thing is that now this stack itself
will be created and it says create in progress.
So if we go back to the stacks, now we can see
that even though I uploaded only one file, nested-stack-ex
within this thing there was a new stack
with a new stack name that is being created
and that is the whole file that I just showed you
from before which is right here.
This stack is, as we can see, nested.
So it says nested right here and within it
we can look at the events and this is where we see
the web server security group, the web server instance,
everything being basically created as we speak.
We get a description, so all these things are from within
the nesting templates.
Now the only thing we have to do is just wait
for the web server instance to configure itself.
Alright, so it looks like this nested stack
has been completely created because the wait condition
has received a success signal again.
Now if you go back to my parent stack,
its called a parent stack, the main one,
then if you go into resources, as you can see,
we get a create complete and the events,
is that as soon as my nested stack has completely been
created then we get a full create complete
for the parent stack as well.
So the really cool thing is that if you go to outputs now
we get a URL to access our underlying ec2 instance
and that came as an output directly from the nested stack.
So let's go have a look and it says,
Welcome to the AWS CloudFormation PHP Sample
and it says I connected to localhost successfully.
So that's pretty awesome, I mean, it's just pretty basic,
but it shows how nested stacks works and then
importantly, you never ever ever ever touch the nested stack
ever, you don't update this directly.
What you would do is you update the parent stack.
So what we would do is to delete everything,
we'll delete the parent stack.
So I'll delete the parent stack and automatically
my nested stack will get deleted as well.
So that's about it.
Okay I hope you enjoyed it and I will see you
in the next lecture.