--As a simple monitoring of the operating server, I want to periodically perform HTTP access and check the result. --I want to run serverless. --Since the IP that accesses the target server is restricted, the monitoring source needs to have a fixed IP. ――I want you to fully automate and be notified only of the monitoring results.
--Overall image
--Place the monitored server data in S3
.
--The data format is as follows.
```json:servers.json
{
"servers": [
{"name": "Google", "url": "http://www.google.co.jp"},
{"name": "Yahoo", "url": "http://www.yahoo.co.jp"}
]
}
```
--ʻRead the above data from AWS Lambda (Python) `and make HTTP access to each server.
--Notify Slack
of the monitoring result.
--Periodically execute the above with CloudWatch Events --Schedule
.
――See what you wanted to achieve, as it was completely covered below. - https://www.joyzo.co.jp/blog/2325
--When creating Lambda
, select Simple Microservice permissions
as the role template.
--Attach the policy of ʻAmazonS3ReadOnlyAccess to access
S3. --Attach the policy of ʻAWS LambdaVPCAccessExecutionRole
to run Lambda
on VPC
.
Slack
-Refer to Incoming Webhooks to get the URL for notification to Slack
.
AWS Lambda(Python)
--There is room for improvement around error handling.
lambda_function.py
import json
import requests
import boto3
BUCKET_NAME = 'xxxxxxxxxx'
OBJECT_NAME = 'xxxxxxxxxx/servers.json'
SLACK_POST_URL = 'https://hooks.slack.com/services/xxxxxxxxxx/xxxxxxxxxx/xxxxxxxxxxxxxxxxxxxx'
def lambda_handler(event, context):
json_data = __getServers()
__check_server(json_data)
def __getServers():
s3 = boto3.resource('s3')
obj = s3.Object(BUCKET_NAME, OBJECT_NAME)
response = obj.get()
body = response['Body'].read()
return body.decode('utf-8')
def __check_server(json_data):
data = json.loads(json_data)
servers = data['servers']
has_error = False
for server in servers:
name = server['name']
url = server['url']
print("Check: " + name)
try:
r = requests.get(url)
if r.status_code != 200:
__send_error_message(name, url)
has_error = True
except requests.exceptions.RequestException as e:
__send_request_error_message(name, url)
has_error = True
if has_error == False:
__send_success_message()
def __send_error_message(name, url):
payload = {
"text": name + '\n' + url + '\n' + '*ERROR!*',
"icon_emoji": ":x:"
}
__send_message(payload)
def __send_request_error_message(name, url):
payload = {
"text": name + '\n' + url + '\n' + '*Request Error!*',
"icon_emoji": ":warning:"
}
__send_message(payload)
def __send_success_message():
payload = {
"text": "All Servers OK!",
"icon_emoji": ":o:"
}
__send_message(payload)
def __send_message(payload):
try:
return requests.post(SLACK_POST_URL, json=payload)
except requests.exceptions.RequestException as e:
return None
--Place the library in the same directory as the source code.
pip install requests -t .
--Put the source code and library together in a zip.
zip -r lambda_function.zip *
―― With the ability to set a VPC for AWS Lambda, you can run Lambda with a fixed IP address as the data source, which has expanded the range of uses. ――As a handy notification destination, cooperation with
Slack is too easy and convenient. --I also tried a method in which the contents of servers.json are packed in the body and executed via ʻAPI Gateway
triggered by HTTP POST communication, but in the end it became the current form that is easy to execute regularly.
Recommended Posts