If you have implemented Amazon Cognito As a Service, chances are you may develop other services that depend on it. The best way to manage this kind of dependency is to export your Amazon Cognito user pool.
For this article, I am going to assume that you have already read my post: Amazon Cognito As a Service – Setting Up Your serverless.yml File. If you have not read this article, you might want to review it before proceeding. The reason for this suggestion is that it’s not difficult to export your Amazon Cognito user pool, but you’ll want to do so in the same serverless.yml file in which you define your Cognito user pool.
Why Export your Amazon Cognito User Pool?
It is not at all required that you export your Amazon Cognito user pool. In fact, it is perfectly acceptable that you might create many Amazon Cognito user pools and never have a need to export them. When considering if you need to export yours, however, the question that needs to be asked is: does one or more of my other services depend on my Cognito user pool? If the answer is “no,” then there is nothing further needed. If the answer is “yes”, then read on.
How to Export Your Amazon Cognito User Pool
Example # 1
https://gist.github.com/kevinchisholm/88bd7a3ea504010b3890c61aea777aed
In the above code example, we have all the steps needed in order to export your Amazon Cognito user pool. I won’t take up time reviewing lines 1-25. If you have any questions about what is happening there, I suggest you review my previous article: Amazon Cognito As a Service – Setting Up Your serverless.yml File.
Starting on line 27, we have the steps needed to export your Amazon Cognito user pool. In the Outputs section, we export two things: the ID of the user pool, and the ID of the user pool client. Notice that this Outputs section is a property of resources.Resources.
And finally, for both outputs, in order to consume them in any services that need them, look at the Export property. The value that’s set tells you how to reference the output. So for example, if you want to consume the value of Outputs.UserPoolId, you would reference: UserPoolId-my-user-pool-dev or UserPoolId-my-user-pool-prod, etc. And if you want to consume the value of Outputs.UserPoolClientId, you would reference: UserPoolClientId-my-user-pool-dev or UserPoolClientId-my-user-pool-prod, etc.
How to Consume Your Exported Amazon Cognito User Pool
Example # 2
https://gist.github.com/kevinchisholm/ef594a2814974e3d81a0985f3e63d7cc
In Example # 2, we have a very simple service that sets two environment variables: UserPoolId and UserPoolClientId. In both cases, we use the !ImportValue syntax to get the value of the property that was exported from our User Pool Service.
Summary
In this article we saw that it’s not difficult to export your Amazon Cognito user pool; you just need to do so in the same serverless.yml file in which you define your Cognito user pool. We hope that you have enjoyed reading about this service and that you’ve learned something new. If you have and it has made your coding easier and more efficient, please share this article on Facebook and Twitter.