При использовании класса 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.
Любая помощь будет принята с благодарностью.
Кому это может быть полезно, я открываю запрос на функцию для реализации моя функция и вручную обрабатывать лямбда-разрешения:
* 10 40 *
https://github.com/aws/aws-cdk/issues/5774