Skip to content

Cloud security logs: what to collect to investigate incidents

When it's time to investigate, the log either exists or it doesn't. See what to collect before you need it and how to organize it.

Reading time
9 min
Published

When an incident happens, the first question is always the same: what do the logs show? That is when many organizations discover that the log they needed wasn't enabled, or was in a region nobody covered, or had already expired. That is why cloud security logs need to be planned before the incident. There is no such thing as retroactive collection: what wasn't recorded at the time is lost.

In AWS environments, a few log sources answer most of the questions in an investigation: who acted, from where, what they accessed and which way the traffic went. Below are those sources, how to centralize them and how long it makes sense to keep them.

The sources that matter most

Each log answers a different question. CloudTrail tells you who did what in the account. VPC Flow Logs show the network traffic between resources, which helps you spot lateral movement and exfiltration. WAF logs show what was blocked and what got through at the edge, and load balancer logs record the requests that reached the application. On top of several of these sources, GuardDuty handles threat detection.

  • CloudTrail: administrative activity and API calls
  • VPC Flow Logs: network traffic and lateral movement
  • WAF logs: what was blocked and what got through at the edge
  • Load balancer logs: requests that reached the application
  • Application logs: the business context of what happened
  • GuardDuty: threat detection that correlates several sources

Centralization and retention

Logs scattered across several accounts and regions are of little help when the pressure is on. Centralize everything in a dedicated destination with restricted access, preferably a separate log account. That way, whoever compromises production cannot erase the evidence. With the logs in one place, you can also correlate different sources into a single timeline.

Set retention based on the time that usually passes between compromise and discovery, which often runs to months. With retention that is too short, the log expires right before you need it. Keep older history in low-cost storage and leave the most recent data ready for fast queries.

Correlation and detection

Collecting is only half the job, because you also need to be able to query and correlate the data. Send the logs to an analysis solution (a data lake with queries, CloudWatch or a SIEM) and build reusable queries for the questions that come up most often in investigations. Set alerts for high-severity events, such as root usage, disabled trails and broad permission changes.

Synchronize clocks and standardize the time zone. To correlate events from different sources you need a reliable time reference, and even a small difference between clocks throws off the timeline reconstruction.

Queries that answer the investigation questions

Centralized logs only help if you can ask them what you need to know, and quickly. Keep queries ready for the most common questions in an investigation, so you don't have to write them from scratch at the worst moment. On AWS, two native tools cover most cases: CloudWatch Logs Insights, for logs in CloudWatch log groups, and Athena, for logs in S3 such as the CloudTrail logs. See the two examples below:

CloudWatch Logs Insights: console login failures
fields @timestamp, sourceIPAddress, userIdentity.arn| filter eventName = "ConsoleLogin"| filter responseElements.ConsoleLogin = "Failure"| sort @timestamp desc| limit 50
Athena on CloudTrail: sensitive identity actions
SELECT eventtime, useridentity.arn, eventname, sourceipaddressFROM cloudtrail_logsWHERE eventname IN ('CreateAccessKey', 'AttachUserPolicy', 'StopLogging')ORDER BY eventtime DESCLIMIT 100

Both queries only read data. The first returns the most recent console login failures, with the source IP and the identity used; the second looks for sensitive identity actions, such as key creation and stopping the trail. In other words, they show who acted, from where and on what, without changing anything. Keep these queries versioned and documented, along with where each log source lives, and the time between suspicion and response gets much shorter.

Integrity, access and cost

If whoever compromised the environment can alter or delete the log, it no longer counts as evidence. So, besides centralizing it in a restricted account, make the files immutable during the retention period, encrypt them with a dedicated key and limit who can read and who can delete. When the source offers integrity validation, use it: that is what proves the records were not tampered with.

Cost is what leads many teams to collect too little or keep logs for too short a time. You don't have to choose between visibility and the monthly bill. Use storage tiers, with recent history ready for fast queries and older data moved to cheaper storage without being deleted. Partitioning the data by date and by source also helps, because each query scans less volume and the cost of investigating drops considerably.

