This is demo repository for my Using ALB and Cognito to add authentication to your apps blog post.
- Install dependencies
You can use mise to install all developer dependencies.
mise installThen, install NodeJS dependencies:
yarn-
Export AWS credentials to connect to the Shared Assets account in our console
-
Change the config values to fit your use-case
Change the src/config/config.dev.ts to fit your use case. The parameter samlMetadataUrl is optional, and if not provided, the authentication will be via Cognito User Pool; otherwise, if the parameter is passed, the authentication will be via SAML 2.0 using IAM Identity Center as IdP.
samlMetadataUrl parameter, then proceeding to add it. This will be explained in the following steps.
- Go to your application endpoint and check if it's redirecting you to log in via Cognito User Pool.
You can create a user in the user pool and validate that this user can actually log in to the application.
- Deploy the
DevWorkloadStack
yarn cdk deploy DevWorkloadStackAfter the deployment, go to the AWS Console > CloudFormation > DevWorkloadStack > Resources > Copy the Physical ID of the CognitoAuthUserPool resource. We are going to use it on step 8.
- Go to the AWS Identity Center
- Select Application of type SAML 2.0
- Configure the SAML 2.0 Application
- The
Application Start URLshould be set to:https://{appSubdomain}.{domainName} - The
Application ACS URLshould be set to:https://{appSubdomain}.{domainName}/saml2/idpresponse. The Cognito documentation specificies the/saml2/idpresponsepath. - The
Application SAML audienceshould be set to:urn:amazon:cognito:sp:{awsRegion}:{userPoolId}based on the Cognito documentation.
Make sure to substitute the variable in the brackets with the correct value:
{appSubdomain}: The value used in yoursrc/config/config.dev.tsfile{domainName}: The value used in yoursrc/config/config.dev.tsfile{awsRegion}: The value used in yoursrc/config/config.dev.tsfile (env.region).{userPoolId}: The value you copied on step 4.
Copy the IAM Identity Center SAML metadata URL to use in the following step.
- Edit attribute mapping
Map:
Subjectto${user:subject}withpersistentformat.emailto${user:email} withbasic` format.
- Deploy the stack with the
samlMetadataUrl
Add the samlMetadataUrl parameter to the src/config/config.dev.ts and then deploy the stack again:
yarn cdk deploy DevWorkloadStack- Assign the Application to the identity center user
- Go to your application endpoint and check if it's redirecting you to log in via Identity Center.
- Check if the application is accessible after the login







