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

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

2 Likes