-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yaml
334 lines (334 loc) · 9.72 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
MemorySize: 3008
Timeout: 30
Runtime: python3.11
Tracing: Active
Architectures:
- arm64
Parameters:
WebhookSiteURL:
Type: String
Default: 'https://webhook.site/YOURSITEHERE'
Resources:
EventBridgeToToSnsPolicy:
Type: AWS::SNS::TopicPolicy
Properties:
PolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: events.amazonaws.com
Action: sns:Publish
Resource: !Ref UsersSNSTopic
Topics:
- !Ref UsersSNSTopic
MarketplaceCustomEventBus:
Type: AWS::Events::EventBus
Properties:
Name: marketplace
MarketplaceAPI:
Type: AWS::Serverless::Api
Properties:
Name: !Sub
- ${ResourceName} From Stack ${AWS::StackName}
- ResourceName: MarketplaceAPI
StageName: Prod
Auth:
ApiKeyRequired: true
DefinitionBody:
openapi: '3.0'
info: {}
paths:
/request/{id}:
get:
x-amazon-apigateway-integration:
httpMethod: POST
type: aws_proxy
uri: !Sub arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetRequestsFunction.Arn}/invocations
responses: {}
post:
x-amazon-apigateway-integration:
httpMethod: POST
type: aws_proxy
uri: !Sub arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${UpdateRequestsFunction.Arn}/invocations
responses: {}
EndpointConfiguration: REGIONAL
TracingEnabled: true
ApiKey:
Type: AWS::ApiGateway::ApiKey
Properties:
Enabled: True
UsagePlan:
Type: AWS::ApiGateway::UsagePlan
Properties:
ApiStages:
- ApiId: !Ref MarketplaceAPI
Stage: Prod
UsagePlanKey:
Type: AWS::ApiGateway::UsagePlanKey
Properties:
KeyId: !Ref ApiKey
KeyType: API_KEY
UsagePlanId: !Ref UsagePlan
GetRequestsFunction:
Type: AWS::Serverless::Function
Properties:
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: GetRequestsFunction
CodeUri: src/GetRequestsFunction
Handler: handler.handler
Environment:
Variables:
TABLE_NAME: !Ref requests
TABLE_ARN: !GetAtt requests.Arn
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref requests
Events:
MarketplaceAPIGETrequestid:
Type: Api
Properties:
Path: /request/{id}
Method: GET
RestApiId: !Ref MarketplaceAPI
Auth:
ApiKeyRequired: true
GetRequestsFunctionLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Retain
Properties:
LogGroupName: !Sub /aws/lambda/${GetRequestsFunction}
UpdateRequestsFunction:
Type: AWS::Serverless::Function
Properties:
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: UpdateRequestsFunction
CodeUri: src/UpdateRequestsFunction
Handler: handler.handler
Environment:
Variables:
TABLE_NAME: !Ref requests
TABLE_ARN: !GetAtt requests.Arn
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref requests
- Statement:
- Effect: Allow
Action:
- events:PutEvents
Resource:
- !GetAtt MarketplaceCustomEventBus.Arn
Events:
MarketplaceAPIPOSTrequestid:
Type: Api
Properties:
Path: /request/{id}
Method: POST
RestApiId: !Ref MarketplaceAPI
UpdateRequestsFunctionLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Retain
Properties:
LogGroupName: !Sub /aws/lambda/${UpdateRequestsFunction}
requests:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: id
KeyType: HASH
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
Update3rdPartyFunction:
Type: AWS::Serverless::Function
Properties:
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: Update3rdPartyFunction
CodeUri: src/Update3rdPartyFunction
Handler: handler.handler
Environment:
Variables:
TABLE_NAME: !Ref requests
TABLE_ARN: !GetAtt requests.Arn
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref requests
Update3rdPartyFunctionLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Retain
Properties:
LogGroupName: !Sub /aws/lambda/${Update3rdPartyFunction}
Update3rdpartyToUpdate3rdPartyFunctionPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt Update3rdPartyFunction.Arn
Principal: !Sub events.${AWS::URLSuffix}
SourceArn: !GetAtt Update3rdPartyFunction.Arn
ThirdrdPartyAPI:
Type: AWS::Serverless::Function
Properties:
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: ThirdrdPartyAPI
CodeUri: src/3rdPartyAPI
Handler: handler.handler
Environment:
Variables:
TABLE_NAME: !Ref requests
TABLE_ARN: !GetAtt requests.Arn
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref requests
ThirdrdPartyAPILogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Retain
Properties:
LogGroupName: !Sub /aws/lambda/${ThirdrdPartyAPI}
ToThirdrdPartyAPI3rdparty2To3rdpartyAPIPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt ThirdrdPartyAPI.Arn
Principal: !Sub events.${AWS::URLSuffix}
SourceArn: !GetAtt To3rdParty2API.Arn
ToThirdrdPartyAPI3rdparty1To3rdpartyAPIPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt ThirdrdPartyAPI.Arn
Principal: !Sub events.${AWS::URLSuffix}
SourceArn: !GetAtt To3rdParty1API.Arn
UsersSNSTopic:
Type: AWS::SNS::Topic
WebhookSiteConnection:
Type: AWS::Events::Connection
Properties:
AuthorizationType: API_KEY
Description: 'WebhookSite connection with an API key'
AuthParameters:
ApiKeyAuthParameters:
ApiKeyName: apikey
ApiKeyValue: apivalue
WebhookSiteDestination:
Type: AWS::Events::ApiDestination
Properties:
Name: 'WebhookSite'
ConnectionArn: !GetAtt WebhookSiteConnection.Arn
InvocationEndpoint: !Ref WebhookSiteURL
HttpMethod: POST
InvocationRateLimitPerSecond: 10
EventBridgeTargetRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- events.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: AllowAPIdestinationAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: 'events:InvokeApiDestination'
Resource: !GetAtt WebhookSiteDestination.Arn
To3rdParty1API:
Type: AWS::Events::Rule
Properties:
EventPattern:
detail:
3rdparty:
- 3rdParty1
EventBusName: marketplace
Targets:
- Id: !Ref ThirdrdPartyAPI
Arn: !GetAtt ThirdrdPartyAPI.Arn
- Arn: !GetAtt WebhookSiteDestination.Arn
RoleArn: !GetAtt EventBridgeTargetRole.Arn
Id: "WebhookSiteDestination"
To3rdParty2API:
Type: AWS::Events::Rule
Properties:
EventPattern:
detail:
3rdparty:
- 3rdParty2
EventBusName: marketplace
Targets:
- Id: !Ref ThirdrdPartyAPI
Arn: !GetAtt ThirdrdPartyAPI.Arn
- Arn: !GetAtt WebhookSiteDestination.Arn
RoleArn: !GetAtt EventBridgeTargetRole.Arn
Id: "WebhookSiteDestination"
ThirdPartytoMarketplaceSNS:
Type: AWS::Events::Rule
Properties:
EventPattern:
Source:
- com.marketplace.3rdparty1
DetailType:
- marketplace status update
detail:
3rdparty:
- 3rdparty1
Targets:
- Id: UsersSNSTopic
Arn: !Ref UsersSNSTopic
EventBusName: marketplace
Update3rdParty:
Type: AWS::Events::Rule
Properties:
EventPattern:
source:
- aws.health
Targets:
- Id: !Ref Update3rdPartyFunction
Arn: !GetAtt Update3rdPartyFunction.Arn
EventBusName: marketplace
Outputs:
# API Gateway endpoint to be used during tests
AppApiEndpoint:
Description: API Endpoint
Value: !Sub "https://${AppApi}.execute-api.${AWS::Region}.amazonaws.com/Prod"
Metadata:
AWS::Composer::Groups:
Group:
Label: EventBridge
Members:
- MarketplaceCustomEventBus
- Update3rdParty
- ThirdPartytoMarketplaceSNS
- To3rdParty2API
- To3rdParty1API
Group3:
Label: EventGateway
Members:
- MarketplaceAPI
- GetRequestsFunction
- UpdateRequestsFunction
- requests
Group4:
Label: SNS
Members:
- EventBridgeToToSnsPolicy
- UsersSNSTopic
Group2:
Label: 3rdParty
Members:
- Update3rdpartyToUpdate3rdPartyFunctionPermission
- Update3rdPartyFunction
- ThirdrdPartyAPI