-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
executable file
·210 lines (210 loc) · 6.53 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
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
AWSTemplateFormatVersion: 2010-09-09
Description: eventbridge-scheduler-demo
Transform: AWS::Serverless-2016-10-31
Resources:
NatibleUsersTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: userId
AttributeType: S
- AttributeName: timezone
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: userId
KeyType: HASH
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
GlobalSecondaryIndexes:
- IndexName: timezone-index
KeySchema:
- AttributeName: timezone
KeyType: HASH
Projection:
ProjectionType: ALL
DailyNatibleFunction:
Type: AWS::Serverless::Function
Properties:
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: DailyNatibleFunction
CodeUri: src/handlers/DailyNatibleFunction
Handler: app.handler
Runtime: nodejs16.x
MemorySize: 512
Timeout: 30
Tracing: Active
Environment:
Variables:
TABLE_NAME: !Ref NatibleUsersTable
TABLE_ARN: !GetAtt NatibleUsersTable.Arn
GSI: timezone-index
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref NatibleUsersTable
Events:
ScheduleUTC:
Type: ScheduleV2
Properties:
ScheduleExpression: cron(30 8 * * ? *)
Input: '{"timezone": "UTC"}'
ScheduleEurope:
Type: ScheduleV2
Properties:
ScheduleExpression: cron(30 8 * * ? *)
ScheduleExpressionTimezone: Europe/Helsinki
Input: '{"timezone": "Europe/Helsinki"}'
ScheduleEST:
Type: ScheduleV2
Properties:
ScheduleExpression: cron(30 8 * * ? *)
ScheduleExpressionTimezone: America/New_York
Input: '{"timezone": "America/New_York"}'
DailyNatibleFunctionLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Retain
Properties:
LogGroupName: !Sub /aws/lambda/${DailyNatibleFunction}
ManualTestQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ManualTestQueue
MessageRetentionPeriod: 345600
DlqTestQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: DlqTestQueue
MessageRetentionPeriod: 345600
UnsubscribeQueue:
Type: AWS::SQS::Queue
Properties:
MessageRetentionPeriod: 345600
UnsubscribeScheduleToSQSRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: scheduler.amazonaws.com
Action:
- sts:AssumeRole
Path: /
Policies:
- PolicyName: send-message-to-sqs
PolicyDocument:
Statement:
- Effect: Allow
Action:
- sqs:SendMessage
Resource: !GetAtt UnsubscribeQueue.Arn
ScheduleUnsubscribeFunction:
Type: AWS::Serverless::Function
Properties:
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: ScheduleUnsubscribeFunction
CodeUri: src/handlers/ScheduleUnsubscribeFunction
Handler: app.handler
Runtime: nodejs16.x
MemorySize: 128
Timeout: 30
Tracing: Active
Policies:
- Statement:
- Effect: Allow
Action:
- scheduler:UpdateSchedule
- scheduler:CreateSchedule
- scheduler:CreateScheduleGroup
Resource:
- !Sub arn:aws:scheduler:${AWS::Region}:${AWS::AccountId}:schedule/*/*
- !Sub arn:aws:scheduler:${AWS::Region}:${AWS::AccountId}:schedule-group/*
- Statement:
- Effect: Allow
Action:
- iam:PassRole
Resource:
- !GetAtt UnsubscribeScheduleToSQSRole.Arn
Events:
unsubscribePOSTuserId:
Type: Api
Properties:
Path: /userId/
Method: POST
RestApiId: !Ref UnsubscribeApi
Environment:
Variables:
SQS_ROLE_ARN: !GetAtt UnsubscribeScheduleToSQSRole.Arn
SQS_ARN: !GetAtt UnsubscribeQueue.Arn
ScheduleUnsubscribeFunctionLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Retain
Properties:
LogGroupName: !Sub /aws/lambda/${ScheduleUnsubscribeFunction}
UnsubscribeFunction:
Type: AWS::Serverless::Function
Properties:
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: UnsubscribeFunction
CodeUri: src/handlers/UnsubscribeFunction
Handler: app.handler
Runtime: nodejs16.x
MemorySize: 128
Timeout: 30
Tracing: Active
Environment:
Variables:
TABLE_NAME: !Ref NatibleUsersTable
TABLE_ARN: !GetAtt NatibleUsersTable.Arn
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref NatibleUsersTable
- Statement:
- Effect: Allow
Action:
- sqs:ReceiveMessage
- sqs:DeleteMessage
Resource:
- !GetAtt UnsubscribeQueue.Arn
Events:
UnsubscribeQueueEvent:
Type: SQS
Properties:
Queue: !GetAtt UnsubscribeQueue.Arn
BatchSize: 10
UnsubscribeFunctionLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Retain
Properties:
LogGroupName: !Sub /aws/lambda/${UnsubscribeFunction}
UnsubscribeApi:
Type: AWS::Serverless::Api
Properties:
Name: !Sub
- ${ResourceName}
- ResourceName: unsubscribe
StageName: Prod
DefinitionBody:
openapi: '3.0'
info: {}
paths:
/userId/:
post:
x-amazon-apigateway-integration:
httpMethod: POST
type: aws_proxy
uri: !Sub arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ScheduleUnsubscribeFunction.Arn}/invocations
responses: {}
EndpointConfiguration: REGIONAL
TracingEnabled: true
Outputs:
ApiEndpoint:
Description: Unsubscribe API Endpoint
Value: !Sub https://${UnsubscribeApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/
ApiEndpointUserId:
Description: Unsubscribe API Endpoint with userId
Value: !Sub https://${UnsubscribeApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/userId/