CDK, Amplify, User Pool Hell.... lost in a sea of confusion.

Hey,
I’ve created some basic test projects via the AWS dashboard/CLI … great…

Then..
I decided to try and embrace using the AWS CDK (v2) for my new project. Thinking I could do it all from the CDK and embrace the code as infrastructure approach. Which suits me as I prefer code anyway.

Simple Goal:
Create a single page web site with a Amplify React login/signup form, linked to cognito pool. Which I could then expand on.

Success & Failures: Success:
CDK successfully deploys and creates Cognito Pool.

Problems:
I did have it also create the Amplify App as well, but despite making the User/Client Pool IDs made available, it would not link it up to the User Pool.

I could try and enable it via the backend in the in the AWS dashboard but again that then risks drift and breaking the CDK approach.

Approach 2:
I use the Amplify CLI (amplify init) and create the amplify app this way.
But how do I get it to use the same pool as created by CDK. (I’ve tried and it just keeps creating new pools).

Failures:

It feels like, despite watching/reading and testing a thousand things. I’m still misunderstanding how this should work and it’s probably bloody obvious. :-).

I could of course choose not use CDK and do it via the dashboard or CLI but that seems to defeat the point of getting to grips with the CDK approach and making something more future proof and learning some new skills along the way.

Been at this simple task for days… and despite learning lots, I’m still lost in the weeds/sea (banging my head against a frustrating brick wall of failures. :slight_smile:

But how do I get it to use the same pool as created by CDK. (I’ve tried and it just keeps creating new pools).

When you call Amplify.configure in your app, you can populate the values yourself and use your own userpool id from CDK. You dont need to use the amplify CLI.

The only thing you need from Amplify is the JS SDK which you use in your frontend app, you can also use Amplify hosting.

Make a userpool in CDK => export the value into your frontend project => use it as a parameter in `Amplify.configure` https://docs.amplify.aws/react/build-a-backend/auth/set-up-auth/#set-up-and-configure-amplify-auth

I have a lot of experience with aws and cdk and I couldn’t get this working either. In the end I had to skip amplify, wasnt working with my gatsby app on so many levels. Im doing auth through api gateway to lambda to cognito…

I do this as well and deploy with Terraform.

Ah that’s oddly good to hear.

As I’m really still finding my feet with this all, I was convinced it was 100% something I was doing wrong and had such high hopes for fully embracing CDK. Onwards!!!

I managed to trial and error my way through deploying an integrated cognito, api gw and lambda setup using CDK. DM if you’d like the code.

This combination of services is one of the biggest dumpster fires in AWS. So much overengineered garbage. And I say this as someone who still marvels at the magic of some of their OG services like S3.

The biggest pain for me was the CORS configuration. Here’s some of my code if it helps. It assumes you already have Cognito set up.

https://github.com/rmchayes/share-stuff.git

Use parameter store in your cdk to store userpool details and the client keys, then in your amplify, access your secrets in the param store. then in your front end, create a script thatbwill create the amplify config file based on your secrets and run it as part of your build.

Would you mind sharing publicly, or DMing me with it please?

Have sent a DM

I agree Cognito is garbage, a hot mess to set up and all AWS documentation is wrong about something, to varying degrees. For the others, I find them simple - once I have got the deployment working exactly how I like it (which is sometimes a little fiddly at first) I save that component and reuse it in all other deployments and projects.

Thanks for commenting… still slightly confused…

So.

  1. I have my CDK, successfully created it’s Cognito User Pools + values exported to frontend.
  2. Amplify app created and linked to Github repo. (Loads as expected)
  3. I want to link this to the CDK created cognito user pool.

Looking at the link you shared, (thankyou) . In ‘using existing resources’ section, it says to use the Amplify.configure method, and the example shows all the config code being shared via an app.js file.

Is that correct? Seems odd to be making that info available this way?

Should I be using an amplifyconfiguratin.json file? (i.e. the sort created if you do, create an auth via the Amplify CLI).

Feels like I’m close to understanding and achieving it, but the last step is proving tricky to grasp. Seems like such a simple thing to achieve… link x to y.

Thanks for any help.

I want to link this to the CDK created cognito user pool.

There is no link. The amplify JS SDK is not a backend, or hosting provider, its simply a javascript library that provides utils to interact with the cognito API (amongst others). It has nothing to do with the Amplify CLI.

Looking at the link you shared, (thankyou) . In ‘using existing resources’ section, it says to use the Amplify.configure method, and the example shows all the config code being shared via an app.js file.

Is that correct? Seems odd to be making that info available this way?

Thats correct, I’d suggest moving the variable declaration elsewhere, which makes it easier to switch between environments based on env vars etc. But IMO much more flexible by having the configuration in typescript rather than JSON / YAML.

Should I be using an amplifyconfiguratin.json file? (i.e. the sort created if you do, create an auth via the Amplify CLI).

The `amplifyconfiguration.json` file is another way of passing in config values to the JS SDK, thats what the Amplify CLI will populate. You can also make a json file and do it that way if you want, adding in your CDK values, but there is less type safety and you wont know what the field names need to be without seeing an Amplify CLI example.
Its like this because its easier for a CLI tool to just make a JSON file rather than populate the values in the app.ts file.

AWS does a terrible job of explaining this, but “Amplify” is not 1 product. There is Amplify Hosting, Amplify Backend (CLI), and the Amplify JS SDK (and others), and they are all separate products and independent of each other.

Sidenote: if youre also using amplify hosting thats defined in CDK, you can populate the env vars using your userpool construct which helps automate different environments.

Thanks for the reply… It’s greatly appreciated.

I’ve spent more days/hours on this… and despite feeling like I’m close at times it still fails.

  • I successfully have my User/Client ID’s stored in AWS System Manager Parameter store via the CDK.

  • Created new Amplify App.

  • Added a get-configuration.js file that retreives the values from the Parameter store.

  • updated my package.json to using the get-configuration.js file, when I run ‘npm run build’ . So it creates a aws-exports.js file with the correct region, userPoolId and userPoolWebClientId.

Load the aws-exports into my main app.js file i.e. with

import awsExports from ‘./aws-exports’;

Amplify.configure(awsExports);

Run ‘amplify push’ in terminal but it detects no changes.

Try and run my simple authenticator page, but it fails… with the below error seen in the console:

Uncaught TypeError: Cannot read properties of undefined (reading ‘loginWith’)

at AmplifyClass.notifyOAuthListener (Amplify.ts:69:1)

at AmplifyClass.configure (Amplify.ts:47:1)

at Object.configure (initSingleton.ts:47:1)

at ./src/App.js (App.js:11:1)

at options.factory (react refresh:6:1)

at __webpack_require__ (bootstrap:22:1)

at fn (hot module replacement:61:1)

at ./src/index.js (aws-exports.js:8:1)

at options.factory (react refresh:6:1)

at __webpack_require__ (bootstrap:22:1)

I can’t find info related to this, but despite not requiring Oauth it seems t be expecting information around it.

(I’ve temporarily added aws exports to my console and can see the correct info is being passed to the app. i..e User and Client Pool id’s).

you cant use parameter store for this, the SDK needs to be able to retrieve the values client side and server side. You shouldnt use SSM client side, you can just use an environment variable (`NEXT_PUBLIC` prefix if youre using nextjs), or hardcode it into the aws-exports file. No need to use SSM here, none of the values are private.

And, you dont need to use the amplify CLI at all, not sure what you are trying to achieve with amplify push. Its saying no changes because nothing exists in the Amplify backend, your userpool is unrelated since its on CDK, your Amplify.configure is just an SDK, and theres no reason to import the userpool into amplify. You dont need to create an amplify app, you can just use the SDK on its own.

Thanks again… ok maybe this is where I’m getting completely and utterly confused. Too many tutorials, too many different approaches, so many different layers upon layers of varying complexity offered by AWS.

Ultimately… I wanted to use CDK to manage the backend as that sounded useful and some what future proof.

On my front end I want to be able to design webpages, and have users singup/sign in via a portal (hence the amplify authenticator approach). I assumed that to have authorisation it would need to be an amplify app, with access to the pools created via y CDK, to allow me to tie it all together as the planned web app develops over time.

On my front end I want to be able to design webpages, and have users singup/sign in via a portal (hence the amplify authenticator approach). I assumed that to have authorisation it would need to be an amplify app, with access to the pools created via y CDK, to allow me to tie it all together as the planned web app develops over time.

it doesnt need to be an amplify app, forget the amplify CLI. The frontend just needs to use the Amplify SDK library, which needs a userpool id.

On an infrastructure level there is no link between the userpool CDK construct, and the amplify app. The SDK in the frontend just needs a value for the userpool ID.

You create a userpool in CDK, and when you call Amplify.configure - give it the userpool ID of the CDK created userpool. That is all you need, no CLI involved since youre managing the backend with CDK.

https://aws.amazon.com/blogs/mobile/announcing-aws-amplifys-graphql-api-cdk-construct-deploy-real-time-graphql-api-and-data-stack-on-aws/ - this guide is related to appsync and not cognito but the principal is the same. They are creating the API in CDK, and then they are using the Amplify client library in the frontend that needs the URL of the API (amongst some other values), and that is all. No CLI or “Amplify backend” involved

Thanks I’ll take a look.

After a good long walk :wink: …

I’m assuming I’m still missing the simplest of understandings on something here. From everything I’d read/watched/consumed the Amplify CLI was a really powerful tool and one worth integrating into development, and an nice way to get away from using the AWS console… especially if the CDK was struggling to do what I was hoping it could.

I’ll check out your link and go from there… Thanks again for the help.

API Gateway is insanely over engineered but I’m not going to argue about it. :slight_smile: