aws-amplify / amplify-backend

Home to all tools related to Amplify's code-first DX (Gen 2) for building fullstack apps on AWS
Apache License 2.0
123 stars 38 forks source link

Gen2: Scheduled Lambdas are not possible anymore #1491

Open jgo80 opened 3 weeks ago

jgo80 commented 3 weeks ago

Before opening, please confirm:

JavaScript Framework

Not applicable

Amplify APIs

Not applicable

Amplify Version

v6

Amplify Categories

function

Backend

Amplify Gen 2 (Preview)

Environment information

``` # Put output below this line ```

Describe the bug

In Gen 2 "Scheduled Lambdas" are not possible anymore. How do do it?

The Documentation introduces Functions to be capable of Scheduled jobs, but there is no hint how to achieve it.

The situation feels very unpleasant. The documentation clearly recommends switching from Gen 1 to Gen 2 and raises fears that Gen 1 will not be available forever. At the same time, fundamental functions are missing (for the moment?) in Gen 2.

Expected behavior

I would love to see something like this:

import { defineFunction } from '@aws-amplify/backend';
import * as events from '@aws-cdk/aws-events';

export const myScheduleFunction = defineFunction({
  name: 'schedule-function',
  schedule: events.Schedule.rate(cdk.Duration.hours(1)),
});

Reproduction steps

Feature is missing, no reproduction

Code Snippet

// Put your code below this line.

Log output

``` // Put your logs below this line ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

jgo80 commented 3 weeks ago

I managed it through CDK. Maybe you can adopt my suggested API for configuring Functions anyway 🙏?

import { defineBackend } from '@aws-amplify/backend';
import * as cdk from 'aws-cdk-lib';
import * as events from 'aws-cdk-lib/aws-events';
import * as targets from 'aws-cdk-lib/aws-events-targets';
import { auth } from './auth/resource';
import { data } from './data/resource';
import { scheduledfunction } from './functions/scheduled-function/resource';

const backend = defineBackend({
  auth,
  data,
  scheduledfunction,
});

// Create a new EventBridge rule that is triggered every hour
const rule = new events.Rule(
  backend.scheduledfunction.resources.lambda,
  'Rule',
  {
    description: 'Rule that triggers the lambda function every hour',
    schedule: events.Schedule.rate(cdk.Duration.hours(1)),
  },
);

// Add the lambda function as a target for the rule
rule.addTarget(
  new targets.LambdaFunction(backend.scheduledfunction.resources.lambda),
);
ykethan commented 3 weeks ago

Hey @jgo80, thank you for reaching out. Marking this as feature request.

MarlonJD commented 2 weeks ago

I managed it through CDK. Maybe you can adopt my suggested API for configuring Functions anyway 🙏?

import { defineBackend } from '@aws-amplify/backend';
import * as cdk from 'aws-cdk-lib';
import * as events from 'aws-cdk-lib/aws-events';
import * as targets from 'aws-cdk-lib/aws-events-targets';
import { auth } from './auth/resource';
import { data } from './data/resource';
import { scheduledfunction } from './functions/scheduled-function/resource';

const backend = defineBackend({
  auth,
  data,
  scheduledfunction,
});

// Create a new EventBridge rule that is triggered every hour
const rule = new events.Rule(
  backend.scheduledfunction.resources.lambda,
  'Rule',
  {
    description: 'Rule that triggers the lambda function every hour',
    schedule: events.Schedule.rate(cdk.Duration.hours(1)),
  },
);

// Add the lambda function as a target for the rule
rule.addTarget(
  new targets.LambdaFunction(backend.scheduledfunction.resources.lambda),
);

That's what I exactly what I was looking into. Thank you so much for sharing this. BTW. This feature request would be very helpful in future.

mtliendo commented 2 weeks ago

Worth noting that gen1 used Cloudwatch Events. This feature has been sunset by AWS. Trying to create a Cloudwatch event in the AWS console will now redirect customers to create an EventBridge rule. As such, if just wanting a way to mirror the behavior found in gen1, the answer provided by the OP is the solution.

For a more feature rich experience in creating scheduled tasks, either one time or recurring, EventBridge Scheduler is a better solution. This blog posts explains the differences: https://aws.amazon.com/blogs/compute/introducing-amazon-eventbridge-scheduler/