Integration & Automation
Automate sports data polling with AWS Step Functions
Using automated data polling with AWS Step Functions, you can build a serverless workflow that integrates AWS services with systems external to AWS. In the fall of 2021, AWS collaborated with the Pacific Science Center (PacSci) in Seattle to build a customized light display in the iconic arches of PacSci’s Seattle Center campus. During night games of the Seattle Kraken hockey team, a celebratory light show illuminates the arches each time the home team scores a goal. This serverless light system uses Step Functions with an AWS Lambda function to poll live game data from Sportradar.
This post will show you how to deploy the PacSci automated sports data polling system for your use. Instead of a light show in the PacSci arches, the sample application sends an email notification each time the score changes during a Seattle Kraken hockey game. It can be modified to follow any team available on Sportradar. We’ll also show you how to deploy all the resources using the AWS Cloud Development Kit (AWS CDK).
About this blog post | |
Time to read | ~6 min. |
Time to complete | ~30 min. |
Cost to complete | ~$1 |
Learning level | Advanced (300) |
AWS services | AWS Cloud Development Kit (AWS CDK) AWS Step Functions Amazon EventBridge AWS Lambda Amazon Simple Notification Service (Amazon SNS) |
Solution overview
Figure 1 shows the sample’s high-level architecture.
- An Amazon EventBridge rule, configured to run every day at 9:00 am (PDT), invokes an AWS Lambda function.
- The Lambda function checks the day’s NHL game schedule on Sportradar. If a game is scheduled, the Lambda function creates a second EventBridge rule.
- The second EventBridge rule invokes a Step Functions workflow (or “state machine”) to orchestrate polling for game status updates. The workflow begins in 5 minutes prior to the start of the game.
Note: As the second EventBridge rule is created dynamically and passes its IAM role to invoke Step Functions, we set an iam:PassRole
permission for first Lambda function.
- Step Functions invokes a second Lambda function to check the game status every minute during the game.
- The status-checking Lambda function retrieves game status updates from Sportradar. It updates the score in Step Functions and sends email notifications using an Amazon Simple Notification Service (Amazon SNS) topic. The Step Functions workflow ends when the Lambda function reports that the game is over.
Prerequisites
Before getting started, ensure that you have the following.
- An AWS account. If you don’t have one, sign up at https://aws.amazon.com.
- An AWS Identity and Access Management (IAM) user in your AWS account. For more information, refer to Creating an IAM user in your AWS account.
- AWS CDK and version 2 of the AWS CLI.
- Node.js and the Npm command line interface.
- A Sportradar US API Portal account. To sign up, refer to Register for an Account.
- A free trial Sportradar API key. For more information, refer to the email you receive from Sportradar after registering for an account. When registering an application, select
Issue a new key for NHL Trial
. A free trial key gives you enough API calls to test the sample application for 2-3 games. To increase your API rate limit, you can apply for a paid package with Sportradar.
Walkthrough
Deploy using AWS CDK
The sample application can be deployed using AWS CDK, an open-source development framework that lets developers define cloud resources using familiar programming languages. To deploy the sample, complete the following steps:
- Use your command-line shell to clone the GitHub repository.
git clone https://github.com/aws-samples/aws-step-functions-sports-data-polling
- Navigate to the repository’s root directory.
cd aws-step-functions-sports-data-polling
- Run the following
npm
commands.
npm install
npm audit
- Run the following
cdk
command to bootstrap your AWS environment. Thecdk
command is the primary tool for interacting with an AWS CDK application.
cdk bootstrap
Bootstrapping launches resources into your AWS environment that are required by AWS CDK. These include an S3 bucket for storing files and AWS Identity and Access Management (IAM) roles that grant permissions needed to deploy the sample.
- Deploy the AWS CDK application. In the command line, replace
<your-email-address>
with the address you want to receive game notifications.
cdk deploy --parameters teamId="sr:team:794340" --parameters emailAddress="<your-email-address>"
In the command, teamId
is the code for the Seattle Kraken, but you can substitute a different team. Find team IDs in teams.json
, located in the root directory of the sample’s GitHub repository.
Note: The email address you use to subscribe to notifications doesn’t have to be the same one you use to create a Sportradar account. After deployment, you’ll receive an email message to confirm the subscription. You must confirm the subscription to receive notifications.
Store the Sportradar API key in AWS Systems Manager Parameter Store
The sample application stores the Sportradar API key in Parameter Store, a capability of AWS Systems Manager. Parameter Store provides secure, hierarchical storage for application configuration data, such as passwords, database strings, and API keys. In this section you’ll store the API key you receive from Sportradar in Parameter Store. You can do this using the AWS CLI or the AWS Management Console.
Use the AWS CLI
To store your API key using the AWS CLI, run the following command. Replace <your-Sportradar-API-key>
with your information.
aws ssm put-parameter \
--name "SportradarApiKey" \
--type "String" \
--value "<your-Sportradar-API-key>" \
--overwrite
Use the AWS Management Console
To store your API key using the Management Console, use the following instructions.
- Sign in to the AWS Management Console as the IAM user created in “Prerequisites”. Then open the Systems Manager console.
- In the navigation pane under Application Management, choose Parameter Store.
- On the My parameters page, choose
SportradarApiKey
. - On the SportradarApiKey page, choose Edit.
- For Value, replace
update-this
with the API key you receive from Sportradar. - Choose Save changes.
Monitor the EventBridge rules
You can monitor the deployed EventBridge rules in action in the EventBridge console.
- Sign in to the AWS Management Console and open the EventBridge console.
- In the navigation pane, choose Rules.
- On the Rules page, choose
GameDayGameStartRule
. - Choose the Monitoring tab. This tab shows the Amazon CloudWatch metrics for the rule. The first rule in our method runs at 9:00am PDT. Note that there may be a delay of 3-5 minutes between the start of a rule and the recording of its first CloudWatch metrics.
Observe the Step Functions workflow
During a game, you can observe the data-polling workflow in the Step Functions console.
- Sign in to the AWS Management Console and open the Step Functions console.
- In the navigation pane, choose State machines.
- Choose
SportsDataStateMachine
.
Check for game notifications
During a game, check the email address used to subscribe to notifications. Figure 2 shows an example of a game score notification.
Cleanup
When you’re finished testing the deployment, run the following command to delete the stack.
cdk destroy
Alternatively, to delete the stack using the console, see Deleting a stack on the AWS CloudFormation console.
Note: Keeping AWS CLI installed does not incur future costs. For more information, see Installing, updating, and uninstalling the AWS CLI version 2.
Conclusion
This post showed you how to implement an automated sports-data polling system. It uses EventBridge rules, Lambda functions, and a Step Functions state machine to send email notifications of game score updates. The architecture is based on the successful collaboration between AWS and PacSci to build a responsive lighting system that reacts when the Seattle Kraken hockey team scores a goal. We invite you to change the deployment to receive notifications for your favorite team. For more information about building apps on serverless architecture, see Serverless Land.
To submit feedback for this post, use the Comments section.