Please note that this content is targeted for SysOps 1
administrators. If you're a Solutions Architect or a developer you may want to 2
skip over this one. Welcome back to backspace Academy in this practical lab 3
on implementing CloudWatch monitoring scripts on ec2 we're going to start off 4
by creating an ec2 server and then associating an IAM role to give it 5
permission to access the CloudWatch service and then we're going to use that 6
ec2 server to push metrics or custom metrics over to the cloud service and 7
then we'll be able to view those metrics in CloudWatch as well. So the first 8
thing we need to do is to create a role for our ec2 instance to access a cloud 9
watch service so we go to roles create role and we're selecting an AWS server 10
so we select ec2 and then use case ec2 to allow ec2 instances to call AWS 11
services which is what we want so we'll select that and next permissions so what 12
we need to do now is we need to create a custom policy so create policy and 13
create your own policy we'll give it a name 14
and I'll just paste in the policy document that I've created that will 15
give the ec2 instance access to that cloud what services have we need okay so 16
we just have a look at this policy document here so we've got an allow to 17
allay our ec2 instance to get metrics statistics to list the metrics and to 18
most importantly to put metric data there and we've just got that for all 19
resources and we've also got another one that it's required for ec2 describeTags 20
as well which is required for that that's script. So we'll create that 21
policy now, now what we need to do is that we need to go back to our role, so 22
now that we've created that policy we need to associate that policy with this 23
role, so we'll just search that policy now and there it is the one that we 24
created, so we select that one and we click on next review will give the 25
roller name we won't worry about a description there and we'll create that 26
role and then we can see there is our role that we just created we just have a 27
look and make sure it's okay and there's a policy that's attached to it and we 28
can see that it's got CloudWatch permissions that seems to be fine there 29
so now we can go into the ec2 management console and start creating this ec2 30
instance they can collect these CloudWatch metrics, so we'll go into the ec2 31
dashboard click on launch instance, we will select an Amazon Linux AMI and a t2 32
micro now what we want to do is that we want to assign that I am 33
role to this ec2 instance so we select on oh I am role and will select that ec2 34
monitoring role that we created the next thing that we want to do is we want to 35
expand advanced details because we want to put in a BASH script in here and what 36
that is going to do it's going to pull in the required files to run the perl 37
script so it will have some some the Perl language environment which will be 38
downloaded and installed and then it will also in download and unzip the 39
actual Perl scripts that are going to be run so just jumping back into the lab 40
notes and I'll just grab that script 41
okay so just put that in there and I'll just expand it out a bit and show you 42
what it looks like okay so we can see they were doing a yum update so just 43
making sure that the the Linux operating system is up to date and then we're 44
installing our Perl requirements that we need then we're doing a curl to download 45
that zip file and then we're just done unzipping it into that CloudWatch 46
monitoring scripts directory that we'll have so that looks fine we'll just 47
review and launch and what I would like to do now is just to give it a name just 48
so we can find it easy enough so just enter tags add a tag and put a name in 49
there for it and just call it monitoring that'll be fine so we review and launch 50
and launch select the key pair and we'll launch that instance 51
so of course after a certain amount of time that will be up and running and 52
then we can start to use our CloudWatch monitoring scripts okay so our ec2 53
instance is up and running it's passed its status checks it's ready to go so 54
what we can do now is connect to it so I just click on connect grab that 55
connection string that I need and jump into cmnder 56
so I just navigated to where my key pair for this instance is and just connect 57
in and now I'll navigate to where the monitoring scripts are 58
so there we can see we've got our mon-get instance stats and Mon put instant 59
data, so we can test that out now without actually publishing anything to 60
CloudWatch just with a simple command so there we've got Mon put instant 61
data and we're going to get the memory utilization metric so let's run 62
that so then we can see its memory utilization is 8.1 3% it's it's tried to 63
look for credentials it hasn't found it so it's trying the default IAM role 64
and it's found our ec2 monitoring om role which is fine and then it's 65
delivered as JSON our payload there and so there we can see the value is 8.13 66
the unit is percent and the metric is memory utilization, so what we'll do 67
now is it will try and publish or push this out to the CloudWatch service and 68
see if we can pick it up in CloudWatch in the CloudWatch console. Ok so 69
this command now will actually push it out to CloudWatch, so we'll see if that 70
works and then we can see it's successfully 71
reported metrics to CloudWatch so we can now jump into CloudWatch and have a 72
look and see if it's actually picked it up ok so let's just go to services and 73
CloudWatch and we'll see if we can find that metric that's been pushed out to 74
Claire watch so we're going to browse metrics and we're looking for Linux 75
system metrics and we select instance ID 76
and then we can see we've got some for some instances and this is the one we're 77
looking as for as our instance name is monitoring and so that we're looking for 78
so we did actually publish memory utilization so I'm just going to select 79
that one and we can see there we've got one point there of 27.6 80
for memory utilization so let's just jump back into the command line 81
interface and see if we can send another data point and see where that gets 82
picked up as well okay so yeah jumping back into the command line interface 83
we've seen how we can publish or push a metric information out to CloudWatch 84
manually so what we don't want to do now is to set up a CRON job that can do that 85
on a regular basis so we've set it up so it does it on every minute we'll give 86
the instance you know 10 minutes or so and we'll just see what happens and 87
whether it actually gets picked up so the first thing we need to do is to go 88
into crontab or a cron table and edit that so I'll just go and grab the 89
command that we need okay so I just press the insert button 90
and they'll bring me into insert I'll paste that in and we can see there that 91
we've got the cron schedule which is just all stars which means every minute 92
it will be running and it's going to run that mine put instant starter and going 93
to publish some metrics out to cloud watch for us so just get out of that by 94
pressing escape and then the colon (:) and WQ 95
exclamation mark and that will quit and save that for us and it's probably good 96
idea just to check that it worked okay so just did crontab -e and we can see 97
that's fine, so I can just get out of that again, okay so we leave that for a 98
while now I'm just going to go away and have a cup of coffee I'll come back and 99
then we'll have a look at CloudWatch through the console and we see if for 100
any of these metrics are being published every minute as they should be okay so 101
it's been about 20 minutes or so and we should have a good amount of data that 102
is coming in from our our cron job that we created so just back here in the in 103
the CloudWatch console I'll select the monitoring instance and memory 104
utilization we'll see what we've got and there we can see they've got quite a bit 105
of data from 11:45 up to 12:05 it's gone from 27.6 up to 27.85 106
down and up again. So that's working great so what we've done is that we've 107
created means ec2 instance with an installed a perl script now that Perl 108
script was created by AWS and it's collecting system information and 109
publishing that to CloudWatch for us and we're now here able to see it so 110
it's something that not only do you need to use AWS scripts you can actually do 111
this yourself and I'll give you a good example is that you may have for example 112
an Internet of Things device that is collecting data and you want to push 113
that data out to somewhere to be stored so a great way to do it is is to really 114
push it out as a metric if you're if you're recording a metric push it out to 115
CloudWatch and you can view it in cloud watch and you can also use CloudWatch 116
logs to archive that information as well so there's some great opportunities to 117
do that and all you need to do is you know if you're using an SDK be using the 118
nodejs SDK or the PHP or Java or whatever SDK it is so we'll have a 119
feature there in CloudWatch to to put metrics out so just as an example I can 120
jump into the AWS SDK for JavaScript documentation go into the API reference 121
select CloudWatch and if I scroll down here we can see that we've got put 122
metric data and so that publishes metric data points to Amazon CloudWatch and 123
it'll do that as a custom metric just the same as our perl script has done so 124
if you have got any use cases for that that's great and you don't need to you 125
know you don't need to use the AWS scripts if you've got your own scripts 126
that you want to use to collect whatever data you want to use by all means go 127
ahead and do that so I think we've learned quite a lot 128
and I'll see you in the next lesson.