AWS Lambda lets you run code without provisioning or managing servers, whereas
Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs.
In this beginners guide we will be creating a Lambda function, we will then create an API and integrate the API to the Lambda function (to invoke the lambda function through API calls). We will do all of this while being in the free tier.
1. Creating and testing the lambda function
• In AWS Console, search and open 'Lambda'
• Click on Create Function
• In the new page, select Author from scratch
• Give a name to the function
In 'Runtime' you can give a programming language of your choice.
Select the x86 instruction set architecture. Unless you are writing very low level stuff you can select arm64 too.
• Hover into the Code
tab
Here, you can write the code which you want the function to perform.
After, every change, we use Deploy
to make the change effective.
Test
is pretty self-explanatory, its used to test the code.
Your code may show different behaviour in
Test
andAPI Calls
since Tests are not invoking HTTP Requests.
Paste the below code in the code editor
import json
def lambda_handler(event, context):
# Parse JSON body
try:
body = json.loads(event.get("body", "{}"))
except json.JSONDecodeError:
return {
"statusCode": 400,
"body": json.dumps({"error": "Invalid JSON body"})
}
x = body.get("message", "no-message")
return {
"statusCode": 200,
"message": x
}
lambda_handler
- loads the event body, and context object of the execution
event
is the request body passed by the API gateway in a json format
body = json.loads(event.get("body", "{}"))
- it first checks for the body
key in the event, if present, it returns the string present in it, which the json.loads()
function converts into a (key,value)
dictionary, if theres no body
key present, it loads an empty dictionary.
x = body.get("message", "no-message")
- assign the value stored by the message
key to x
, if there's no message
key, assign "no message"
to x
At last we return the message recieved by the API with a status code.
• Testing the code with Test Events
Test events can be used to create various tests for our code.
Click on + Create new test event
.
A new pane in the code appears.
Give a name to the test (for now, we keep the tests private).
Paste a valid test case like the case below -
{
"body": "{\"message\": \"Hello from client\"}"
}
Click on Save
and then the play button next to the test event in the sidebar to perform tests.
A more comprehensive way to handle tests is available in the Test
tab next to Code
.
2. Creating REST API and integrating it with Lambda function
• Search and open API Gateway
• Scroll down to REST API
and select Build
• Give a name to your API, scroll down and select Create API
• Creating a Method
Every API handles its tasks using different HTTP Methods
. Most popularly used htttp methods are GET, POST, PUT, PATCH, and DELETE
, these methods can perform CRUD
operations on the server.
Therefore, we need to create Methods
which our API can handle.
In the newly created API, select Create method
.
In Method type
section, select POST
from the drop down menu.
Select the Integration type
as Lambda function.
Under the Lambda function
section, click on the search bar and select the lambda function created to integrate with the API.
• Testing the API Method
In the method created, go to Test
tab
In the request body paste the following HTTP request:
{
"body": "{\"message\": \"Hello from client\"}"
}
and, click Test
.
You should see a response body like this
• Creating Stages
and Deploying the API
Stages are environments where you can deploy your API. Stages create a referecne to different lifecycles of the API, e.g. dev
, prod
, beta
, etc.
In the sidebar, select Stages
In the stages section, select Create stage
, give a name to the stage, e.g. prod, test or dev, and select *new stage*
for stage deployement. The select Create stage
.
In the sidebar, select Resources
and click on Deploy API
Your API is now live!
• Sending requests to the API
In the Stages
section, copy the Invoke url
. This is the url created for a stage, to send requests.
Open ReqBin, and paste the invoke url and request body, select POST
as request method type and Send
the request.
You should see a response like this
Sending requests using curl --
Paste the below command in the command line (curl should be installed) --
curl -X POST "" -d '{"body": "{\"message\": \"Hello from Command Line\"}"}'
You should see a reponse like this from AWS --
{"statusCode": 200, "message": "Hello from Command Line"}%