30 Days DevOps Challenge - NFL Schedule API
#Week2-Day1 #DevOpsAllStarsChallenge
NFL Schedule API Project
Introduction
Welcome to Week 2 of the 30 Days DevOps Challenge! Today, we're diving into an exciting project where you'll learn how to create an API to fetch the NFL schedule using SerpAPI and Flask. We'll also containerize the application using Docker and deploy it on Azure Container Apps with API Management. Let's get started!
https://www.youtube.com/watch?v=09WfkKc0x_Q
This is link where I based my azure project, it was originally on AWS but since I only have Azure and took up the challenge to convert the resources used in AWS to Azure.
Special Thanks to Ifeanyi Otuonye for providing a very detailed guide and explanation.
Prerequisites
Before we begin, make sure you have the following tools and accounts ready:
Python / Flask
Azure Account
Azure CLI
VS Code (Optional)
Docker CLI / Desktop
Installation
Clone the Repository: First, clone the project repository to your local machine:
git clone https://github.com/annoyedalien/week2-day1.git cd week2-day1
Create a
.env
File: Next, create a.env
file and add your SerpAPI key:SPORTS_API_KEY=your_serpapi_key
Docker
Verify the Dockerfile: Ensure the Dockerfile details are correct.
Build the Docker Image: Build the Docker image with the following command:
docker build -t flask-app .
Update the Variables in resource.sh
Open the resource.sh
script and update the variables with your specific details:
vim resource.sh
# Variables
RESOURCE_GROUP="Resource Group Name"
CONTAINER_REGISTRY="Container Registry Name"
CONTAINER_APP="Container App Name"
ENVIRONMENT="Environment Name"
LOCATION="Region"
IMAGE_NAME="Docker Image Name"
APIM_NAME="API Management Service Name"
SKU="Consumption/Standard/Premium"
COMPANY_NAME="Your Company Name"
ADMIN_EMAIL="Your Email"
Save and exit the vim editor.
Run the resource.sh
Script
Execute the script to create the necessary Azure resources:
Resource Group
Azure Container Registry
Push the Docker image from local to Azure Container Registry
Environment workspace for Container Apps
Azure Container Apps + Replicas of the container + Scaling Metrics
API Management Service
API for the Container App
API Operation GET
Script breakdown
Resource Group:
A Resource Group in Azure is a container that holds related resources for an Azure solution. It helps manage and organize resources easily.
The script will create a Resource Group using the Azure CLI command:
az group create --name $RESOURCE_GROUP --location $LOCATION
Azure Container Registry:
Azure Container Registry (ACR) is a managed Docker registry service based on the open-source Docker Registry 2.0.
The script will create an ACR using the command:
az acr create --resource-group $RESOURCE_GROUP --name $CONTAINER_REGISTRY --sku Basic
Push the Docker Image from Local to Azure Container Registry:
After building the Docker image locally, you need to push it to the ACR.
First, log in to the ACR:
az acr login --name $CONTAINER_REGISTRY
Then, tag the Docker image:
docker tag flask-app $CONTAINER_REGISTRY.azurecr.io/$IMAGE_NAME
Finally, push the image to the ACR:
docker push $CONTAINER_REGISTRY.azurecr.io/$IMAGE_NAME
Environment Workspace for Container Apps:
An environment workspace is required for deploying Container Apps.
The script will create an environment workspace using:
az containerapp env create --name $ENVIRONMENT --resource-group $RESOURCE_GROUP --location $LOCATION
Azure Container Apps + Replicas of the Container + Scaling Metrics:
Azure Container Apps allow you to run microservices and containerized applications on a serverless platform.
The script will create a Container App and configure replicas and scaling metrics:
az containerapp create --name $CONTAINER_APP --resource-group $RESOURCE_GROUP --environment $ENVIRONMENT --image $CONTAINER_REGISTRY.azurecr.io/$IMAGE_NAME --cpu 0.5 --memory 1.0Gi --min-replicas 1 --max-replicas 5 --scale-rule-name http --scale-rule-type http
API Management Service:
Azure API Management (APIM) helps you create and manage modern API gateways for existing backend services.
The script will create an APIM instance:
az apim create --name $APIM_NAME --resource-group $RESOURCE_GROUP --location $LOCATION --publisher-email $ADMIN_EMAIL --publisher-name $COMPANY_NAME --sku-name $SKU
API for the Container App:
Once the APIM instance is created, you need to create an API that points to your Container App.
The script will create an API:
az apim api create --service-name $APIM_NAME --resource-group $RESOURCE_GROUP --api-id nfl-schedule-api --path /day4containerapp --display-name "NFL Schedule API" --service-url "https://$CONTAINER_APP.$LOCATION.azurecontainerapps.io"
API Operation GET:
Define the operations for your API, such as GET requests to fetch the NFL schedule.
The script will create an API operation:
az apim api operation create --service-name $APIM_NAME --resource-group $RESOURCE_GROUP --api-id nfl-schedule-api --url-template /sports --method GET --display-name "Get NFL Schedule" --description "Fetches the NFL schedule"
Test
Finally, copy the URL link from the API Management service:
https://[api_management_service_name].azure-api.net/day4containerapp/sports
And there you have it! You've successfully set up an NFL Schedule API using Flask, Docker, and Azure. Keep up the great work, and stay tuned for more exciting projects in the 30 Days DevOps Challenge! ๐
Feel free to share your progress and any questions you have in the comments below. Happy coding! ๐