NBA Game Notifications 🏀 with Azure Event Grid & Azure Functions
Welcome to Day 2 of the 30-Day DevOps Challenge!
For today’s project, I’m building a notification system that sends NBA game notifications using Azure Event Grid and Azure Functions. This architecture demonstrates how to build a scalable, event-driven solution using Azure Functions for fetching NBA game data, Event Grid for event routing, and Logic Apps for sending real-time notifications. Let’s dive into the details!
Project Overview
In this project, I’ve built a solution that:
Fetches NBA game results via an API.
Publishes the results as events to Azure Event Grid.
Uses Azure Logic Apps to send email notifications when new NBA game results are available.
This setup illustrates a typical event-driven architecture, providing a solid foundation for real-time notifications and automated workflows in modern DevOps.
Prerequisites
Before getting started, make sure you have the following tools and resources:
Environment Setup:
An Azure account (to set up resources like Azure Functions, Event Grid, and Logic Apps).
Python 3.8 or later installed locally.
NBA API key from sportsdata.io (for fetching game data).
Required Python Libraries:
- Install dependencies using the following command:
pip install -r requirements.txt
Required libraries:
requests
azure-functions
azure-identity
azure-mgmt-eventgrid
Environment Variables: Create a
local.settings.json
file in your project directory with the following variables:# NBA API Key NBA_API_KEY=your_nba_api_key # Azure Event Grid Topic Endpoint EVENT_GRID_TOPIC_ENDPOINT=your_event_grid_topic_endpoint # Azure Event Grid Topic Key EVENT_GRID_TOPIC_KEY=your_event_grid_topic_key
How It Works
1. Fetch NBA Game Data
Using the NBA API, the function fetches real-time game data for a specified set of NBA games.
# Fetch data from the API
api_url = f"https://api.sportsdata.io/v3/nba/scores/json/GamesByDate/{today_date}?key={api_key}"
try:
with urllib.request.urlopen(api_url) as response:
data = json.loads(response.read().decode())
except Exception as e:
logging.info(f"Error fetching data from API: {e}")
2. Publish Game Data to Azure Event Grid
Once the game data is fetched, it’s published to Azure Event Grid as an event. This enables other services (like Logic Apps) to react to this event.
# Process games data
messages = [format_game_data(game) for game in data]
final_message = "<br>---<br>".join(messages) if messages else "No games available for today."
# Publish to Event Grid
try:
client = EventGridPublisherClient(event_grid_topic, AzureKeyCredential(event_grid_key))
event = {
"id": "1",
"subject": "NBA Game Notification",
"data": final_message,
"eventType": "GameResult",
"dataVersion": "1.0"
}
client.send(event)
logging.info("Notifications successfully published to Event Grid.")
except Exception as e:
logging.info(f"Error publishing to event grid: {e}")
3. Send Notifications Using Logic Apps
Once the event is published to Event Grid, Azure Logic Apps subscribes to this event and sends an email notification to users.
The Logic App is configured to send an email via SendGrid when a new NBA game result is published.
Azure Configuration Steps
Here’s how to set up the Azure services:
Create an Azure Function App:
Go to the Azure Portal and create a new Function App.
Set the runtime stack to Python.
Deploy your function using Azure CLI or GitHub Actions.
Set Up Azure Event Grid:
In the Azure Portal, create a new Event Grid Topic under the Event Grid section.
Copy the Endpoint and Key to use in your
local.settings.json
file.
Create Logic Apps:
Create a new Logic App in the Azure Portal.
Set up the trigger to listen for events from your Event Grid topic.
Configure the action to send email notifications (via SendGrid or any other email service).
Usage Guide
Clone the Repository:
git clone https://github.com/<your-username>/NBA-Game-Notifications.git cd NBA-Game-Notifications
Install Dependencies:
pip install -r requirements.txt
Configure Environment:
- Update the
.env
file with your NBA API key, Event Grid endpoint, and key.
- Update the
Run the Script Locally:
func start
This will fetch NBA game data, publish it to Event Grid, and trigger the Logic App to send an email notification.
Sample Output
You will receive an email notification similar to:
Key Takeaways
Event-Driven Architecture: Using Azure Event Grid and Azure Functions to create scalable, event-driven solutions.
Automation: Automating data fetching, event publishing, and notification sending reduces manual work and improves efficiency.
DevOps in Action: This project reinforces the DevOps principles of automation, cloud services, and continuous integration.
GitHub Repository
You can find the full code for this project on GitHub. Feel free to fork, experiment, and contribute!
GitHub Repository: NBA Game Notifications
What’s Next?
This project is just the beginning of my 30 Days DevOps Challenge. Stay tuned as I explore more DevOps practices like CI/CD pipelines, containerization with Docker.
Let’s connect and continue this DevOps journey together! 💻💡
#30DaysDevOpsChallenge #DevOpsAllStarsChallenge #Azure #Automation #CloudComputing