In today's hybrid cloud environment, managing your on-premise servers can present unique challenges. You might want to consolidate data monitoring from your on-premise servers with the data you collect in the AWS Cloud. A great solution for this is using the AWS CloudWatch Agent.
Understand how to register Debian-based on-premise servers to AWS Systems Manager using Hybrid Activation. Configure how to install and set up the CloudWatch Agent on the server. Send logs from your on-premise server to AWS CloudWatch.
An AWS account with the necessary permissions. A Debian-based server (in this example, we will use an EC2 instance in a different AWS account).
Step 1: Setting Up Hybrid Activation in SSM
Access the Systems Manager Console: Login to the AWS Management Console and open the Systems Manager console.Create a New Hybrid Activation: In the Systems Manager navigation pane, choose Hybrid Activations under Node Management. Click Create Activation .
Configure the Hybrid Activation: Activation Description: Provide a clear description, for example, "On-Premise Server Registration".Instance Limit: Specify the number of on-premise servers you want to register.IAM Role: Choose or create an IAM role that has the necessary permissions for Systems Manager.Registration Expiration Date: Set an expiration date for this activation; after this date, the activation will no longer be usable.Click Create Activation .
Save the Activation Code and Activation ID: After creating the activation, you will receive an Activation Code and Activation ID. Carefully note this information as you will need it later to register your on-premise server.
Step 2: Registering Your On-Premise Server to AWS Systems Manager
Update the Package List: Start by updating your server's package list:sudo apt-get update
Install SSM Agent: Download and install the SSM Agent:mkdir /tmp/ssm cd /tmp/ssm wget https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/debian_amd64/amazon-ssm-agent.deb sudo dpkg -i amazon-ssm-agent.deb
Register the Server to SSM: Register your server using the Activation Code, Activation ID, and your AWS region:sudo amazon-ssm-agent -register -code "<your-activation-code>" -id "<your-activation-id>" -region "<your-region>"
Example: sudo amazon-ssm-agent -register -code "h7FfWBbOrDCeXexxxxxx" -id "914e2266-e1c1-4c3a-b638-2azzzzzzzzzz" -region "eu-central-1"
Start the SSM Agent: Once registered, start the SSM Agent:sudo systemctl start amazon-ssm-agent
Enable the SSM Agent to Start on Boot: Ensure the agent starts automatically on boot:sudo systemctl enable amazon-ssm-agent
Verify the SSM Agent Status: Finally, confirm that the agent is running:sudo systemctl status amazon-ssm-agent
Step 3: Install and Configure Nginx (For Log Collection)
Install Nginx: sudo apt update sudo apt install nginx sudo systemctl status nginx
Nginx Log Files: The Nginx log files we will send to Cloudwatch are:/var/log/nginx/error.log /var/log/nginx/access.log
Step 4: Install CloudWatch Agent Using SSM
Access the Systems Manager Console: Go to the Systems Manager console in your AWS Management Console.Run Command to Install CloudWatch Agent: Navigate to Run Command .Choose AWS-ConfigureAWSPackage from the list of command documents.Select your on-premise server as the target. Set Action toInstall .Enter AmazonCloudWatchAgent in theName box.Leave the Version field blank to install the latest version.Choose Run .
Step 5: Configure CloudWatch Agent
Create an IAM User with Necessary Permissions: First, you need to create an IAM user that has permissions to send logs to CloudWatch. Steps to create IAM user: Log in to the AWS Management Console and open the IAM console. Create a New User: Navigate to Users and clickAdd user .Enter a user name (e.g., CloudWatchAgentUser). Under Access type , selectProgrammatic access to generate an access key ID and secret access key for this user.
Attach Permissions: Click Attach policies directly .Attach the following managed policies to the user: CloudWatchAgentServerPolicy CloudWatchAgentAdminPolicy AmazonSSMManagedInstanceCore These policies provide the necessary permissions to send logs to CloudWatch, access SSM, and interact with the CloudWatch Agent.
Finish Creating the User: Proceed to review and create the user. On the final page, ensure you download the .csv file containing the Access Key ID and Secret Access Key, or copy them to a secure location. You will need these credentials in the next step.
Configure the Server with the IAM User Credentials: Now that you have the Access Key ID and Secret Access Key, you need to configure your server to use these credentials by creating an AWS CLI profile named AmazonCloudWatchAgent. Configure AWS CLI with IAM User Credentials: On your on-premise server, run the following command to configure the AWS CLI with the IAM user credentials: sudo aws configure --profile AmazonCloudWatchAgent
When prompted, enter the following details: AWS Access Key ID: Enter the Access Key ID you obtained earlier.AWS Secret Access Key: Enter the Secret Access Key.Default region name: Enter the region where you want the logs to be sent (e.g., eu-central-1).Default output format: Leave this field blank or enter json.This creates a profile named AmazonCloudWatchAgent on your server that the CloudWatch Agent will use to send logs to AWS CloudWatch.
Update the CloudWatch Agent Configuration: If you are simulating an on-premise environment using an EC2 instance, you might need to update the CloudWatch Agent configuration file (common-config.toml) to use the newly created profile. Update the Configuration File: Open the common-config.toml file: sudo nano /opt/aws/amazon-cloudwatch-agent/etc/common-config.toml
Uncomment and update the following section to include the profile name and credentials file: [credentials] shared_credential_profile = "AmazonCloudWatchAgent" shared_credential_file = "/root/.aws/credentials"
Save and exit the file.
Step 6: Start CloudWatch Agent with the Pre-Created Configuration File Using SSM
Create and Store the Configuration File in SSM Parameter Store: First, create the CloudWatch Agent configuration file on your local machine. Here is an example configuration: { "agent": { "metrics_collection_interval": 60, "logfile": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log" }, "logs": { "logs_collected": { "files": { "collect_list": [ { "file_path": "/var/log/nginx/access.log", "log_group_class": "INFREQUENT_ACCESS", "log_group_name": "{instance_id}-nginx-access.log", "log_stream_name": "{instance_id}", "retention_in_days": 7 }, { "file_path": "/var/log/nginx/error.log", "log_group_class": "INFREQUENT_ACCESS", "log_group_name": "{instance_id}-nginx-error.log", "log_stream_name": "{instance_id}", "retention_in_days": 7 } ] } } } }
Once your configuration file is ready, store it in the AWS Systems Manager Parameter Store: aws ssm put-parameter --name "CloudWatchAgentConfig" --type "String" --value file://configuration_file_pathname
Replace configuration_file_pathname with the actual path to your configuration file.For more information on how to create a cloudwatch agent configuration file, visit [Link].
Access the Systems Manager Console: Return to the Systems Manager console.
Run Command to Start CloudWatch Agent: Navigate to Run Command .Choose AmazonCloudWatch-ManageAgent from the Command documents.Select your on-premise server as the target. Set Action toconfigure .Set Mode toonPremise .In the Optional Configuration Location box, enter the name of the configuration file stored in the Parameter Store (e.g., CloudWatchAgentConfig).Choose Run .
Verifying CloudWatch Agent Logs
sudo tail -f /opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log
Notes on KMS Encryption for Log Groups
aws logs associate-kms-key --log-group-name LOG_GROUP_NAME --kms-key-id KEY_ARN
0 comments:
Post a Comment