amplify-backend icon indicating copy to clipboard operation
amplify-backend copied to clipboard

Gen2: Scheduled Lambdas are not possible anymore

Open jgo80 opened this issue 1 year ago • 4 comments

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 avatar May 10 '24 14:05 jgo80

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),
);

jgo80 avatar May 10 '24 14:05 jgo80

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

ykethan avatar May 10 '24 16:05 ykethan

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.

MarlonJD avatar May 14 '24 13:05 MarlonJD

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/

mtliendo avatar May 19 '24 01:05 mtliendo

Suggestion: Add the preferred pattern for scheduled lambda execution in Gen 2 to the documentation site, please.

designbystephen avatar Jun 24 '24 20:06 designbystephen

it's not even in the default typescript function in defineFunction. I don't think it'll be in near future because they tagged other functions as p4, I don't sure if it's same priority with that but after merged my pr about other functions, I can try to add schedule functions to gen 2 library.

MarlonJD avatar Jun 25 '24 19:06 MarlonJD