How to install awslogs on EC2 instance

Admin
Posted:
March 24, 2025
Home
Posts
Detection Engineer
How to install awslogs on EC2 instance
Contents

We have an application running on an EC2 instance, and we've set up CloudWatch to monitor it. First, we’ll look the how do we install awslogs to the EC2. Application hosting on Apache server and log file is an default log path which is /var/log/apache2/access.log

Install awslogs to the EC2 instance

Installing AWS CloudWatch Logs agent (awslogs) on an EC2 instance allows you to monitor and store logs centrally in Amazon CloudWatch. Here are the step-by-step instructions to install awslogs using the provided `curl` command:

1. Connect to Your EC2 Instance

Use SSH to connect to your EC2 instance. Replace `your-instance-ip` with your actual EC2 instance's public IP address.

ssh ec2-user@your-instance-ip

2. Download the awslogs Agent Script

Use the `curl` command to download the awslogs agent setup script from Amazon S3:

curl https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py -O 

3. Run the Setup Script

Run the downloaded setup script using Python. This script will guide you through the configuration process.

sudo python ./awslogs-agent-setup.py --region us-east-1

Replace `us-east-1` with your desired AWS region.

4. Follow the Configuration Wizard

The setup script will prompt you for various configuration options. Here's what you might need to configure:

  • AWS Access Key ID and Secret Access Key: Provide your AWS credentials with the necessary permissions to create CloudWatch Logs groups and streams.
  • Default Region Name: Enter the AWS region where your logs will be sent.
  • Default Output Format: Choose an appropriate output format. JSON is a common choice.
  • Log File to Ship: Specify the log files you want to ship to CloudWatch Logs. You can specify multiple files and file patterns.
  • Log Group Name: Define a name for the log group in CloudWatch Logs.
  • Log Stream Name: Optionally, specify a pattern for generating log stream names.

5. Start and Enable the awslogs Service

Once the configuration is complete, start and enable the awslogs service to ensure it starts automatically on system boot:

sudo service awslogs start

6. Verify the Installation

Check the status of the awslogs service to ensure it's running without any errors:

sudo service awslogs status

That's it! AWS CloudWatch Logs agent (awslogs) is now installed and configured on your EC2 instance, and it's shipping logs to the specified CloudWatch Logs group. Make sure to adjust configurations according to your specific use case and security best practices.

Please check Quick Start: Install and configure the CloudWatch Logs agent on a running EC2 Linux instance documentation for further information.

Detect SQL Injection with Cloudwatch Logs Insights

You can potentially detect SQL Injection (SQLi) attacks using CloudWatch Logs Insights, but it's important to understand the limitations of log-based detection methods. While log analysis can identify certain patterns indicative of SQL Injection attempts, it might not catch all sophisticated attack techniques.

To detect potential SQL Injection (SQLi) attacks in your Apache access logs using CloudWatch Logs Insights, you would need to create custom log queries that search for specific patterns or keywords indicative of SQL injection attempts. By crafting queries in CloudWatch Logs Insights, you can identify potential security threats within your log data.

  1. Go to the AWS Management Console.
  2. Navigate to CloudWatch.
  3. In the CloudWatch dashboard, click on "Logs" in the navigation pane.
  4. Choose the log group containing your Apache access logs.
  5. Click on "Logs Insights" in the log group details page.
  6. Write your query into the query editor.
  7. Click on the "Run query" button to execute the query.

For example, you can write a query like the following to identify potential SQL Injection attacks in Apache access logs:

fields @timestamp, @message
| filter @message like /.*(SELECT|UNION|INSERT|UPDATE|DELETE|FROM|WHERE|DROP|AND|OR).*/

It looks like indicate that our query in CloudWatch Logs Insights matched 121 records out of 746 records scanned. This means that 121 log entries in your Apache access logs contain patterns indicative of potential SQL Injection attempts, based on the query you ran.

If you want to investigate these matched records further, you can analyze the specific log entries to gain more context about the potential SQL Injection attempts. Look for details such as IP addresses, requested URLs, and other parameters in these log entries to understand the nature of the attacks.

If you need more details about AWS Cloudwatch, you can check this course on LetsDefend.

https://app.letsdefend.io/training/lessons/aws-cloudwatch

Conclusion

In the rapidly evolving landscape of cybersecurity, having a robust Security Operations Center (SOC) is paramount. Our training focused on harnessing the power of Amazon CloudWatch to bolster SOC capabilities, providing real-time insights, proactive monitoring, and efficient incident response. As we conclude this training, it’s evident that CloudWatch stands as a cornerstone in the realm of cloud-based security.

Share
letsdefend description card

You might also be interested in ...

Start learning cybersecurity today