Store generated user pool id into environment variable

Hi All,

I have a yml resources file which creates a cognito user pool. Then from another serverless.yml file need to store ther user pool id created into an environment variable called USER_POOL_ID.

My cognito-user-pool.yml is:

Resources:
  CognitoUserPool:
    Type: AWS::Cognito::UserPool
    Properties:
      # Generate a name based on the stage
      UserPoolName: ${self:custom.root.serviceName}-${self:custom.root.stage}-${self:custom.root.cognito.USER_POOL_NAME}
      # Set email as an alias
      UsernameAttributes:
        - email
      AutoVerifiedAttributes:
        - email

  CognitoUserPoolClient:
    Type: AWS::Cognito::UserPoolClient
    Properties:
      # Generate an app client name based on the stage
      ClientName: ${self:custom.root.serviceName}-${self:custom.root.stage}-${self:custom.root.cognito.USER_POOL_CLIENT_NAME}
      UserPoolId:
        Ref: CognitoUserPool
      ExplicitAuthFlows:
        - ADMIN_NO_SRP_AUTH
      GenerateSecret: false

Outputs:
  UserPoolId:
    Value:
      Ref: CognitoUserPool
    Export:
      Name: ${self:custom.root.serviceName}-${self:custom.root.stage}-UserPoolId

  UserPoolClientId:
    Value:
      Ref: CognitoUserPoolClient
    Export:
      Name: "UserPoolClient::Id"

And then I am referencing the exported output as follows in serverless.yml

USER_POOL_ID:
      "Fn::ImportValue": "${self:custom.root.serviceName}-${self:custom.root.stage}-UserPoolId"

Any help is highly appreciated!

Thanks in advance guys

If you have two separate stacks (.yml files) that interact, one of them will need to have the environment variable hard coded, or you need to use a much more complicated set up where the 2nd .yml is (re)generated when the first is deployed.

We just do the first option.

Hi @TomC,

Thanks for your reply. I am trying to create a user pool dynamically in cognito-user-pool.yml, of which I don’t know the id until it is created at deploy time. ie:

Stack Outputs
UserPoolClientId: xxxxxx
UserPoolId: xxxxxx

So what I am trying to do is to get the contents of UserPooId and map it to an environment variable in my serverless.yml file (The one that includes cognito-user-pool.yml file to create the user pool)

Is there any way to do this?

Thanks!

The pool name will always be fixed for the lifetime of the stack, is my understanding, it doesn’t change on each deploy.

So you can put whatever it is into the other stacks environment, as per Managing Stages and Environments ( provider:, environment:, USER_POOL_CLIENT_ID : foo )