Skip to content

Commit 5c53eb2

Browse files
committed
instructions
1 parent af63412 commit 5c53eb2

File tree

1 file changed

+27
-49
lines changed

1 file changed

+27
-49
lines changed

README.md

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,39 @@
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.
1+
# AWS X-Ray
2+
Documentation: [X-Ray SDK for Java Sample Application](http://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java-sample.html)
33

4+
If you haven't used X-Ray with Scorekeep yet, try the [`xray-gettingstarted`](https://github.com/awslabs/eb-java-scorekeep/tree/xray-gettingstarted) branch first.
45

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:
6+
This branch shows advanced instrumentation with the AWS X-Ray SDK and includes features from other branches. Deploy this branch to see additional trace data in the X-Ray console. Then, follow the instructions below to add an instrumented AWS Lambda function and PostgreSQL database to the application.
7+
8+
## AWS Lambda
9+
From branch: [`lambda`](https://github.com/awslabs/eb-java-scorekeep/tree/lambda)
10+
11+
In the [`UserFactory`](https://github.com/awslabs/eb-java-scorekeep/tree/xray/src/main/java/scorekeep/UserFactory.java) class, Scorekeep calls a Node.js AWS Lambda function to generate random usernames. If the call to Lambda fails, Scorekeep falls back on a public API to generate names.
12+
13+
Run the script in the `_lambda` folder to create the AWS Lambda function that Scorekeep calls to generate random names:
14+
eb-java-scorekeep/_lambda$ ./create-lambda-role.sh
15+
16+
The script uses a CloudFormation template and the AWS CLI to create the function and its execution role:
717
- `_lambda/lambda-role.yml` - Template that defines the role
818
- `_lambda/create-lambda-role.sh` - Script to create the role
919
- `_lambda/delete-lambda-role.sh` - Script to delete the role
1020

11-
Run the script to create the role:
12-
eb-java-scorekeep/_lambda$ ./create-lambda-role.sh
21+
If you don't have the AWS CLI, [install it](http://docs.aws.amazon.com/cli/latest/userguide/installing.html) or use the CloudFormation console to create a stack with the template.
1322

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:
23+
Next, add Lambda permission to your instance profile ([aws-elasticbeanstalk-ec2-role](https://console.aws.amazon.com/iam/home#/roles/aws-elasticbeanstalk-ec2-role)) to let the environment update the Lambda function:
1724
- AWSLambdaFullAccess
1825

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.
26+
Note: In the `lambda` branch, Scorekeep creates the Lambda function with a configuration file. In this branch, you create the function independently with the same template that creates the role. This lets the `xray` branch work even if the Lambda function and role have not been created, whereas in the `lambda` branch, the deployment fails if you haven't created the required role.
27+
28+
## Amazon RDS
29+
From branch: [`sql`](https://github.com/awslabs/eb-java-scorekeep/tree/sql)
30+
Documentation: [Instrumenting Calls to a PostgreSQL Database](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java-sample.html#xray-sdk-java-sample-postgresql)
31+
32+
In [`application-pgsql.properties`](https://github.com/awslabs/eb-java-scorekeep/tree/xray/src/main/resources/application-pgsql.properties), Scorekeep adds the X-Ray SDK tracing interceptor to the JDBC data source used by Hibernate.
33+
34+
[Add a PostgreSQL database](http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.managing.db.html) to your Elastic Beanstalk environment to enable RDS tracing on the X-Ray demo page.
35+
36+
Hibernate also calls the database during application startup. No segment is available to the X-Ray SDK during startup, so we create one manually in [`RdsWebConfig.java`](https://github.com/awslabs/eb-java-scorekeep/blob/xray/src/main/java/scorekeep/RdsWebConfig.java#L83) by overriding Hibernate's `SchemaExport` class.
5937

6038
# Scorekeep
6139
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.

0 commit comments

Comments
 (0)