This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
template.yaml
220 lines (200 loc) · 6.22 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
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
---
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: AWS AppSync Long Running Query
Resources:
## AppSync ##
SearchApi:
Type: AWS::AppSync::GraphQLApi
Properties:
AuthenticationType: API_KEY
Name: SearchApi
SearchApiKey:
Type: AWS::AppSync::ApiKey
Properties:
ApiId: !GetAtt SearchApi.ApiId
Description: API Key for Search API
MessagesSchema:
Type: AWS::AppSync::GraphQLSchema
Properties:
ApiId: !GetAtt SearchApi.ApiId
DefinitionS3Location: schema.graphql
SearchQueryResolver:
Type: AWS::AppSync::Resolver
Properties:
ApiId: !GetAtt SearchApi.ApiId
TypeName: Query
FieldName: search
DataSourceName: !GetAtt StepFunctionsHttpDataSource.Name
# Generate a unique execution id for the step function execution so
# that it can be passed through to Lambda on Step Function start.
# Use context stash so it is also available in response mapping.
RequestMappingTemplate: !Sub |
$util.qr($ctx.stash.put("executionId", $util.autoId()))
{
"version": "2018-05-29",
"method": "POST",
"resourcePath": "/",
"params": {
"headers": {
"content-type": "application/x-amz-json-1.0",
"x-amz-target":"AWSStepFunctions.StartExecution"
},
"body": {
"stateMachineArn": "${SearchStateMachine}",
"input": "{ \"name\": \"$ctx.stash.executionId\" }"
}
}
}
# Return the execution id from Step Functions for client to subscribe.
ResponseMappingTemplate: |
{
"id": "${ctx.stash.executionId}",
"status": "PENDING"
}
PublishResultMutationResolver:
Type: AWS::AppSync::Resolver
Properties:
ApiId: !GetAtt SearchApi.ApiId
TypeName: Mutation
FieldName: publishResult
DataSourceName: !GetAtt PublishResultDataSource.Name
RequestMappingTemplate: |
{
"version": "2017-02-28",
"payload": {
"id": "${ctx.arguments.result.id}",
"status": "${ctx.arguments.result.status}",
"listings": "${ctx.arguments.result.listings}"
}
}
ResponseMappingTemplate: |
$util.toJson($ctx.arguments.result)
StepFunctionsHttpDataSource:
Type: AWS::AppSync::DataSource
Properties:
ApiId: !GetAtt SearchApi.ApiId
Name: StepFunctionsHttpDataSource
Description: Step Functions HTTP
Type: HTTP
ServiceRoleArn: !GetAtt AppSyncServiceRole.Arn
HttpConfig:
Endpoint: !Sub https://states.${AWS::Region}.amazonaws.com/
AuthorizationConfig:
AuthorizationType: AWS_IAM
AwsIamConfig:
SigningRegion: !Ref AWS::Region
SigningServiceName: states
PublishResultDataSource:
Type: AWS::AppSync::DataSource
Properties:
ApiId: !GetAtt SearchApi.ApiId
Name: PublishResultDataSource
Description: Local data source to publish results
Type: NONE
SearchStateMachine:
Type: AWS::StepFunctions::StateMachine
Properties:
StateMachineName: aws-appsync-long-query-search
RoleArn: !GetAtt SearchStateMachineRole.Arn
DefinitionString: !Sub |
{
"Comment": "Wait and return a result",
"StartAt": "Wait For One Minute",
"States": {
"Wait For One Minute": {
"Type": "Wait",
"Seconds": 60,
"Next": "Return Result"
},
"Return Result": {
"Type": "Task",
"Resource": "${ReturnResultFunction.Arn}",
"ResultPath": "$",
"TimeoutSeconds": 10,
"Catch": [{
"ErrorEquals": [ "States.ALL" ],
"ResultPath": "$.error",
"Next": "HandleError"
}],
"End": true
},
"HandleError": {
"Type": "Fail",
"Cause": "$.error"
}
}
}
ReturnResultFunction:
Type: AWS::Serverless::Function
Properties:
Description: Returns a result by calling AppSync mutation, triggering subscription
CodeUri: result/
Runtime: nodejs10.x
Handler: index.handler
Timeout: 10
Environment:
Variables:
APPSYNC_API_KEY: !GetAtt SearchApiKey.ApiKey
APPSYNC_ENDPOINT: !GetAtt SearchApi.GraphQLUrl
#
# IAM
#
SearchStateMachineRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: !Sub states.${AWS::Region}.amazonaws.com
Action: sts:AssumeRole
Path: /long-query/
Policies:
- PolicyName: StatesExecutionPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- lambda:InvokeFunction
Resource:
- !GetAtt ReturnResultFunction.Arn
AppSyncServiceRole:
Type: AWS::IAM::Role
Properties:
Path: /long-query/
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSAppSyncPushToCloudWatchLogs
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- sts:AssumeRole
Principal:
Service:
- appsync.amazonaws.com
Policies:
- PolicyName: appsync-long-query-service-policy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- states:StartExecution
Resource:
- !Ref SearchStateMachine
Outputs:
ApiEndpoint:
Description: AppSync Endpoint
Value: !GetAtt SearchApi.GraphQLUrl
ApiId:
Description: AppSync API ID
Value: !GetAtt SearchApi.ApiId
ApiKey:
Description: AppSync API Key
Value: !GetAtt SearchApiKey.ApiKey