|
| 1 | +# Lambda integration |
| 2 | +This branch uses a Node.js Lambda function to generate random names for new users, instead of calling a web API. The Scorekeep API uses the AWS SDK to invoke the Lambda by function name (`random-name`) with Bean classes to represent (and serialize to/from) the input and output JSON. |
| 3 | + |
| 4 | + |
| 5 | +## Configuration |
| 6 | +A CloudFormation template and [AWS CLI](http://docs.aws.amazon.com/cli/latest/userguide/installing.html) scripts to create and delete the function's execution role are included in the `_lambda` folder: |
| 7 | +- `_lambda/lambda-role.yml` - Template that defines the role |
| 8 | +- `_lambda/create-lambda-role.sh` - Script to create the role |
| 9 | +- `_lambda/delete-lambda-role.sh` - Script to delete the role |
| 10 | + |
| 11 | +Run the script to create the role: |
| 12 | + eb-java-scorekeep/_lambda$ ./create-lambda-role.sh |
| 13 | + |
| 14 | +If you don't have the AWS CLI, use the CloudFormation console to create a stack with the template. |
| 15 | + |
| 16 | +Next, add the following policy to your instance profile ([aws-elasticbeanstalk-ec2-role](https://console.aws.amazon.com/iam/home#/roles/aws-elasticbeanstalk-ec2-role)) to let the environment create the Lambda function: |
| 17 | +- AWSLambdaFullAccess |
| 18 | + |
| 19 | +Deploy this branch to your Elastic Beanstalk environment. No further configuration is required. |
| 20 | +If you don't have an environment, see below. |
| 21 | + |
| 22 | +## Implementation |
| 23 | +The Lambda function code is included in `_lambda/random-name/index.js`. On deploy, configuration files in the .ebextensions folder create a function with the following settings: |
| 24 | +- Name: `random-name` |
| 25 | +- Runtime: Node.js 4.3 |
| 26 | +- Description: `Generate random names` |
| 27 | +- Code: in `_Lambda/random-name` |
| 28 | +- Environment variables: |
| 29 | + - REGION_NAME: The region, e.g. `us-east-2` |
| 30 | + - TOPIC_ARN: The ARN of an [SNS Topic](https://console.aws.amazon.com/sns/v2/home) |
| 31 | +- Role named "scorekeep-lambda" |
| 32 | + |
| 33 | +The role named "scorekeep-lambda" has the following policies: |
| 34 | +- Managed policy - AWSLambdaBasicExecutionRole |
| 35 | +- Managed policy - AmazonSNSFullAccess |
| 36 | +- Managed policy - AWSXrayWriteOnlyAccess (optional) for compatibility with the xray branch |
| 37 | +- Trust policy - |
| 38 | + |
| 39 | + { |
| 40 | + "Version": "2012-10-17", |
| 41 | + "Statement": [ |
| 42 | + { |
| 43 | + "Effect": "Allow", |
| 44 | + "Principal": { |
| 45 | + "Service": "lambda.amazonaws.com" |
| 46 | + }, |
| 47 | + "Action": "sts:AssumeRole" |
| 48 | + } |
| 49 | + ] |
| 50 | + } |
| 51 | + |
| 52 | +The Scorekeep API integration is implemented in the following files- |
| 53 | +`src/main/java/scorekeep/` |
| 54 | +- `RandomNameInput.java` - Bean for the input, a user ID and category. |
| 55 | +- `RandomNameOutput.java` - Bean for the output, a first name. |
| 56 | +- `RandomNameService.java` - Defines the method used to call the Lambda function. Combined with an AWS SDK Lambda client to create a Lambda Invoker. |
| 57 | +- `UserFactory.java` - **UserFactory.randomNameLambda** Creates the Lambda Invoker with `com.amazonaws.services.lambda.invoke.LambdaInvokerFactory`. Calls the Lambda function to generate a random name. |
| 58 | +- `build.gradle` - Adds the Lambda module of the AWS SDK to the Gradle build. |
| 59 | + |
1 | 60 | # Scorekeep |
2 | 61 | Scorekeep is a RESTful web API implemented in Java that uses Spring to provide an HTTP interface for creating and managing game sessions and users. This project includes the scorekeep API and a frontend web app that consumes it. The frontend and API can run on the same server and domain or separately, with the API running in Elastic Beanstalk and the frontend served statically by a CDN. |
3 | 62 |
|
|
0 commit comments