To enable logging from the EC2 instance to CloudWatch, a logging agent needs to be installed on the EC2 instances. As an example, we will show you the logging related to a Tomcat access log file. Other log files can be handled similarly. Ensure you install this agent in your base AMI image:
- Install the AWS command client library as described in Chapter 4, in the Scripting Auto Scaling section.
- The next step is to install the logging agent itself. Since our EC2 is based on Ubuntu, the agent needs to be downloaded and installed on the EC2 instance:
- Download the CloudWatch logging agent:
wget https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py
-
- Install and configure the Cloudwatch agent. The –region command-line parameter specifies the AWS region in which your current AWS EC2 instances and infrastructure is running. The log file to push to CloudWatch is defined step 4; make sure the filename exists:
sudo python ./awslogs-agent-setup.py --region --us-west-2
Launching interactive setup of CloudWatch Logs agent ...
Step 1 of 5: Installing pip ...DONE
Step 2 of 5: Downloading the latest CloudWatch Logs agent bits ... DONE
Step 3 of 5: Configuring AWS CLI ...
AWS Access Key ID [None]:
AWS Secret Access Key [None]:
Default region name [us-west-2]:
Default output format [None]:
Step 4 of 5: Configuring the CloudWatch Logs Agent ...
Path of log file to upload [/var/log/syslog]: /var/log/tomcat7/access_log.log
Destination Log Group name [/var/log/tomcat7/access_log.log]:
Choose Log Stream name:
1. Use EC2 instance id.
2. Use hostname.
3. Custom.
Enter choice [1]: 1
Choose Log Event timestamp format:
1. %b %d %H:%M:%S (Dec 31 23:59:59)
2. %d/%b/%Y:%H:%M:%S (10/Oct/2000:13:55:36)
3. %Y-%m-%d %H:%M:%S (2008-09-08 11:52:54)
4. Custom
Enter choice [1]: 3
Choose initial position of upload:
1. From start of file.
2. From end of file.
Enter choice [1]: 2
More log files to configure? [Y]: n
- After configuring and installing the CloudWatch instance in your base AMI image, start the logging agent:
sudo service awslogs start
- From the CloudFormation web console, navigate to Logs; there will be an entry for /var/log/tomcat7/access_log.log, which implies the log agent has been installed and configured correctly.