Task 4: Link a Serverless Function to a Web App - Build a Basic Web Application

Task 4: Link a Serverless Function to a Web App

Minimum time to complete

10 minutes

Services used

AWS Amplify

Get help

Troubleshooting Amplify

Learn about Auth

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

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. Update the resource file

    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 } });
    The resource creation interface.
  2. Start the sandbox, if necessary

    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. View the confirmation message

    Once the cloud sandbox has been fully deployed, your terminal will display a confirmation message.

    Interface element requiring manual review.
  4. View the file

    The amplify_outputs.json file will be generated/updated and added to your profilesapp project.

    Interface element requiring manual review.

Conclusion

You used Amplify to configure auth and configured the Lambda function to be invoked when the user signs in to the app.