Skip to main content

Command Palette

Search for a command to run...

Serverless API with AWS Lambda and API Gateway

Updated
3 min read
A

"I'm Abdul Samad Mahmoodi, and I have more than six years experience in IT Area specially in IT- Administration and IT-Support and my Tasks were DHCP, DNS, Mail server configuration, creating LAN, WAN also working with Active Directory and monitoring, also configuration of Switches, Routers, Firewalls and Monitoring the network finally I was the first contact person for network problems also I am an AWS Certified Solutions Architect passionate about IT- Administrator, IT-Support, DevOps, Cloud Engineering, and CI/CD automation. I love building Robust, scalable, highly available Infrastrukturen in on-premises, Hybrid or cloud solutions AWS and Azure, Terraform, Kubernetes, and Docker."

This project demonstrates how to build a simple, serverless API using AWS Lambda and API Gateway. The goal is to create an API endpoint that returns the client's IP address using the (X-Forwarded-For header) from incoming HTTP requests. The project utilizes AWS services to handle the backend logic and exposes the Lambda function through a scalable HTTP API using API Gateway.

I used Terraform for creation of this serverless infrastructure and the steps I went for creation are as below:

  1. I used VSCode and created a directory (Lambda-api) and inside that I created my main.tf for terraform and another folder (Lambda) including lambda.py file.

  2. Inside my lambda.py I used (def lambda_handler(event, context):) and at the end return statuscode 200 to return the IP.

  3. In Terraform file main.tf I did below steps: a- Provider for terraform b- data "archive_file" for our lambda.zip c- resources for creation of structure like, IAM Role, Lambda Functions, apigateway, apigateway_stage, gateway integration, apigateway_route and lambda_permission.

4- After my code completed I used terraform commands as below:

a- terraform init: for downloading the plugins

b- terraform fmt: for format of my code

c- terraform validate: for validation of my code syntax based on terraform requirements

d- terraform plan: to see the planned infrastructure and possible mistakes inside the code

e- terraform apply: to create our infrustructure

f- terraform destroy for destroying infrastructure to prevent from costs

5- when the serverless infrastructure created

a- I opened my AWS Consule and navigated to Lambda and then function there was a function with the name of lambda-function created as I defined in my VSCode for terraform.

b- then I opend the function I saw the Python code which I defined in my VSCode for terraform

c- and I went to api gateway and there was also an api created with the name which I specified in my code

d- finally I got the URL and opened in my browser and api returned the IP as below:

Key Components: AWS Lambda: The backend function that processes HTTP requests and extracts the client's IP address from the X-Forwarded-For header. API Gateway: Exposes the Lambda function as an HTTP endpoint. Requests are routed through API Gateway and trigger the Lambda function. IAM Role: Grants the necessary permissions for Lambda to be invoked by API Gateway. API Gateway Integration: AWS Proxy integration links the API Gateway route to the Lambda function, allowing dynamic request handling.

Usecases:

This project is a great example of a serverless architecture where you don’t need to manage servers. It's ideal for scenarios like returning user-specific data (like IP address), handling simple backend logic, and creating scalable APIs without worrying about infrastructure.