title: AWS CloudWatch Logs Insight query to find out service availability using ALB logs
body: <p>I have Access Logs enabled on my ALB which are published to AWS CloudWatch. We have a ServiceHealth check endpoint which returns 200 if the service is behaving normally. If any of the components of the are not behaving as expected, it will return 503 HTTP response code. There could be other scenarios, such as service unavailable for which ALB returns 5XX status.</p>
  <p>I have managed to write CloudWatch Logs insights query which gives me the count for each of the HTTP response code.</p>
  <pre><code>fields @message
  | parse @message &quot;* * * *:* * * * * * * * * * * * *&quot; as type, time, elb, client, port, target_port, request_processing_time, target_processing_time, response_processing_time, elb_status_code, target_status_code, received_bytes, sent_bytes, protocol, url, rest_of_the_message
  | filter (strcontains(url, &quot;ServiceHealth.php&quot;) and (elb_status_code = 200 or elb_status_code = 502 or elb_status_code = 503)
  | stats count(*) by elb_status_code, elb_status_code
  </code></pre>
  <p>This outputs the results as:</p>
  <p><a href="https://i.stack.imgur.com/Tfmww.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Tfmww.png" alt="Current Output" /></a></p>
  <p>I would like the results to be displayed in terms of percentage which is calculated as:</p>
  <pre><code>Service Availability = (Number of responses with 200 Status Code/ (Total number of calls with 200 + 5XX Status Code)) * 100
  
  Service Unvailability = (Number of responses with 5XX Status Code / (Total number of calls with 200 + 5XX Status Code)) * 100
  </code></pre>
  <p>Is it possible to achive this using Logs Insights query?</p>