It's a fairly common scenario where you want to secure a web application in the cloud; but with so many technologies and different ways to secure your applications, it can be difficult to know where to get started. You may have questions, such as:
At Mechanical Rock, two of our pillars of excellence are
We leverage best of breed cloud native and managed services to build high quality, cutting edge solutions. Delivering these solutions using Infrastructure-As-Code (IAC) is not simply part of our commitment to architecting steadfast continuous integration workflows; it underpins the way we collaborate and share learnings with the client teams we engage with.
Most large organisations invest in single sign on capabilities for their colleagues, which is typically good for security practice - provide one authentication method, and make it strong, with password rotations and multi-factor authentication.
The solution we will focus on here is a pattern we commonly use when working with large enterprise clients, that have an established identity provider:

Our example assumes a web application running on ECS or EC2 or similar, but in reality it can be anything that can update a Load Balancer Target Group.
Summarising the solution:
Whilst AWS Cognito is a powerful security product, it is not without some significant shortcomings. The inability to export or backup User Pool users is exacerbated by the ease with which the whole user pool can be replaced (and thus, users destroyed) by simple Cloudformation changes.
DevOps practitioners have also been left wanting by Cognito's poor cloudformation support. With virtually no updates since 2017, current Cloudformation support covers perhaps 50% of what CLI and SDK users can do with Cognito, leaving users to write their own custom resources for common Cognito use cases.
This brings us to reason for this post - and the supporting repository - to provide an infrastructure-as-code solution for setting up and managing an AWS ALB, Cognito and Azure AD single sign on.
The repository helps you setup the following:
The flow of setting up single sign on tends to go as follows:

A notable pain point is that when ever you need to amend the attributes associated with your user pool, the whole user pool must be recreated, meaning a new user pool ID is generated. This means that the Entity Id within your Azure Enterprise Application - which contains the user pool ID - must be updated. If you are lucky, this is something you can do yourself, but that is unlikely to be the case in most large organisations.
Typical attributes to configure in a user pool will be a principal name and email. Ensure that they are configured with mutable set to true so that Cognito can continue to synchronise the dynamic users it creates, with those users in Azure Active Directory. It's often a good idea to also include the groups attribute.
It's an unfortunate trait of Cognito that it is not possible to amend custom attributes once created - including their mutable flag - so it's good to understand what attributes you need as early as possible, to avoid having to re-create the Cognito User Pool over and over.
When using Cognito as a facilitator for single sign on with Azure Active Directory, you will probably want to use the principal's directory based security groups. Note that it is also possible to manage your own user groups in Cognito, though many organisations prefer to manage their authentication and authorisation with a single identity provider.
Adding custom attributes is as simple as defining them in your Cognito User Pool. From there, you can add mappings to them from your identity provider's available claim types - these are available in the metadata xml file provided by Azure AD when your Enterpise Application is created. Remember - make your cognito attributes mutable if you want to map them from your identity provider.
There can be various stumbling blocks along the way when bringing together a single sign on solution with Cognito and Azure AD. Here are some things to watch out for:
Once you've made this work once, you'll find it easy to share among your team and customers in the future, and repeat over and again.