I want to just create a really simple lambda function to demo how do I let's start by changing the output okay. I have already created lambda function.
Create Lambda function
You use a Lambda function for the backend of your API. Lambda runs your code only when needed and scales automatically, from a few requests per day to thousands per second.
Lambda functions and Lambda layers See the full story before going on
I'm going to create some JSON object that is my response and let's assume that my JSON object is some kind of student right so. I have an ID a name and email whatever other additional details I care about for a student okay so I'm going to say that there's an ID that'll be a numeric a name which will be some string an email an age weight that should be fine. so this is my response I build an object that has some key value pairs and save the lambda function.
exports.handler = async (event) => {
const response = {
"id": 1,
"name": "joe bloN",
"email": "m.shakeel0581@gmial.com",
"age": 44,
"weight": 258,
}
return response;
};
Here is code of lambda function, update the lambda function with above code and hit the deploy.
Connect lambda function to an API gateway
Now I want to connect this lambda function to an API gateway so that I can access this function over the Internet through an API so maybe I want to write a JavaScript application that's gonna connect to that URL okay. So in order to do, that I'm going to go over to API and I'm going to build out a RESTful API.
I already created and setup RESTful API in my previous story. See the full story before going on
So open 'sample-Api' from API Gateway that have created above mentioned story. so right now, we're not doing anything with this to reshape our data but I'm able to test this.
Choose the method POST and go to Method Test page.
okay so if I just click on test. I want to see what the return value. I can hit test there are no parameters and you'll notice if I test it, you can see that I get that student object that I created in lambda.
okay so a couple of things about this pipeline. The top two boxes are for the request, as the request is coming in the method request I can do validation like did you pass in JSON web token for authorization or did you pass in an API key or if you're inserting a new record.
In the integration response and then the method response will have information about like your requests are your response headers if we're gonna enable cores which is cross-origin resource sharing so that we can access our API from remote domains.
So all of those different filters or boxes are up for a reason.
Models
API gateway there's a section called models alright. I have explained about modal in my last story.
You can find out modal section in this story here.
I'm building out a restful api and I'm gonna have different HTTP verbs I might be getting students I might be inserting students I may be updating students I might be deleting students I need to know what a student actually looks like I know what a student looks like because I built it in lambda it has an ID a name and email and agent weight so that's what I wanted to find as a student object but then I want to clarify what a student object is I would argue that the ID is a number the name is a string the emails a string the age is a number and the weights a number okay so I want to be specific about what a student is I want to provide a definition saying these are the properties that compose a student and those properties are of these specific data types okay if I want to do hat I do that in models so I'm jumping over to model.
I'm gonna create a model. My model is gonna be student and the content type is going to be application/JSON. The model name is required the content type is required the descriptions not required but I'm just putting in kind of a generic description and then I'm going to paste that schema definition that I had taken from Amazon ad just create the model.
{
"$schema": "https://json-schema.org/draft-04/schema",
"title": "MATCDemoStudentOutputModel",
"type": "object",
"properties": {
"id": { "type": "number" },
"name": { "type": "string" },
"email": { "type": "string"},
"age": { "type": "number" },
"weight": { "type": "number" }
}
}
So now I'm going to integrate model schema to the Integration Response.
Select Integration Response from POST - Method Execution.
I'm going into that mapping template and then click on Add mapping template.
Add Content-Type application/json and click check icon to add.
Now I can choose that model is student. So if I choose that model as my template it builds me dummy data it says here's the properties that I know about for a student this is what should be their data gives dummy data right.
we don't make any changes I just say based on the model I have some dummy data if I save it right now and go back and I want to test this see if it's working. Remember I'm gonna make a request that's all done for me that's what the test does I end up at my lambda end point and before what was happening is as returning a student that looked like this this is what's defined in lambda this fake student now that I've defined my mapping template in my integration response. I said no that's not what the data is gonna look like I want to use some dummy data. I'm kind of ignoring the response from lambda right those values were hard-coded pi and foo for the name and email. So now if I test this, you'll notice that I get the data from the integration response that dummy data.
okay so we can get a little closer so if I go back into my mapping template let's assume that age and weight are sensitive right so we don't want to send that information. I'm just not going to include it in my template I know what the model was I know the model included agent weight I don't want people that are gonna use my API to see that information so I've changed my mapping template so now I save it .
Now if I go back and I test it again now you'll notice the age and weight are removed it doesn't have the data that we want.
They'll right it has the hard-coded data for the ID and the name and the email like I probably want to get that information from my lambda function I just want to strip off the variables that I don't want my API consumers to see it's that fair.
so this Joe Blow this is the data assuming that Joe Blow is an actual student but I want his age and weight stripped off okay so if I go back over to my integration response the way I'm going to get that is with that templating language okay so for the ID the way I'm gonna get the ID is I'm going to use my variable input root an input root is talking about the root element of my response so the root element of my JSON response so input root is really this object so I want the input root Dot ID and here I want the input root name and here I want the input root email okay so I'm saying from my response that I got back from lambda I have a variable called input root as which is referring to that root element that got returned and now I'm saying pick out the individual components so that I can build an object that looks like this
Okay now if I go back and I test it I intentionally okay here's the mapping template.
so I know that initially I started with a student that had an email maybe my client application expects it to be email address.
let's assume that I don't want to return email to the end user I want to return an email address I can create a new property
Okay now I go back I test it you can see that my output is email address all of these changes are great I've built all of these changes in my API gateway.
if we want to reshape the data we're going to be under resources and then a specific method in the integration response is where we would reshape our data.
Top comments (0)