Created
November 16, 2019 18:09
-
-
Save albertollamaso/436a29931618e906e06f9aaf76e2264e to your computer and use it in GitHub Desktop.
AWS Cloudformation for website uptime monitoring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Parameters: | |
EmailNotification: | |
Type: String | |
Description: Enter the destination email to receive the alarms. | |
Websiteurl: | |
Type: String | |
Description: Enter the website url. | |
ScheduleExpression: | |
Type: String | |
Default: "rate(5 minutes)" | |
Resources: | |
# cloudwatch alarm | |
uptimeAlarm: | |
Type: AWS::CloudWatch::Alarm | |
Properties: | |
AlarmName: !Join [ "-", [ "My-Website", !Ref Websiteurl, "is-down" ] ] | |
AlarmDescription: "Alarm if lambda errors out too many times" | |
AlarmActions: [ !Sub "arn:aws:sns:${AWS::Region}:${AWS::AccountId}:MySNSTopic" ] | |
Namespace: "AWS/Lambda" | |
MetricName: "Errors" | |
Dimensions: | |
- Name: "FunctionName" | |
Value: "UptimeLambdaFunction" | |
Statistic: "Average" | |
ComparisonOperator: "GreaterThanThreshold" | |
Threshold: 0 | |
EvaluationPeriods: 1 | |
Period: 300 | |
TreatMissingData: "missing" | |
# sns topic | |
MySNSTopic: | |
Type: AWS::SNS::Topic | |
Properties: | |
TopicName: "MySNSTopic" | |
# sns subscription | |
MySubscription: | |
Type: AWS::SNS::Subscription | |
Properties: | |
Endpoint: !Ref EmailNotification | |
Protocol: email | |
TopicArn: !Ref 'MySNSTopic' | |
# eventRule for lambda function | |
ScheduledRule: | |
Type: AWS::Events::Rule | |
Properties: | |
Description: "ScheduledRule" | |
ScheduleExpression: !Ref ScheduleExpression | |
State: "ENABLED" | |
Targets: | |
- | |
Arn: | |
Fn::GetAtt: | |
- "UptimeLambdaFunction" | |
- "Arn" | |
Id: "TargetFunctionV1" | |
PermissionForEventsToInvokeLambda: | |
Type: AWS::Lambda::Permission | |
Properties: | |
FunctionName: | |
Ref: "UptimeLambdaFunction" | |
Action: "lambda:InvokeFunction" | |
Principal: "events.amazonaws.com" | |
SourceArn: | |
Fn::GetAtt: | |
- "ScheduledRule" | |
- "Arn" | |
# lambda execution role | |
LambdaExecutionRole: | |
Type: AWS::IAM::Role | |
Properties: | |
AssumeRolePolicyDocument: | |
Version: '2012-10-17' | |
Statement: | |
- Effect: Allow | |
Principal: | |
Service: | |
- lambda.amazonaws.com | |
Action: | |
- sts:AssumeRole | |
Policies: | |
- PolicyName: allow-logs | |
PolicyDocument: | |
Version: '2012-10-17' | |
Statement: | |
- Effect: Allow | |
Action: | |
- 'logs:*' | |
Resource: arn:aws:logs:*:*:* | |
- PolicyName: put-cw-data | |
PolicyDocument: | |
Version: '2012-10-17' | |
Statement: | |
- Effect: Allow | |
Action: | |
- 'cloudwatch:PutMetricData' | |
Resource: '*' | |
# lambda function | |
UptimeLambdaFunction: | |
Type: AWS::Lambda::Function | |
Properties: | |
FunctionName: "UptimeLambdaFunction" | |
Handler: "index.handler" | |
Runtime: nodejs10.x | |
Timeout: 300 | |
Role: | |
Fn::GetAtt: | |
- "LambdaExecutionRole" | |
- "Arn" | |
Code: | |
ZipFile: !Sub | | |
'use strict'; | |
var url = require('url'); | |
var target = '${Websiteurl}'; | |
exports.handler = function(event, context, callback) { | |
var urlObject = url.parse(target); | |
var mod = require( | |
urlObject.protocol.substring(0, urlObject.protocol.length - 1) | |
); | |
console.log('[INFO] - Checking ' + target); | |
var req = mod.request(urlObject, function(res) { | |
res.setEncoding('utf8'); | |
res.on('data', function(chunk) { | |
console.log('[INFO] - Read body chunk'); | |
}); | |
res.on('end', function() { | |
console.log('[INFO] - Response end'); | |
callback(); | |
}); | |
}); | |
req.on('error', function(e) { | |
console.log('[ERROR] - ' + e.message); | |
callback(e); | |
}); | |
req.end(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment