Scalable serverless voting app - built with AWS Amplify, AWS AppSync, and Amazon DynamoDB
You can deploy this app and the back end infrastructure in one of two ways:
- The Amplify CLI
- One click deploy button
- First install and configure the Amplify CLI.
For a complete walkthrough of how to configure the CLI, see this video
$ npm install -g @aws-amplify/cli
$ amplify configure
- Clone the repo, install dependencies
$ git clone https://github.com/dabit3/this-or-that.git
$ cd this-or-that
$ npm install
- Initialize the app
$ amplify init
? Enter a name for the environment: (your preferred env name)
? Choose your default editor: (your preferred editor)
? Do you want to use an AWS profile? Yes
? Please choose the profile you want to use: your-profile-name
? Do you want to configure Lambda Triggers for Cognito? No
- Deploy the back end
$ amplify push --y
- Run the app
$ npm start
While the voting API is built with DynamoDB and AppSync, the main functionality really is within a single GraphQL resolver that sends an atomic update to DynamoDB.
This atomic update allows the DynamoDB table to stay consistent regardless of the number of other operations that are happening.
Request mapping template
{
"version": "2018-05-29",
"operation": "UpdateItem",
"key" : {
"id" : $util.dynamodb.toDynamoDBJson($context.arguments.id)
},
"update": {
"expression" : "set #upvotes = #upvotes + :updateValue",
"expressionNames" : {
"#upvotes" : "upvotes"
},
"expressionValues" : {
":updateValue" : { "N" : 1 }
}
}
}
Response mapping template
$util.quiet($ctx.result.put("clientId", "$context.arguments.clientId"))
$util.toJson($ctx.result)