aws-lambdaaws-amplify

Amplify post-confirmation doesn't trigger lambda function


I am following this Amplify Gen 2 documentation to trigger a post-confirmation function. It should execute during the authentication flow once the user verifies their account and create a new UserProfile entity associated with the new user. After I deploy the changes the function is not being triggered. Below is my post-confirmation lambda set up.

  1. update define data model for user's profile in amplify/data/resource.ts
const schema = a.schema({ 
  UserProfile: a 
    .model({
      name: a.string().required(),
      profileOwner: a.string(),
      ...
    })
    .authorization(allow => [
      allow.ownerDefinedIn('profileOwner')
    ]
  ),
  ...
})
.authorization((allow) => [allow.resource(postConfirmation)]);
  1. create new directory and resource amplify/auth/post-confirmation/resource.ts and create defineFunction function
import { defineFunction } from '@aws-amplify/backend';

export const postConfirmation = defineFunction({
  name: 'post-confirmation',
});
  1. create handler file amplify/auth/post-confirmation/handler.ts
import type { PostConfirmationTriggerHandler } from "aws-lambda";
import { type Schema } from "../../data/resource";
import { Amplify } from "aws-amplify";
import { generateClient } from "aws-amplify/data";
import { getAmplifyDataClientConfig } from '@aws-amplify/backend/function/runtime';
import { env } from "$amplify/env/post-confirmation";

const { resourceConfig, libraryOptions } = await getAmplifyDataClientConfig(
  env
);

Amplify.configure(resourceConfig, libraryOptions);

const client = generateClient<Schema>();

export const handler: PostConfirmationTriggerHandler = async (event) => {
  await client.models.UserProfile.create({
      email: event.request.userAttributes.email,
      profileOwner: `${event.request.userAttributes.sub}::${event.userName}`,
  });

  return event;
};
  1. create new trigger in amplify/auth/resource.ts to customize authentication flow
export const auth = defineAuth({
  ...
  triggers: {
    postConfirmation
  }
});

According to the docs, "You can use defineAuth and defineFunction to create a Cognito post confirmation Lambda trigger to create a profile record when a user is confirmed." so the function should be triggered when the user registers and verifies their account which is not happening atm.

I've gone through a few similar questions found on SO however most are dated 3+ years old and are Amplify Gen 1 and there doesn't seem to be any relevant Gen 2 related information on GitHub either

Any ideas as to why the function is not being triggered during the authentication flow?


Solution

  • as @rw_ pointed out, my UserProfile.create in amplify/auth/post-confirmation/handler.ts and the UserProfile defined in amplify/data/resource.ts had different attributes. I updated the UserProfile model and changed name to email. This made the function work

    amplify/data/resource.ts

    const schema = a.schema({ 
      UserProfile: a 
        .model({
          email: a.string().required() // changed from 'name'
          ...
        })
        ...
    })
    ...