要使用API Gateway部署无服务器框架和调用HTTP端点,可以使用以下步骤:
exports.handler = async (event, context) => {
// 处理事件和上下文对象
// 执行业务逻辑
// 返回响应
return {
statusCode: 200,
body: JSON.stringify({ message: 'Hello, World!' })
};
};
Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
DefinitionBody:
swagger: '2.0'
info:
title: My API
version: '1.0'
paths:
/my-endpoint:
get:
responses:
'200':
description: OK
x-amazon-apigateway-integration:
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyFunction.Arn}/invocations
passthroughBehavior: when_no_match
httpMethod: POST
type: aws_proxy
MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
Handler: index.handler
Runtime: nodejs14.x
Events:
MyApi:
Type: Api
Properties:
Path: /my-endpoint
Method: get
sam build
sam deploy --guided
/my-endpoint
端点提供一个URL。以上是一个使用API Gateway部署无服务器框架和调用HTTP端点的解决方法。在实际应用中,您可能需要根据具体需求进行调整和扩展。