Narrator: All right, let's get
advanced with CloudFormation now.
Let's talk about user data.
So we know we can have user data
when an EC2 instance launches,
and we can set it through the console.
But there is also a way for us
to include this in CloudFormation directly.
And so for this, we'll pass the entire script
through the function called Fn::Base64,
and that will convert our script to a basic C4 string
that is what user data needs in CloudFormation.
So the good thing to know is that
when you run the script, all the log ...
And that's just not direct CloudFormation,
but just user data in general ...
all the log of the user data will be
in /var/log/cloud-init-output.log,
and we'll see this in a second.
So let's see how we can do this
in CloudFormation right away.
Okay, so I am in CloudFormation.
Make sure you're in North Virginia region as well.
And there's a new redesigned AWS CloudFormation console.
It doesn't change much, but we'll just click on it,
just to make sure that it looks like
you may get to see in the future.
All right, so we have this new UI.
It's exactly the same thing, don't worry.
So let's go ahead, and we're going to create a stack.
So the template is ready,
and we'll use and upload a template file.
But before that, let's go and see what the template is.
So we'll upload 3-user-data.yaml.
And so if you look at this template, it has one parameter,
which is the SSHKey we're going to use,
so make sure you have a Key Pair available in us-east-1.
If not, create it.
Then we're going to create an instance,
and it will be in us-east-1a.
It will use this ami, and this is why
we're blocked to use us-east-1a, is because
ami is our region based.
And then we'll have a Key Name Ref SSHKey,
so we'll then be able to SSH into our instance,
and then we have an SSH Security Group,
which allows us to SSH on Port 22 and also view on Port 80
because we'll install a web server.
And so the important thing here is user data.
As you can see, we pass through the function,
interesting function, Base 64, we pass a script,
and this whole script is preceded with a vertical pipe.
So the vertical pipe here means that this whole thing
is one giant string with new lines.
If you delete this, it won't work,
so you need to have that vertical pipe
to save all this as a script.
And so in the script, very simple.
We've seen this one before.
We install httpd, we start the system,
and then we say "Hello world" into index.html.
So that's about it.
Let's go and have a look at how we can do it.
So we upload a template file, so I'll choose it,
and it's user data, and then I click on next.
And so the stack name here,
I'll just call it user-data-example.
And SSHKey, if you do have a SSHKey, it will show up here.
Otherwise you can go in EC2.
So if you go to Service, EC2, and then on the lefthand side
of EC2, you would go all the way to Key Pairs,
and in Key Pairs you could create a key,
name it whatever you want,
and then it will download it for you.
So for me, I've already done that, so I'll just use
this CloudFormation Key Pair that I have.
I'll click on Next, and then I'll just ...
Won't specify any option, click on Next.
As you can see, the template is in s3,
and the parameter is SSHKey equals CloudFormation Key Pair.
Everything looks good.
And I'll click on "Create Stack."
So let's go ahead and now we're just going to have to wait
for our EC2 instance to come up
and ensure that the EC2 user data get applied.
Okay, so it looks like my stack is now completed,
so if I go to Resources and refresh, I can see that
there is my instance in a security group that was created,
so I'll click on my instance, and it will take us
straight to the EC2 management console, and in there,
we can look at the Public IP, for example.
Here it is.
And if I go to the Public IP url,
I get to Hello World from user data.
The cool thing I want to show you is
that we can SSH directly into this instance,
and we can see that the user data get executed
by doing cat /var/log/
and then cloud-init-output.log,
and this is where all the user data output will go.
If you press Enter, you can see that, for example,
this was the command when we were running yum install httpd,
so everything got installed,
and we get the output right here.
And then it was systemctl start httpd, enable httpd,
and then echo Hello World from user data, and then it says
Cloud-init has finished at this time.
So if there was any issue in our EC2 user data script,
we would look into that file.
Again, we would look into /var/log/cloud-init-output.log,
and in that file, we will discover where the error was,
which is quite nice when you debug.
But something to think about right here
is that we created our stack,
but our stack was create_complete working,
even if our user data did not work,
we would still have gotten a good green tick.
So we have to think about, how do we solve that problem?
And we'll see this in the next lecture.
But for now, you've learned how to use
user data with CloudFormation.
Congratulations.
I will see you in the next lecture.
And don't forget to always delete your stacks
right after you're done.