Building an NBA Game Day Notification.

Olugbenga AjayiOlugbenga Ajayi
4 min read

Introduction

This project is for Day 2 of the #DevOpsAllStarsChallenge, and I am excited about it. This project connects to the sports data NBA API to get the available game for the day and sends the notification to the email. It can also be used with other sports API of choice (Football, Baseball, etc).

Architecture Diagram

Prerequisites

AWS Simple Notification Service (SNS) - This service sends notifications to the email and/or phone configured.

AWS Lambda - This executes the code without provisioning or managing servers (serverless).

AWS EventBridge - This is used to trigger scheduled notifications.

Python - This is used for scripting and working with the API

SportData - External API

Project Setup

  1. Clone the repository

git clone https://github.com/ifeanyiro9/game-day-notifications
  1. Create Simple Notification Service (SNS)

In the AWS Dashboard, type SNS --> click it, create a topic, and add the name of the topic.

Then click the Create Topic.

Afterward, create the subscription. You can subscribe with your email, phone, or both. Other options can also be configured.

After this, a notification is sent to your email for confirmation.

Once the email is confirmed, the status is changed in your SNS Dashboard.

  1. Create SNS Publish Policy

Go to the IAM dashboard, and click on the policy to create it.

Select the service that will use the policy, this is SNS.

Click on the JSON tab and clear the JSON command in the policy editor.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "sns:Publish",
            "Resource": "arn:aws:sns:REGION:ACCOUNT_ID:gd_topic"
        }
    ]
}

Replace the Region and Account_ID with the one particular to your AWS account as shown below.

Then click Next, → Review the policy to be sure and create the policy.

  1. Create an IAM role for the Lambda function.

IAM roles give temporary permission to the lambda function to be able to publish to the SNS topic.

Select AWS Service → the service that will use the role (Lambda)

Check both the previous policy (game_day_policy) and AWSLambdaBasicExecution Policy and click next.

Provide the name of the role, review the policies, and create the role.

  1. Create the Lambda Function

Go to the AWS Lambda dashboard, create the Lambda function, and select Python3.x because the code

Select the Code tab, empty the hello-world code, and paste in the Python code into the box.

Add environment variables under the configuration settings - both NBA_API_KEY and the SNS_TOPIC_ARN

Add the Python code from the cloned git account above to the lambda_function.py and click Deploy. Once it is deployed, click on the test Tab to test the function.

  1. Error encountered

When I clicked the function, I got an error shown below:

This error occurred because the default time to run the lambda function is 3s and the function cannot be completed then, hence the error. I edited the time for the function by clicking on the configuration tab → general configuration and edited the time to 10s.

Redo the test and it is successful.

Setup Automation using EventBridge.

The automation for the notification is created using Amazon Eventbridge which can be triggered depending on what is configured in the cron expression

Go to the EventBridge dashboard, click on Create Rule, provide a name for the rule, and select the schedule to provide the interval for the notification to be triggered.

Select a recurring schedule to set the time for the automated notification and turn off the flexible time window. Also, remember to create a cron job for how frequently the notification should be triggered. I set mine to every minute.

Select the target API (AWS Lambda) and the Lambda function (game_day_notification) created previously above.

The automated triggers are shown below:

Thank you for reading. Please follow, like, comment, and share for a wider reach and more DevOps-related articles.

To access the code you can visit my GitHub account: https//github.com/gbejula/30DaysDevOpsChallenge.git

1
Subscribe to my newsletter

Read articles from Olugbenga Ajayi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Olugbenga Ajayi
Olugbenga Ajayi

I am a DevOps engineer committed to life-long learning and continuous growth.