How to get Cognito User Pool ID and the App client Id in Lambda

I am trying to create a cognito user pool ID and the app client using Serverless.yml file

I am referring this link -

I want to use this Cognito user pool id in my code.

Is there any way I can get this User Pool ID and app client ID in my lambda code?

Is there any way serverless can create some envieonment variables, which can hold the values for user pool ID and the App client ID?

1 Like

Hi, you can do it in this way

resources:
....
  Outputs:
    UserPoolId:
      Value:
        Ref: CognitoUserPool
      Export:
        Name: ${self:provider.stage}-UserPoolId

    UserPoolClientId:
      Value:
        Ref: CognitoUserPoolClient
      Export:
        Name: ${self:provider.stage}-UserPoolClientId
functions:
   your-function:
     ....
     environment:
       USER_POOL_ID: !ImportValue ${self:provider.stage}-UserPoolId
       USER_POOL_CLIENT_ID: !ImportValue ${self:provider.stage}-UserPoolClientId

your-function.js

   const { USER_POOL_ID, USER_POOL_CLIENT_ID } = process.env;
   ...

p.s.
If your functions + resources are in the same stack - you can just use USER_POOL_ID: !Ref CognitoUserPool and USER_POOL_CLIENT_ID: !Ref CognitoUserPoolClient

Thanks for solution ! it works