Practical checklist

  • Enable a multi-region CloudTrail trail as the baseline
  • Turn on VPC Flow Logs in the relevant networks
  • Collect WAF and load balancer logs
  • Forward application logs with their business context
  • Enable GuardDuty for correlated detection
  • Centralize logs in a dedicated, restricted account
  • Set retention that matches the typical time to discovery
  • Send the logs to an analysis solution
  • Build reusable queries and alerts for high-severity events
  • Standardize the time zone and synchronize clocks
  • Keep investigation queries ready for the common questions
  • Make logs immutable and encrypted, with restricted access
  • Use storage tiers and partitioning to control cost

Best practices

  • Plan your logs before the incident, because during it is too late
  • Centralize in a separate account to preserve the evidence
  • Cover network, identity, edge and application
  • Keep logs for as long as actually passes between compromise and discovery
  • Build queries and alerts for the most common questions
  • Keep a reliable time reference so you can correlate
  • Have investigation queries ready before you need them
  • Protect log integrity and restrict who can delete logs

Common mistakes

Finding out mid-incident that the needed log was not enabled
The investigation lacks evidence exactly at the critical point.
Logs scattered across accounts and regions
Correlation becomes slow and difficult right in the middle of an incident.
Retention that is too short
Logs expire before the incident is discovered.
Collecting without the ability to query
Lots of stored data and few answers when it is time to investigate.
Unsynchronized clocks
The timeline becomes inconsistent and correlation suffers.
Logs with no protection against tampering
Whoever compromises the environment deletes or alters the evidence the investigation would rely on.
Collecting too little for fear of cost
Without storage tiers, the cut tends to fall on exactly the log the investigation would miss.

Authorship and review

Cesar Gargiulo

CEO and founder of GUARDIASEC. Technical and editorial lead for the content published in this library.

Published on . Found a technical error? Write to [email protected] and we will correct it and keep a record of the change.

Frequently asked questions

Which logs should I enable first if I am short on time?
Start with a multi-region CloudTrail trail, which answers who did what in the account, and with GuardDuty, which adds detection across several sources with little effort. Next, turn on VPC Flow Logs and the edge and application logs, in order of criticality. That way you first cover the questions that come up most in an investigation.
How long should I keep logs?
Ideally, long enough to cover the time that usually passes between a compromise and its discovery, which is often months. Many organizations keep at least a year, with older history in cheaper storage. The risk of keeping too little is watching the most important log expire before you realize you needed it.
Do I need a SIEM to make good use of logs?
Not necessarily. A SIEM makes correlation and alerting easier, but you can start with native solutions, such as queries over a data lake and alerts in CloudWatch. What you cannot do without is centralization, the ability to query and alerts for critical events. A SIEM starts to pay off as volume and complexity grow.
Why centralize logs in a different account?
Because whoever compromises the production account usually tries to erase their tracks and, if the logs are in the same account, they will succeed. With logs centralized in a separate, restricted log account, the evidence stays protected even when the source environment is compromised, and the investigation has something to work with.
How do I control the cost of storing so many logs?
Use storage tiers instead of choosing between visibility and cost. Keep recent history ready for fast queries and move older data to cheaper storage, without deleting anything before the retention period ends. Partitioning the data by date and by source reduces the volume scanned in each query, which lowers both the cost and the investigation time.
Which queries should I have ready before an incident?
The ones that answer the most frequent questions: login failures, root account usage, creation of keys and users, policy changes and access from unusual sources or regions. With them versioned and documented, nobody has to write a query from scratch under pressure. CloudWatch Logs Insights handles logs in CloudWatch log groups, and Athena handles logs in S3, such as the CloudTrail logs.
CloudTrail Lake, Athena or CloudWatch Logs Insights: which should I use?
It depends on where the log lives and how much you want to manage. CloudWatch Logs Insights queries CloudWatch log groups and is practical for alerts and quick investigations. Athena queries files in S3 and handles a large historical volume well, but it costs you the work of setting up tables and partitions. CloudTrail Lake is the managed option, with long retention and SQL queries and no infrastructure to maintain. Many teams use more than one, depending on the source.

When to bring in specialist help

Through its monitoring and response service, GUARDIASEC helps define what to collect, how to centralize it and how to detect threats, and structures the collection, the detection use cases and the investigation workflows for your environment.