Is it possible to pass an API Gateway variable into a Lambda Context? - aws-lambda

I'd quite like to be able to use context.stage in a Lambda call, passed in from the API gateway context - but I can only see how to put it into the message body (event object).
Is there any way to add arbitrary information to the Lambda Context?

I believe your only option is to add it to the event. You can always extract the value at the start of your Lambda function and then delete the property.
The Rest API example from AWS does something similar:
var operation = event.operation;
delete event.operation;

Related

AWS API Gateway Routing

How can route a request from API Gateway based towards the lambda functions A/B/C based in the fact that specific header is present or not?
https://api.example.com/prod
if the headers "UserType" is:
guest -> redirect to lambda A
normal -> redirect to lambda B
admin -> redirect to lambda C
I know how only using different URL, e.g:
https://api.example.com/prod
https://api.example.com/dev
It's actually not possible. We can bind only one Lambda function with a specific resource of API Gateway. The headers are accessible as an event parameter within the lambda function itself.
Preferred way would be to create 3 different functions(your business logic's will reside here) within your lambda & based on usertype you can call the function.
And if you really want to make use of three different lambdas, you can make use of invoke method available in aws-sdk. You can refer following link for code syntax & example.
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html#invoke-property
2nd method will definitely increase the response time.

Does the value of global variable persist in multiple API calls

So I have done a lot of research and could not find a proper answer. This might be a bit long post so sorry for that. I am making a backend API using golang. I am using gingonic for routing and api stuffs.
There are 2 part of the service. Application and user. When lets say createAccount endpoint is called from the other micro service, it need to pass the user information and application token in body. Each application is like micro service that is registered to this micro service that I am building and have a unique token. If the token they pass matches then I will get the id of that row and use that to create an entry in user table which will have the id associate with it.
Now for every API call to this micro service, it is important that they are sending the valid token and that row id is needed to do all sort of functionality like login user, edit user info and so on as the each user is connected with that app id by foreign key. Currently, I wrote a middleware and when any api call is made I get row id and save it to a global variable and then when necessary I am using it in any part of the codebase.
Lets say if 5 multiple API call is made, will that global variable information will be persisted or for each call its brand new value? If it is persisted then what can I do to achieve the brand new of Global variable for every API call or if there are better approach can you please recommend it?
A global variable is not the answer here. It will be overwritten by each request as you suspected. Instead, the typical way to handle this situation is to have a context object that is created within the scope of the HTTP Request and passed to each method that requires knowledge of that context.
One basic rule is to AVOID using the global variables, it is bad practices, you cannot manage the state and you are limited for testing and concurrency using.
In my mind come two basic solutions:
Use context for this. In your handler, add the value in context and propagate this context by all your service calls. It is also useful for tracing if you are working with microservices, then you also should take a look for this. And in the place where you need the value from your global variable, do simple call: ctx.Value(YOUR_KEY) Take a look at the end of the page, you shouldn't use string as the key to context values.
You can wrap your data in the struct with this variable value. For example:
type CreateReq struct {
Token string // value from global variable
User user
}
and use this Token in your services.

How to execute different aws lambda depending on request body variable/item (aws api gateway)

I want to be able to execute different lambda based on a request parameter. How can I do this?
example:- If I have two aws lambdas abc and pqr, I want to send lambda-function-name parameter in request body (for a POST request). Depending on value of lambda-function-name I want to execute either abc or pqr?
Is this possible?
Unfortunately api gateway can only differentiate based on method and path.
A solution, however, would be to have a "proxy" lambda that inspected the POST body, invoked the desired lambda (using the aws-sdk; documentation for node here) and then returned the result.

Does Parse.Cloud.beforeRead exist is some form?

In Parse there is something called:
Parse.Cloud.beforeSave
I wonder if there is something to play the role of a:
Parse.Cloud.beforeRead
I need a way to control what is going to be returned to the user when a request is made to the DB.
In particular in certain circomstances, depending on information on the server, I want to force blank fields in the result of the DB request made by the user. Any standard way to do this?
There is no Parse.Cloud.beforeRead kind of function supported by Parse.
Instead, you can define a custom cloud function using
Parse.Cloud.define('readObjects', function(request, response) {...} );
that returns array of objects. This function will act as a wrapper over the Parse query.
Then, your client apps should be calling this cloud function to fetch objects rather than direct Parse.Query requests.

OpenLayers: value of Event parameter in OpenLayers.Event.triggerEvent

I'm looking at the
documentation
for triggering events in OpenLayers. It lists triggerEvent as taking in a type:String argument and a evt:Event argument. I understand what the type parameter is, but what object is the Event object? I can't find a clear example.
If it is relevant, I am working on an interface between Google Maps v3 and OpenLayers, and I'm trying to come up with a way so that I can have a the same API interface to calls to:
google.maps.event.trigger(instance:Object, eventName:String, var_args:*)
openLayersObj.events.triggerEvent(type:String, evt:Event)
Doc is wrong. It should be Event || object. Object that will be args passed to the listeners callback.
Look at the source: https://github.com/openlayers/openlayers/blob/master/lib/OpenLayers/Events.js#L820
Example of use: https://github.com/openlayers/openlayers/blob/master/lib/OpenLayers/Map.js#L1042

Resources