-: Alright we've seen user data, but it's pretty clunky,
as soon as you wanna have something
quite complex and quite structured.
So for this we'll use cfn-init.
And there is this block in the metadata of the resource,
that we will see in a second, called CloudFormation init.
And if you include it, you can use a script called
the cfn-init script,
which stands for CloudFormation init,
and it helps make basically,
complex EC2 configurations readable.
And the EC2 instance will query our CloudFormation service
to get the init data and then apply it.
And as soon as it applies it,
all the logs will go to the /var/log/cfn-init.log file
and we'll see it in a second.
So why does it matter?
Well you'll see in a second I'll show you it looks quite
more readable then our script.
So we have the CloudFormation Service and it will lanch
the EC2 Instance.
On this EC2 Instance, as part of the user data script,
I will run cfn-init, and the action of such will make the
EC2 Instance query CloudFormation Service to
retrieve the init data.
And then you will apply it.
So let's how this works now through an example.
Okay so as we can see in 3-user-data.yaml,
we have this giant script Base64, and it was a simple one,
but it could get way more complicated.
Think of like all the thing you want your EC2 Instances
to do when they start.
So now if you go to cfn-init.yaml,
we have the same parameters in a SSHKey,
and we have an Instance as well, running the same ami,
with the same key name and the same security groups,
but now the UserData is going to be a little bit different.
It's still a Base64, but now we on top of it,
passing through the Sub function.
We saw that vertical pipe.
And here we're going to do first thing,
update the cfn-bootstrap script to get the latest version,
and this is just something you have to do,
and we have then installed already because we use a
lynics ami, lynics two ami.
And then we are going to start the cfn-init script.
So we are going to run /opt/aws/bin/cfn-init.
Then we pass in the StackId, so this a StackId that
represents a variable of our stack.
Then once we want to retrieve some MyTnstance.
We want to retrieve the cfn-init metadata from MyInstance
which is right here on line 26.
So we're going to retrieve this metadata.
We specify the region,
which is the region of the stack, and in case of error
it will say exit failed to run cfn-init.
So the metadata itself,
is going to install a simple Apache HTTP page,
and it has an AWS CloudFormation init block.
And in there, there's a config,
and now we can read that config through.
I won't walk you through how I wrote this.
The important thing is to know
how it understands for the SysOps exam.
So here we have packages, and we say okay,
from yum packages I want to install httpd.
So here we just said install httpd,
and which is the exact same thing as
when we did yum install, httpd before.
So we are good here.
Then we have files, and files is say at this file at
var/www/html/index.html, must have some content,
the content being all these things.
So here you can see,
I have more stuff at h1 block and p block,
and was easier because I could use that vertical pipe again.
Where as in the user data script,
I had to this ECHO thing, which was a bit clunky.
So here we have more content, more freedom.
We can also set the mode of the file to be equal to 000644.
Then we can send some commands.
We can say hello,
and we'll just run the command echo hello world.
And finally we say services should start,
and we say okay, the httpd service should be enabled,
and we should ensure that it is running.
And so the cool thing about this CloudFormation init,
is that I think it is way more readable,
then the user data script, which is a basic bash script.
And so this is why we would use CloudFormation init.
The rest is the exact same thing,
but lets have a look at how this works in the console.
So here's the console, and we'll go create stack.
Template is ready, we'll upload it
and we'll choose the file cfn.init.yam
I'll click on next, and I'll call it cfn-init example,
and the SSH key, I'll use the same key as before.
Next, and then create stack.
Okay now our stack is created and I'll just wait a minute.
Okay so my stack has been created,
and if I go to my EC2 management console and refresh,
and see all my instances, this one has been running.
So I can go to the public IP
and make sure the cfn-init worked,
and it says hello world form EC2 instance.
This was created using cfn-init.
So pretty cool, it's working.
But the cool thing I want to do is to SSH into the instance,
and see what happened exactly.
So let's just go in SSH into this instance,
and we'll have to remove EC2 http.
Copy and paste mistake.
Here we go, we're inside of it.
So now as I said there is a var/log/cloud-init- output log
that we can look at.
And as you can see,
we just run this command cfn-init in there,
and we don't get really and output out of it.
We don't see anything.
So to see the output of this command,
what you have to do is
look at the file named var/logcfn-init.log.
And this file will contain the output of
our cfn-init script.
So as we can see here, it's installed httpd,
it did the command hello, it enables service httpd
and started the service successfully.
So in this, basically var/log directory,
you can see a lot of things.
You can see all the logs that come out of
the CloudFormation things and so it's pretty cool.
You can also look at this cfn-init-cmd.log,
that I felt could be interesting as well.
But the idea is to understand that if you need to
troubleshoot, everything they will be right here
and here we get the actual outputs of all the commands,
which if we wanted to debug for whatever reason,
we could look at this.
So yum install httpd, it worked very fine.
And then at the very bottom command hello world,
it said hey hello world, and it completed successfully.
So that's what I found really cool,
and now we have more structure and we have more files
to look at incase things go wrong and I think this is a
better way to configure your instances.
So the only problem we have is that,
this instance still is being created.
So we have still create complete,
but it doesn't matter if our cfn-init works or not.
What we'd like to do is for the create complete to be done,
only when I know that this has worked.
So how do we do this?
Well I will see you in the next lecture.