How do I create a Cognito UserPool Authorizer with CDK when the UserPool is in another Account #26222
-
If the the UserPool is in different account to the API Gateway, how do you create an Authorizer? Using CognitoUserPoolsAuthorizer we only seem to be able to pass in an IUserPool but I'm unable to import an existing UserPool if it's in a different account. (It can be done from the console using the ARN but I can't implement that in CDK) I seem to be struggling to import a UserPool from another account, is that possible? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Turns out this is actually straight-forward: You CAN import from another account: My confusion arose from trying to do the same thing with I guess that leads to my next question... |
Beta Was this translation helpful? Give feedback.
-
I solve this by creating the authorizer := awscdkapigatewayv2alpha.NewHttpAuthorizer(stack, jsii.String("MyHttpAuthorizer"), &awscdkapigatewayv2alpha.HttpAuthorizerProps{
AuthorizerName: jsii.String("MyHttpAuthorizer"),
HttpApi: httpApi,
Type: awscdkapigatewayv2alpha.HttpAuthorizerType_JWT,
JwtIssuer: jsii.String("https://cognito-idp." + *props.Env.Region + ".amazonaws.com/" + props.CognitoUserPoolId),
JwtAudience: jsii.Strings(props.CognitoAppClientId), // Look this up in Cognito Userpool App Client settings. It’s the App client ID.
IdentitySource: jsii.Strings("$request.header.Authorization"),
})
httpApiAuthorizer := awscdkapigatewayv2alpha.HttpAuthorizer_FromHttpAuthorizerAttributes(stack, jsii.String("MyHttpAuthorizer4Test"), &awscdkapigatewayv2alpha.HttpAuthorizerAttributes{
AuthorizerId: authorizer.AuthorizerId(),
AuthorizerType: jsii.String("JWT"),
}) Finally you can simply attach the authorizer to the route of your API gateway. That work's for me, hope that helps. |
Beta Was this translation helpful? Give feedback.
-
Hello! Reopening this discussion to make it searchable. |
Beta Was this translation helpful? Give feedback.
I solve this by creating the
UserPoolClient
in that account/cdk-app where theUserPool
was created. Then I simply pass theUserPoolClientId
via environment param and set this toJwtAudience
of the authorizer.This is my code (note this is golang):