forked from stevemac007/aws-to-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yaml
146 lines (133 loc) · 3.96 KB
/
template.yaml
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'SNS notification sent to Slack channel'
Transform:
- AWS::Serverless-2016-10-31
Metadata:
'AWS::CloudFormation::Interface':
ParameterGroups:
- Label:
default: 'Slack Setup'
Parameters:
- HookUrl
- Channel
- KmsDecryptKeyArn
- Label:
default: 'Default Subscriptions'
Parameters:
- ErrorAlertEmail
Parameters:
ErrorAlertEmail:
Description: 'Optional: email address to receive alert that function is failing. The email will only contain a count of failures -- you will need to then review CloudWatch logs to determine cause of the issue. HIGHLY RECOMMEND handling failures like this within a DeadLetterQueue via ParentAlertStack instead.'
Type: String
Default: ''
HookUrl:
Type: String
Description: 'Slack webhook URL; see https://example.slack.com/apps/'
Channel:
Type: String
Description: 'Optional: Channel name to post within'
Default: ''
KmsDecryptKeyArn:
Type: String
Description: 'Optional: Key used to encrypt Hook or Channel values. If provided will create IAM policy to grant access to decrypt the values.'
Default: ''
Conditions:
HasChannel: !Not [!Equals [!Ref Channel, '']]
HasKmsKey: !Not [!Equals [!Ref KmsDecryptKeyArn, '']]
HasAlertEmail: !Not [!Equals [!Ref ErrorAlertEmail, '']]
Resources:
SlackAlertTopic:
Type: AWS::SNS::Topic
Properties:
TopicName:
Fn::Sub: ${AWS::StackName}
Tags:
- Key: Name
Value:
Fn::Sub: ${AWS::StackName}
SlackFunction:
Type: AWS::Serverless::Function
Properties:
Handler: src/index.handler
Runtime: nodejs16.x
Role: !GetAtt FunctionRole.Arn
MemorySize: 256
Timeout: 15 # Cross-region metrics lookup requires at least 10s
Tracing: Active
Environment:
Variables:
SLACK_CHANNEL: !If
- HasChannel
- !Ref Channel
- !Ref 'AWS::NoValue'
SLACK_HOOK_URL: !Ref HookUrl
Events:
SNSEvent:
Type: SNS
Properties:
Topic:
Ref: SlackAlertTopic
FunctionRole:
Type: 'AWS::IAM::Role'
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- arn:aws:iam::aws:policy/CloudWatchReadOnlyAccess
- arn:aws:iam::aws:policy/AWSCodeCommitReadOnly
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Action: [ 'sts:AssumeRole' ]
Effect: Allow
Principal:
Service: [ 'lambda.amazonaws.com' ]
KmsDecryptPolicy:
Condition: HasKmsKey
Type: 'AWS::IAM::Policy'
Properties:
Roles: [ !Ref FunctionRole ]
PolicyName: AllowDecryptKms
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: [ 'kms:Decrypt' ]
Resource: !Ref KmsDecryptKeyArn
#
# CloudWatch Alarm
#
FunctionFailing:
Type: 'AWS::CloudWatch::Alarm'
Properties:
AlarmDescription: 'AWS-to-Slack Lambda function is failing'
Namespace: 'AWS/Lambda'
MetricName: Errors
Statistic: Sum
Period: 300
EvaluationPeriods: 1
ComparisonOperator: GreaterThanThreshold
Threshold: 0
TreatMissingData: notBreaching
Dimensions:
- Name: FunctionName
Value: !Ref SlackFunction
AlarmActions:
- !Ref FunctionFailingTopic
FunctionFailingTopic:
Type: 'AWS::SNS::Topic'
Properties: {}
FunctionFailingEmailSubscription:
Condition: HasAlertEmail
Type: 'AWS::SNS::Subscription'
Properties:
TopicArn: !Ref FunctionFailingTopic
Protocol: email
Endpoint: !Ref ErrorAlertEmail
Outputs:
LambdaFunction:
Description: 'Lambda function name created by this stack.'
Value: !Ref SlackFunction
LambdaFunctionArn:
Description: 'Lambda function ARN created by this stack.'
Value: !GetAtt SlackFunction.Arn