Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Choose your framework/language

Gen1 DocsLegacy

Page updated Feb 13, 2025

関数のスケジューリング

Amplify は、自然言語または cron 式 を使用して、特定の間隔で実行するように関数をスケジュールする機能を提供しています。開始するには、defineFunctionschedule プロパティを指定してください。

注意: defineFunction でスケジュールを構成することは、カスタム関数 ではサポートされていません。

amplify/jobs/weekly-digest/resource.ts
import { defineFunction } from "@aws-amplify/backend";
export const weeklyDigest = defineFunction({
name: "weekly-digest",
schedule: "every week",
});

関数のスケジュールは Amazon EventBridge ルール によって駆動されており、以下のようなユースケースに活用できます。

  • 高パフォーマンスの投稿の「フロントページ」を生成する
  • 高パフォーマンスの投稿の週刊ダイジェストを生成する
  • 倉庫在庫の月次レポートを生成する

ハンドラーは EventBridgeHandler 型を使用して型付けできます。

amplify/jobs/weekly-digest/handler.ts
import type { EventBridgeHandler } from "aws-lambda";
export const handler: EventBridgeHandler<"Scheduled Event", null, void> = async (event) => {
console.log("event", JSON.stringify(event, null, 2))
}

注意: AWS Lambda 型は以下でインストールできます。

Terminal
npm add --save-dev @types/aws-lambda

スケジュールは単一の間隔、または複数の間隔の場合があります。

amplify/jobs/generate-report/resource.ts
import { defineFunction } from "@aws-amplify/backend";
export const generateReport = defineFunction({
name: "generate-report",
schedule: ["every week", "every month", "every year"],
});

スケジュールは省略形構文を使用して分または時間で実行するように定義することもできます。

amplify/jobs/drink-some-water/resource.ts
import { defineFunction } from "@aws-amplify/backend";
export const drinkSomeWater = defineFunction({
name: "drink-some-water",
schedule: "every 1h"
})

または複合して複雑なスケジュールを作成することができます。

amplify/jobs/remind-me/resource.ts
import { defineFunction } from "@aws-amplify/backend";
export const remindMe = defineFunction({
name: "remind-me",
schedule: [
// every sunday at midnight
"every week",
// every tuesday at 5pm
"0 17 ? * 3 *",
// every wednesday at 5pm
"0 17 ? * 4 *",
// every thursday at 5pm
"0 17 ? * 5 *",
// every friday at 5pm
"0 17 ? * 6 *",
]
})

自然言語を使用する

スケジュールは、毎日使う用語を使用して自然言語で記述できます。Amplify は以下の期間をサポートしています。

  • day は常に真夜中に開始します
  • week は常に日曜日の真夜中に開始します
  • month は常に月の初日の真夜中に開始します
  • year は常に年の初日の真夜中に開始します
  • m は分の場合
  • h は時間の場合

自然言語式の前には「every」を付けます。

amplify/jobs/drink-some-water/resource.ts
import { defineFunction } from "@aws-amplify/backend";
export const drinkSomeWater = defineFunction({
name: "drink-some-water",
schedule: "every 1h"
})

cron 式を使用する

スケジュールは cron 式を使用して記述できます。

amplify/jobs/remind-me/resource.ts
import { defineFunction } from "@aws-amplify/backend";
export const remindMe = defineFunction({
name: "remind-me-to-take-the-trash-out",
schedule: [
// every tuesday at 9am
"0 9 ? * 3 *",
// every friday at 9am
"0 9 ? * 6 *",
]
})