aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.72k stars 3.94k forks source link

(apigateway): Add support for importing existing Cognito user pool authorizers #16238

Open TomasChmelik opened 3 years ago

TomasChmelik commented 3 years ago

Add support to import existing Cognito user pool authorizers, so I could use aws_apigateway.Method instead of aws_apigateway.CfnMethod.

Use Case

I have multiple stacks using same API gateway. Base stack creates the authorizer and exports authorizer ID. Other stacks should be able to use something like apigateway.CognitoUserPoolsAuthorizer.from_authorizer_id(scope, 'Id', core.Fn.ImportValue('exported-authorizer-id'))

Proposed Solution

Other


This is a :rocket: Feature Request

nija-at commented 3 years ago

Thanks for submitting this feature request.

I'm marking this as p2 which means we are unable to get to this immediately. We use 👍 and community engagement to determine priority.

ghost commented 2 years ago

+1

chrisgrounds commented 1 year ago

👍

Is there a workaround for this atm?

TomasChmelik commented 1 year ago

Yes

from typing import Optional

import aws_cdk
from aws_cdk import aws_apigateway
from constructs import Construct
import jsii

@jsii.implements(aws_apigateway.IAuthorizer)
class ImportedAuthorizer(Construct):
    def __init__(self, scope: Construct, construct_id: str):
        super().__init__(scope, construct_id)
        self._authorizer_id = ""
        self._authorization_type : Optional[aws_apigateway.AuthorizationType] = None

    @classmethod
    def from_id(cls, scope: Construct, construct_id: str, *, authorizer_id: str, authorization_type: aws_apigateway.AuthorizationType):
        authorizer = cls(scope, construct_id)
        authorizer._authorizer_id = authorizer_id # pylint: disable=protected-access
        authorizer._authorization_type = authorization_type # pylint: disable=protected-access
        return authorizer

    @property
    def authorizer_id(self) -> str:
        return self._authorizer_id

    @property
    def authorization_type(self) -> Optional[aws_apigateway.AuthorizationType]:
        return self._authorization_type

this allows creating any API gateway authorizer from imported authorizer_id and its type

ImportedAuthorizer.from_id(scope, 'ImportedAuthorizer', 'afd51ngf', AuthorizationType.COGNITO)
msaavedra-earnd commented 1 year ago

👍🏻