Build a Basic Web Application
TUTORIAL
Task 4: Link a Serverless Function to a Web App
In this task, you will update the Amplify Auth resources to use the created Lambda function.
Overview
In this task, you will update the Amplify Auth resources to use the Lambda function you created in the previous module as an Amazon Cognito post confirmation invocation. When the user completes the sign up, the function will use the GraphQL API and capture the user’s email into the DynamoDB table.
Key concepts
Lambda invocation: The type of event that will make a Lambda (serverless) function run. This can be another AWS service or an external input.
Implementation
Minimum time to complete
10 minutes
Services used
Get help
-
Step 1: Set up Amplify Auth
By default, your auth resource is configured allowing the user to sign up using email, but you need to update the resource to invoke the previously created postConfirmation function.
1. On your local machine, navigate to the amplify/auth/resource.ts file and update it with the following code. Then, save the file.
import { defineAuth } from '@aws-amplify/backend'; import { postConfirmation } from './post-confirmation/resource'; export const auth = defineAuth({ loginWith: { email: true, }, triggers: { postConfirmation } });
2. The sandbox will automatically get updated and redeployed once the file is updated. If the sandbox is not running, you can run the following command in a new terminal window to start it.
npx ampx sandbox
3. Once the cloud sandbox has been fully deployed, your terminal will display a confirmation message and the amplify_outputs.json file will be generated/updated and added to your profilesapp project.
Conclusion
You used Amplify to configure auth and configured the Lambda function to be invoked when the user signs in to the app.