In this example, we'll create a Cron job.
The Cron Job will use AWS EventBridge's scheduled rules to trigger a job in a lambda Lambda Functions.
Then we'll deploy and test the application.
To see the completely application, go here.
yarn create functionless
For a more detailed guide, see: https://functionless.org/docs/getting-started/setup
Replace your app.ts with (the imports will be useful later):
import { App, Duration, Stack } from "aws-cdk-lib";
import { Schedule } from "aws-cdk-lib/aws-events";
import { EventBus, Function } from "functionless";
const app = new App();
const stack = new Stack(app, "functionlessCron");
EventBus.schedule(stack, "cron", Schedule.rate(Duration.minutes(1)));This create a rule which executes any targets every minutes. Lets give it something to do.
A scheduled rule is just an Event Bridge rule and an event bridge rule can trigger many different things. For now, we'll trigger a lambda function.
We are going to use an HTTP API, so we need a library to make the http call, lets use node-fetch.
yarn add node-fetch @types/node-fetch
Add this line under the other imports
import fetch from "node-fetch";Then add this function below that.
const job = new Function(stack, "cronJob", async () => {
const metaphors = await fetch("http://metaphorpsum.com/sentences/5").then(
(x) => x.text()
);
console.log(metaphors);
});When this Function is invoked, it will call the metaphorsum api to get some text and then we'll print it out.
Finally, we need to trigger the function using the scheduled rule.
Update the line with EventBus.schedule... to this:
EventBus.schedule(stack, "cron", Schedule.rate(Duration.minutes(1))).pipe(job);Notice the .pipe(job); at the end. Now when our rule is triggered, every minute, the lambda will call the metaphor api and log the results.
But wait, when do I create policies and permissions for my resources to talk to each other?
Functionless handles this automatically. When you .pipe() to your lambda functions, a policy is created that grants access for EventBus to call Lambda. likewise, a policy is created to allow API Gateway to call PutEvents on the event bus used in AwsMethod.
To deploy, run:
yarn deploy
For a more in depth overview of the deployment process, see deploy project.
We can look at the lambda logs to watch our cron in action.
- Go the lambda UI
- Find search for "cronJob"
- click on the function name
- click on monitoring
- click on view cloudwatch logs
There should be a log stream, in that log stream you should see the metaphors the api returns.
Every minute a new set of metaphors will be displayed.
Try another example or visit the Functionless documentation to learn about more of what Functionless can do.