AWS Serverless with Lambda & API Gateway Hands-On

AutOpsAutOps
4 min read

Author: Ujwal Pachghare

Step 1

πŸ‘‰
Go to lambda console -> Click on the function -> Click on Create function

Create a Lambda function with the following configurations:

Function name: api-testing

Runtime: Python 3.10

Architecture: x86_64

Permissions -> Execution role: Create a new role with basic lambda permissions


Step 2

πŸ‘‰
Open Visual Studio Code/other IDEs -> Create file lambda_function.py -> Paste below code in that file
AWSGI
AWSGI allows you to use WSGI-compatible middleware and frameworks like Flask and Django with the AWS API Gateway/Lambda proxy integration.
FlASK
Flask is a Python web framework that provides configuration, conventions, and tools to create and deploy web applications.
import awsgi
from flask import (
    Flask,
    jsonify,
)

app = Flask(__name__)

@app.route("/")
def index():
    return jsonify(status=200, message="OK")

@app.route("/version")
def get_version():
    return {"version":"1.0.0"}

@app.route("/profile")
def get_name():
    return {"name":"AWS Dev"}

def lambda_handler(event, context):
    return awsgi.response(app, event, context, base64_content_types={"image/png"})
πŸ‘‰
Open terminal -> Run the following commands one by one (Install Python 3.10 if not)
# Install pip on ubuntu(if not):
sudo apt install python3-pip -y  

# Install zip on ubuntu(if not):
sudo apt install zip -y 

# Install awsgi and flask in api directory 
pip install aws-wsgi flask -t api

# zip the code of api directory 
(cd api; zip ../api.zip -r .)

# Add lambda_function.py file in zipped directory
zip api.zip -u lambda_function.py


Step 3

πŸ‘‰
Go to our Lambda function -> Click on the Upload from button -> Select the .zip file -> Search for api.zip file that we have just created and upload


Step 4

πŸ’‘
Creating Amazon API Gateway to call the function with the help of APIs Go to Amazon API Gateway Console -> Scroll down and Select REST API by clicking on Build button

Select the following API Details:

Choose New API

API name: lambda-api

API endpoint type: Edge-optimized (it can be accesible from browser)

πŸ‘‰
Click on the Create Method button

Fill in the following method details:

Method type:GET (To get the value)

Integration type:Lambda function (API integration with lambda)

Lambda proxy integration:ON (For sending request to lambda)

Lambda function: choose region and lambda function ARN

Default timeout:default (29 sec)

πŸ‘‰
Creating Resource for version apiClick on the Create Resource button -> Give Resource name as version

πŸ‘†
Follow the same strategy to create the GET method that we followed in the previous method creation. Ex: Click on the Create method button, then.......

πŸ‘‰
Creating Resource for profile apiClick on the Create Resource button -> Give Resource name as profile (make sure it is in "/" Resource Path)

πŸ‘†
Now Create the same GET method that you created for the version resource


Step 5

πŸ‘‰
Click on the Deploy API button

Fill in the following Deploy API configurations:

Stage: \New stage**

Stage name: dev

Description: optional

πŸ‘‡
After deploying the API, you will get the following URL

Copy that URL and paste it into the browser; you will get the following result for a GET API of the root.

Testing version API: you will get result as follows:

Testing profile API: you will get result as follows:

Conclusion:

We have successfully created an automation task with the help of AWS Lambda and the Amazon API Gateway, and it will perform the following actions:

-> Whenever any of the above APIs are called by any user, it will trigger the Lambda function.


Final Step

Cleaning Up

  1. Delete Lambda function

    ❎
    Go to the Lambda console -> Select function to delete -> Click on Delete -> Type delete in pop-up -> Delete

Delete API Gateway

❎
Go to API Gateway console -> Select API Gateway to Delete -> click on Delete button -> type confirm in pop-up -> Delete


If you find this blog enjoyable, please show your appreciation with some claps and stay connected by subscribing to our newsletter. This way, you won’t miss any updates from AutOps. Thank you for reading...πŸ™πŸ™πŸ™
0
Subscribe to my newsletter

Read articles from AutOps directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

AutOps
AutOps