DEV Community

Cover image for Build a Weather Info API Gateway with AWS Lambda & Open Weather Map
Busa Ayim-Odu
Busa Ayim-Odu

Posted on

Build a Weather Info API Gateway with AWS Lambda & Open Weather Map

Overview:
Create a simple API that returns current weather information for a given city using AWS Lambda, API Gateway, and OpenWeatherMap API.

An API (Application Programming Interface) is a set of rules that allows two software applications to communicate with each other.

Tools & Skills Involved:
AWS Lambda (Python runtime)

Amazon API Gateway

IAM Roles & Permissions

OpenWeatherMap API (free tier)

Python (requests library)

Postman or browser for testing

Powershell

Step 1: Sign Up for OpenWeatherMap
Go to https://openweathermap.org/api
API

Sign up and get your free API key.
ap2

Step 2: Create Your Lambda Function
Go to AWS Console > Lambda > Create Function.
Api3

Choose: “Author from scratch”

Runtime: Python 3.x

Name: weather-api-lambda

Click create

Step 3: Add Your Lambda Code
code:
API4

Step 4: Add Environment Variable
Go to your AWS Lambda function.

Scroll down to Environment variables.

Click Edit, then Add environment variable:

Key: WEATHER_API_KEY

Value: (Your API key from OpenWeatherMap)
API5

Step 5: Add Permissions for Internet Access
Attach an execution role with AWSLambdaBasicExecutionRole

Ensure Lambda is in a VPC that allows internet access (or no VPC for easier setup).
Check if Your Lambda Is in a VPC
Go to the Lambda Console

Open your function

Scroll to the "Configuration" > "VPC" section

If it says "Not attached to any VPC", your Lambda already has internet access
API6

Step 6: Create API Gateway
Api7
Go to API Gateway > Create API > HTTP API > Click Build
API8

Choose integration → Select Lambda

Select your Lambda function from the dropdown

Click Next
API9

Method: GET

Resource path: /weather
API10

Create a stage name — e.g., APIweather

Click Next, then Create
API 11

Step 7: Deploy & Test
Deploy the API

Test your endpoint with:
https://olxheqwg4c.execute-api.us-west-2.amazonaws.com/
api12
I tested the endpoint URl and got this.
api13
That is my base endpoint, but it doesn't tell us which route or resource path is configured. That’s why you’re getting:
{"message":"Not Found"}
Lets fix this error.

Check Your Lambda Integration Path:
If it's an HTTP API:
Click Routes
You’ll see something like: ANY /weather or GET /
Note the full path (e.g., /weather)
api15
I found the error and the correct path which includes a stage name, but it's not working without it, here's exactly how to fix and use the full correct URL:
Breakdown of Your Components
Base endpoint: https://olxheqwg4c.execute-api.us-west-2.amazonaws.com

Stage name: APIweather

Route/Path: /weather-api-lambda

Query string: ?city=Lagos

This is the correct URL PATH:
https://olxheqwg4c.execute-api.us-west-2.amazonaws.com/APIweather/weather-api-lambda?city=Lagos
API16
My API Gateway is correctly connected to your Lambda function,
But my Lambda function isn't running the weather fetching logic yet — it's still using the default "Hello from Lambda!" response.

I need to replace the "Hello from Lambda!" code in your function with the weather-fetching logic.

Open PowerShell
Press Windows + R, type powershell, and hit Enter.

Create the project folder on your local computer/desktop
mkdir lambda-weather
cd lambda-weather
Api17
Create the Python file
In the same terminal: notepad lambda_function.py
api18
Here is the lambda code scripted inside
api19
Save and close the notepad.
Install the requests package locally
In the same PowerShell window: pip install requests -t .
api
Zip everything
Still inside the lambda-weather folder: Compress-Archive -Path * -DestinationPath lambda-weather.zip
api20

Upload to Lambda
Go to AWS Lambda console.

Choose your function.

Click Upload from → .zip file.

Select lambda-weather.zip.
API21
API22
Click Deploy.

Test the Function Directly (Optional)
I ran a test before setting up API Gateway:

  • Click the Test tab in Lambda API23
  • If it works, you’ll see a 200 OK response with weather data.

Test the API via Browser:
https://olxheqwg4c.execute-api.us-west-2.amazonaws.com/APIweather/weather-api-lambda
API24
There you go

Brief Discussion

What problem did I solved getting weather info: I built a serverless solution to fetch real-time weather data using AWS Lambda and API Gateway. The problem was accessing weather info by city without managing servers. Now, users can get live forecasts by simply hitting a public API endpoint.

How API Gateway & Lambda work together: API Gateway acts as the front door, receiving HTTP requests and passing them to AWS Lambda. Lambda runs backend code in response, processes the request, and sends back a result. Together, they enable scalable, serverless APIs without managing servers.

How this demonstrates serverless and cloud-native skills: This project shows serverless and cloud-native skills by using AWS Lambda to run code without managing servers and API Gateway to expose it as a RESTful API. It demonstrates event-driven design, scalability, cost-efficiency, and integration of cloud services.

This project demonstrates the power of serverless architecture using AWS Lambda and API Gateway to build a scalable weather information API. It showcases my ability to integrate third-party APIs, handle cloud deployments, and write clean, resilient code.

Top comments (0)