AWS Compute Blog
Amazon Cognito for Alexa Skills User Management
This post is courtesy of Tom Moore, Solutions Architect – AWS
If your Alexa skill is a general information skill, such as a random facts skill or a news feed, you can provide information to any user who has an Alexa enabled device with your skill turned on. However, sometimes you need to know who the user is before you can provide information to them. You can fulfill this user management scenario with Amazon Cognito user pools.
This blog post will show you how to set up an Amazon Cognito user pool and how to use it to perform authentication for both your Alexa skill and a webpage.
Getting started
In order to complete the steps in this blog post you will need the following:
- An AWS account
- An Amazon developer account
- A basic understanding of Amazon Alexa skill development
This example will use a sample Alexa skill deployed from one of the available skill templates. To fully develop your own Alexa skill, you will need a professional code editor or IDE, as well as knowledge of Alexa skill development. It is beyond the scope of this blog post to cover these details.
Before you begin, consider the set of services that you will use and their availability. To implement this solution, you will use Amazon Cognito for user accounts and AWS Lambda for the Alexa function.
Today, AWS Lambda supports calls from Alexa in the following regions:
- Asia Pacific (Tokyo)
- EU (Ireland)
- US East (N. Virginia)
- US West (Oregon)
These four regions also support Amazon Cognito. While it is possible to use Amazon Cognito in a different region than your Lambda function, I recommend choosing one of the four listed regions to deploy your entire solution for simplicity.
Setting up Amazon Cognito
To set up Amazon Cognito, you’ll need to create a user pool, create an Alexa client, and set up your authentication UI.
Create your Amazon Cognito user pool
- Sign in to the Amazon Cognito console. You might be prompted for your AWS credentials.
- From the console navigation bar, choose one of the four regions listed above. For the purposes of this blog, I’ll use US East (N. Virginia).
- Choose Manage User Pools.
- Choose Create a user pool, and provide a name for your user pool. Remember that user pools may be used across multiple applications and platforms including web, mobile, and Alexa. The pool name does not have to be globally unique, but it should be unique in your account so you can easily find the pool when needed. I have named my user pool “Alexa Demo.”
- After you name your pool, choose Step through settings. You can accept the defaults for the remaining steps to set up your user pool, with the following exceptions:
- Choose email address or phone number as the sign-in method, and then choose Allow both email addresses and phone numbers.
- Enable Multi-Factor Authentication (MFA).You can use Amazon Cognito to enforce Multi-Factor Authentication for your users. Amazon Cognito also allows you to validate email and phone numbers when the user is created. The verification process for phone numbers requires that Amazon Cognito is able to access the Amazon Simple Notification Service (SNS) service in order to dispatch the SMS message for phone number verification. This access is granted through the use of an AWS Identity and Access Management (IAM) service role. The Amazon Cognito Setup process can automatically create this role for you.
- To set up Multi-Factor Authentication:
- Under Do you want to enable Multi-Factor Authentication (MFA), choose Optional.
- Choose SMS text message as a second authentication factor, and then choose the options you want to be verified.
- Choose Create Role, and then choose Next Step.
- For more information, see Adding Multi-Factor Authentication (MFA) to a User Pool.Because the verification process sends SMS messages, some costs will be incurred on your account. If you have not already done so, you will need to request a spending increase on your account to accommodate those charges. To learn more about costs for SMS messages, see SMS Text Messages MFA.
- Review the selections that you have made. If you are happy with the settings that you have selected, choose Create Pool.
Create the Alexa client
By completing the steps above, you will have created an Amazon Cognito user pool. The next task in setting up account linking is to create the Alexa client definition inside the Amazon Cognito user pool.
- From the Amazon Cognito console, choose Manage User Pools. Select the user pool you just created.
- From the General settings menu, choose App Clients to set up applications that will connect to your Amazon Cognito user pool.
- Choose Add an App Client, and provide the App client name. In this example, I have chosen “Alexa.” Leave the rest of the options set to default and choose Create App Client to generate the client record for Alexa to use. This process creates an app client ID and a secret.
To learn more, see Configuring a User Pool App Client.
Set up your Authentication UI
Amazon Cognito can set up and manage the Authentication UI for your application so that you don’t have to host your own sign-in and sign-up UI for your Alexa application.
- From the App integration menu, choose Domain name.
- For this example, I will use an Amazon Cognito domain. Provide a subdomain name and choose Check Availability. If the option is available, choose Save Changes.
Setting up the Alexa skill
Now you can create the Alexa skill and link it back to the Amazon Cognito user pool that you created.
For step-by-step instructions for creating a new Alexa skill, see Create a New Skill in the Alexa documentation. Follow those instructions, with the following specific selections:
Under Choose a model to add to your skill, keep the default option of Custom.
Under Choose a method to host your skill’s back end resources, keep the default selection of Self Hosted.
For a custom skill, you can choose a predefined skill template for the back end code for your skill. For this example, I’ll use a Fact Skill template as a starting point. The skill template prepopulates the Lambda function that your Alexa skill uses.
After you create your sample skill, you’ll need to complete a few basic operations:
- Set the invocation name of the skill
- Prepare a Lambda function to handle the skill invocation
- Connect the Alexa skill to your lambda
- Test your skill
A full description of these steps is beyond the scope of this blog post. To learn more, see Manage Skills in the Developer Console. Once you have completed these steps, return to this post to continue linking your skill with Amazon Cognito.
Linking Alexa with Amazon Cognito
To link your Alexa skill with Amazon Cognito user pools, you’ll need to update both the Amazon Cognito and Alexa interfaces with data from the other service. I recommend that you have both interfaces open in different tabs of your web browser to make it easy to move back and forth between the two services.
- In Amazon Cognito, open the app pool that you created. Under General Settings, choose App Clients. Next, choose Show Details in the section for the Alexa Client that you set up earlier. Make a note of the App client ID and the App client secret. These will be needed to configure Alexa skills app linking.
- Switch over to your Alexa developer account and open the skill that you are linking to Amazon Cognito. Choose Account Linking.
- Select the option to allow users to link accounts. Leave the default option for an Auth Code Grant selected.The
Authorization URI will be made up of the following template: - Replace the {Sub-Domain} with the sub domain that you selected when you set up your Amazon Cognito user pool. In my example, it was “mooretom-alexademo”
- Replace {Vendor ID} with your specific vendor ID for your Alexa development account. The easiest way to find this is to scroll down to the bottom of the account linking page. Your Vendor ID will be the final piece of information in the Redirect URI’s.
- Replace {Region} with the name of the region you are deploying your resources into. In my example, was us-east-1.
- The Access Token URI will be made up of the following template:
- Enter the app client ID and the app client secret that you noted above, or return to the Amazon Cognito tab to copy and paste them.
- Choose Save at the top of the page. Make a note of the redirect URLs at the bottom of the page, as these will be required to finish the Amazon Cognito configuration in the next step.
- Switch back to your Amazon Cognito user pool. Under App Integration, choose App Client Settings. You will see the integration settings for the Alexa client in the details panel on the right.
- Under Enabled Identity Providers, choose Cognito User Pool.
- Under Callback URL(s) enter in the three callback URLs from your Alexa skill page. For example, here are all three URLs separated by commas:
The Sign Out URL will follow this template:
- Under Allowed OAuth Flows, select Authorization code grant.
- Under Allowed OAuth Scopes, select phone, email, and openid.
- Choose Save Changes.
Testing your Alexa skill
After you have linked Alexa with Amazon Cognito, return to the Alexa developer console and build your model. Then log into the Alexa application on your mobile phone and enable the skill. When the skill is enabled, you will be able to configure access and create a new user with phone number authentication included automatically.
After going through the account creation steps, you can return to your Amazon Cognito user pool and see the new user you created.
Conclusion
By completing the steps in this post, you have leveraged Amazon Cognito as a source of authentication for your Amazon Alexa skill. Amazon Cognito provides user authentication as well as sign-in and sign-up functionality without requiring you to write any code. You can now use the Amazon Cognito user ID to personalize the user experience for your Alexa skill. You can also use Amazon Cognito to authenticate your users to a companion application or website.