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.

Architecture for automating sports data polling with AWS Step Functions

Figure 1. Architecture for automating sports data polling with AWS Step Functions.

  • 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.

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:

  1. Use your command-line shell to clone the GitHub repository.

git clone https://github.com/aws-samples/aws-step-functions-sports-data-polling

  1. Navigate to the repository’s root directory.

cd aws-step-functions-sports-data-polling

  1. Run the following npm commands.

npm install
npm audit

  1. Run the following cdk command to bootstrap your AWS environment. The cdk 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.

  1. 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.

  1. Sign into the AWS Management Console as the IAM user created in “Prerequisites”. Then open the Systems Manager console.
  2. In the navigation pane under Application Management, choose Parameter Store.
  3. On the My parameters page, choose SportradarApiKey.
  4. On the SportradarApiKey page, choose Edit.
  5. For Value, replace update-this with the API key you receive from Sportradar.
  6. Choose Save changes.

Monitor the EventBridge rules

You can monitor the deployed EventBridge rules in action in the EventBridge console.

  1. Sign into the AWS Management Console and open the EventBridge console.
  2. In the navigation pane, choose Rules.
  3. On the Rules page, choose GameDayGameStartRule.
  4. 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.

  1. Sign into the AWS Management Console and open the Step Functions console.
  2. In the navigation pane, choose State machines.
  3. 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.

Example AWS notification message

Figure 2. Example AWS notification message

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.

About the authors

Simsek Mert

Simsek Mert is a cloud application architect with AWS Professional Services. Simsek helps customers with their application architecture, containers, and serverless applications, leveraging his over 20 years of experience.

Kishore Dhamodaran

Kishore Dhamodaran is a senior solutions architect at AWS. Kishore helps strategic customers with their cloud enterprise strategy and migration journey, leveraging his years of industry and cloud experience.