Collecting Logs from Amazon EC2 CentOS Instances

  1. Create IAM roles or users that enable the agent to collect metrics from the server and optionally to integrate with AWS Systems Manager.
  2. Install the agent package.
  3. Modify the CloudWatch agent configuration file and specify the metrics that you want to collect.
  4. Install and start the agent on your servers. As you install the agent on an EC2 instance, you attach the IAM role that you created in step 1. As you install the agent on an on-premises server, you specify a named profile that contains the credentials of the IAM user that you created in step 1.

If you’re going to use the agent on Amazon EC2 instances, you must create an IAM role.

To create the IAM role necessary to run the CloudWatch agent on EC2 instances

Sign in to the AWS Management Console and open the IAM console
Make sure if its a first system then add CloudWatchAgentAdminPolicy policy if its not and you will use system manager use CloudWatchAgentServerPolicy

  • In the navigation pane on the left, choose Roles and then Create role.
  • For Choose the service that will use this role, choose EC2 Allows EC2 instances to call AWS services on your behalf. Choose Next: Permissions.
  • In the list of policies, select the check box next to CloudWatchAgentServerPolicy. (Use CloudWatchAgentAdminPolicy if it is first deployment with systems manager as it will allow writing to parameters store and also add AmazonEC2RoleForSSM if planning to use systems manager) If necessary, use the search box to find the policy.
  • Choose Next: Review.
  • Confirm that CloudWatchAgentServerPolicy appears next to Policies. In Role name, enter a name for the role, such as CloudWatchAgentServerRole. Optionally give it a description. Then choose Create role.The role is now created.

 

Assign IAM Role to EC2 Instances

Go to EC2 and right click on your CentOS Instance choose Instance Settings and then Attach/Replace IAM Role

Install agent with Configuration Manager

 

  • In the navigation pane, choose Run Command.-or-If the AWS Systems Manager home page opens, scroll down and choose Explore Run Command.
  • Choose Run command.
  • In the Command document list, choose AWS-ConfigureAWSPackage.
  • In the Targets area, choose the instance to install the CloudWatch agent on. If you don’t see a specific instance, it might not be configured as a manged instance for use with Systems Manager. For more information, see Setting Up AWS Systems Manager for Hybrid Environments in the AWS Systems Manager User Guide.
  • In the Action list, choose Install.
  • In the Name field, enter AmazonCloudWatchAgent.
  • Keep Version set to latest to install the latest version of the agent.
  • Choose Run.
  • Optionally, in the Targets and outputs areas, select the button next to an instance name and choose View output. Systems Manager should show that the agent was successfully installed.

 

Install agent Manually(if you not using systems manager)

wget https://s3.amazonaws.com/amazoncloudwatch-agent/centos/amd64/latest/amazon-cloudwatch-agent.rpm

rpm -U amazon-cloudwatch-agent.rpm

Configure agent on first system
Ones configured on first system and loaded to parameter store the rest can be configured via systems manager

/opt/aws/amazon-cloudwatch-agent/bin/./amazon-cloudwatch-agent-config-wizard

After completed make sure you choose saving to parameter store if its first system of its class

 

To start agent

AWS Systems Manager

In systems Manager choose AmazonCloudWatch-ManageAgent under run command and also under Optional Configuration Location choose AmazonCloudWatch-linux

Manually


./amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:config.json -s

Note:
If you run into issues and want to use collectd

mkdir /usr/share/collectd, cd /usr/share/collectd and touch types.db

or

yum install collectd