Page updated Nov 30, 2023


Coming soon: The Functions experience for Amplify (Gen 2) is still under development. You can add functions to your Gen-2 app with the AWS Cloud Development Kit (AWS CDK).

To use Functions with the preview version of Amplify (Gen 2), you must use the AWS CDK. In the following sections, we walk through installing the CDK library package and building an example backend function using the CDK.

To get started, install the CDK library package:

npm add --save-dev aws-cdk-lib
1npm add --save-dev aws-cdk-lib

Depending on the Node.js package manager you're using, you may encounter warnings where it is now unable to resolve the peer dependency version @aws-amplify/backend has on aws-cdk-lib. If you encounter a warning similar to the following, reinstall the version specified in the warning text:

npm WARN Could not resolve dependency: npm WARN peer aws-cdk-lib@"~2.103.0" from @aws-amplify/backend@0.4.0 npm WARN node_modules/@aws-amplify/backend npm WARN dev @aws-amplify/backend@"^0.4.0" from the root project
1npm WARN Could not resolve dependency:
2npm WARN peer aws-cdk-lib@"~2.103.0" from @aws-amplify/backend@0.4.0
3npm WARN node_modules/@aws-amplify/backend
4npm WARN dev @aws-amplify/backend@"^0.4.0" from the root project

Using the sample warning text above, you would need to install aws-cdk-lib@2.103.0.

Example - Create a Function trigger for Auth

You can use the NodejsFunction construct in the AWS CDK to create the Function definition, and apply it to the Amplify-generated Amazon Cognito resource as a Cognito trigger. This is made possible through the L2 UserPool construct and its provided .addTrigger() method.

First, create the trigger's shell of a Function handler by creating a file at amplify/custom/post-confirmation.ts with the following contents:

// amplify/custom/post-confirmation.ts import type { PostConfirmationTriggerHandler } from 'aws-lambda'; export const handler: PostConfirmationTriggerHandler = async (event) => { console.log('event: ', JSON.stringify(event)); };
1// amplify/custom/post-confirmation.ts
2import type { PostConfirmationTriggerHandler } from 'aws-lambda';
4export const handler: PostConfirmationTriggerHandler = async (event) => {
5 console.log('event: ', JSON.stringify(event));

Next, create the instance of NodejsFunction using the scaffolding below, and use the .addTrigger() method to set the Function on your Amazon Cognito resource.

// amplify/backend.ts import * as url from 'node:url'; import * as cognito from 'aws-cdk-lib/aws-cognito'; import * as lambda from 'aws-cdk-lib/aws-lambda-nodejs'; import { defineBackend } from '@aws-amplify/backend'; import { auth } from './auth/resource.js'; import { data } from './data/resource.js'; const backend = defineBackend({ auth, data }); // create function stack for the auth triggers const authTriggerStack = backend.createStack('AuthTriggerStack'); // create the PostConfirmation trigger const postConfirmationTrigger = new lambda.NodejsFunction( authTriggerStack, 'PostConfirmation', { entry: url.fileURLToPath( new URL('./custom/post-confirmation.ts', import.meta.url) ) } ); // add the newly created trigger to the auth resource const userPool = backend.resources.auth.resources.userPool as cognito.UserPool; userPool.addTrigger( cognito.UserPoolOperation.POST_CONFIRMATION, postConfirmationTrigger );
1// amplify/backend.ts
2import * as url from 'node:url';
3import * as cognito from 'aws-cdk-lib/aws-cognito';
4import * as lambda from 'aws-cdk-lib/aws-lambda-nodejs';
5import { defineBackend } from '@aws-amplify/backend';
6import { auth } from './auth/resource.js';
7import { data } from './data/resource.js';
9const backend = defineBackend({
10 auth,
11 data
14// create function stack for the auth triggers
15const authTriggerStack = backend.createStack('AuthTriggerStack');
17// create the PostConfirmation trigger
18const postConfirmationTrigger = new lambda.NodejsFunction(
19 authTriggerStack,
20 'PostConfirmation',
21 {
22 entry: url.fileURLToPath(
23 new URL('./custom/post-confirmation.ts', import.meta.url)
24 )
25 }
28// add the newly created trigger to the auth resource
29const userPool = backend.resources.auth.resources.userPool as cognito.UserPool;
31 cognito.UserPoolOperation.POST_CONFIRMATION,
32 postConfirmationTrigger