AWS API Gateway call Lambda versions by header information - amazon-web-services

I've read a lots of API versioning hints for integrating AWS API Gateway with Lambda functions. But most of them used the URL to provide the version number e.g. /v1/orders
But I do not want to use this pattern. Instead I'd like to evaluate the header information to retrieve the requested version.
What I want to do is the following:
configure API Gateway (via CloudFormation) in order to define only /orders
use request mapping to read a header 'version'
call a lambda function e.g. getOrders:v1 if 'version' header is set to 'v1'
Is this possible with AWS Api Gateway? Or is there a similar approach that doesn't use URL to distinguish between versions?
I saw a solution by using stage variables:
https://dzone.com/articles/api-versioning-approach-with-aws-api-gateway
Regarding this approach: Is there an equivalent to ${stageVariables.v1fn} for headers? E.g. ${request.headers.version}?

A possible solution would be to use a lambda which dispatches the request to the lambda in question, API Gateway unfortunately only supports one lambda per endpoint and method

Related

How to use aws lambda without HTTP api?

Since there is aditional costs for using HTTP and REST apis on AWS lambda, i would like to know if i could make AWS Lambda receive gets and posts without the need of these HTTP API services.
In this example it seems to be possible:
https://github.com/serverless/examples/tree/master/aws-node-simple-http-endpoint
You will need to use the API Gateway to expose your lambda. Your example is actually using an API Gateway, because the endpoint is execute-api.us-east-1.amazonaws.com and that is the Amazon API Gateway Data Plane.
Just to be clear; if you need to expose the Lambda externally you need to use the API Gateway. If the Lambda needs to be invoked internally then you don't need the API GW.
Best regards
Lambda also exposes a client API in all languages. Therefore, you can invoke a Lambda function by using the client API (not use API Gateway if you prefer). For example, assume you want the ability to invoke a Lambda function from a Java web app. In this situation, you can use the LambdaClient object to do so. You can find an example here:
https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javav2/example_code/lambda/src/main/java/com/example/lambda/LambdaInvoke.java

Call AWS Lambda from Step Funtion using specific URL with parameters

I am trying to orchestrate UI calls using step function with the minimal impact. Currently I have a lambda function that can be called using different URLs via API gateway, for instance, following URLs are used to call the same lambda:
http://base.url/orders/get/order/{userid}
http://base.url/orders/get/allorders/
I know that it isn't a best practice for lambdas, but we have what we have. Now I need to add a step function between API gateway and lambda to orchestrate calls. I need step function to be able to call step function using these urls, but I cannot understand how to do that.
Here are some links that I already checked:
https://docs.aws.amazon.com/step-functions/latest/dg/concepts-input-output-filtering.html
https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-paths.html
https://docs.aws.amazon.com/step-functions/latest/dg/connect-parameters.html
Is there a way to do what I need to do?
It sounds like you just need to format the message to the lambda in a way that looks like what would be coming from the API Gateway. If that's the case you can see what an API Gateway request would look like by selecting Amazon API Gateway AWS Proxy from the lambda test events in the console. From there you should be able to modify the payload to match your needs.

Authorization Header In Place Of x-api-key

I currently have created an API Gateway using AWS. This gateway requires an API Key that is stored on AWS. From what I've seen, the correct way to pass this key in the header is to use x-api-key. For continuity, I was hoping there would be a way to pass the key in the header of the POST using key Authorization instead of x-api-key. From what I've read though, since this gateway is hosted in aws, x-api-key is the only way to pass the key. Is there a workaround to this?
Lambda Authorizer
IMO it is easier if you just stick with the convention, it's actually a bit of work to change this. You need to create a Lambda Authorizer. This will be a Lambda function that will return the iam policy for a given request to your gateway, but also an optional context. This context can contain what API key should be used, although you need to keep in mind that your Lambda function does the Authorization, this key is used only in the usage plan I believe.
So to summarize when you create your authorizer you will pick the header that is used Authorizaion in your case. In you Lambda function you reference this header here: event.authorizationToken. Using that header you construct your iam policy, this may mean you need to manually look up this API key if it does have access to your API. Your Lambda function will also need to set the the api-key to the $context.authorizer, which contains the api key you want to use, which will be event.authorizationToken in your case. That will apply the UsagePlan to your API.
CloudFront
Another option is to create a CloudFront distribution in-front of your API and add the header in manually yourself. This will probably cost more though because you will need a CloudFront distribution and then your Lambda#Edge will execute with every request. As opposed to a Lambda Authorizer which can be cached. You can find an example here.

Can I do some pre-processing in AWS API Gateway before forwarding it to the actual backend?

I am looking for a solution where the aws api gateway can add few extra headers before forwarding to the actual backend. I can imagine that keeping lambda function as a call back function can be one way to execute it. My calls can be slow, which means, if i use lambda functions in between, i pay a lot.
Is there any other way to do it ?
I am looking for something similar to a 'pre-processor' in Tibco Mashery. Which means, when the request comes, this method/logic is executed by api gateway, which will add the extra headers, and api gateway will forward the request to the actual backend.
Edit:
I need to fill the header dynamically based on the incoming request and some mapping tables in db.
Thanks
Add a CloudFront distribution in front of your API Gateway endpoint. Then add a Lambda#Edge function for origin requests that adds the extra headers.
See some example Lambda#Edge functions here: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-examples.html.
Yes you can.
In the integration request you can add headers :
You can store your new headers value in the stage variables :

GET aws lambda functions list using rest

I want to write a unit test for the availability of AWS lambda functions. Is there any way to GET AWS lambdas list. I know using CMD we can get the list of AWS function. But I want any other way to get the lambda functions list.
After research, I find out a rest API of all AWS services. But I didn't understand how to use them.
I found out AWS HTTP calls, links are attached https://docs.aws.amazon.com/general/latest/gr/rande.html.
Yes, you can do that using AWS Infrastructure REST API interface.
To do so,
You first need to create a signature 4 authentication key for every call you make.
Reference: Authenticating Requests (AWS Signature Version 4)
Then you need to pass it via Authorization header in any REST request.
Reference: Authenticating Requests: Using the Authorization Header (AWS Signature Version 4)
Finally, call using ListFunctions API.
Reference: ListFunctions
Call Example:
GET
/2015-03-31/functions/?FunctionVersion=FunctionVersion&Marker=Marker&MasterRegion=MasterRegion&MaxItems=MaxItems
HTTP/1.1
This gives you the list of Lambda functions deployed.
Hope it helps.