如果您需要更改API Gateway托管的资源,则可能会遇到“API Gateway托管资源仅为只读”错误。此错误意味着您无法更新资源。解决此错误的方法是通过AWS CloudFormation模板创建API Gateway资源,而不是在控制台中手动创建它们。
以下是一个简单的AWS CloudFormation模板示例,它创建了一个包含一个API Gateway HTTP端点的API:
Resources:
MyApiGateway:
Type: AWS::ApiGateway::RestApi
Properties:
Name: my-api-gateway
MyResource:
Type: AWS::ApiGateway::Resource
Properties:
ParentId: !GetAtt MyApiGateway.RootResourceId
PathPart: myresource
MyMethod:
Type: AWS::ApiGateway::Method
Properties:
RestApiId: !Ref MyApiGateway
ResourceId: !Ref MyResource
HttpMethod: GET
AuthorizationType: NONE
Integration:
Type: MOCK
IntegrationResponses:
- StatusCode: "200"
ResponseTemplates:
application/json: |
{"message": "Hello, world!"}
MethodResponses:
- StatusCode: "200"
Outputs:
ApiEndpoint:
Value: !Sub "https://${MyApiGateway}.execute-api.${AWS::Region}.amazonaws.com"
该模板创建了一个名为“my-api-gateway”的API Gateway REST API,并将其映射到根资源。它还创建了一个名为“myresource”的子资源,并将HTTP GET方法与该子资源相关联。最后,该方法使用MOCK集成响应。
您可以使用AWS CloudFormation控制台或AWS CLI部署此模板。一旦完成,您可以使用API Gateway的API端点来测试HTTP GET方法并检查响应。
上一篇:api网关吞吐量影响因素
下一篇:API响应的值