AWS SAM模板不仅仅包含API网关,还可以包含其他AWS服务和资源的定义,如Lambda函数、DynamoDB表格、S3存储桶等。以下是一个包含API网关和Lambda函数的示例AWS SAM模板:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.8
Events:
HelloWorldApi:
Type: Api
Properties:
Path: /hello
Method: get
ApiGatewayRestApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
Outputs:
HelloWorldApi:
Description: "API Gateway endpoint URL for the Hello World function"
Value: !Sub "https://${ApiGatewayRestApi}.execute-api.${AWS::Region}.amazonaws.com/prod/hello"
在这个示例中,模板定义了一个名为HelloWorldFunction的Lambda函数,它的代码位于hello_world/目录下,使用Python 3.8运行时,并在API网关的/hello路径上注册了一个GET方法。
模板还定义了一个名为ApiGatewayRestApi的API网关资源,它将在部署过程中创建一个名为prod的阶段。
最后,通过Outputs部分定义了一个输出值HelloWorldApi,它提供了HelloWorldFunction函数在API网关上的终端URL。
你可以根据自己的需求在AWS SAM模板中添加其他资源和服务定义。