Serverless Endpoint Monitoring - check uptime of your app
Tags: aws, serverless, monitoring
Published: 2024-01-18 03:12:25

---
title: Serverless Endpoint Monitoring - check uptime of your app
published: true
description: 
tags: aws, serverless, monitoring
---

Do you need to monitor your application endpoints and have a dashboard to check the detail - all this in a serverless way on AWS. 
Let's see how it's done.

Solution Design
![Ping Service Architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eqbhemik1babx92b2sgl.png)

* This setup creates a s3 webpage that takes in the required information for monitoring.
* Backend contains a lambda to create eventbridge schedule (cron) and cloudwatch alarm.
* The eventbridge schedule trigger will invoke a lambda to check the endpoint and update cloudwatch with custom metric of the response status code.

Requirements:
* AWS knowledge of how to create lambda function | iam roles and s3 websites

### Step 1 - Setup the Lambda Function

Clone the code from github
```bash
https://github.com/uptownaravi/ping_service.git
```

Create Lambda function checkEndpoint that will check the endpoint and update cloudwatch metric.
Use file checkEndpoint.py from the app folder and for the role for this lambda use the iam policy document in the file checkEndpointPolicy.json. 

Replace all the url / region name / account number in the code.

Create Lambda function addPingEndpoint using the file addPingEndpoint.py in app folder and the related iam policy in addPingEndpointPolicy.json

This Lambda will be creating the eventbridge schedule with the payload of the requested service to check. That will be show in the target section once created. This is the connecting part that tells the checkEndpoint Lambda what url to check and which cloudwatch metric to update.

Enable Function URL for addPingEndpoint function as we need that to be added to the s3 website. Allow headers for accept and content-type
CORS needs to be enabled - check steps after s3 bucket for this.

### Step 2 - S3 Website Creation

Next create s3 website using the code in web folder
In the file app.html update the function-url to the lambda function url created in the previous step.
Steps to create a website in s3 https://docs.aws.amazon.com/AmazonS3/latest/userguide/HostingWebsiteOnS3Setup.html

>Note for CORS in the addPingEndpoint Lambda function add the CORS origin of function url as the s3 bucket like - http://<bucket-name>.s3-website.<aws-region-name>.amazonaws.com
this will make sure the function url can be used from the s3 website.

After deploying the website and enabling static hosting 
below page should be visible if you visit http://<bucket-name>.s3-website.<aws-region-name>.amazonaws.com

![webpage with details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/46eqdrcjoxkawns4vhoc.png)

### Step 3 - Test the solution

Enter the details in the webpage
Service Name: name of the service to monitor
Endpoint: URL to check
Details: description of the service
cron: cron expression of the schedule to check ( AWS Document https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-cron-expressions.html )

after submitting we get the message

![success message after form submission](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/geqxpmvxie9a9cqm9yvh.png)

Check your EventBridge Schedules and CloudWatchAlarm / Metric Dashboard to see the results

![Event Bridge Schedule](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q8fzs8awq82xmt5ddhrq.png)


![metric namespace](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a6gahdwap0lhh5uuhwqi.png)

Cloud watch metric showing the custom metric status code for the AnalyticsApp which we added the details in the webpage

![metric graph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8l8ooey6c8fbahm6vqnq.png)

and in alarms

![cloudwatch alarm](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7oq3l9h9du6l8dr9clfs.png)


This can be done for many services and all those will be available as custom metrics in their namespace.

> Note There are no actions added to this alarm. 
Add the required SNS alerts if required, to get the update on downtime of your endpoints.