/ SERVERLESS

Building Knative Serverless function to detect weather using OpenWeatherMap and Python

Building Knative Serverless Python Function

Knative is a Kubernetes-based platform designed to deploy, run, and manage serverless workloads. This article will walk you through creating a Knative serverless function using Python to detect the current weather using the OpenWeatherMap API.


Table of Contents:


Prerequisites

  1. A Kubernetes cluster with Knative Serving installed
  2. kubectl CLI installed and configured to interact with your cluster
  3. Python 3 installed
  4. An OpenWeatherMap API key

Creating the Function

Create a new directory for your function:

mkdir weather-function && cd weather-function

Create a requirements.txt file inside the weather-function directory to include the requests library:

requests

Create a main.py file inside the weather-function directory with the following code:

import os
import json
import requests
from flask import Flask, request, jsonify

app = Flask(__name__)
OPENWEATHERMAP_API_KEY = os.getenv("OPENWEATHERMAP_API_KEY")

@app.route('/', methods=['POST'])
def weather():
    try:
        data = request.json
        location = data['query']['location']
    except (KeyError, ValueError):
        return jsonify({"error": "Invalid request payload"})

    url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={OPENWEATHERMAP_API_KEY}&units=metric"
    response = requests.get(url)
    if response.status_code != 200:
        return jsonify({"error": "Error fetching weather data"})

    weather_data = response.json()
    weather = weather_data['weather'][0]['description']
    temperature = weather_data['main']['temp']

    return jsonify({
        "location": location,
        "weather": weather,
        "temperature": temperature
    })

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))

This function uses the requests library to fetch the current weather data from the OpenWeatherMap API based on the provided location.

Create a Dockerfile inside the weather-function directory with the following content:

FROM python:3.9-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY main.py .

ENV PORT 8080
CMD ["python", "main.py"]

This Dockerfile sets up a Python environment, installs the required libraries, and runs the Flask application.

Deploying the Function

Build the function Docker image:

docker build -t your-docker-username/weather-function .

Push the function Docker image to a Docker registry:

docker push your-docker-username/weather-function

Create a weather-function.yaml file with the following content:

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: weather-function
spec:
  template:
    spec:
      containers:
        - image: your-docker-username/weather-function
          env:
            - name: OPENWEATHERMAP_API_KEY
              value: YOUR_API_KEY

Replace your-docker-username with your actual Docker username and YOUR_API_KEY with your existing OpenWeatherMap API key.

Deploy the function to your Knative environment:

kubectl apply -f weather-function.yaml

Testing the Function

After deploying your function, you can test it by sending an HTTP request to its endpoint.

Get the domain name for your function:

kubectl get ksvc weather-function -o jsonpath='{.status.url}'

This command will return the domain name of your function.

Send a POST request to the function using curl:

curl -X POST -H "Content-Type: application/json" -d '{"query": {"location": "San Francisco"}}' http://weather-function.default.example.com

Replace http://weather-function.default.example.com with the domain name you obtained in the previous step.

You should receive a JSON response containing the weather information:

{
  "location": "San Francisco",
  "weather": "clear sky",
  "temperature": 20.8
}

Conclusion

In this article, we demonstrated how to create and deploy a Knative serverless function using Python to detect the current weather using the OpenWeatherMap API. Knative enables you to build serverless applications on Kubernetes, providing a powerful platform for deploying and scaling your functions as needed.

Keep Learning: More on Serverless

Discover the world of serverless architecture in our comprehensive collection of articles! Equip yourself with valuable knowledge and skills in the exciting realm of serverless computing by exploring the following topics:

Don’t just stop there; continue your journey and delve even further into the fascinating and expansive world of serverless technologies and their endless possibilities.

faizan

Faizan Bashir

Principal Engineer | Architecting and building distributed applications in the Cloud | Adventurer

Read More