In this article, I am going to show you how to build a serverless news summarizer using Lambda function and rest API and host it using API Gateway. I will use AWS SAM to build, invoke locally and then deploy using IaC (infrastructure as code) approach.
Lambda function will fetch the desired news content using a public news API and will be integrated via rest API to provide the response to the consuming app.
I plan to build this solution in multiple part where I will incrementally add features to this solution.
- Part 1: Building a Simple News Summarizer with AWS Lambda and API Gateway
- Part 2: Automating News Alerts with AWS Lambda, SNS, and EventBridge
- Part 3: Adding Generative AI Summarization with Amazon Bedrock to Your News
- Part 4: Build a Serverless Generative AI News Summarizer API with AWS Lambda and Bedrock
- Part 5: Serverless HTML News Delivery Using Bedrock, EventBridge, Lambda, and SNS
Before diving in, let’s review the architecture diagram to understand how these components interact.
Architecture
Prerequisites
- Familiarity with the AWS Management Console.
- Knowledge of Python/Boto3
- Basic understanding of AWS SAM
- Basic understanding of Lambda, and API Gateway.
- Basic familiarity with event driven design and architecture
- Sign up at https://newsapi.org and get the API Key
What Are We Building and Why?
We are building a serverless news summarizer that fetches the latest technology news from a public news API and exposes it through a REST API. This API can be invoked using tools like Postman or integrated into an application that offers users a personalized tech news experience.
Why are we building this solution?
Because staying up to date with the latest tech news is exciting, but manually checking blogs, RSS feeds, and recap sites can be time-consuming. What if you could automate that process using AWS Serverless and AI tools?
By automating this workflow, you can retrieve tech news highlights on demand via an API or even extend the solution to deliver updates straight to your inbox!
Introduction to Public News API
NewsAPI.org is a news aggregation service that provides programmatic access to headlines and articles from major publications including CNN, BBC, TechCrunch and many more.
According to their website, NewsAPI returns JSON search results from over 150,000 sources!
The API allows developers to retrieve top headlines filtered by country or category (such as technology or business) and search across thousands of articles from various sources.
The service offers a free tier with up to 100 requests per day, though this free access is restricted to non-commercial use. For developers building news applications or integrating current events into their projects, NewsAPI provides a straightforward way to access real-time news content from reputable sources worldwide.
Review AWS SAM template
Let's review the template.yaml for the AWS SAM:
This template.yaml file contains the code to create both the Lambda function and the associated API resources. It defines a serverless application using AWS SAM and deploys a Python 3.12-based Lambda function named GbNewsSummarizerHandler that summarizes GB news articles. The function is exposed via a REST API endpoint /summarize using API Gateway, with a POST method and API key authentication. An environment variable provides the News API key used to fetch articles. A usage plan limits API access to 50 requests per month, with throttling in place. The Lambda function is configured with a 15-second timeout and 128 MB of memory.
Review AWS Lambda function (Python/Boto3 Library)
Let's review the Lambda function code:
Build function locally using AWS SAM
Since I am using AWS SAM for this function, let’s begin by building the function. Use the following command to build the function:
Command: sam build
Invoke function locally using AWS SAM
Once the build is successful, you can invoke the function locally using the sam local invoke command:
Command: sam local invoke
For example:
sam local invoke GbNewsSummarizerFn
When this function is executed, you should expect the following output:
Deploy function using AWS SAM
After validating the function locally, you can deploy it to the AWS Cloud using the sam deploy command:
Command: sam deploy
This concise workflow demonstrates how to create, validate, and deploy Lambda functions using AWS SAM. By leveraging AWS SAM, you can streamline the development process and ensure your functions are tested thoroughly before deployment.
Review API endpoint & response using Postman
Now that I have the API endpoint, I will use the Postman to invoke the API and will review the response returned.
Remember, API is secured by the key, hence if api key is not provided, a 403 forbidden message will be displayed.
Review Cloud Watch log
Review the logs for the event data and response, logs msg will be based on how you have implemented the logging in your function.
Cleaning Up Resources
Once you have completed the setup, ensure you delete the Lambda function to avoid unnecessary resource usage.
Since these resources were build and deployed using AWS SAM, these can be deleted using AWS SAM command.
Command: sam delete
You can also delete the resources via AWS Console.
Conclusion
In this article, I demonstrated how to build a news summarizer using a Lambda function and a REST API. The Lambda function uses a news aggregator API from NewsApi.org, which returns news results in JSON format.
This API can be integrated with a UI to deliver a customized news experience for users.
I hope you found this tutorial both helpful and informative!
If you're interested in extending this project further, stay tuned for my upcoming 5-part deep dive series, where I’ll walk through building a full-featured, AI-powered news delivery system using AWS:
- Part 1: Building a Simple News Summarizer with AWS Lambda and API Gateway
- Part 2: Automating News Alerts with AWS Lambda, SNS, and EventBridge
- Part 3: Adding Generative AI Summarization with Amazon Bedrock to Your News
- Part 4: Build a Serverless Generative AI News Summarizer API with AWS Lambda and Bedrock
- Part 5: Serverless HTML News Delivery Using Bedrock, EventBridge, Lambda, and SNS
Thank you for reading!
Watch the video here:
𝒢𝒾𝓇𝒾𝓈𝒽 ℬ𝒽𝒶𝓉𝒾𝒶
𝘈𝘞𝘚 𝘊𝘦𝘳𝘵𝘪𝘧𝘪𝘦𝘥 𝘚𝘰𝘭𝘶𝘵𝘪𝘰𝘯 𝘈𝘳𝘤𝘩𝘪𝘵𝘦𝘤𝘵
𝘈𝘞𝘚 𝘊𝘦𝘳𝘵𝘪𝘧𝘪𝘦𝘥 𝘋𝘦𝘷𝘦𝘭𝘰𝘱𝘦𝘳 𝘈𝘴𝘴𝘰𝘤𝘪𝘢𝘵𝘦
𝘈𝘞𝘚 𝘊𝘦𝘳𝘵𝘪𝘧𝘪𝘦𝘥 𝘎𝘦𝘯𝘈𝘐 𝘗𝘳𝘢𝘤𝘵𝘪𝘵𝘪𝘰𝘯𝘦𝘳
𝘈𝘞𝘚 𝘊𝘭𝘰𝘶𝘥 𝘛𝘦𝘤𝘩𝘯𝘰𝘭𝘰𝘨𝘺 𝘌𝘯𝘵𝘩𝘶𝘴𝘪𝘢𝘴𝘵
Top comments (0)