AWS SAM 可以通过使用 AWS::Serverless::Function 资源类型和 AWS::ApiGateway::Method 资源类型来实现向现有 API Gateway 资源添加新的无服务器函数。
以下是示例模板:
Resources:
MyApi:
Type: "AWS::Serverless::Api"
Properties:
StageName: "prod"
DefinitionUri: "./swagger.yaml"
MyImportedApi:
Type: "AWS::Serverless::Api"
Properties:
StageName: "prod"
DefinitionBody:
Fn::Transform:
Name: "AWS::Include"
Parameters:
Location: !Sub "s3://${ApiBucket}/${ApiKey}"
MyFunction:
Type: "AWS::Serverless::Function"
Properties:
CodeUri: "./src"
Handler: "index.lambda_handler"
Runtime: "python3.7"
Events:
MyApi:
Type: "Api"
Properties:
RestApiId: !Ref MyImportedApi
Path: "/mynewfunction"
Method: "POST"
此模板中,MyFunction 资源通过向 MyApi 和 MyImportedApi 的 API Gateway 资源添加新的方法来实现向现有的导入的 API Gateway 资源添加新的无服务器函数。使用 Fn::Ref 函数可以按名称引用 MyImportedApi,这将使用 Fn::ImportValue 函数导入此资源。