Navigate to the S3 Console .
Click Create bucket .
Name the bucket (e.g., myweathertests3).
Select a region (e.g., ap-southeast-2).
Enable versioning (optional, but recommended).
Click Create bucket .
2. Create IAM Role for Lambda
Navigate to the IAM Console .
Click Roles > Create role .
Choose AWS service > Lambda .
Attach the following policies:
AWSLambdaBasicExecutionRole
A custom policy granting S3 permissions:
{
"Version" : " 2012-10-17" ,
"Statement" : [
{
"Effect" : " Allow" ,
"Action" : [
" s3:PutObject" ,
" s3:GetObject" ,
" s3:ListBucket"
],
"Resource" : [
" arn:aws:s3:::my-weather-data-bucket" ,
" arn:aws:s3:::my-weather-data-bucket/*"
]
}
]
}
Name the role (e.g., LambdaWeatherRole) and save.
3. Create Lambda Functions
Navigate to the Lambda Console .
Click Create function > Author from scratch .
Name: getCurrentWeather
Runtime: Node.js 20.x
Role: Use the role created earlier (LambdaWeatherRole).
Upload the code:
Package the getCurrentWeather.js, getHistoricalWeather.js, and index.mjs into a ZIP file along with a package.json file.
Use the AWS Lambda editor to specify index.mjs as the handler.
Example handler: index.handler.
Navigate to the API Gateway Console .
Click Create API > HTTP API .
Configure routes:
Route 1 : GET /weather/{city}
Integration: Link to the getCurrentWeather Lambda function.
Route 2 : GET /weather/history/{city}
Integration: Link to the getHistoricalWeather Lambda function.
Deploy the API and note the API endpoint .
5. Configure Environment Variables
In the Lambda Console , for each function:
Go to the Configuration tab > Environment variables .
Add:
API_KEY: The OpenWeatherMap API key.
BUCKET_NAME: The name of the S3 bucket.
Example for Current Weather
curl https://< api-id> .execute-api.< region> .amazonaws.com/weather/Melbourne
curl https://< api-id> .execute-api.< region> .amazonaws.com/weather/history/Melbourne