46
loading...
This website collects cookies to deliver better user experience
A Lambda layer is a .zip file archive that can contain additional code or data. A layer can contain libraries, a custom runtime, data, or configuration files.
nodejs
. Here the folder name has to be nodejs
so that aws lambda can identify the dependencies. Let us now build a layer which contains the node-fetch
module that creates http requests with node js. nodejs
.package.json
file using npm init -y
.node-fetch
will be installed using npm i node-fetch
.nodejs
folder there will be one folder named node_modules
and two files named package.json
& package-lock.json
. Compress the nodejs
folder to a zipped file.GET
request to an api & show the response in a json
format. Our lambda function code is:const fetch = require("node-fetch");
exports.handler = async (event) => {
try {
const response = await fetch(event.url);
const json = response.json();
return json;
} catch(e) {
return e;
}
};
{
"url": "https://reqres.in/api/users"
}
Test
button we should see a response like following:{
"page": 1,
"per_page": 6,
"total": 12,
"total_pages": 2,
"data": [
{
"id": 1,
"email": "[email protected]",
"first_name": "George",
"last_name": "Bluth",
"avatar": "https://reqres.in/img/faces/1-image.jpg"
},
{
"id": 2,
"email": "[email protected]",
"first_name": "Janet",
"last_name": "Weaver",
"avatar": "https://reqres.in/img/faces/2-image.jpg"
},
{
"id": 3,
"email": "[email protected]",
"first_name": "Emma",
"last_name": "Wong",
"avatar": "https://reqres.in/img/faces/3-image.jpg"
},
{
"id": 4,
"email": "[email protected]",
"first_name": "Eve",
"last_name": "Holt",
"avatar": "https://reqres.in/img/faces/4-image.jpg"
},
{
"id": 5,
"email": "[email protected]",
"first_name": "Charles",
"last_name": "Morris",
"avatar": "https://reqres.in/img/faces/5-image.jpg"
},
{
"id": 6,
"email": "[email protected]",
"first_name": "Tracey",
"last_name": "Ramos",
"avatar": "https://reqres.in/img/faces/6-image.jpg"
}
],
"support": {
"url": "https://reqres.in/#support-heading",
"text": "To keep ReqRes free, contributions towards server costs are appreciated!"
}
}
requests
to make http request and fetch data from remote api. Now we have to do as following:build/python/lib/python3.8/site-packages
. We must not make any spelling mistake otherwise we will encounter error while running the lambda. Another important thing is the version of python. You must use the same version of python everywhere. Here I am using 3.8 so while selecting runtime I must select Python 3.8 to avoid errors.site-packages
folder and install requests
using pip like this where -t .
means install everything inside this folder.build
folder & compress the python
folder to a zipped file.Python 3.8
as runtime.
mkdir build/python/lib/python3.8/site-packages
cd build/python/lib/python3.8/site-packages
pip install requests -t .
import json
import requests
def lambda_handler(event, context):
response = requests.get(event['url'])
data = response.json()
return data
46