How can i reuse an existing authorizer in a rest api through AWS CDK?

0

It seems there is no method to get an existing authorizer by its attributes, is there any other way?

Eran
已提問 2 個月前檢視次數 173 次
1 個回答
0

In AWS CDK, you can reuse an existing authorizer in a REST API by referencing it using its ARN (Amazon Resource Name). Since there isn't a direct method to get an existing authorizer by its attributes, you would typically create an instance of CfnAuthorizer using its ARN. Here's how you can do it in typescript:


import * as cdk from '@aws-cdk/core';
import * as apigateway from '@aws-cdk/aws-apigateway';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');

// Assuming you have an existing authorizer ARN
const existingAuthorizerArn = 'arn:aws:apigateway:region::/restapis/api-id/authorizers/authorizer-id';

// Create a reference to the existing authorizer
const existingAuthorizer = apigateway.CfnAuthorizer.fromAuthorizerId(stack, 'ExistingAuthorizer', existingAuthorizerArn);

// Now you can use this existing authorizer in your API Gateway
const api = new apigateway.RestApi(stack, 'MyRestApi');

const resource = api.root.addResource('myresource');
resource.addMethod('GET', new apigateway.HttpIntegration('http://example.com'), {
    authorizer: existingAuthorizer // Attach the existing authorizer
});

app.synth();
profile picture
專家
已回答 2 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南