-
Notifications
You must be signed in to change notification settings - Fork 0
/
Combining_import_validate_campaign.yaml
258 lines (223 loc) · 7.74 KB
/
Combining_import_validate_campaign.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
AWSTemplateFormatVersion: 2010-09-09
Description: Amazon Pinpoint Combinging S3 Triggered Import, Phone Number Validate, Create Campaign
Parameters:
PinpointProjectId:
Type: String
Description: Amazon Pinpoint Project ID trigger a new campaign
SmsTemplateName:
Type: String
Description: SMS Template to use when creating the campaign
ImportStatusSNSTopicArn:
Type: String
Description: SNS Topic used to provide updates to status. Found in the Outputs of the CloudFormation.
PhoneValidateStateMachineArn:
Type: String
Description: The Phone Number Validate State Machine ARN. Found in the Outputs of the CloudFormation.
PhoneValidateNotificationTopicArn:
Type: String
Description: SNS Topic used to provide updates to status. Found in the Outputs of the CloudFormation.
CampaignCreateStateMachineArn:
Type: String
Description: The Create Campaign State Machine Arn. Found in the Outputs of the CloudFormation.
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
-
Label:
default: Campaign Details
Parameters:
- PinpointProjectId
- SmsTemplateName
-
Label:
default: Amazon S3 Triggered Endpoint Imports Reference Architecture Details
Parameters:
- ImportStatusSNSTopicArn
-
Label:
default: Automatic Phone Number Validate Reference Architecture Details
Parameters:
- PhoneValidateNotificationTopicArn
- PhoneValidateStateMachineArn
-
Label:
default: Automatic Amazon Pinpoint Campaign Creation Reference Architecture Details
Parameters:
- CampaignCreateStateMachineArn
Resources:
## Trigger Phone Validate from Success Import
TriggerPhoneValidate:
Type: AWS::Lambda::Function
Properties:
Handler: index.lambda_handler
Role: !GetAtt TriggerPhoneValidateRole.Arn
Runtime: python3.7
Timeout: 60
Environment:
Variables:
LOG_LEVEL: "INFO"
PINPOINT_PROJECT_ID: !Ref PinpointProjectId
PHONE_VALIDATE_SM_ARN: !Ref PhoneValidateStateMachineArn
Code:
ZipFile: |
import boto3
import os
import logging
import json
client = boto3.client('stepfunctions')
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']:
j = json.loads(record['Sns']['Message'])
response = client.start_execution(
stateMachineArn=os.environ.get('PHONE_VALIDATE_SM_ARN'),
input=json.dumps(j['ImportResult'])
)
logging.info(response)
return {
'Success': True
}
TriggerPhoneValidateSNSTriggerPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt TriggerPhoneValidate.Arn
Principal: sns.amazonaws.com
TriggerPhoneValidateSNSTrigger:
Type: AWS::SNS::Subscription
Properties:
Endpoint: !GetAtt TriggerPhoneValidate.Arn
Protocol: lambda
TopicArn: !Ref ImportStatusSNSTopicArn
FilterPolicy: "{\"notification_type\": [\"success\"]}"
TriggerPhoneValidateRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: "/"
Policies:
-
PolicyName: "root"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource: !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*"
-
Effect: "Allow"
Action:
- "states:StartExecution"
Resource: !Ref PhoneValidateStateMachineArn
## Trigger Create Campaign from Phone Verify
TriggerCampaignCreate:
Type: AWS::Lambda::Function
Properties:
Handler: index.lambda_handler
Role: !GetAtt TriggerCampaignCreateRole.Arn
Runtime: python3.7
Timeout: 60
Environment:
Variables:
LOG_LEVEL: "INFO"
PINPOINT_PROJECT_ID: !Ref PinpointProjectId
CREATE_CAMPAIGN_SM_ARN: !Ref CampaignCreateStateMachineArn
SMS_TEMPLATE_NAME: !Ref SmsTemplateName
Code:
ZipFile: |
import boto3
import os
import logging
import json
client = boto3.client('stepfunctions')
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']:
j = json.loads(record['Sns']['Message'])
input = {
"SegmentId": j['ValidatePhoneResult']['ValidateInput']['Definition']['SegmentId'],
"SegmentName": j['ValidatePhoneResult']['ValidateInput']['Definition']['SegmentName'],
"SmsTemplateName": os.environ.get('SMS_TEMPLATE_NAME'),
}
response = client.start_execution(
stateMachineArn=os.environ.get('CREATE_CAMPAIGN_SM_ARN'),
input=json.dumps(input)
)
logging.info(response)
return {
'Success': True
}
TriggerCampaignCreateSNSTriggerPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt TriggerCampaignCreate.Arn
Principal: sns.amazonaws.com
TriggerCampaignCreateSNSTrigger:
Type: AWS::SNS::Subscription
Properties:
Endpoint: !GetAtt TriggerCampaignCreate.Arn
Protocol: lambda
TopicArn: !Ref PhoneValidateNotificationTopicArn
FilterPolicy: "{\"notification_type\": [\"success\"]}"
TriggerCampaignCreateRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: "/"
Policies:
-
PolicyName: "root"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource: !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*"
-
Effect: "Allow"
Action:
- "states:StartExecution"
Resource: !Ref CampaignCreateStateMachineArn