Okay, so I'm going to give you
a short introduction to CloudFormation,
because it's so important to see how this service works.
So in CloudFormation we're going to create a stack
and then you have to provide a stack, templates.
So I wanna make sure that before you create a stack,
the stack template I have to use
has to be used in US East 1,
so please click on the region,
and click on US East Northern Virginia, US East 1,
otherwise things will not work for you
and you will get an error.
So please everyone, go to the US East Northern Virginia,
US East 1 region.
Okay, so next we have to create a stack through a template,
so we can use some sample templates,
or even create one in designer,
but I already have a template that is ready for us.
So we're going to use the ready to make templates,
and then we can either say that the template
is in Amazon S3, or we can upload it.
And we are going to upload it,
so I'm going to click on upload a file,
and within the code I will choose CloudFormation.
And within it I will choose 0-just-ec2.
This CloudFormation template is going to create
and EC2 Instance for us.
If you wanted to look at what is within that template,
it is not necessary to know how this works,
but as you can see it's quite easy to read.
We are going to create resources,
one is going to be called My Instance,
the type of it is an EC2 Instance
and then you define some properties about that EC2 Instance.
For example the availability zone,
UE East 1a,
the image ID so the AMI ID,
and the instance type, t2 micro.
And that's it.
This should create for us an EC2 Instance.
So we are uploading this file, this is good,
then we click on View in Designer
if you want to see what this template look like
in the designer, in the graphical way.
As we can see, very quickly as soon as I zoom out.
This is a very simple Instance,
just an EC2 Instance.
And it is alone right now.
Okay, I will click on next
and the I will call it DemoCloudFormation.
Okay, great.
Next you're able to set some parameters but for now
we don't have one in our template so there
is no input here.
I will click on next,
we could tag it, I will call it Name DemoCF
and I will scroll down.
We don't need to worry about these options,
we'll click on next and then you can see here
that you can estimate the cost.
I don't know how long this link is going to be working
because the Simple Monthly Calculator is going away,
but right now you can click on estimate cost
if you wanted to.
So we review everything, I will scroll down
and then I will click on create stack.
And as you can see what I did is that I uploaded
a template so the template is here,
this is just some lines of codes.
And this template is actually going to be used
to create an EC2 Instance.
So this is why CloudFormation is called
Infrastructure as Code.
So if we go to events we can see all the events
that are happening for our stack creation.
And as we can see MyInstance is now in
create-in-progress, and so that means that the
EC2 Instance is now being created.
We can verify this by going into the EC2 console.
So let's go into the EC2 console,
and as we can see in the EC2 console,
I am also in the US East Northern Virginia region.
Then you click on Instances and you will see that
yes this EC2 Instance was created for me,
the name is DemoCF, the type is T2 micro,
and the AMI ID is the one we specified.
As well as the availability zone which is US East 1a.
So whatever we had specified in our template is
what has been created in AWS.
So when we're done we get into the create-complete status.
And now if you look at the stack info, it is
create-complete and it fully done.
So now we can use that EC2 Instance,
and do whatever we want with it.
And this is the power of Infrastructure as Code.
So these are our confirmation templates,
but on top of creating one you can update it.
If I go to update,
I'm able to replace the current template,
and upload a new one.
And in this case I will be choosing
1-ec2-with-sg-eip.yaml,
and this template is a little bit more complete.
Because if we have a look at it, we have more stuff.
We have some parameters,
we have some resources which contains my EC2 Instance
but in this now there is security groups attached
to my EC2 Instance.
I defined an Elastic IP,
I defined two security groups
and their security group rules.
These security groups are going to be attached
to my EC2 Instance.
So this template is more complicated,
but now we have four kind of resources.
CloudFormation is smart enough to figure out what
to do with a template and how to create things
in the right order.
Let's have a look.
I'm going to click on Next,
and now we want to specify the Security Group Description,
so Demo Security Group Description through CloudFormation.
As you can see,
we can enter whatever we want in the parameters.
I will click on next, and maybe it's going to be too long
so I'll just do Demo Security Group just to be safe.
I will click on next, I can change the tags
but I will keep the as is.
We'll scroll down click on next,
and then I will scroll down again
and as we can see in the very bottom.
There is something called a change set.
Now a change set is what CloudFormation will do
if we apply this stack.
As we can see it will add an Elastic IP,
it will add two security groups, okay,
and for the instance it will modify it.
And to modify it, it will do replacement True
so that means that the previous instance is
going to be terminated and a new instance
will be created with the right configuration.
In this example, CloudFormation will have to destroy
and terminate the previous instance.
Something that's good to know.
Okay, so we're happy with these changes,
so let's go ahead and update the stack.
And now we are into update-in-progress.
And so CloudFormation will do behind the scenes
all the necessary changes in our AWS Cloud,
to get to the end state we have specified through the
templates.
In the meantime we can do this template in designer,
to see that it's a little bit more complete.
In this example, we have MyInstance,
connected to an Elastic IP,
and connected to two security groups.
And this is again, a nice way to visualize this.
Back into our CloudFormation template,
let's have a look at the other tabs,
so parameters is what we had as the value of the parameter.
Resources is what is getting created by our CloudFormation
template, so as we can see so far,
two security groups have been created and
MyInstance is in update-in-progress.
I will need to wait a little bit.
We are waiting for the events,
for my Elastic IP to also be created,
and it happens right now,
so create is in progress.
It is going to be very very quick.
As soon as I finish refreshing this,
we can see in the resources that now
four resources have been created,
and so my template is almost done.
Back into my EC2 console, if I refresh this,
I can see that now I have two EC2 Instances.
One that was created from before, and
one that is created right now.
And because we only need one EC2 Instance,
then expect the other one to be terminated very very soon
by CloudFormation.
If I refresh we'll see this in seconds so the
Elastic IP is now being fully completed.
If I go to events, now there's a cleanup in progress,
and the cleanup is actually going to terminate
the other EC2 Instance I had created.
Back into these two instances,
I will refresh this and now these other
instance is shutting down.
As you can see, I did nothing but write code,
and CloudFormation figured out exactly what to do,
what to create in the right order,
when to delete EC2 Instances and so on.
That's the really, the big magic of CloudFormation.
As we can see, instances have been created,
if I go to tags they're tagged with a name I gave it,
but also they also get some CloudFormation tags
automatically.
If I go to my security groups in here,
I will see the security groups created by CloudFormation,
there are two right here and if I go to
Elastic IP,
we haven't really seen Elastic IP but it is
a static public IP.
We can see one has been created right here.
CloudFormation really allowed us to have all these
things created for me, automatically,
and now the update is complete
for my CloudFormation templates.
And what about cleanup?
Well, as you can expect,
CloudFormation can cleanup after itself,
so we don't need to go and delete things manually
in the EC2 console.
No, no, no.
Instead what we can do is to get the stack,
and then you click on delete and this will delete
all the resources associated with a stack.
So I'll click on delete,
and now the delete is initiated,
and again CloudFormation will figure out
in which order to delete these stack elements
and resources.
And this is great because we can just clean up after
ourselves and will be done for this hands-on.
So, all in all, to summarize
CloudFormation is a really easy way to define templates
and resources and Infrastructure as Code.
And CloudFormation allows you to take the same template
and if it's well written you can deploy it to many
AWS regions or many AWS accounts.
And that makes it, really a base foundation
for so many other types of infrastructure on AWS.
So I hope you like this lecture,
I hope that makes sense,
and I will see you in the next lecture.