AWS CDK user pool authorizer

前端 未结 7 1403
滥情空心
滥情空心 2021-02-12 23:31

I\'m trying to create an API gateway using the AWS-CDK and protect the REST endpoints with a Cognito user pool authorizer.

I cannot find any examples how one would do th

相关标签:
7条回答
  • 2021-02-13 00:16

    As of September 2019 @bgdnip answer doesnt translate exactly for typescript. I got it working with the following:

    const api = new RestApi(this, 'RestAPI', {
        restApiName: 'Rest-Name',
        description: 'API for journey services.',
    });
    
    const putIntegration = new LambdaIntegration(handler);
    
    const auth = new CfnAuthorizer(this, 'APIGatewayAuthorizer', {
        name: 'customer-authorizer',
        identitySource: 'method.request.header.Authorization',
        providerArns: [providerArn.valueAsString],
        restApiId: api.restApiId,
        type: AuthorizationType.COGNITO,
    });
    
    const post = api.root.addMethod('PUT', putIntegration, { authorizationType: AuthorizationType.COGNITO });
    const postMethod = post.node.defaultChild as CfnMethod;
    postMethod.addOverride('Properties.AuthorizerId', { Ref: auth.logicalId });
    

    This is from https://docs.aws.amazon.com/cdk/latest/guide/cfn_layer.html#cfn_layer_resource_props

    UPDATE October

    The above is already out of date and unnecessary and can be achieved with the following with aws-cdk 1.12.0

    const api = new RestApi(this, 'RestAPI', {
        restApiName: 'Rest-Name',
        description: 'API for journey services.',
    });
    
    const putIntegration = new LambdaIntegration(handler);
    
    const auth = new CfnAuthorizer(this, 'APIGatewayAuthorizer', {
        name: 'customer-authorizer',
        identitySource: 'method.request.header.Authorization',
        providerArns: [providerArn.valueAsString],
        restApiId: api.restApiId,
        type: AuthorizationType.COGNITO,
    });
    
    const post = api.root.addMethod('PUT', putIntegration, {
        authorizationType: AuthorizationType.COGNITO,
        authorizer: { authorizerId: auth.ref }
    });
    
    0 讨论(0)
提交回复
热议问题