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 Mar 26, 2025

Function をセットアップする

Amplify Functions は AWS Lambda によって実装されており、自己完結型の 関数 を通じて多くのカスタマイズを実行することができます。関数は他のリソースからのイベントに応答したり、認証フローなどのイベント間で何らかのロジックを実行したり、スタンドアロンジョブとして機能したりすることができます。これらは様々な設定とユースケースで使用されます:

  • 認証フローのカスタマイズ (例: 属性検証、メールドメインのホワイトリスト)
  • GraphQL API のリゾルバー
  • 個別の REST API ルートのハンドラー、または API 全体をホストするためのハンドラー
  • スケジュール済みジョブ

開始するには、新しいディレクトリとリソースファイル amplify/functions/say-hello/resource.ts を作成します。その後、defineFunction で Function を定義します:

amplify/functions/say-hello/resource.ts
import { defineFunction } from '@aws-amplify/backend';
export const sayHello = defineFunction({
// optionally specify a name for the Function (defaults to directory name)
name: 'say-hello',
// optionally specify a path to your handler (defaults to "./handler.ts")
entry: './handler.ts'
});

次に、amplify/functions/say-hello/handler.ts に対応するハンドラーファイルを作成します。ここに関数コードが入ります。

amplify/functions/say-hello/handler.ts
import type { Handler } from 'aws-lambda';
export const handler: Handler = async (event, context) => {
// your function code goes here
return 'Hello, World!';
};

ハンドラーファイルは「handler」という名前の関数をエクスポートする 必要があります。これは関数のエントリーポイントです。関数の記述の詳細については、Node.js を使用した Lambda 関数ハンドラーに関する AWS ドキュメントを参照してください。

最後に、この関数をバックエンドに追加する必要があります。

amplify/backend.ts
import { defineBackend } from '@aws-amplify/backend';
import { sayHello } from './functions/say-hello/resource';
defineBackend({
sayHello
});

npx ampx sandbox を実行するか、Amplify でアプリをデプロイすると、Function が含まれます。

Function を呼び出すには、Function を Amplify Data リソースのカスタムクエリのハンドラーとして追加することをお勧めします。これにより、Function の引数とリターンステートメントに強い型付けを行い、これを使用して Function のビジネスロジックを作成することができます。開始するには、amplify/data/resource.ts ファイルを開き、スキーマで新しいクエリを指定します:

amplify/data/resource.ts
import { type ClientSchema, a, defineData } from "@aws-amplify/backend"
import { sayHello } from "../functions/say-hello/resource"
const schema = a.schema({
sayHello: a
.query()
.arguments({
name: a.string(),
})
.returns(a.string())
.authorization(allow => [allow.guest()])
.handler(a.handler.function(sayHello)),
})
export type Schema = ClientSchema<typeof schema>
export const data = defineData({
schema,
authorizationModes: {
defaultAuthorizationMode: "iam",
},
})

これで、Schema エクスポートからこのクエリを使用して Function ハンドラーに強い型付けを行うことができます:

amplify/functions/say-hello/handler.ts
import type { Schema } from "../../data/resource"
export const handler: Schema["sayHello"]["functionHandler"] = async (event) => {
// arguments typed from `.arguments()`
const { name } = event.arguments
// return typed from `.returns()`
return `Hello, ${name}!`
}

最後に、データクライアントを使用して、関連するクエリを呼び出すことで Function を呼び出します。

src/main.ts
import type { Schema } from "./amplify/data/resource"
import { Amplify } from "aws-amplify"
import { generateClient } from "aws-amplify/api"
import outputs from "./amplify_outputs.json"
Amplify.configure(outputs)
const client = generateClient<Schema>()
client.queries.sayHello({
name: "Amplify",
})

次のステップ

最初の Function のセットアップが完了したので、追加の機能を追加したり、いくつかの設定を変更したりすることもお勧めします。以下について詳しく学ぶことをお勧めします: