Howdy! Anybody had luck creating a `HttpUserPoolAu...
# help
p
Howdy! Anybody had luck creating a
HttpUserPoolAuthorizer
from existing
userPoolClient
and
userPool
? I cannot seem to find any
fromArn
or similar methods in CDK for this.
a
Hi, Yes I did
Copy code
const userPool = cognito.UserPool.fromUserPoolId(this, "dinenation-users", "userPoolID");


    const userPoolClient = cognito.UserPoolClient.fromUserPoolClientId(this, "dinenation-app", "clientId");
with this
Copy code
import * as cognito from "@aws-cdk/aws-cognito";
and then
Copy code
defaultAuthorizer: new apigAuthorizers.HttpUserPoolAuthorizer({
        userPool,
        userPoolClient,
      }),
p
Wonderful! Thank you so much @Artem Kalantai! In sharing fashion, I export these values like this in `serverless.yml`:
Copy code
...
resources:
  Outputs:
    CognitoUserPoolId:
      Value:
        Ref: CognitoUserPool
      Export:
        Name: ${self:custom.stage}-CognitoUserPoolId
    CognitoUserPoolClientId:
      Value:
        Ref: CognitoUserPoolClient
      Export:
        Name: ${self:custom.stage}-CognitoUserPoolClientId
And then consume like this in my `sst-stack.js`:
Copy code
this.userPool = cognito.UserPool.fromUserPoolId(this, 'existingUserPool', cdk.Fn.importValue(`${scope.stage}-CognitoUserPoolId`))
this.userPoolClient = cognito.UserPoolClient.fromUserPoolClientId(this, 'existingUserPoolClient', cdk.Fn.importValue(`${scope.stage}-CognitoUserPoolClientId`))
this.userPoolAuthorizer = new apigAuthorizers.HttpUserPoolAuthorizer({
  userPool: this.userPool,
  userPoolClient: this.userPoolClient
})

this.api = new apig.HttpApi(this, `Api_${id}`)
this.api.addRoutes({
  path: `/${id}/secure-ping`,
  methods: [apig.HttpMethod.GET],
  integration: new apigIntegrations.LambdaProxyIntegration({
    handler: this.pingFn
  }),
  authorizer: this.userPoolAuthorizer
})
Thanks again @Artem Kalantai!
a
Happy to help