Voiced by Amazon Polly |
Introduction
When building modern applications, we often look for ways to keep things simple, scalable, and cost-effective. That’s where serverless computing shines. Instead of managing servers, we can focus on writing code, AWS Lambda lets me do that. On the other hand, we have recently been working with FASTAPI, a lightning-fast Python framework for building APIs. It’s clean, easy to use, and surprisingly powerful. In this post, we will go through how we consumed a FASTAPI endpoint from an AWS Lambda function and how this integration can help simplify your backend workflows.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
FASTAPI
If you haven’t heard of it, FASTAPI is a modern Python web framework that’s great for quickly building APIs. It uses standard Python type hints, which means your code is easier to read and gets automatic validation and documentation out of the box. Plus, it’s fast—fast.
Example endpoint:
@app.get(“/greet”)
def greet(name: str):
return {“message”: f”Hello, {name}!”}
AWS Lambda
AWS Lambda is a serverless compute service. Instead of worrying about provisioning servers, you write and upload a function, and AWS runs it when needed. You only pay for the compute time you use. It is perfect for short tasks responding to events like HTTP requests, file uploads, or database triggers.
Why Use AWS Lambda to Call a FASTAPI Endpoint?
Sometimes, your AWS Lambda function might need to interact with external APIs for validation, data enrichment, or triggering another system. In my case, we had a FASTAPI service running elsewhere and wanted my AWS Lambda function to call it, pass some data, and get a response back.
Steo-by-Step Guide
Step 1: Writing the AWS Lambda Function
Here’s a basic example of what the AWS Lambda function looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import json import requests def lambda_handler(event, context): name = event.get("name", "Guest") try: response = requests.get(f"https://your-fastapi-url.com/greet?name={name}") data = response.json() return { 'statusCode': 200, 'body': json.dumps(data) } except Exception as e: return { 'statusCode': 500, 'body': json.dumps({'error': str(e)}) } |
Ensure your FASTAPI endpoint is accessible publicly or at least reachable from within the same Amazon VPC if you’re running it privately.
Step 2: Deploying the Function
There are several ways to deploy your AWS Lambda function:
– Use the AWS Console (easy for quick testing)
– Use the AWS CLI (great for automation)
– Try the Serverless Framework or AWS SAM for more structured projects
Also, don’t forget to add the requests library if you use it. You can package it with your code or use an AWS Lambda layer.
Step 3: Triggering the AWS Lambda
You can trigger the AWS Lambda in multiple ways, depending on your use case:
- Amazon API Gateway – If you want to expose it as a public HTTP endpoint
- Amazon CloudWatch – For scheduled jobs
- Amazon S3, Amazon SNS, or Amazon SQS – If you’re responding to events
Using Amazon API Gateway made the most sense for me since we wanted the AWS Lambda to respond to HTTP requests.
Wrapping Up
Integrating AWS Lambda with a FASTAPI service turned out to be smoother than expected. We didn’t need to spin up servers or worry about scaling. The AWS Lambda function calls the API, processes the response, and returns the result, all in a few lines of code.
This setup is great for microservices, automation, background tasks, or building lightweight integrations between systems. And the best part? It scales automatically and costs nothing if you’re running a few thousand invocations.
Conclusion
To wrap things up, integrating AWS Lambda with a FASTAPI service is simple and extremely efficient. By leveraging the power of serverless computing, we reduce infrastructure overhead, while FASTAPI ensures our APIs remain fast, reliable, and easy to maintain.
It is highly encouraged to experiment with this setup in your projects. It’s a great way to learn more about serverless architecture and modern API development.
Drop a query if you have any questions regarding FASTAPI and we will get back to you quickly.
Empowering organizations to become ‘data driven’ enterprises with our Cloud experts.
- Reduced infrastructure costs
- Timely data-driven decisions
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 850k+ professionals in 600+ cloud certifications and completed 500+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner, Amazon CloudFront Service Delivery Partner, Amazon OpenSearch Service Delivery Partner, AWS DMS Service Delivery Partner, AWS Systems Manager Service Delivery Partner, Amazon RDS Service Delivery Partner, AWS CloudFormation Service Delivery Partner, AWS Config, Amazon EMR and many more.
FAQs
1. Can I host a FASTAPI app directly in AWS Lambda?
ANS: – Yes, you can. Tools like Mangum allow you to wrap your FASTAPI (ASGI) app and run it inside AWS Lambda. You can also deploy it using containers if your app gets more complex. You might still prefer services like Fargate or Amazon EC2 for performance-heavy APIs.
2. Does my FASTAPI app need to be public to call it from AWS Lambda?
ANS: – Not necessarily. If your AWS Lambda function is in the same Amazon Virtual Private Cloud (VPC) as your FASTAPI server (e.g., hosted on an Amazon EC2 inside a private subnet), you can keep it private. Otherwise, it needs to be publicly accessible.

WRITTEN BY Guru Bhajan Singh
Guru Bhajan Singh is currently working as a Software Engineer - PHP at CloudThat and has 7+ years of experience in PHP. He holds a Master's degree in Computer Applications and enjoys coding, problem-solving, learning new things, and writing technical blogs.
Comments