-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcloudformation.yml
72 lines (65 loc) · 2.43 KB
/
cloudformation.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
AWSTemplateFormatVersion: '2010-09-09'
Description: 'CloudFormation template to schedule a Lambda function using AWS EventBridge (formerly CloudWatch Events)'
Parameters:
Email:
Type: String
Description: Email address to receive notifications
LambdaFunctionName:
Type: String
Description: Name of the Lambda function to trigger
LambdaFunctionArn:
Type: String
Description: ARN of the Lambda function to trigger
Resources:
# Lambda function is defined from AWS Console
# https://us-west-1.console.aws.amazon.com/lambda/home?region=us-west-1#/functions
# Define AWS EventBridge (CloudWatch Events) rule to schedule the Lambda function
# You can see here: https://us-west-1.console.aws.amazon.com/events/home?region=us-west-1#/eventbus/default/rules/SchedulerEventRule
SchedulerEventRule:
Type: AWS::Events::Rule
Properties:
Name: SchedulerEventRule
Description: "Schedule Lambda function to run every weekday at 0 AM UTC"
ScheduleExpression: cron(0 0 ? * MON-FRI *) # min hour day month day-of-week year
State: ENABLED
Targets:
- Arn: !Ref LambdaFunctionArn
Id: "LambdaFunctionTarget"
# Permission for AWS EventBridge to invoke the Lambda function
LambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref LambdaFunctionName
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
SourceArn: !GetAtt SchedulerEventRule.Arn
# SNS Topic for sending notifications
ErrorNotificationTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: LambdaErrorNotificationTopic
# SNS Subscription to send notifications to an email
ErrorNotificationSubscription:
Type: AWS::SNS::Subscription
Properties:
TopicArn: !Ref ErrorNotificationTopic
Protocol: email
Endpoint: !Ref Email
# CloudWatch Alarm for Lambda function errors
LambdaErrorAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: LambdaFunctionErrorAlarm
AlarmDescription: Alarm for Lambda function errors
MetricName: Errors
Namespace: AWS/Lambda
Statistic: Sum # Total number of errors in each period
Period: 300 # in seconds
EvaluationPeriods: 1
Threshold: 1
ComparisonOperator: GreaterThanOrEqualToThreshold
AlarmActions:
- !Ref ErrorNotificationTopic
Dimensions:
- Name: FunctionName
Value: !Ref LambdaFunctionName