Front-End Web & Mobile
Amplify Framework adds support for AWS Lambda Triggers in Auth and Storage categories
June 27, 2024: This blog post covers Amplify Gen 1. For new Amplify apps, we recommend using Amplify Gen 2. You can learn more about Gen 2 in our launch blog post.
The Amplify Framework is an open source project for building cloud-enabled mobile and web applications. Today, we’re happy to announce that you can set up AWS Lambda triggers directly from the Amplify CLI.
Using Lambda triggers, you can call event-based Lambda functions for authentication, database actions, and storage operations from other AWS services like Amazon Simple Storage Service (Amazon S3), Amazon Cognito, and Amazon DynamoDB. Now, the Amplify CLI allows you to enable and configure these triggers. The CLI further simplifies the process by providing you with trigger templates that you can customize to suit your use case.
The Lambda trigger capabilities for Auth category include:
- Add Google reCaptcha Challenge: This enables you to add Google’s Captcha implementation to your mobile or web app.
- Email verification link with redirect: This trigger enables you to define an email message that can be used for an account verification flow.
- Add user to a Amazon Cognito User Pools group: This enables you to add a user to an Amazon Cognito User Pools group upon account registration.
- Email domain filtering: This enables you to define email domains that would like to allow or block during sign up.
- Custom Auth Challenge Flow: This enables you add custom auth flow to your mobile and web application by providing a basic skeleton which you can edit to achieve custom authentication in your application.
The Lambda trigger for Storage category can be added when creating or updating the storage resource using the Amplify CLI.
Auth Triggers for Authentication with Amazon Cognito
The Lambda triggers for Auth enable you to build custom authentication flows in your mobile and web application.
These triggers can be associated with Cognito User Pool operations such as sign-up, account confirmation, and sign-in. The Amplify CLI provides the template triggers for capabilities listed above which can be customized to suit your use case.
A custom authentication flow using Amazon Cognito User Pools typically comprises of 3 steps:
- Define Auth Challenge: Determines the next challenge in the custom auth flow.
- Create Auth Challenge: Creates a challenge in the custom auth flow.
- Verify Auth Challenge: : Determines if a response is correct in a custom auth flow.
When you add auth to your Amplify project, the CLI asks you if you want to add capabilities for custom authentication. It generates the trigger templates for each step in your custom auth flow depending on the capability chosen. The generated templates can be edited as per your requirements. Once complete, you push your project using ‘amplify push’ command. For more information on these capabilities, refer to our documentation.
Here is an example of how you add one of these custom auth capabilities in your application.
Adding a new user to group in Amazon Cognito
Using Amazon Cognito User Pools, you can create and manage groups, add users to groups, and remove users from groups. With groups, you can create collections of users to manage their permissions or to represent different user types.
You can now use the Amplify CLI to add a Lambda trigger to add a user to a group after they have successfully signed up. Here’s how it works.
Creating the authentication service and configuring the Lambda Trigger
From the CLI, create a new Amplify project with the following command:
Next, add authentication with the following command:
The command line interface then walks you through the following steps for adding authentication:
The interface should then open the appropriate Lambda function template, which you can edit in your text editor. The code for the function will be located at amplify/backend/function/<functionname>/src/add-to-group.js.
The Lambda function that you write for this example adds new users to a group called STUDENTS when they have an .edu email address. This function triggers after the signup successfully completes.
Update the Lambda function add-to-group.js with the following code:
To deploy the authentication service and the Lambda function, run the following command:
Now, when a user signs up with an .edu email address, they are automatically placed in the STUDENTS group.
Integrating with a client application
Now that you have the authentication service up and running, let’s integrate with a React application that signs the user in and recognizes that the user is part of the STUDENTS group.
First, install the Amplify and Amplify React dependencies:
Next, open src/index.js and add the following code to configure the app to recognize the Amplify project configuration:
Next, update src/App.js. The code recognizes the user groups of a user after they have signed in and displays a welcome message if the user is in the STUDENTS group.
Now, if the user is part of the STUDENTS group, they will get a specialized greeting.
Storage Triggers for Amazon S3 and Amazon DynamoDB
With this release, we’ve also enabled the ability to setup Lambda triggers for Amazon S3 and Amazon DynamoDB. This means you can execute a Lambda function on events such as create, update, read, and write. When adding or configuring storage from the Amplify CLI, you now have the option to add and configure a storage trigger.
Resizing an image with AWS Lambda and Amazon S3
Let’s take a look at how to use one of the new triggers to resize an image into a thumbnail after it has been uploaded to an S3 bucket.
From the CLI, create a new Amplify project with the following command:
Next, add storage with the following command:
The interface then walks you through the add storage setup.
The CLI then generates a code template for the new Lambda function, which you can modify as needed. It will be located at amplify/backend/function/<functionname>/src/index.js.
Replace the code in index.js with the following code:
You can trace the execution of the code above in Amazon CloudWatch Logs on an event such as upload to the S3 bucket.
Next, install the GraphicsMagick library in the Lambda function directory. This ensures that you have the needed dependencies to execute the Lambda function.
To deploy the services, run the following command:
Next, visit the S3 console, open your bucket and upload an image. Once the upload has completed, a folder named thumbnails will be created and the resized image will be stored there.
To learn more about creating storage triggers, check out the documentation.
Feedback
We hope you like these new features! As always, let us know how we’re doing, and submit any requests in the Amplify Framework GitHub Repository. You can read more about AWS Amplify on the AWS Amplify website.