This project is a part of the 30 Days All Star Devops Challenge that aims to increase proficiency in Devops through a series of hands-on projects and documentation.
Project Description
This project makes use of Python and AWS Services (S3) to retrieve weather data from OpenWeatherAPI and stores the results in a json format in an S3 Bucket.
Tools Used:
- Python: Language used to set up the Weather Dashboard
- AWS S3 ( Simple Storage Service): AWS Storage Solution
- OpenWeather API Key
- Boto3 : AWS SDK used by python to interact with the S3 Bucket
- requests: Python library used to send requests and fetch data
Environment Setup
Before implementing the weather dashboard, here are a few things needed to set up your environment.
-
AWS Account and AWS CLI: You need an AWS Account where your bucket will be created and the AWS CLI to set up credentials to interact with AWS
-
Python: Make sure Python
3.x
is installed on your system as well as pip, It will be needed to install libraries -
OpenWeather API Key: Get the OpenWeather API Key by signing up to this page OpenWeather
Architecture Diagram
Project Workflow
This project uses a python script that fetches weather data from openweather via its API Key and then sends/stores it to a S3 bucket in JSON Format
Here are some excerpts of the code:
class WeatherDashboard:
def __init__(self):
self.api_key = os.getenv('OPENWEATHER_API_KEY') # Get the OpenWeather API key from environment variables
self.bucket_name = os.getenv('AWS_BUCKET_NAME') # Get the AWS S3 bucket name from environment variables
self.s3_client = boto3.client('s3') # Create an S3 client for interacting with AWS S3
This sets up the API key, bucket name, and S3 client when you create an instance of the WeatherDashboard class.
def fetch_weather(self, city):
base_url = "http://api.openweathermap.org/data/2.5/weather" # The API endpoint for weather data
params = {
"q": city, # The city name to fetch weather for
"appid": self.api_key, # The API key for authentication
"units": "imperial" # Use imperial units (Fahrenheit)
}
try:
response = requests.get(base_url, params=params) # Make a GET request to the API
response.raise_for_status() # Raise an error if the response indicates failure
return response.json() # Return the weather data as JSON
except requests.exceptions.RequestException as e:
print(f"Error fetching weather data: {e}") # Print error if the request fails
return None
This section gets weather information like temperature and conditions for a given city
def save_to_s3(self, weather_data, city):
if not weather_data: # If no data is passed, return False (skip saving)
return False
timestamp = datetime.now().strftime('%Y%m%d-%H%M%S') # Create a timestamp (e.g., 20250107-153045)
file_name = f"weather-data/{city}-{timestamp}.json" # Define the file path and name
try:
weather_data['timestamp'] = timestamp # Add a timestamp to the weather data
self.s3_client.put_object( # Save the data to S3
Bucket=self.bucket_name, # The S3 bucket name
Key=file_name, # The file path in the bucket
Body=json.dumps(weather_data), # Convert data to JSON format for saving
ContentType='application/json' # Specify the file type as JSON
)
print(f"Successfully saved data for {city} to S3") # Confirm success
return True
except Exception as e:
print(f"Error saving to S3: {e}") # Print error if saving fails
return False
This section saves it in s3 bucket
To run the project on your machine, follow these steps:
-
Clone the repository from here: Github Repo
-
Follow the instructions in the README here: README.md
When the project has been run, you should get the following output
Conclusion
This project successfully demonstrates how to build a simple yet functional weather dashboard application by integrating Python, OpenWeatherAPI, and AWS S3. Through this project, we explored retrieving real-time weather data using APIs, processing it in Python, and securely storing the results in an S3 bucket in JSON format. It highlights practical use cases of Python libraries like boto3 and requests, emphasizing the importance of cloud storage solutions in managing and persisting data. This project is an excellent starting point for developers looking to enhance their skills in API integration, cloud computing, and DevOps practices.
Source link
lol