How to create a usage plan for AWS HTTP API? - amazon-web-services

I am using AWS API Gateway with HTTP API which invokes a lambda function. However HTTP API doesn't include USAGE feature. According to my requirement I need to create a usage for a client depending on the status code of the response sent back by the lambda. Since I cant access the response sent by lambda in API Gateway, I am looking for an custom solution. I am planing to use STEP function.
For example:
Instead of API Gateway directly invoking a lambda function it can call a STEP function where I can execute LambdaA. next it would trigger LambdaB with response from LambdaA as input to LambdaB in a sequential manner. I don't know If this is the right approach
I would like to know what is best way of solving this problem...thanks in advance

Related

How to make an API Gateway trigger call a lambda asynchronously?

I setup my lambda in AWS in the following way:
There is an API Gateway trigger which triggers a Lambda, which in turn is supposed to write to SQS.
The last part doesn't happen. From what I've read, this is because API Gateway invokes lambdas synchronously by default since calling it async-ly sacrifices the ability to return a response from the lambda.
I have also read that nonetheless, it is possible to configure API Gateway to call lambas async: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-integration-async.html
However, it is unclear how to do this when the API Gateway is created by Lambda, as is the case with triggers. There is no clear way of adding additional headers to them.
Is it possible make it do an async invocation?
After digging into it, it seems that API Gateway does not (yet) support async invocations for lambdas in any way for HTTP APIs, it is possible REST APIs.
Step 1: Create a trigger using the REST configuration
Step 2: Disable proxy integration
Async is explicitly not supported for proxies.
Step 3: Add magic header to make lambda async
Step 4: Confirm
To confirm if it worked, look into your destination (in my case SQS) to confirm.
Additionally, your API (when called via API Gateway) should now return an empty page.

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.

AWS Service Proxy to SQS

We are exposing a AWS Api Gateway which needs to act as proxy and push body as message to AWS SQS. Our API gateway body can be array of object which we need to parse and send each object to SQS as separate message. Is there any way to achieve this without using Lambda ?
To answer your question, for something as simple as parsing a message and posting to SQS, I don't believe you need Lambda, no. Lambdas are designed for serverless-architecture. That is, when you don't have a server and you still want to run code. In this case, you do have a server behind your API Gateway, so you don't need Lambda (unless you want fancy branching error handling). You can use your API Gateway directly, yep. Here's a code review I found:
https://dzone.com/articles/creating-aws-service-proxy-for-amazon-sqs

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.

Handing back a response to API Gateway from Lambda

I'm using API Gateway-to-Lambda for a few micro-services but in at least one case the service will take 20-30 seconds to complete so in cases like this I'd like to pass back an immediate response to the client, something like:
status: 200
message: {
progressId: 1234
}
and then allow the Lambda Function to continue on (and periodically updating the "processId" somewhere that is accessible to a client. The problem is that if you call context.succeed(), context.fail(), or context.done() that apparently stops the lambda function from further execution and yet it's the only way I know to flush the stdout buffer back to the API Gateway.
This has led me to a second approach which I haven't yet try to tackle (and for simplicity sake would love to avoid) which involves API Gateway calling a "Responder" Lambda function that then asynchronously fires off the Microservice and then immediately responds to the API Gateway.
I've tried to illustrate these two options in sketch format below. I'd love to hear how anyone's been able to solve this problem.
Currently API Gateway requires that the AWS Lambda integration is synchronous. If you desire asynchronous invocation of your Lambda function, you have 2 options:
Invoking the Lambda asynchrously, either with an AWS integration calling InvokeAsync on Lambda, or using an intermediate service such as SNS or Kinesis to trigger the Lambda function.
You're #2 diagram, using a synchronous Lambda invoke to initiate the asynchronous invoke.
As of Apr/2016 is it is possible to create async Lambda execution through API Gateway by using AWS Service Proxy. See http://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-lambda.html
You can send the X-Amz-Invocation-Type header, it supports async calls through the Event value
You can optionally request asynchronous execution by specifying Event as the InvocationType
http://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html#API_Invoke_RequestSyntax
Also, if you can't send it via your micro-service, you can configure this header to be passed by default through the Method Execution -> Integration Request -> HTTP Headers in your API Gateway Resource
This worked for me on a micro-service -> API Gateway -> Lambda scenario, like the mentioned on the question.