generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathtemplate.yaml
More file actions
218 lines (201 loc) · 6.49 KB
/
template.yaml
File metadata and controls
218 lines (201 loc) · 6.49 KB
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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: An Amazon API Gateway REST API that integrates with a Kinesis Data Stream. (uksb-1tthgi812) (tag:apigw-kinesis)
Resources:
KinesisStream:
Type: AWS::Kinesis::Stream
Properties:
ShardCount: 1
APIGatewayRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- 'sts:AssumeRole'
Effect: Allow
Principal:
Service:
- apigateway.amazonaws.com
Policies:
- PolicyName: APIGatewayKinesisPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'kinesis:PutRecord'
- 'kinesis:PutRecords'
- 'kinesis:GetShardIterator'
- 'kinesis:GetRecords'
Resource: !Sub
- '${varStreamArn}*'
- varStreamArn: !GetAtt KinesisStream.Arn
Api:
Type: 'AWS::ApiGateway::RestApi'
Properties:
Name: apigw-kinesis-integration
Description: Kinesis integration REST API
streams:
Type: 'AWS::ApiGateway::Resource'
Properties:
RestApiId: !Ref Api
ParentId: !GetAtt Api.RootResourceId
PathPart: 'streams'
streamName:
Type: 'AWS::ApiGateway::Resource'
Properties:
RestApiId: !Ref Api
ParentId: !Ref streams
PathPart: '{stream-name}'
record:
Type: 'AWS::ApiGateway::Resource'
Properties:
RestApiId: !Ref Api
ParentId: !Ref streamName
PathPart: 'record'
records:
Type: 'AWS::ApiGateway::Resource'
Properties:
RestApiId: !Ref Api
ParentId: !Ref streamName
PathPart: 'records'
sharditerator:
Type: 'AWS::ApiGateway::Resource'
Properties:
RestApiId: !Ref Api
ParentId: !Ref streamName
PathPart: 'sharditerator'
recordMethodPost:
Type: 'AWS::ApiGateway::Method'
Properties:
RestApiId: !Ref Api
ResourceId: !Ref record
HttpMethod: PUT
ApiKeyRequired: false
AuthorizationType: NONE
Integration:
Type: AWS
Credentials: !GetAtt APIGatewayRole.Arn
# Should always be POST when integrating with AWS services
IntegrationHttpMethod: POST
# More info: https://docs.aws.amazon.com/apigateway/api-reference/resource/integration/
Uri: !Sub 'arn:aws:apigateway:${AWS::Region}:kinesis:action/PutRecord'
PassthroughBehavior: WHEN_NO_TEMPLATES
RequestTemplates:
application/json: !Sub |
{
"StreamName": "$input.params('stream-name')",
"Data": "$util.base64Encode($input.json('$.Data'))",
"PartitionKey": "$input.path('$.PartitionKey')"
}
IntegrationResponses:
- StatusCode: '200'
MethodResponses:
- StatusCode: '200'
recordsMethodPost:
Type: 'AWS::ApiGateway::Method'
Properties:
RestApiId: !Ref Api
ResourceId: !Ref records
HttpMethod: PUT
ApiKeyRequired: false
AuthorizationType: NONE
Integration:
Type: AWS
Credentials: !GetAtt APIGatewayRole.Arn
# Should always be POST when integrating with AWS services
IntegrationHttpMethod: POST
# More info: https://docs.aws.amazon.com/apigateway/api-reference/resource/integration/
Uri: !Sub 'arn:aws:apigateway:${AWS::Region}:kinesis:action/PutRecords'
PassthroughBehavior: WHEN_NO_TEMPLATES
RequestTemplates:
application/json: !Sub |
{
"StreamName": "$input.params('stream-name')",
"Records": [
#foreach($elem in $input.path('$.records'))
{
"Data": "$util.base64Encode($elem.data)",
"PartitionKey": "$elem.partition-key"
}#if($foreach.hasNext),#end
#end
]
}
IntegrationResponses:
- StatusCode: '200'
MethodResponses:
- StatusCode: '200'
sharditeratorMethodGET:
Type: 'AWS::ApiGateway::Method'
Properties:
RestApiId: !Ref Api
ResourceId: !Ref sharditerator
HttpMethod: GET
ApiKeyRequired: false
AuthorizationType: NONE
RequestParameters:
method.request.querystring.shard-id: false
Integration:
Type: AWS
Credentials: !GetAtt APIGatewayRole.Arn
# Should always be POST when integrating with AWS services
IntegrationHttpMethod: POST
# More info:
Uri: !Sub 'arn:aws:apigateway:${AWS::Region}:kinesis:action/GetShardIterator'
PassthroughBehavior: WHEN_NO_TEMPLATES
RequestTemplates:
application/json: !Sub |
{
"ShardId": "$input.params('shard-id')",
"ShardIteratorType": "TRIM_HORIZON",
"StreamName": "$input.params('stream-name')"
}
IntegrationResponses:
- StatusCode: '200'
MethodResponses:
- StatusCode: '200'
recordsMethodGET:
Type: 'AWS::ApiGateway::Method'
Properties:
RestApiId: !Ref Api
ResourceId: !Ref records
HttpMethod: GET
ApiKeyRequired: false
AuthorizationType: NONE
RequestParameters:
method.request.header.shard-Iterator: false
Integration:
Type: AWS
Credentials: !GetAtt APIGatewayRole.Arn
# Should always be POST when integrating with AWS services
IntegrationHttpMethod: POST
# More info:
Uri: !Sub 'arn:aws:apigateway:${AWS::Region}:kinesis:action/GetRecords'
PassthroughBehavior: WHEN_NO_TEMPLATES
RequestTemplates:
application/json: !Sub |
{
"ShardIterator": "$input.params('Shard-Iterator')"
}
IntegrationResponses:
- StatusCode: '200'
MethodResponses:
- StatusCode: '200'
ApiDeployment:
Type: 'AWS::ApiGateway::Deployment'
DependsOn:
- recordMethodPost
Properties:
RestApiId: !Ref Api
StageName: 'dev'
Outputs:
ApiRootUrl:
Description: Root Url of the API
Value: !Sub
- 'https://${ApiId}.execute-api.${AWS::Region}.${AWS::URLSuffix}/dev'
- ApiId: !Ref Api
KinesisStream:
Description: Kinesis Data Stream name
Value: !Ref KinesisStream