How do I create a rule that captures an HTTP GET that has some data and schedules the running of a lambda function at a specific time? I can write the lambda function but I am having trouble with creating an API endpoint to which I can send a "fetch" GET request. I tried creating an HTTP API in the API Gateway service. It returns this output:
{
"message": "Forbidden"
}
I am not much familiar with AWS. Please help me with creating a simple endpoint in AWS to which my app can send a GET request with some data, which will trigger an EventBridge rule to schedule the running of a lambda function at a specific time.
You don't need an API Gateway or a separate Lambda function.
To schedule the running of a specific Lambda function you can call the EventBridge put rule API.
You can find examples of the EventBridge user guide.
Related
I have a REST API built using API Gateway with a couple of methods. I need to run a POST request on a method /generate-stats once a week. I currently call this method through the AWS console by pasting a request body into the "Test" feature that exists in API Gateway under the Method Execution flowchart.
How would I go about automating this call? Would a lambda that runs once a week be the simplest solution? Ideally I can store the response or trigger an alarm if the request fails.
If you want to automate a request to happen once a week you would want to look at using Amazon EventBridge.
The service itself supports either being triggered by an event (such as a new PutObject into S3 or an instance being launched) or can run based on a schedule. You would want to use the latter to set a cron expression for running this.
The next part of the rule is the target which in this case are a couple of approaches.
API Gateway requests are a supported target from within the event. If the supported functionality with EventBridge is suitable for you then you will be able to perform the request directly without any additional services.
If additional functionality is required you would need to create a Lambda function that could perform the request to API Gateway. This Lambda would then be the trigger for the event leading to the same functionality being performed.
You can build a Lambda function that can use code to perform a POST request. Then you can use scheduled events to schedule when the Lambda function will be invoked. Using a CRON expression, you can schedule your Lambda to fire once a week. For details, see:
Schedule AWS Lambda Functions Using CloudWatch Events
I have an AWS Lambda function that takes in and processes logs from CloudWatch Logs that are sent to specific log groups. The thing is, I may need to add more triggers as more log groups are created. The only way I have found to create a trigger for a specific log group is to use the AWS Lambda console and the AWS CloudFront console. Is it possible to create a trigger for an AWS Lambda function programmatically? For instance, in some Java code?
Yes, one of the common ways of triggering server-less functions is using endpoints. I believe you can expose an API endpoint from the Function's console using a an API Gateway, and call this endpoint URL from your java code or whatever programmatic entity you wish.
Im assuming lambda is like the missing piece of the puzzle for a complete api request. So you create the apigateway and then write the lambda function which bridges the gap between taking a request and returning the output of the lambda function as the http response.
I've successfully followed guides on how to set up an API gateway that triggers AWS lambda to do something, but I still don't really understand what is being done.
How is the function def handler(event, context): being called by the aws apigateway? How does it get triggered and how is the output of handler sent back?
You do not need a Lambda "in the middle". Using Lambda Proxy integration in the API Gateway you can receive the full information about the request (endpoint URL, query parameters, etc) in your targeted Lambda event.
Have a look at the following Tutorial how to setup Lambda Proxy integration with API Gateway.
The tricky thing you should care about is the structure of the response that you will return from your lambda_handler. See the requirements here.
Answering the question of "how this happens"... In short, when an HTTP request comes to your API endpoint it is automatically routed to the mapped Lambda function. Behind the scenes a new container for the Function is spawned and your request comes to the event of the lambda_handler. API Gateway by default also creates a CloudFront distribution in front of itself to serve your requests more efficiently. Once your Lambda returns the response, API Gateway parses it and constructs the HTTP response out of it. The nice thing is that all of this is managed by AWS.
I currently have a Web hook that's calling AWS API Gateway -> AWS Lambda function proxy. I'd like to make the web hook more responsive and return an early reply while continuing processing in the Lambda.
I went ahead and did this early reply from the Lambda (Node v6.10) but it didn't appear to have improved responsiveness. Is API Gateway somehow waiting for the Lambda to finish executing despite having the response from the callback already?
The other idea is to post an SNS notification from Lambda and have a second Lambda listen and continue processing but would rather avoid that complication if there's a simpler way.
API Gateway currently only supports synchronous invocation (aka InvocationType: RequestResponse) of Lambda functions, so yes, it is waiting for the full response from the Lambda.
To support your use case, you could use SNS or an another intermediary AWS service like Kinesis, SQS, etc. But you could also do it with Lambda alone. Have the first Lambda function trigger a second Lambda function asynchronously with InvocationType: 'Event', this will achieve the effect you desire.
See this post for more details: https://stackoverflow.com/a/31745774/5705481
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.