-
Notifications
You must be signed in to change notification settings - Fork 0
/
Event_Based_Segmentation.yaml
200 lines (170 loc) · 5.97 KB
/
Event_Based_Segmentation.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
AWSTemplateFormatVersion: 2010-09-09
Description: Event Based Segmentation
Parameters:
PinpointProjectId:
Type: String
Description: Amazon Pinpoint Project ID if one already exists, blank to create one
PinpointProjectName:
Type: String
Default: "My Pinpoint Project"
Description: "If no PinpointProjectId provided, name to be used to create the Pinpoint project"
Conditions:
NeedsPinpointProjectId: !Equals
- ''
- !Ref PinpointProjectId
Resources:
PinpointApplication:
Type: AWS::Pinpoint::App
Condition: NeedsPinpointProjectId
Properties:
Name: !Ref PinpointProjectName
PinpointEventStream:
Type: AWS::Pinpoint::EventStream
Properties:
ApplicationId: !If
- NeedsPinpointProjectId
- !Ref PinpointApplication
- !Ref PinpointProjectId
DestinationStreamArn: !GetAtt PinpointEventKinesis.Arn
RoleArn: !GetAtt PinpointKinesisStreamRole.Arn
PinpointEventKinesis:
Type: AWS::Kinesis::Stream
Properties:
ShardCount: 1
StreamEncryption:
EncryptionType: KMS
KeyId: alias/aws/kinesis
PinpointKinesisStreamRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- pinpoint.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: "/"
Policies:
-
PolicyName: "root"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "kinesis:PutRecords"
- "kinesis:DescribeStream"
Resource: !GetAtt PinpointEventKinesis.Arn
EventProcessingLambda:
Type: AWS::Lambda::Function
Properties:
Handler: index.lambda_handler
Role: !GetAtt EventProcessingLambdaRole.Arn
Runtime: "python3.7"
Timeout: 60
Environment:
Variables:
LOG_LEVEL: "INFO"
PINPOINT_PROJECT_ID: !If
- NeedsPinpointProjectId
- !Ref PinpointApplication
- !Ref PinpointProjectId
Code:
ZipFile: |
import boto3
import logging
import os
import json
import base64
pinpoint = boto3.client('pinpoint')
def lambda_handler(event, context):
global log_level
log_level = str(os.environ.get('LOG_LEVEL')).upper()
if log_level not in [
'DEBUG', 'INFO',
'WARNING', 'ERROR',
'CRITICAL'
]:
log_level = 'ERROR'
logging.getLogger().setLevel(log_level)
logging.info(event)
for record in event['Records']:
#Kinesis data is base64 encoded so decode here
try:
payload=base64.b64decode(record["kinesis"]["data"])
logging.info('Found Event: %s', payload)
pinpoint_event = json.loads(payload)
if pinpoint_event['event_type'] == '_email.open':
# We have an email open event, update the endpoint with the attribute "opened_email" = "true"
pinpoint.update_endpoint(
ApplicationId=os.environ['PINPOINT_PROJECT_ID'],
EndpointId=pinpoint_event['client']['client_id'], #this is true for campaign sends
EndpointRequest={
'Attributes': {
'opened_email': [
'true'
]
}
}
)
elif pinpoint_event['event_type'] == '_custom.registered_for_webinar5':
# We have a custom event via the Pinpoint PutEvents API, update attribute
pinpoint.update_endpoint(
ApplicationId=os.environ['PINPOINT_PROJECT_ID'],
EndpointId=pinpoint_event['client']['client_id'],
EndpointRequest={
'Attributes': {
'webinar_registration': [
'webinar5'
]
}
}
)
except Exception as e:
logging.error('Received Error while processing s3 file: %s', e)
EventProcessingLambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- "sts:AssumeRole"
Path: "/"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSLambdaKinesisExecutionRole"
Policies:
-
PolicyName: "LambdaExecutionPolicy"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "mobiletargeting:UpdateEndpoint"
Resource: !Sub
- 'arn:aws:mobiletargeting:${AWS::Region}:${AWS::AccountId}:apps/${ProjectId}*'
- {ProjectId: !If [NeedsPinpointProjectId, !Ref PinpointApplication, !Ref PinpointProjectId] }
-
Effect: "Allow"
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource: !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*"
EventProcessingLambdaTrigger:
Type: AWS::Lambda::EventSourceMapping
Properties:
Enabled: true
EventSourceArn: !GetAtt PinpointEventKinesis.Arn
FunctionName: !GetAtt EventProcessingLambda.Arn
StartingPosition: 'TRIM_HORIZON'