CDK переопределить привязку при использовании LambdaIntegration - PullRequest
3 голосов
/ 13 января 2020

При использовании класса LambdaIntegration функция привязки автоматически добавляет разрешение к лямбде:

    bind(method) {
        super.bind(method);
        const principal = new iam.ServicePrincipal('apigateway.amazonaws.com');
        const desc = `${method.restApi.node.uniqueId}.${method.httpMethod}.${method.resource.path.replace(/\//g, '.')}`;
        this.handler.addPermission(`ApiPermission.${desc}`, {
            principal,
            scope: method,
            sourceArn: method.methodArn,
        });
        // add permission to invoke from the console
        if (this.enableTest) {
            this.handler.addPermission(`ApiPermission.Test.${desc}`, {
                principal,
                scope: method,
                sourceArn: method.testMethodArn,
            });
        }
    }

В настоящее время я создаю несколько шлюзов API, 90% из которых запускают одну и ту же функцию лямбда, это вызывает у меня следующая ошибка:

The final policy size (XXX) is bigger than the limit (20480)

Подробнее здесь .

Моя цель - переопределить функцию связывания моей собственной функцией и обработать разрешения самостоятельно, что-то в этом роде :

arn:aws:execute-api:{AWS_REGION}:{AWS_ACCOUNT}:{API_ID}/*/*/*

Я знаю, что это не лучшая практика, но сейчас это единственный рабочий способ.

Это новый класс, который я создал:

    class customLambdaIntegration extends apigateway.LambdaIntegration{
          myHandler: lambda.IFunction;
          constructor(handler: lambda.IFunction, options?: LambdaIntegrationOptions) {
            super(handler, options);
            this.myHandler = handler;
          }
          bind(method: Method) {
            const principal = new iam.ServicePrincipal('apigateway.amazonaws.com');
            const desc = `${method.restApi.node.uniqueId}.${method.httpMethod}.${method.resource.path.replace(/\//g, '.')}`;
            this.myHandler.addPermission(`ApiPermission.${desc}`, {
              principal,
              scope: method,
              sourceArn: method.methodArn.toString().replace(api.deploymentStage.stageName,'*')
            });
          }
        }

Получение этой ошибки при запуске cdk list:

if (!this.scope) { throw new Error('AwsIntegration must be used in API'); }

Проблемный c фрагмент кода, который выдает ошибку:

    class AwsIntegration extends integration_1.Integration {
        constructor(props) {
            const backend = props.subdomain ? `${props.subdomain}.${props.service}` : props.service;
            const type = props.proxy ? integration_1.IntegrationType.AWS_PROXY : integration_1.IntegrationType.AWS;
            const { apiType, apiValue } = util_1.parseAwsApiCall(props.path, props.action, props.actionParameters);
            super({
                type,
                integrationHttpMethod: props.integrationHttpMethod || 'POST',
                uri: cdk.Lazy.stringValue({ produce: () => {
                        if (!this.scope) {
                            throw new Error('AwsIntegration must be used in API');
                        }
                        return cdk.Stack.of(this.scope).formatArn({
                            service: 'apigateway',
                            account: backend,
                            resource: apiType,
                            sep: '/',
                            resourceName: apiValue,
                        });
                    } }),
                options: props.options,
            });
        }
        bind(method) {
            this.scope = method;
        }
    }

Документация LambdaIntegration.

enter image description here

Любая помощь будет принята с благодарностью.


Кому это может быть полезно, я открываю запрос на функцию для реализации моя функция и вручную обрабатывать лямбда-разрешения:

* 10 40 *https://github.com/aws/aws-cdk/issues/5774

1 Ответ

2 голосов
/ 13 января 2020

Обнаружена проблема, this['scope'] = method; отсутствует внутри функции связывания, поскольку класс AwsIntegration реализует this.scope=method.

Полный код:

       class customLambdaIntegration extends apigateway.LambdaIntegration{
          // myScope : cdk.IConstruct;
          myHandler: lambda.IFunction;
          MyOptinos: apigateway.LambdaIntegrationOptions | undefined;
          constructor(handler: lambda.IFunction, options?: LambdaIntegrationOptions) {
            super(handler, options);
            this.myHandler = handler;
            this.MyOptinos = options;
          }
          bind(method: Method) {
            this['scope'] = method;
            const principal = new iam.ServicePrincipal('apigateway.amazonaws.com');
            const desc = `${method.restApi.node.uniqueId}.${method.httpMethod}.${method.resource.path.replace(/\//g, '.')}`;
            this.myHandler.addPermission(`ApiPermission.${desc}`, {
              principal,
              scope: method,
              sourceArn: method.methodArn.toString().replace(api.deploymentStage.stageName,'*')
            });
        }
      }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...