3. Backend - KiteSync/realm-sync-js GitHub Wiki

Instructions to SetUp AWS Remote DataStore

Perform the following steps to create AWS DynamoDB table and the AWS Lambda functions and Amazon API Gateway APIs after logging into the AWS console.

Setup IAM User to get Access keys

Follow the instructions on the below link to create a AWS IAM User Access Key http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html#Using_CreateAccessKey

Setup DynamoDB

Create a table in Amazon DynamoDB. In the Amazon DynamoDB console, click on Create Table, name the table RealmSync, select a Hash Key Type of String, and name the key userId and a Sort Key Type of Number, and name the key usn. Press Create.

Setup IAM Role

Next create a policy that allows your AWS Lambda functions to access CloudWatch logs and the RealmSync table. Select the AWS IAM console. Click on Roles in the left menu, and then click the Create New Role button. Name the role APIGatewayLambdaExecRole and click Next Step. Under AWS Service Roles, select AWS Lambda. Attach the following Policies, AWSLambdaFullAccess, AmazonAPIGatewayInvokeFullAccess, AmazonDynamoDBFullAccess, AWSLambdaDynamoDBExecutionRole, CloudWatchLogsFullAccess, AWSLambdaExecute, AmazonAPIGatewayAdministrator, AWSLambdaBasicExecutionRole, AWSLambdaInvocation-DynamoDB Click Next Step and then Create Role

Setup the Lambda function

The next three steps create the AWS Lambda functions for getting and putting information into the Table you just created. In the AWS Lambda console, select Create a Lambda function (if you have not created an AWS Lambda function before, you will click Get Started Now). Click Skip for selecting a blueprint, and enter GetLatestUpdates for the Name. Select Node.js for the runtime, and paste the following code to read from the dynamodb table: from the repo services/LambdaFunctions/GetLatestUpdates.js For Role, select the APIGatewayLambdaExecRole role you just created and leave the default for all other settings. Click Next, and then click Create function. Click Test. You should see an empty output ({}) in theExecution Results section since the table is empty.

Repeat the previous step, naming the new function PushUpdates and paste this code: from the repo services/LambdaFunctions/PushUpdates.js Test the function by clicking on the Actions drop down and choosing Configure sample event. Enter the following for sample data, and click Submit: { "userId": "123", "logs": [ { "body": "", "modified": 1463439756301, "realmSyncId": "LCA465E-F199-AECB", "type": "Note", "usn": 1 } ] }

Repeat the previous step, naming the new function GetLastUpdateCount and paste this code: from the repo services/LambdaFunctions/GetLastUpdateCount.js Test the function by clicking on the Actions drop down and choosing Configure sample event. Enter the following for sample data, and click Submit: { "userId": "123" }

Setup the API Gateway

  • Go to the Amazon API Gateway console, and click Create API. Name the API RealmSync and click Create API.
  • Click Create Resource. Name the resource sync, and click Create Resource again.
  • In the left pane, select /sync and then click the Create Resource button. Name the resource count, and click Create Resource again.
  • In the left pane, select /sync and then click the Create Method button. In the drop down, select GET and click the checkmark button. Select Lambda Function for integration type, select the region you are in, and select GetLatestUpdates for the Lambda function. Click Save and then OK in the popup.
  • Select the GET method you just created. On the right pane, click Method Request. Add two query string parameters userId and lastUpdate.
  • For the same GET method, click Integration Request on the right pane. Add the following body mapping template of type application/json. { "lastUpdate": $input.params('lastUpdate'), "userId": "$input.params('userId')" }
  • In the left pane, select /count under /sync and then click the Create Method button. In the drop down, select GET and click the checkmark button. Select Lambda Function for integration type, select the region you are in, and select GetLatestUpdatesCount for the Lambda function. Click Save and then OK in the popup.
  • Select the GET method you just created. On the right pane, click Method Request. Add query string parameter userId.
  • For the same GET method, click Integration Request on the right pane. Add the following body mapping template of type application/json. { "userId": "$input.params('userId')" }
  • In the left pane, select /sync and then click the Create Method button. In the drop down, select POST and click the checkmark button. Select Lambda Function for integration type, select the region you are in, and select PushUpdates for the Lambda function. Click Save and then OK in the popup.