Skip to content

Commit 921d884

Browse files
committed
Merge branch 'master' into lambda
2 parents d006815 + 357b2f1 commit 921d884

File tree

6 files changed

+58
-4
lines changed

6 files changed

+58
-4
lines changed

.ebextensions/env.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
option_settings:
2+
aws:elasticbeanstalk:customoption:
3+
NotificationEmail: [email protected]
24
aws:elasticbeanstalk:application:environment:
35
AWS_REGION: '`{"Ref" : "AWS::Region"}`'
6+
NOTIFICATION_TOPIC: '`{"Ref" : "NotificationTopic"}`'

.ebextensions/sns-topic.config

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Resources:
2+
NotificationTopic:
3+
Type: AWS::SNS::Topic
4+
Properties:
5+
Subscription:
6+
- Endpoint:
7+
Fn::GetOptionSetting: {DefaultValue: [email protected], OptionName: NotificationEmail}
8+
Protocol: email

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ The project shows the use of Spring, Angular, nginx, the AWS SDK for Java, Dynam
4343
- [`lambda`](https://github.com/awslabs/eb-java-scorekeep/tree/lambda) - Call a Lambda function to generate random names.
4444
- [`sql`](https://github.com/awslabs/eb-java-scorekeep/tree/sql) - Use JDBC to store game histories in an attached PostgreSQL database instance.
4545
- [`xray`](https://github.com/awslabs/eb-java-scorekeep/tree/xray) - Use the AWS X-Ray SDK to instrument incoming requests, SDK clients, SQL queries, HTTP clients, and sections of code.
46-
- [`xray-gettingstarted`](https://github.com/awslabs/eb-java-scorekeep/tree/xray-gettingstarted) - Use the AWS X-Ray to instrument incoming requests and SDK clients (no additional configuration required).
46+
- [`xray-gettingstarted`](https://github.com/awslabs/eb-java-scorekeep/tree/xray-gettingstarted) ([tutorial](https://docs.aws.amazon.com/xray/latest/devguide/xray-gettingstarted.html)) - Use the AWS X-Ray to instrument incoming requests and SDK clients (no additional configuration required).
4747

4848
Use the procedures in the following sections to run the project on AWS Elastic Beanstalk and configure it for local testing and development.
4949

@@ -57,14 +57,15 @@ Create a Java 8 SE environment in Elastic Beanstalk to host the application.
5757
3. When your environment is ready, the console redirects you to the environment Dashboard.
5858
4. Click the URL at the top of the page to open the site.
5959

60-
## Give the application permission to use DynamoDB
61-
When the Scorekeep API runs in AWS Elastic Beanstalk, it uses the permissions of its EC2 instance to call AWS. Elastic Beanstalk provides a default instance profile that you can extend to grant the application the permissions it needs to read from and write to resource tables in DynamoDB.
60+
## Give the application permission to use DynamoDB and SNS
61+
When the Scorekeep API runs in AWS Elastic Beanstalk, it uses the permissions of its EC2 instance to call AWS. Elastic Beanstalk provides a default instance profile that you can extend to grant the application the permissions it needs to read from and write to resource tables in DynamoDB, and send notifications with SNS.
6262

63-
*To add DynamoDB permissions to the instances in your Elastic Beanstalk environment*
63+
*To add DynamoDB and SNS permissions to the instances in your Elastic Beanstalk environment*
6464

6565
1. Open the Elastic Beanstalk instance profile in the IAM console: [aws-elasticbeanstalk-ec2-role](https://console.aws.amazon.com/iam/home#roles/aws-elasticbeanstalk-ec2-role)
6666
2. Click **Attach Policy**.
6767
3. Select [AmazonDynamoDBFullAccess](https://console.aws.amazon.com/iam/home#policies/arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess) and click **Attach Policy**.
68+
3. Select [AmazonSNSFullAccess](https://console.aws.amazon.com/iam/home#policies/arn:aws:iam::aws:policy/AmazonSNSFullAccess) and click **Attach Policy**.
6869

6970
## Deploy the application
7071
Deploy the source code for the project to your Elastic Beanstalk environment.
@@ -82,6 +83,17 @@ Deploy the source code for the project to your Elastic Beanstalk environment.
8283

8384
Click through the app to explore its functionality. Use the network console in your browser to see the HTTP requests that it sends to the API to read and write users, sessions, games, moves and game state to DynamoDB via the API.
8485

86+
## Configure Notifications
87+
The API uses Amazon SNS to send a notification email whenever a game ends. Configure your email address in the source code and redeploy to enable notifications.
88+
89+
*To enable notifications*
90+
1. Clone this repository
91+
2. Add your email address to the `env.config` configuration file in the `.ebextensions` folder.
92+
3. [Create a source bundle](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/applications-sourcebundle.html).
93+
4. [Deploy the source bundle](http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.deploy-existing-version.html#deployments-newversion).
94+
5. Check your email for a subscription confirmation.
95+
6. Complete a game to trigger a notification.
96+
8597
# How it works
8698
The project includes two independent components, an HTML and JavaScript frontend in Angular 1.5 and a Java backend that uses Spring to provide a public API.
8799

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dependencies {
3131
testCompile("org.springframework.boot:spring-boot-starter-test")
3232
compile("com.amazonaws:aws-java-sdk-dynamodb")
3333
compile("com.amazonaws:aws-java-sdk-lambda")
34+
compile("com.amazonaws:aws-java-sdk-sns")
3435
testCompile("junit:junit:4.11")
3536
compile("com.fasterxml.jackson.core:jackson-databind:2.8.4")
3637
}

src/main/java/scorekeep/MoveFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public Move newMove(String sessionId, String gameId, String userId, String moveT
5555
} catch ( ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw new RulesException(rulesName); }
5656
// save new game state
5757
State newState = new State(stateId, sessionId, gameId, newStateText, newTurn);
58+
// send notification on game end
59+
if ( newStateText.startsWith("A") || newStateText.startsWith("B")) {
60+
Utils.sendNotification("Scorekeep game completed", "Winner: " + userId);
61+
}
5862
// register state and move id to game
5963
gameController.setGameMove(sessionId, gameId, moveId);
6064
gameController.setGameState(sessionId, gameId, stateId);

src/main/java/scorekeep/Utils.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package scorekeep;
2+
3+
import com.amazonaws.regions.Regions;
4+
import com.amazonaws.services.sns.AmazonSNS;
5+
import com.amazonaws.services.sns.AmazonSNSClientBuilder;
6+
import com.amazonaws.services.sns.model.PublishRequest;
7+
import com.amazonaws.services.sns.model.PublishResult;
8+
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
public class Utils {
13+
private static final Logger logger = LoggerFactory.getLogger("scorekeep.Utils");
14+
private static AmazonSNS snsclient = AmazonSNSClientBuilder.standard()
15+
.withRegion(Regions.fromName(System.getenv("AWS_REGION")))
16+
.build();
17+
/*
18+
* Send a notification email.
19+
*/
20+
public static void sendNotification(String subject, String body) {
21+
String topicarn = System.getenv("NOTIFICATION_TOPIC");
22+
PublishRequest publishRequest = new PublishRequest(topicarn, body, subject);
23+
PublishResult publishResult = snsclient.publish(publishRequest);
24+
logger.info("Email sent: " + publishResult.getMessageId());
25+
}
26+
}

0 commit comments

Comments
 (0)