要在AWS SAM中使用SAM模板在API Gateway方法中强制请求验证,你可以按照以下步骤进行操作:
Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
Auth:
Authorizer: AWS_IAM
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: path/to/code
Handler: lambda_function.handler
Runtime: python3.8
Events:
MyApiEvent:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /my-endpoint
Method: GET
Auth:
Authorizer: AWS_IAM
sam deploy --guided
这将创建一个具有请求验证的API Gateway方法,并将其与Lambda函数关联起来。接下来,你可以使用AWS IAM角色来控制对该API方法的访问。