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

Page updated May 2, 2024

Set up a Function

Amplify Functions are powered by AWS Lambda, and allow you to perform a wide variety of customization through self-contained functions. Functions can respond to events from other resources, execute some logic in-between events like an authentication flow, or act as standalone jobs. They are used in a variety of settings and use cases:

  • Authentication flow customizations (e.g. attribute validations, allowlisting email domains)
  • Resolvers for GraphQL APIs
  • Handlers for individual REST API routes, or to host an entire API
  • Scheduled jobs

To get started, create a new directory and a resource file, amplify/functions/say-hello/resource.ts. Then, define the Function with defineFunction:

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'
});

Next, create the corresponding handler file at amplify/functions/say-hello/handler.ts. This is where your function code will go.

amplify/functions/say-hello/handler.ts
export const handler = async (event) => {
// your function code goes here
return 'Hello, World!';
};

The handler file must export a function named "handler". This is the entry point to your function. For more information on writing functions, refer to the AWS documentation for Lambda function handlers using Node.js.

Lastly, this function needs to be added to your backend.

amplify/backend.ts
import { defineBackend } from '@aws-amplify/backend';
defineBackend({
});

Now when you run npx ampx sandbox or deploy your app on Amplify, it will include your backend function. See the examples below for connecting your functions to event sources.

Next steps

Now that you have completed setting up your first Function, you may also want to add some additional features or modify a few settings. We recommend you learn more about: