简体   繁体   English

Cognito - 错误:无效的 UserPoolId 格式

[英]Cognito - Error: Invalid UserPoolId format

I am using AWS CDK to create a userpool and userpool client.我正在使用 AWS CDK 创建用户池和用户池客户端。 I would like to be able to access the userpool id and userpool client id from a lambda once they have been created.我希望能够在创建后从 lambda 访问用户池 ID 和用户池客户端 ID。 I pass these two values to the lambda via environmental variables.我通过环境变量将这两个值传递给 lambda。 Here is my code:这是我的代码:

import { Construct } from 'constructs';
import {
  IResource,
  LambdaIntegration,
  MockIntegration,
  PassthroughBehavior,
  RestApi,
} from 'aws-cdk-lib/aws-apigateway';
import {
  NodejsFunction,
  NodejsFunctionProps,
} from 'aws-cdk-lib/aws-lambda-nodejs';
import { Runtime } from 'aws-cdk-lib/aws-lambda';
import * as amplify from 'aws-cdk-lib/aws-amplify';

import {
  aws_s3,
  aws_ec2,
  aws_rds,
  aws_cognito,
  aws_amplify,
  Duration,
  CfnOutput,
} from 'aws-cdk-lib';

export class FrontendService extends Construct {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    const userPool = new aws_cognito.UserPool(this, 'userpool', {
      userPoolName: 'frontend-userpool',
      selfSignUpEnabled: true,
      signInAliases: {
        email: true,
      },
      autoVerify: { email: true },
    });

    const userPoolClient = new aws_cognito.UserPoolClient(
      this,
      'frontend-app-client',
      {
        userPool,
        generateSecret: false,
      }
    );

    const bucket = new aws_s3.Bucket(this, 'FrontendStore');

    const nodeJsFunctionProps: NodejsFunctionProps = {
      environment: {
        BUCKET: bucket.bucketName,
        DB_NAME: 'hospoFEDB',
        AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
        USER_POOL_ID: userPool.userPoolId,
        USER_POOL_CLIENT_ID: userPoolClient.userPoolClientId,
      },
      runtime: Runtime.NODEJS_14_X,
    };

    const registerLambda = new NodejsFunction(this, 'registerFunction', {
      entry: 'dist/lambda/register.js',
      memorySize: 1024,
      ...nodeJsFunctionProps,
    });

    const registerIntegration = new LambdaIntegration(registerLambda);

    const api = new RestApi(this, 'frontend-api', {
      restApiName: 'Frontend Service',
      description: 'This service serves the frontend.',
    });

    const registerResource = api.root.addResource('register');
    registerResource.addMethod('POST', registerIntegration);
  }
}

Here is my lambda function and how I intend to use the USER_POOL_ID and USER_POOL_CLIENT_ID env variables:这是我的 lambda function 以及我打算如何使用USER_POOL_IDUSER_POOL_CLIENT_ID变量:

import {
  CognitoUserPool,
} from 'amazon-cognito-identity-js';

export const handler = async (event: any, context: any) => {
  try {
    console.log(process.env.USER_POOL_ID);
    console.log(process.env.USER_POOL_CLIENT_ID);

    const userPool = new CognitoUserPool({
      UserPoolId: process.env.USER_POOL_ID as string,
      ClientId: process.env.USER_POOL_CLIENT_ID as string,
    });

    return {
      statusCode: 200,
    };
  } catch (error) {
    if (error instanceof Error) {
      const body = error.stack || (JSON.stringify(error, null, 2) as any);
      return {
        statusCode: 400,
        headers: {},
        body: JSON.stringify(body),
      };
    }
    return {
      statusCode: 400,
    };
  }
};

The idea with this setup is that I would create a cognito user pool and client then be able to pass those id's directly down.此设置的想法是我将创建一个认知用户池,然后客户端能够直接向下传递这些 ID。 Currently if I run this locally via sam local start-api it generates the following USER_POOL_ID : Frontenduserpool87772999 .目前,如果我通过sam local start-api在本地运行它,它会生成以下USER_POOL_IDFrontenduserpool87772999 If I try and use this id in the new CognitoUserPool({... part of my lambda function I get the following error:如果我尝试在new CognitoUserPool({...我的 lambda function 的一部分中使用此 ID,我会收到以下错误:

Error: Invalid UserPoolId format.错误:无效的 UserPoolId 格式。

If I deploy the app however and execute the lambda function from the deployed environment with the exact same code I get a USER_POOL_ID that looks more like: us-east-1_HAjkUj9hP .但是,如果我部署应用程序并使用完全相同的代码从已部署环境执行 lambda function,我会得到一个USER_POOL_ID ,它看起来更像: us-east-1_HAjkUj9hP This works fine and I do not get the error above.这工作正常,我没有收到上述错误。

Should I assume that I can not create a user pool locally and will always have to point to the deployed user pool?我是否应该假设我不能在本地创建用户池并且必须始终指向已部署的用户池?

Should I assume that I can not create a user pool locally and will always have to point to the deployed user pool我是否应该假设我不能在本地创建用户池并且必须始终指向已部署的用户池

Yes.是的。 See the docs : start-api creates an emulated local API endpoint and Lambda for local testing. 请参阅文档start-api创建一个模拟的本地 API 端点和 Lambda 用于本地测试。 It does not deploy or emulate other resources.它不部署或模拟其他资源。

You can reference previously deployed AWS resources by passing a JSON file with the deployed physical values using the --env-vars flag.您可以通过使用--env-vars标志传递包含已部署物理值的 JSON 文件来引用以前部署的 AWS 资源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sls离线POST测试收到Error: Both UserPoolId and ClientId are required and 502 Bad Gateway - Sls offline POST test receives Error: Both UserPoolId and ClientId are required and 502 Bad Gateway AWS boto3 Cognito 无效访问令牌错误 - AWS boto3 Cognito Invalid Access Token Error Cognito 中的刷新令牌不断失败并出现“invalid_grant”/“无效的刷新令牌”错误 - Refreshing tokens in Cognito constantly fails with "invalid_grant" / "Invalid Refresh Token" error Cognito 授权端点(没有客户端密码)返回无效的客户端错误消息 - Cognito authorization endpoint (without client secret) returning Invalid client error message 密钥工具错误:java.io.IOException:密钥库格式无效 - keytool error: java.io.IOException: Invalid keystore format Golang 和 Cognito _ 无效 lambda function output - Golang and Cognito _ Invalid lambda function output Amazon Rekogntion Image:错误 InvalidImageFormatException:请求的图像格式无效 - Amazon Rekogntion Image: error InvalidImageFormatException: Request has invalid image format Azure 函数错误:受监视集合的连接字符串格式无效 - Azure Functions error: The connection string for the monitored collection is in an invalid format GITLAB CI 加载密钥“/dev/fd/63”时出错:格式无效错误:作业失败:退出代码 1 - GITLAB CI Error loading key "/dev/fd/63": invalid format ERROR: Job failed: exit code 1 Cognito jwt 令牌验证抛出 400 错误 - Cognito jwt token validation throwing 400 error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM