Mircoervice which can be run on AWS Lambda to monitor and check websites and metric value of website status code and response time taken per request is sent to AWS CloudWatch. Setup Alarm and SNS as per your preferences can also be done in AWS.
Inorder to run the project, you need to have following,
- golang 1.13 or above (For those who don't have go installed, check out https://golang.org/doc/install)
Optional (In order to run on lambda)
- AWS account with IAM Role (* policy) with the following policy below for the Lambda Function and cloudwatch metric: (in order to run with Lambda)
Find the basic preveliage policy for the lambda and cloudwatch metric
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowMeticAlarmCreation",
"Effect": "Allow",
"Action": [
"cloudwatch:PutMetricAlarm",
"cloudwatch:PutMetricData"
],
"Resource": "*"
},
{
"Sid": "AllowLogCollection",
"Effect": "Allow",
"Action": [
"logs:PutLogEvents",
"logs:CreateLogStream",
"logs:CreateLogGroup"
],
"Resource": "arn:aws:logs:*:*:*"
}
]
}
- AWS SNS Topic - to send alert and notification
- golang 1.15
- AWS (Lambda, SNS, cloudwatch)
Add the request urls, eg website url, health-check apis or any other GET request in this section. You can add as many request as you like, it will be run in concurrent goroutine which means the process will complete as per the slowest request url in the section.
websiteUrls := []string {
"http://google.com",
"https://www.facebook.com",
"https://www.gmail.com",
}
To run this project locally,
$ go mod vendor
$ go run main.go
To setup in lambda, follow the below steps,
- Uncomment this line on main.go in line number 33.
lambda.Start(lambdaService.StartService)
- Build the project with below command to create a zip file which is necessary to deploy on lambda
1. GOOS=linux go build -o main main.go
2. zip monitor-service.zip main
- Setup the lambda function
- Create a function as follow (with the policy defined under (policy) section)
- Add the zip file which is generated from earlier in the step 2.
- Add the trigger using eventbridge, define the frequent time to trigger (eg every 5 min, 1 hour etc)
- After that, check the metric log in cloudwatch, under metric section, you will see the metric sent from lambda
- here is the sample graph from cloudwatch metric
- Add the alarm as per your preferences. You may add SNS together with Alarm