Skip to content

Commit bcd00e1

Browse files
committed
docs: add explanation of dynamodb proxy
1 parent 7eec8c5 commit bcd00e1

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

README.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@
55

66
This Serverless Framewrok plugin supports the AWS service proxy integration feature of API Gateway. You can directly connect API Gateway to AWS services without Lambda.
77

8+
## TOC
9+
10+
- [Install](#install)
11+
- [Supported AWS services](#supported-aws-services)
12+
- [How to use](#how-to-use)
13+
* [Kinesis](#kinesis)
14+
* [SQS](#sqs)
15+
+ [Customizing request parameters](#customizing-request-parameters)
16+
* [S3](#s3)
17+
* [SNS](#sns)
18+
* [DynamoDB](#dynamodb)
19+
+ [Sample request after deploying.](#sample-request-after-deploying)
20+
- [PutItem](#putitem)
21+
- [UpdateItem](#updateitem)
22+
- [Common API Gateway features](#common-api-gateway-features)
23+
* [Enabling CORS](#enabling-cors)
24+
* [Adding Authorization](#adding-authorization)
25+
* [Customizing request body mapping templates](#customizing-request-body-mapping-templates)
26+
+ [Kinesis](#kinesis-1)
27+
+ [SNS](#sns-1)
28+
829
## Install
930

1031
Run `servelress plugin install` in your Serverless project.
@@ -22,6 +43,7 @@ Please pull request if you are intersted in it.
2243
- SQS
2344
- S3
2445
- SNS
46+
- DynamoDB
2547

2648
## How to use
2749

@@ -201,6 +223,128 @@ Sample request after deploying.
201223
curl https://xxxxxx.execute-api.us-east-1.amazonaws.com/dev/sns -d '{"message": "testtest"}' -H 'Content-Type:application/json'
202224
```
203225

226+
### DynamoDB
227+
228+
The (DynamoDB Operation)[https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Operations.html] is automatically mapped by the `method` with following HTTP specification if you omit the `action`.
229+
230+
| Method | Mapped action | Note |
231+
----|----|----
232+
| post | PutItem | hashkey is set apigateway requestid automatically by default |
233+
| put | PutItem | hashkey is set `pathParam` or `queryStringParam` |
234+
| patch | UpdateItem ||
235+
| get | GetItem ||
236+
| delete | DeleteItem ||
237+
238+
Sample syntax for DynamoDB proxy in `serverless.yml`.
239+
240+
```yaml
241+
custom:
242+
apiGatewayServiceProxies:
243+
- dynamodb:
244+
path: /dynamodb
245+
method: post
246+
tableName: { Ref: 'YourTable' }
247+
hashKey: 'id' # You must define hashKey attribute with `post` method
248+
cors: true
249+
- dynamodb:
250+
path: /dynamodb/{id}
251+
method: put
252+
tableName: { Ref: 'YourTable' }
253+
hashKey:
254+
pathParam: id
255+
attributeType: S
256+
action: PutItem # optional
257+
cors: true
258+
- dynamodb:
259+
path: /dynamodb/{id}/{sort}
260+
method: patch
261+
tableName: { Ref: 'YourTable' }
262+
hashKey:
263+
pathParam: id
264+
attributeType: S
265+
rangeKey:
266+
pathParam: sort
267+
attributeType: S
268+
action: UpdateItem
269+
cors: true
270+
- dynamodb:
271+
path: /dynamodb
272+
method: get
273+
tableName: { Ref: 'YourTable' }
274+
hashKey:
275+
queryStringParam: id
276+
attributeType: N
277+
rangeKey:
278+
queryStringParam: sort
279+
attributeType: S
280+
action: GetItem
281+
cors: true
282+
- dynamodb:
283+
path: /dynamodb/{id}
284+
method: delete
285+
tableName: { Ref: 'YourTable' }
286+
hashKey:
287+
pathParam: id
288+
attributeType: S
289+
action: DeleteItem
290+
cors: true
291+
292+
resources:
293+
Resources:
294+
YourTable:
295+
Type: AWS::DynamoDB::Table
296+
Properties:
297+
TableName: YourTable
298+
AttributeDefinitions:
299+
-
300+
AttributeName: id
301+
AttributeType: S
302+
-
303+
AttributeName: sort
304+
AttributeType: S
305+
KeySchema:
306+
-
307+
AttributeName: id
308+
KeyType: HASH
309+
-
310+
AttributeName: sort
311+
KeyType: RANGE
312+
ProvisionedThroughput:
313+
ReadCapacityUnits: 1
314+
WriteCapacityUnits: 1
315+
```
316+
317+
#### Sample request after deploying.
318+
319+
##### PutItem
320+
```bash
321+
curl -XPUT https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/dynamodb/<hashKey> \
322+
-d '{"name":{"S":"john"},"address":{"S":"xxxxx"}}' \
323+
-H 'Content-Type:application/json'
324+
```
325+
326+
##### UpdateItem
327+
```bash
328+
curl -XPUT https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/dynamodb/<hashKey>/<rangeKey> \
329+
-d <Body> \
330+
-H 'Content-Type:application/json'
331+
```
332+
333+
Request body
334+
```json
335+
{
336+
"UpdateExpression": "SET #n = :newName",
337+
"ExpressionAttributeValues": {
338+
":newName": {
339+
"S": "michel"
340+
}
341+
},
342+
"ExpressionAttributeNames": {
343+
"#n": "name"
344+
}
345+
}
346+
```
347+
204348
## Common API Gateway features
205349

206350
### Enabling CORS

0 commit comments

Comments
 (0)