Skip to content

Commit 6171fd6

Browse files
committed
docs: edit explanation of dynamodb proxy
1 parent 6045a21 commit 6171fd6

File tree

4 files changed

+967
-6400
lines changed

4 files changed

+967
-6400
lines changed

README.md

Lines changed: 32 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,26 @@ This Serverless Framework plugin supports the AWS service proxy integration feat
1010
- [Install](#install)
1111
- [Supported AWS services](#supported-aws-services)
1212
- [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)
13+
- [Kinesis](#kinesis)
14+
- [SQS](#sqs)
15+
- [Customizing request parameters](#customizing-request-parameters)
16+
- [S3](#s3)
17+
- [Customizing request parameters](#customizing-request-parameters-1)
18+
- [Customize the Path Override in API Gateway](#customize-the-path-override-in-api-gateway)
19+
- [Can use greedy, for deeper Folders](#can-use-greedy--for-deeper-folders)
20+
- [SNS](#sns)
21+
- [DynamoDB](#dynamodb)
22+
- [Sample request after deploying.](#sample-request-after-deploying)
2023
- [PutItem](#putitem)
2124
- [UpdateItem](#updateitem)
2225
- [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)
26+
- [Enabling CORS](#enabling-cors)
27+
- [Adding Authorization](#adding-authorization)
28+
- [Using a Custom IAM Role](#using-a-custom-iam-role)
29+
- [Customizing API Gateway parameters](#customizing-api-gateway-parameters)
30+
- [Customizing request body mapping templates](#customizing-request-body-mapping-templates)
31+
- [Kinesis](#kinesis-1)
32+
- [SNS](#sns-1)
2833

2934
## Install
3035

@@ -248,10 +253,12 @@ custom:
248253
'integration.request.path.object': 'context.requestId'
249254
'integration.request.header.cache-control': "'public, max-age=31536000, immutable'"
250255
```
256+
251257
This will result in API Gateway setting the Path Override attribute to `{bucket}/{folder}/{file}.xml`
252258
So for example if you navigate to the API Gatway endpoint `/language/en` it will fetch the file in S3 from `{bucket}/language/en.xml`
253259

254260
##### Can use greedy, for deeper Folders
261+
255262
The forementioned example can also be shortened by a greedy approach. Thanks to @taylorreece for mentioning this.
256263

257264
```yaml
@@ -302,55 +309,31 @@ curl https://xxxxxx.execute-api.us-east-1.amazonaws.com/dev/sns -d '{"message":
302309

303310
### DynamoDB
304311

305-
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`.
306-
307-
| Method | Mapped action | Note |
308-
----|----|----
309-
| post | PutItem | hashkey is set apigateway requestid automatically by default |
310-
| put | PutItem | hashkey is set `pathParam` or `queryStringParam` |
311-
| patch | UpdateItem ||
312-
| get | GetItem ||
313-
| delete | DeleteItem ||
314-
315-
Sample syntax for DynamoDB proxy in `serverless.yml`.
312+
Sample syntax for DynamoDB proxy in `serverless.yml`. Currently, the supported (DynamoDB Operations)[https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Operations.html] are `PutItem`, `GetItem` and `DeleteItem`.
316313

317314
```yaml
318315
custom:
319316
apiGatewayServiceProxies:
320-
- dynamodb:
321-
path: /dynamodb
322-
method: post
323-
tableName: { Ref: 'YourTable' }
324-
hashKey: 'id' # You must define hashKey attribute with `post` method
325-
cors: true
326-
- dynamodb:
327-
path: /dynamodb/{id}
328-
method: put
329-
tableName: { Ref: 'YourTable' }
330-
hashKey:
331-
pathParam: id
332-
attributeType: S
333-
action: PutItem # optional
334-
cors: true
335317
- dynamodb:
336318
path: /dynamodb/{id}/{sort}
337-
method: patch
319+
method: put
338320
tableName: { Ref: 'YourTable' }
339-
hashKey:
321+
hashKey: # set pathParam or queryStringParam as a partitionkey.
340322
pathParam: id
341323
attributeType: S
342-
rangeKey:
324+
rangeKey: # required if also using sort key. set pathParam or queryStringParam.
343325
pathParam: sort
344326
attributeType: S
345-
action: UpdateItem
327+
action: PutItem # specify actions to the table what you want
328+
condition: attribute_not_exists(Id) # optional Condition Expressions parameter for the table
346329
cors: true
347330
- dynamodb:
348331
path: /dynamodb
349332
method: get
350333
tableName: { Ref: 'YourTable' }
351334
hashKey:
352-
queryStringParam: id
353-
attributeType: N
335+
queryStringParam: id # use query string parameter
336+
attributeType: S
354337
rangeKey:
355338
queryStringParam: sort
356339
attributeType: S
@@ -373,18 +356,14 @@ resources:
373356
Properties:
374357
TableName: YourTable
375358
AttributeDefinitions:
376-
-
377-
AttributeName: id
359+
- AttributeName: id
378360
AttributeType: S
379-
-
380-
AttributeName: sort
361+
- AttributeName: sort
381362
AttributeType: S
382363
KeySchema:
383-
-
384-
AttributeName: id
364+
- AttributeName: id
385365
KeyType: HASH
386-
-
387-
AttributeName: sort
366+
- AttributeName: sort
388367
KeyType: RANGE
389368
ProvisionedThroughput:
390369
ReadCapacityUnits: 1
@@ -393,35 +372,12 @@ resources:
393372

394373
#### Sample request after deploying.
395374

396-
##### PutItem
397375
```bash
398-
curl -XPUT https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/dynamodb/<hashKey> \
376+
curl -XPUT https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/dynamodb/<hashKey>/<sortkey> \
399377
-d '{"name":{"S":"john"},"address":{"S":"xxxxx"}}' \
400378
-H 'Content-Type:application/json'
401379
```
402380

403-
##### UpdateItem
404-
```bash
405-
curl -XPUT https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/dynamodb/<hashKey>/<rangeKey> \
406-
-d <Body> \
407-
-H 'Content-Type:application/json'
408-
```
409-
410-
Request body
411-
```json
412-
{
413-
"UpdateExpression": "SET #n = :newName",
414-
"ExpressionAttributeValues": {
415-
":newName": {
416-
"S": "michel"
417-
}
418-
},
419-
"ExpressionAttributeNames": {
420-
"#n": "name"
421-
}
422-
}
423-
```
424-
425381
## Common API Gateway features
426382

427383
### Enabling CORS

lib/package/dynamodb/compileMethodsToDynamodb.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const _ = require('lodash')
4+
const { oneLineTrim } = require('common-tags')
45

56
module.exports = {
67
compileMethodsToDynamodb() {
@@ -165,7 +166,11 @@ module.exports = {
165166
},
166167

167168
getGetItemDefaultDynamodbResponseTemplate() {
168-
return `#set($item = $input.path('$.Item')){#foreach($key in $item.keySet())#set ($value = $item.get($key))#foreach( $type in $value.keySet())"$key":"$value.get($type)"#if($foreach.hasNext()),#end#end#if($foreach.hasNext()),#end#end}`
169+
return oneLineTrim`
170+
#set($item = $input.path('$.Item')){#foreach($key in $item.keySet())
171+
#set ($value = $item.get($key))#foreach( $type in $value.keySet())"$key":"$value.get($type)"
172+
#if($foreach.hasNext()),#end#end#if($foreach.hasNext()),#end#end}
173+
`
169174
},
170175

171176
buildDefaultDynamodbRequestTemplate(http) {

0 commit comments

Comments
 (0)