AWS API Gateway with CloudWatch Events - amazon-web-services

I've been tinkering with AWS API Gateway for creating a rest api for one of my projects.
I've managed to connect it to DynamoDB Queries and to launch Lambda functions quite easily.
However one of my ideas is to be able to create CloudWatch Event Rules though it but i have not been able to set this up yet.
I want to be able to create a new scheduled task for a lambda though api gateway. The scheduled task should be a cron task.
I've been reading the documentation but i feel stuck. I know that I can solve it using another lambda but I would like to avoid it as CloudWatch Events exists as one of the connected AWS Services.
Thanks

Related

AWS Lambda alternative on Oracle Cloud Infrastructure

We are currently using AWS Lambda for some of the services with the following flow.
A rails application (kubernetes) adds a message to SQS queue
Lambda function is invoked via SQS trigger
Lambda function adds the notification to SNS
SNS calls the configured https endpoint to notify the rails application of the status
This has been working well for us. The function takes about 15 seconds to run (for generating some pdf with headless-chrome)
Due to Geographical data security restrictions for a separate installation of our application, we are unable to use AWS and the only feasible option is to use Oracle Cloud Infrastructure (OCI). OCI has cloud functions and also a Queue service, however unlike AWS, OCI doesn't seem to have inbuilt integration between cloud functions and Queue service.
One of the solutions we have discussed in the team is to deploy a service in kubernetes to consume the messages from the OCI Queue and invoke the cloud function and send the results to Notifications service.
I would appreciate any inputs that can simplify this flow but also maintain the async nature and scalability.
Rather than using OCI Queues you can send the events using OCI Streaming with a single subscriber
then you can link Functions easily and Notification service is available
I guess that when you are talking about service in K8s is 24/24 7/7 service and don't want to manage it through HPA/VPA.
If so, you can use https://knative.dev or alternatives https://ramitsurana.github.io/awesome-kubernetes/projects/projects/#serverless-implementations

Serverless Cube.js - No messages published to SNS

Trying to turn a containerized Cube.js deployment into a serverless one. I’m getting Continue wait on every API call, and I notice the cubejsProcess lambda is never invoked. I checked SNS reachability by (successfully) publishing a message to it using the AWS SDK in the cubejs lambda. The cubejsProcess does get invoked with this inserted message. What might be preventing cubejs from publishing messages to SNS?
This is similar to this question, however, connectivity to SNS has been verified in this case.
I'd check IAM permissions to see if the cubejs Lambda's role. Does the role allow making requests to SNS?
Another thing that could be causing the issue is if the cubejs Lambda is in a VPC with no Internet access; calling any AWS API endpoint would fail since they all require an Internet connection.

How to call an API Gateway API once a week

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

Creating AWS Lambda Triggers Programmatically

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.

Trigger Lambda on deploy API in API Gateway

I'm trying to trigger a Lambda function when I click on deploy in the API-Gateway console to deploy API on a stage.
I already tried with cloudwatch rule, but there is no event patterns for API-Gateway deployment.
My questions are:
Is it possible to trigger a lambda function when I click on the deploy button on API-Gateway console?
If yes, how can I do that?
Thank you
Unfortunately, there is no straight forward way for achieving this.
CloudWatch rule will not help as there is no logging generated on API deployment.
The only thing left behind a deploy action is a CloudTrail event.
The best solution I could think for this involves Amazon EventBridge which is an event bus managed service provided by AWS.
In EventBridge you can create rules that collect specific events from various AWS services within (and beyond) your AWS account.
API Gateway is not one of these services, but CloudTrail is! (For reference here is a list of the EventBridge supported services)
An API deployment in API Gateway emits an event to CloudTrail which has CreateDeployment as event name and apigateway.amazonaws.com as event source. The event payload also includes data such as the restApiId, the stage, the IAM identity details of the deploying agent and more.
Note, that there is not much documentation around CloudTrail event schemas, but the event would look something like the one listed here
Now, we need to create an EventBridge rule that captures such CloudTrail events.
This is a very good, step by step, guide on how to do this.
For your use case, you need to choose API Gateway as the service name and add CreateDeployment as a Specific Operation as shown in the screenshot below:
Once the EventBridge rule is set up then you can directly attach it as a trigger in any Lambda function. See relevant tutorial.
Downsides
The above solution cannot be applied on the individual API level. The EventBridge rule will capture the deployments of all APIs of any stage in a specific region. Additional filtering has to be implemented within the lambda logic.
This will lead to unnecessary lambda executions if the solution is scoped for anything less than all the APIs of a region. However as we're talking about API deployments, the extra lambda execution cost will be negligible.