How to call two endpoints for a single request - amazon-web-services

We have an existing endpoint published in AWS Api Gateway POST https://publicendpoint/app/users which points to the internal endpoint https://microservice1/app/users.
As we're in the process of backend database migration, whenever https://publicendpoint/app/users is called, we need to call two endpoints
https://microservice1-olddb/app/users and
https://microservice1-newdb/app/users .
How to do this?
Have tried with creating a API Gateway trigger with lambda.
But not able to achieve this scenario with lambda trigger.

API Gateway doesn't do any orchestration, so you can't point it at both endpoints. And even if it did, how would you handle one service failing, which service takes precedence with the response given etc.
The simplest answer is probably to use a Lambda as an orchestration layer. That is point API Gateway at a Lambda which in turn calls both endpoints.

Related

Changing Rest API Endpoint Url dynamically in AWS Api Gateway

I'm looking after solution where AWS Api Gateway changes method endpoint Url dynamically.
I am familiar with stage variables and in Integration request I can change endpoint per method like (https://${stageVariables.Url}/api/DoSomething).
What I need is that information how parse endpoint is included in requests.
https://${RequestData.Url}/api/DoSomething
I have same Api in different locations and to implement centralized Api keys and logging services I try to forward all traffic through this one Api Gateway.
After first request client gets its endpoint information, but I don't know how to solve that clients next requests to Gateway should forward to that endpoint which client get earlier.
I got an answer from AWS support. They told that I have to make a lambda function to process all requests or just use Stage variables.

How to execute a Lambda function before a method is called in API gateway?

I need to execute some Lambda function for collecting statistics before any method from API gateway is called. Is it possible to achieve this in AWS? I am using proxy integration in Api Gateway.
This can be done in the CDN level if you have one.
If you are using cloudfront then the steps could be,
Create a regional endpoint.
Set up own cloudfron distribution - https://aws.amazon.com/premiumsupport/knowledge-center/api-gateway-cloudfront-distribution/
Handle the request with Lambda#Edge, add a trigger for Origin Request.https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-cloudfront-trigger-events.html
Few Ideas,
If the requests may be whitelisted and blacklisted based on the parameter, lambda custom authorizor can be used to plug in the logic.
if the statistics don't influence the business logic, then cloudwatch logs can be parsed (log insights or cloudwatch rules) and the statistics can be gathered.
Aggregation pattern can be used for the statistics- either Gateway or Lambda level , but this will add additional latency for each requests.

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

AWS API Gateway Service Orchestration

Is there any way to orchestrate the micro services from API gateway? I have two micro servicess and I am tring to aggregate the data from the API gateway. Tried with STEP function but it is asynchronized in nature.
Request will come to the API gateway, we need to call multiple services and aggregate the data and send it back
You can try lambda function to aggregate different services response.

Is it possible to use AWS Lambda to request an oauth 2.0 token?

I am looking for ways to avoid creating an ec2 instance in order to have a valid callback URL to perform the oauth handshake.
I plan to use Lambda to connect to a remote API, but I need to be able to get the token first, which is valid only for 6 hours.
Is there any way I can make the handshake via Lambda functions?
I think Lambda along with API Gateway offer a good solution. API Gateway allows you to create a persistent, publicly accessible HTTP endpoint. You can define specific 'resources' that map HTTP methods to lambda function calls.
I'm not especially familiar with OAuth 2, but I'm imagining something like this: In API Gateway, define a resource '/callback' with a GET method that invokes your Lambda function.
Register the API Gateway endpoint as your application's callback URI, which would look something like this:
https://c1bql2cdxy.execute-api.us-east-1.amazonaws.com/callback
By doing so, the remote service will invoke your lambda function, which can then read the authorization token from the request and use it however needed, whether that involves 1) storing the token in a database for future use (and reuse) by other services, 2) directly invoking the services within the same Lambda function, etc.