Skip to content

Commit

Permalink
Added the Lambda implementation of the crud-blog sample
Browse files Browse the repository at this point in the history
  • Loading branch information
jdubray committed May 8, 2016
1 parent 568dd76 commit 5f08d55
Show file tree
Hide file tree
Showing 12 changed files with 1,571 additions and 0 deletions.
118 changes: 118 additions & 0 deletions crud-blog-lambda/DDBsession.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*******************************************************************************************
* The MIT License (MIT)
* -----------------------------------------------------------------------------------------
* Copyright (c) 2016 Convergence Modeling LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in the
* Software without restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* https://opensource.org/licenses/MIT
*
*/





// /////////////////////////////////////////////////////////////////
// DynamoDB session manager
//
//


'use strict' ;

// DynamoDB session manager
var DOC = require('dynamodb-doc') ;
// var BlueBirdPromise = require('bluebird') ;
// var docClient = BlueBirdPromise.promisifyAll(new DOC.DynamoDB()) ;
var docClientSync = new DOC.DynamoDB() ;

var DynamoDBSessionManager = {

dehydrateSession: (model,req) => {

model.__token = model.__token || '1234' ;
var params = {
TableName: getTableName(req),
Item: {
token: model.__token.toString(),
model: JSON.stringify(model)
}
};


var p = new Promise(function(resolve, reject){
docClientSync.putItem(params, function(error, data) {
if (error) {
reject(error);
} else {
// not expecting anything
resolve(data);
}
});
}) ;

// return p ;

// bluebird implementation
// return docClient.putItemAsync(params) ;
},

rehydrateSession: (token,req) => {
req = req || {env: {tableName: 'dynamodb-sam-session'}} ;
let params = {
TableName: getTableName(req),
Key: {
token: token.toString()
}
};

// bluebird implementation
// var item = docClient.getItemAsync(params) ;
// return docClient.getItemAsync(params) ;

var p = new Promise(function(resolve, reject){
docClientSync.getItem(params, function(error, data) {
if (error) {
reject(error);
} else {
data.Item = data.Item || {} ;
data.Item.model = data.Item.model || "{}" ;
resolve(JSON.parse(data.Item.model));
}
});
}) ;

return p ;

}

} ;

function getTableName(request) {
// The table name is stored in the Lambda stage variables
// Go to https://console.aws.amazon.com/apigateway/home/apis/[YOUR API ID]/stages/latest
// and click Stages -> latest -> Stage variables

// These values will be found under request.env
// Here's I'll use a default if not set
// return request.env.tableName || 'dynamodb-sam-session';
return 'dynamodb-sam-session';
}

module.exports = DynamoDBSessionManager ;
43 changes: 43 additions & 0 deletions crud-blog-lambda/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# SAM Lambda

SAM Lambda is a boiler plate project that implements the SAM pattern in AWS Lambda using DynamoDB to manage application/session state

SAM Lambda is leveraging the SAM SAFE library to wire its components and dehydrate/rehydrate the application state.

```
|---Server-----
|-actions.js the system's actions
|-model.js the model that is managed by the SAFE container
|-state.js the state function that returns the application state
|-view.js the view components that are used to render the application state
|-server.js the lambda implementation
|-DDBsession.js the DynamoDB session manager
|
|---Client-----
|-blog.html the home page of the application
|-blog.js the wiring of the client events to the Lambda actions
|
```

To deploy the Lambda:
0. `npm install`
1. Make sure that all the files that will be deployed are chmod'ed to 777
2. Create the lambda
```
claudia create --name io_xgen_sam_safe_samples_lambda --region us-west-2 --api-module server --policies policies
```
Claudia will create a claudia.json files that contains the project's configuration. Subsequent changes will be deployed with the `claudia update`
command.

3. Create a `dynamodb-sam-session` table in the DynamoDB console
4. Make sure the lambda executor role has the appropriate policies (e.g Read/Write the session table)
5. Make sure that the Lambda is configured to run v4.3 of node.js
6. Deploy the API endpoint in the API Gateway console
7. Copy the API endpoint to the blog.html file (API_ID from claudia.json file):
```
var endpoint = 'https://YOUR_API_ID.execute-api.us-west-2.amazonaws.com/latest' ;
```
8. Open the blog.html in your favorite browser



14 changes: 14 additions & 0 deletions crud-blog-lambda/access-dynamodb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"dynamodb:DeleteItem",
"dynamodb:GetItem",
"dynamodb:PutItem"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
70 changes: 70 additions & 0 deletions crud-blog-lambda/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
///////////////////////////////////////////////////////////////////////////////
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <http://unlicense.org/>

'use strict' ;

var actions = {} ;

actions.init = (present) => {
actions.present = present ;
} ;

actions.present = (data) => {
return false ;
} ;

actions.edit = (data,next) => {
data.lastEdited = {title: data.title, description: data.description, id: data.id } ;
actions.present(data,next) ;
return false ;
} ;

actions.save = (data,next) => {
data.item = {title: data.title, description: data.description, id: data.id || ''} ;
if (data.item.id !== '') {
actions.present(data,next) ;
}
else {
// proceed as normal when created a new item
actions.present(data,next) ;
}
return false ;
} ;

actions.delete = (data,next) => {
data.deletedItemId = data.id ;
actions.present(data,next) ;
return false ;
} ;

actions.cancel = (data,next) => {
actions.present(data,next) ;
return false ;
} ;

actions.intents = { edit: 'edit', save: 'save', delete: 'delete', cancel: 'cancel' } ;

module.exports = actions ;
Loading

0 comments on commit 5f08d55

Please sign in to comment.