DEV Community

Cover image for Creating a Subscriber Service for NBA Games using AWS technologies!
Evan
Evan

Posted on

Creating a Subscriber Service for NBA Games using AWS technologies!

Hey there πŸ‘‹πŸΌ thanks for stopping by! Today, I will be teaching you how to create a subscriber service in AWS to pull real-time NBA game data and send it via email or SMS. Stick around to learn more on AWS, APIs and more!

Purpose

The purpose of this article is to walk you through how to leverage AWS to create a subscriber service that utilizes an API and allows users to subscribe for NBA game information. The technologies that will be used in this article include: GitHub, AWS (Lamba, EventBridge, SNS) and Python.

Resources

Discord
YouTube Video from Ifeanyi
My GitHub Repo

Prerequisites

VS Code (or your favorite text editor)
AWS Free Tier Account
GitHub Account
Knowledge of Git, Linux commands, APIs and AWS

Architecture

AWS Architecture

Setup

1. Clone the repo

```
git clone https://github.com/asciikeyboard/gd-notifications.git
cd gd-notifications
```
Enter fullscreen mode Exit fullscreen mode

2. Create AWS SNS Topic and Subscriber

  • Login to AWS console
  • Search for SNS
  • Select Topics in left-hand menu
    • Select Create topic
    • Type = Standard
    • Name = gd-topic
  • Select Create subscription
    • Protocol = email
    • Endpoint = email address

It should look something like this :]
Subscription in AWS console

  • Login to your email
    • Confirm subscription

It should look something like this :]
Subscription confirmation email

3. Create IAM Policy for SNS Topic

  • Open IAM in AWS console
  • Select Policies in left-hand menu
  • Select Create policy
    • Service = SNS
    • Change Policy editor to JSON format
    • Navigate to Git repo and copy code from gd_sns_policy.json file within the policies folder
    • Paste this code into the Policy editor in the AWS console
    • Update the ARN on line 7 of the code with your gd-topic ARN
    • Select Next
  • Set the Policy Name
    • Policy name = gd_sns_policy
    • Select Create Policy

4. Create the IAM Role SNS Topic

  • Navigate to IAM page
  • Select Roles
  • Select Create role
    • Trusted entity type = AWS service
    • Use case = Lambda
  • Select Next
  • Search for "gd_sns_policy" and select the checkbox
  • Search for "AWSLambdaBasicExecutionRole" and select the checkbox
  • Role name = gd_lamba_role
  • Select Create role

5. Create the Lambda function

  • Search for Lambda in the AWS console
  • Select Create function
    • Select Author from scratch
      • Function name = gd_notifications
      • Runtime = Python 3.13
    • Select the Change default execution role dropdown and change Execution role to Use an existing role
      • Existing role = gd_lambda_role
    • Select Create function
  • Copy code from "gd_notifications.py" file in Git and paste into "lambda_function.py" within AWS console
  • Select Deploy from left-hand menu
  • Change from Code to Configuration menu and add the following Environment Variables:
    • NBA_API_KEY = your API key
    • SNS_TOPIC_ARN = your SNS topic ARN
  • Select Save

6. Understanding the lambda_function python script
For a detailed breakdown please reference the YouTube video from Ifeanyi

Here's an overview of what the script does: 
1. Retrieves environment variables.
2. Adjusts the current time to Central Time (UTC-6).
3. Fetches NBA game data from the external API.
4. Formats the game data.
5. Publishes the formatted data to the specified SNS topic.
Enter fullscreen mode Exit fullscreen mode

7. Leverage EventBridge to run a scheduled lambda function

  • Search for EventBridge in the AWS console
  • Select Create rule
    • Name = gd_rule
    • Rule type = schedule
  • Select Continue in EventBridge scheduler
  • Select Recurring schedule under the Schedule Pattern option
  • Input Cron expression variables
    • Minutes = 0
    • Hours = 9-23/2,0-2/2
    • Day of the month = *
    • Month = *
    • Day of the week = ?
    • Year = *
  • Set Flexible time window off
  • Select Next
  • Target detail = AWS Lambda Invoke
  • Lambda functions = gd_notifications
  • Select Next
  • Select Next
  • Select Create schedule

Review

Now you have successfully created a scheduled SNS Topic to retrieve NBA game data and send that to a subscriber. Pretty neat!

Top comments (0)