Instructor: Let's talk about resources.
So resources, they're your core
of your CloudFormation templates and they're mandatory.
Your CloudFormation template cannot work
without the Resources block.
And the resources, as the name indicates,
represent the different AWS components--
that's just a synonym--
that will be created and configured.
And the resources are declared
and they can reference each other,
so you can link the resources together.
For example, you can link a security group
and an EC2 instance.
AWS will figure out the creation, updates,
and deletion of the resources for us,
which is super nice and you should know there are
over 224 types of resources.
So I cannot teach you all of them, right?
But any resource has the following form.
They're AWS, then the product name,
and then the data type name.
So usually, you're reading this identifier variable
to figure out what we're going to create.
So how do I find all these resources documentation?
Well, there is this great link that has all of them,
so I can't teach you all the resources,
but I can teach you how to find a link that shows them
and so then you just read the doc.
For example, we're going to read the doc together
just for the EC2 instance,
just to get a feeling for it.
The first webpage I referenced to you is called
the AWS Resource Type References
and this contains all the references
that are supported by CloudFormation.
And the resource naming convention is here
and if you scroll down
you can see there are a lot of resources.
Honestly you could count them, too many.
So we can see that we can create a resource
for pretty much everything.
Let's scroll down and see if there's something we know.
Yes, Auto Scaling.
We know how to create Auto Scaling group.
We're able to create Auto Scaling group,
scaling policies, launch configuration,
all through this CloudFormation resources.
If we scroll down, we're able to do code builds,
code commit, code deploy, code pipelines.
So all these things we've seen before,
we're able to do them.
And if we scroll down we can see
there is the EC2 Instances,
the elastic IP,
and then the security group.
So let's have a look at EC2 Instance
because I think that's the one
we're the most familiar with.
So the AWS EC2 Instance creates an EC2 instance.
Because that's pretty obvious.
Now basically, we can scroll down
and look at the syntax of how to declare this EC2 instance.
We have a JSON form,
which to me is a little bit unreadable,
but we scroll down and we get the YAML form,
which to me is much clearer.
So this is the YAML form for EC2 instance.
It must be of type AWS EC2 and then Instance,
and it will have some properties.
So when you create the resource,
must have a type,
must have properties.
And properties are going to be key value pairs.
So as you can see here,
you can customize a lot of things
for your EC2 instance.
For example, if you wanted to
customize the IamInstanceProfile, we click on this
and we get teleported to the documentation for that,
and it says, okay, you need a string.
It's not required,
and if you update this there's no interruptions.
That means that your EC2 instance
won't get terminated and recreated.
It will just attach the instance profile to it.
But if we change the ImageId for example,
which is also a string but not required,
then we in terms, if we update it basically
it will do a replacement.
That means that it will terminate the old instance
and replace it with a new one.
So let's go scroll back up.
And so here we understand that
there are so many things we can customize.
To know what we should specify
we should just look through the documentation.
Now if we look at what we created from before,
let's just take an example as just EC2,
we have an AvailabilityZone, an ImageId,
and an InstanceType.
And so basically I knew how to fill those
because if you click on AZ,
then you see that you have to specify
the name of the AZ where the instance is located.
And so this is quite great
and you can just keep on going with this
as much as you want.
So now we understand basically how this was created.
For fun, you could go into the resources here
and see we have an EC2 instance
but also we have an EIP
and so if we look at EIP,
so let's go back
one up and then search for EIP on this page.
There we go, we have EIP.
Here we can see that the syntax is much shorter.
We have an InstanceID and a Domain.
And so the InstanceID is what I have specified,
and the Domain I have not specified.
So here when we go back to our EIP
we can see that yeah, it makes sense.
I did specify the type to be an AWS EC2 EIP,
and in terms of properties
I only have specified the InstanceID.
We'll see what this Ref means in a second, okay?
But so the idea is that
we are able to link the documentation
to what we want to do.
And so obviously for a security group
we need to provide the security group rules
for ingress traffic
and because we can have many rules,
they're an array so let's go to
the security group documentation just to prove that point.
Security group is right here
and we go to YAML.
And if we look at SecurityGroupIngress,
we can see that is a list of Amazon EC2 group rules.
And so if we click on EC2 Group Rule
and we click on it, we go to YAML,
we get all the parameters that are available.
So going back to the syntax,
we get this little hyphen here
that defines it to be an array.
And here we get the first rule,
and here we get the second rule.
And so yeah, what we get out of this
is that everything we configure through the UI
can be written as code into your CloudFormation templates,
and this is how you know
how to redact your CloudFormation templates.
So that's it for resources.
That's honestly all you need to know.
Just remember there's a type,
and there's properties,
and all of this goes below the block called Resources.
So you need to picture there is a small indent
underneath Resources every time you declare a resource.
Now the frequently asked questions for resources.
Can I create a dynamic amount of resources?
No, you cannot.
Everything CloudFormation has to be declared,
so you cannot perform code generation.
You cannot have dynamic type of code generation.
And is every AWS Service supported?
The answer is almost.
There is only few small ones that are not there yet,
and you can work around that
using the AWS Lambda Custom Resources.
Just a little bit of trivia here.
You just need to know about it.
We will not write to Custom Resources Lambda in this course.
So that's it for resources, hope you enjoyed it.
Hope that makes more sense now,
and I will see you in the next lecture.