DEV Community

Building a Simple Weather App With AWS Lambda and API Gateway

This is my second open-source project built using ChatGPT as my code review assistant and for a lot of the debugging, and troubleshooting.
Behind this simple app, the heavy lifting is done using AWS Lambda, API Gateway, and OpenWeatherMap.

Problem Statement
I wanted to see build a really simple serverless weather app to check if it’s raining, snowing, cloudy, etc. I wanted to call a protected public API, run a lambda function, and get a simple short result with the weather conditions within my local zip code.

Solution Overview
As you may be aware AWS Lambda is a serverless computing service that allows you to run code without provisioning or managing servers, storage, etc. The AWS API Gateway is a fully managed service for creating, deploying, and managing APIs. By leveraging AWS Lambda and API Gateway, you can benefit from the serverless architecture, which eliminates the need to manage infrastructure and enables auto-scaling and high availability. This approach allows me to focus on the app’s logic, and its features, without worrying about server setup, maintenance, or scalability.

I will leverage the OpenWeatherMap API to retrieve weather data for a given location. To achieve this, I will use Node.js and Express.js to develop the app’s logic. As I mentioned above, the core of the app will be done as an AWS Lambda function, which will be triggered when an API request is made. The AWS API Gateway acts as the front end for my simple app, providing an API endpoint that users can access.

Setting Up the Development Environment
Choose a code editor that you’re comfortable with. Some popular options include Visual Studio Code, Atom, or Sublime Text. Install the code editor of your choice and open the project directory in it. I tend to use either Sublime or Visual Studio (VS) Code, VS Code is nice as you can easily commit, and push code to GitHub once you are ready.

Deploying to AWS Lambda and API Gateway
Since there are no servers, there is nothing to install or update, we just need to set up and configure a few things with AWS Lambda, and AWS API Gateway. I won't cover creating a deployment package of the node.js app, and all other necessary files, you can simply clone the repository I have out on GitHub You can simply upload the Zip file to Lambda when you edit the code and enter your API key and specific location information for OpenWeatherMap.

Create an AWS Lambda Function:
— Go to the AWS Management Console and navigate to the AWS Lambda service.
— Click on “Create function” and choose “Author from scratch”.
— Provide a name for your function, select “Node.js” as the runtime, don't worry about the version, and choose an appropriate execution role. As a note, I did use node.js 18.x, and set the handler as index.handler.
— In the “Function code” section, select “Upload a .zip file” and upload the ZIP file you downloaded from above.
— Set the handler to the appropriate value, such as app.handler or index.handler.
— Configure the necessary environment variables, such as the OpenWeatherMap API key, and any other required configuration values such as your Zip code and Country for example.
— Save the Lambda function.

Set Up an API Gateway:
— Go to the AWS Management Console and navigate to the API Gateway service.
— Click on “Create API” and choose “REST API”.
— Configure your API by specifying a name and selecting the appropriate security settings.
— Create a new resource, e.g., “/weather” or whatever makes sense for you.
— Add a method to the resource, such as “GET”.
— Configure the integration type as “Lambda Function” and select the Lambda function you created above. If you can remember, just head back to your lambda function, and under the function overview, you will find a line for function ARN which you can copy and paste into the Lambda function field.
— Save the API configuration.

Deploying the API:
— In the API Gateway console, select the API you created.
— Go to the “Actions” dropdown and choose “Deploy API”.
— Choose a stage name (e.g., “prod”) and deploy the API.
— Note the generated API endpoint URL which will be up toward the top of the screen.

Test the Deployed App:
— Use the generated API endpoint URL to test your deployed app by making an HTTP GET request to the appropriate resource path, such as https://your-api-endpoint/prod. From a terminal, you can also just use a simple cURL https://your-api-endpoint/prod
— Verify that the response indicates whether it’s raining, cloudy, snowing, or not.

Final Thoughts and Wrap up

Remember to review and configure the necessary security settings, such as authentication and authorization, based on your specific requirements.

I did decide to use a simple WAF Web ACL to restrict what or rather who could access from API, and from a specific set of IP Sets I specified. The AWS API gateway also allows for custom domains to help simplify the API URL instead of the default which is random and follows, your specific URL may look a little different depending on the AWS region you are using for your deployments.

Top comments (1)

haxeeb_eth1 profile image
Muhammad haseeb iqbal

I want to develop a tool for my site.