AWS API Gateway REST API 是否没有设置来禁用 CloudFormation 模板中的execute-api 端点?

问题描述 投票:0回答:6

我已经使用 CloudFormation 模板设置了 API 网关(v1,而不是 v2)REST API 资源。最近我注意到还创建了默认的execute-api端点,我可以在设置中禁用它。

该API的类型为

AWS::ApiGateway::RestApi

当然,我希望通过模板来完成此操作,所以问题是:是否可以在 CloudFormation 模板中定义此设置,而不是在 AWS 控制台中手动单击?此选项适用于 APIGateway V2 API 资源 (AWS::ApiGatewayV2::Api

),但不适用于 CloudFormation 模板中的 APIGateway V1 REST API 资源 (
AWS::ApiGateway::RestApi
),即使可以在 CloudFormation 模板中为 APIGateway V1 REST API 手动更改该选项。控制台。

对于

AWS::ApiGateway::RestApi,还有一种 CLI 方式来执行此操作。

以下是我用来搜索此设置的一些链接:

AWS::ApiGatewayV2::API
AWS::ApiGateway::RestApi
通过 CLI 禁用默认 api 执行端点

amazon-web-services rest aws-cloudformation aws-api-gateway serverless-framework
6个回答
8
投票
DisableExecuteApiEndpoint

MyRestApi: Type: 'AWS::ApiGateway::RestApi' Properties: DisableExecuteApiEndpoint: true



1
投票
自定义资源

禁用它。下面是一个这样的完全工作模板的示例: Resources: MyRestApi: Type: 'AWS::ApiGateway::RestApi' Properties: Description: A test API Name: MyRestAPI LambdaBasicExecutionRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Statement: - Effect: Allow Principal: Service: lambda.amazonaws.com Action: sts:AssumeRole Path: / ManagedPolicyArns: - arn:aws:iam::aws:policy/AmazonAPIGatewayAdministrator - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole MyCustomResource: Type: Custom::DisableDefaultApiEndpoint Properties: ServiceToken: !GetAtt 'MyCustomFunction.Arn' APIId: !Ref 'MyRestApi' MyCustomFunction: Type: AWS::Lambda::Function Properties: Handler: index.lambda_handler Description: "Disable default API endpoint" Timeout: 30 Role: !GetAtt 'LambdaBasicExecutionRole.Arn' Runtime: python3.7 Code: ZipFile: | import json import logging import cfnresponse import boto3 logger = logging.getLogger() logger.setLevel(logging.INFO) client = boto3.client('apigateway') def lambda_handler(event, context): logger.info('got event {}'.format(event)) try: responseData = {} if event['RequestType'] in ["Create"]: APIId = event['ResourceProperties']['APIId'] response = client.update_rest_api( restApiId=APIId, patchOperations=[ { 'op': 'replace', 'path': '/disableExecuteApiEndpoint', 'value': 'True' } ] ) logger.info(str(response)) cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData) else: logger.info('Unexpected RequestType!') cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData) except Exception as err: logger.error(err) responseData = {"Data": str(err)} cfnresponse.send(event,context, cfnresponse.FAILED,responseData) return



1
投票

const restApi = new apigw.RestApi(...); const executeApiResource = new cr.AwsCustomResource(this, "execute-api-resource", { functionName: "disable-execute-api-endpoint", onCreate: { service: "APIGateway", action: "updateRestApi", parameters: { restApiId: restApi.restApiId, patchOperations: [{ op: "replace", path: "/disableExecuteApiEndpoint", value: "True" }] }, physicalResourceId: cr.PhysicalResourceId.of("execute-api-resource") }, policy: cr.AwsCustomResourcePolicy.fromStatements([new iam.PolicyStatement({ effect: iam.Effect.ALLOW, actions: ["apigateway:PATCH"], resources: ["arn:aws:apigateway:*::/*"], })]) }); executeApiResource.node.addDependency(restApi);



1
投票

const api = new apigateway.RestApi(this, 'api', ); (api.node.children[0] as apigateway.CfnRestApi).addPropertyOverride('DisableExecuteApiEndpoint','true')



1
投票
answer

的 Python 变体。 rest_api = apigateway.RestApi(self,...) cfn_apigw = rest_api.node.default_child cfn_apigw.add_property_override('DisableExecuteApiEndpoint', True)

亚马逊关于“抽象和逃生舱口”的文档

对于理解这里发生的事情非常有用。


0
投票

const api = new RestApi(this, "identifier", { ... disableExecuteApiEndpoint: true })

© www.soinside.com 2019 - 2024. All rights reserved.