Adding Cognito User Management to Your Serverless App


In this video we’ll learn to set up a new Cognito User Pool and a User Pool Client. This will allow us to build a React app using AWS Amplify that has signup, login, logout and API security build it which we’ll be doing in the next video.

Amazon Cognito is a service that lets you add user sign-up, sign-in, and access control to your web and mobile apps quickly and easily. We’ll be setting up the back end so that the next video we can create a react app with built in signup, login and authentication.

To start we need to head our serverless.yml file and to the Resources section. In here we need to add two new resources.

CognitoUserPool:
        Type: AWS::Cognito::UserPool
        Properties:
            UserPoolName: ${self:provider.stage}-user-pool
            UsernameAttributes:
                - email
            AutoVerifiedAttributes:
                - email
                
    CognitoUserPoolClient:
        Type: AWS::Cognito::UserPoolClient
        Properties:
            ClientName: ${self:provider.stage}-user-pool-client
            UserPoolId:
                Ref: CognitoUserPool
            ExplicitAuthFlows:
                - ADMIN_NO_SRP_AUTH
            GenerateSecret: false

The first part of this is the user pool itself. This is what is going to contain all of the users for this app. I’ve used the stage to make sure I deploy a new user pool for dev, UAT, and production branches.

We’ve also set their username to be their email address. This is probably the easiest thing to go with for now.

Next, we need to make a client for that user pool. A client is just an identifier for the app that we’re going to be building in the next video. You could have multiple clients if you wanted multiple apps to share the same user pool.

In this client, we’re setting the UserPoolId to Ref: CognitoUserPool and this will automatically put the aren’t from the user pool into that field for us.

With all of this done we can deploy our new Cognito services with sls deploy. Once that is done we can head over to our AWS account and see what we’ve got.

If we go into the Cognito service and click Manage User Pools then we can see that we have one new pool. Clicking into that we see all of the details we expect. We can also click into App clients and see that there is a single client, named as we would expect. This has an App client id which we’ll be needing for the next video where we’ll be setting up our React app with AWS Amplify.

Sam Williams

Sam is a Serverless Obsessive who runs Complete Coding, helping developers learn Serverless and companies make the most of the competitive advantage that Serverless gives them. Previous projects include: - Designing a chat platform that currently resolves over 250,000 customer inquiries a month for international retailers and local government - Architecting a backend system to support a 3D clothing visualisation tool - Building a solution to manage millions of dollars of communication software - Designing a "Video Editing in the Cloud" platform, enabling everyone from movie studios to indie film makers to harness the power of the Cloud - without needing to be cloud experts. - Launching a profitable Serverless Startup in under 30 days He has also been teaching cloud-based software development for 5 years and has taught Serverless development to thousands of people. The Complete Coding Youtube channel now has over 15,000 subscribers and over 1 million views

Recent Posts