AWS CloudWatch allows you to monitor events and logs from the services you are running. There are a set of default metrics provided, and you can also create you own custom metrics.
From a running EC2 instance, let’s look at the metrics displayed beneath your selected instance, on the Monitoring tab:
By default we get metrics displayed for:
- CPU utilization
- Disk reads/writes bytes and operations
- Network in/out bytes and packets
- Status checks failed
Now let’s create a new CloudWatch dashboard and add some metrics. Press ‘Create Dashboard’:
Next you can select a chart type, and then select from the available metrics. For EC2 there are 105 metrics to pick from:
Let’s see what options we have – you can enter filter values in the entry field. Let’s say I’m interested in disk reads/writes:
Notice the second column is by InstanceId, so if you have many instances (including it seems terminated instances which are showing in my list), make sure you pick the stats for the instance you want to monitor – here I’ve added a widget for Disk read/write bytes, and CPU utilization:
When an AWS EC2 User Data script fails, you’ll see something like this in /var/log/cloud-init.log in your instance:
2018-02-03 06:08:16,536 - util.py[DEBUG]: Failed running /var/lib/cloud/instance/scripts/part-001 
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/cloudinit/util.py", line 806, in runparts
subp(prefix + [exe_path], capture=False)
File "/usr/lib/python3/dist-packages/cloudinit/util.py", line 1847, in subp
cloudinit.util.ProcessExecutionError: Unexpected error while running command.
Exit code: 127
2018-02-03 06:08:16,541 - cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
2018-02-03 06:08:16,541 - handlers.py[DEBUG]: finish: modules-final/config-scripts-user: FAIL: running config-scripts-user with frequency once-per-instance
It tells you something failed, but not what. The trouble seems that output from your user data script does not go to the cloud-init.log by default.
One of the answers in this post suggests to pipe your script commands and output to logger into a separate log file like this:
exec > >(tee /var/log/user-data.log|logger -t user-data ) 2>&1
date '+%Y-%m-%d %H:%M:%S'
Now running my script with a ‘apt-get update -y’ looks like:
+ echo BEGIN
+ date '+%Y-%m-%d %H:%M:%S'
+ apt-get update -y
... output continues here
And further down, here’s my specific error I was looking for:
+ java -Xmx1024M -Xms1024M -jar minecraft_server.1.12.2.jar nogui
/var/lib/cloud/instance/scripts/part-001: line 11: java: command not found
My EC2 running the Ubuntu AMI does not have Java installed by default, so I need to install it with (adding to my User Data script):
apt-get install openjdk-8-jre-headless -y
… and now my script runs as expected.
EC2 pricing at first look seems simple: for on-demand pricing the current prices are listed here, as fractions of a dollar per hour, Here’s a quick look (below) at how these prices are listed for the first few t2 instance types. The list of different instance types is pretty extensive (check out the link for the complete list):
The page says the price is:
by the hour or second (minimum of 60 seconds) with no long-term commitments
and further down:
Pricing is per instance-hour consumed for each instance, from the time an instance is launched until it is terminated or stopped. Each partial instance-hour consumed will be billed as a full hour or per-second depending on which Amazon EC2 instances you run
I added the emphasis as this part is not entirely clear. So to summarize:
- charged by the second, minimum 60 seconds (this was a recent change, introduced in October 2017, see here)
- charged as a full hour depending on which Amazon EC2 instances you run
The last part of this last statement is (I think) the key. It refers to which AMI image you’re running, since some images have an hourly charge (if I’m wrong, please leave me a comment and let me know!) :
If you click the Marketplace link, you’ll notice AMI images with commercial products, along with an hourly charge for usage. Ahah, there you go!