要在AWS SAM中指定API Gateway中的方法请求,您可以使用AWS::Serverless::Api资源,并在其中使用AWS::Serverless::Function资源来定义函数。
以下是一个示例模板,其中定义了一个GET方法请求的API Gateway和一个Lambda函数:
Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
DefinitionBody:
swagger: "2.0"
info:
title: "My API"
paths:
/myresource:
get:
x-amazon-apigateway-integration:
type: "aws_proxy"
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyFunction.Arn}/invocations
responses: {}
MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: my-function/
Handler: app.lambda_handler
Runtime: python3.8
Events:
MyApi:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /myresource
Method: get
上述示例中,MyApi资源定义了一个GET方法请求的API Gateway,并使用x-amazon-apigateway-integration指定了与Lambda函数的集成。
MyFunction资源定义了一个Lambda函数,并在Events属性中指定了与API Gateway的集成。RestApiId属性使用!Ref MyApi引用到了MyApi资源。
您可以根据自己的需求调整模板中的资源定义和属性。