[英]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_ID
和USER_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_ID
: Frontenduserpool87772999
。 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.