-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.yaml
149 lines (134 loc) · 4.53 KB
/
lambda.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
147
148
149
AWSTemplateFormatVersion: '2010-09-09'
Description: >
This CloudFormation template creates simple Lambda functions,
which prints CloudFormation resource Arn from the stack.
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Advanced Settings
Parameters:
- LambdaFunctionLayerARN
ParameterLabels:
LambdaFunctionLayerARN:
default : Lambda Layer Arn
Parameters:
LambdaFunctionLayerARN:
Type: String
Default: 'None'
Description: Provide Arn of the layer need to attach to Lambda Function
Resources:
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
Description: Role for the execution of Lambda function
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
LambdaFunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
RoleName: !Join ['-', [!Ref 'AWS::StackName', Role, !Ref 'AWS::Region']]
Policies:
- PolicyName: LambdaFunctionPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: '*'
LambdaFunction:
Type: AWS::Lambda::Function
Properties:
Runtime: python3.6
Timeout: 5
Handler: index.handler
Role: !GetAtt LambdaFunctionRole.Arn
FunctionName: !Join ['-', [!Ref 'AWS::StackName', Lambdafunction, !Ref 'AWS::Region']] # Added Function name
Environment:
Variables:
WEBHOOK_URL: "https://hooks.slack.com/"
CHANNEL: "#aakashtestchannel"
Layers:
- !Ref LambdaFunctionLayerARN
Code:
ZipFile:
!Sub
- |-
#!/usr/bin/env python3
import json
import logging
import os
import requests
def lambda_handler(event, context):
detail = event["detail"]
username = detail["userIdentity"]["arn"]
event_type = detail["eventName"]
region = detail["awsRegion"]
user_agent = detail["userAgent"]
webacl = detail["requestParameter"]["name"]
title_text = "AWS EventBridge Alarm."
alarm_description = "User : " + str(username) + "\n Event : " + str(event_type) + "\n Region : " + str(region) + "\n User-Agent : " + str(user_agent) + "\n Web-ACL" + str(webacl)
payload = {
"channel": os.environ.get("CHANNEL"),
"text": title_text,
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*Reason*:\n ```{alarm_description}```"
}
}
]
}
webhook_url = os.environ.get("WEBHOOK_URL")
response = requests.post(webhook_url, json=payload, headers={'Content-Type': 'application/json'})
print(response.text)
-
lambda_function_role_arn: !Ref LambdaFunctionRole
EventBridgeWAF:
Type: AWS::Events::Rule
Properties:
Description: Rule for Alert on any changes on WAF on console.
EventPattern:
source:
- aws.wafv2
detail:
eventSource:
- wafv2.amazonaws.com
eventName:
- UpdateWebACL
Name: EventBridgeWAF
Targets:
- Arn: !GetAtt LambdaFunction.Arn
Id: EventBridgeWAFLambdaFunction
LambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
SourceArn: !GetAtt EventBridgeWAF.Arn
FunctionName: !Ref LambdaFunction