仅具有API网关的AWS SAM模板?

问题描述

我们计划仅在最初使用API​​网关作为REST API HTTP代理-因为我们已有REST服务,并且希望在不更改客户端界面的情况下将其拆分为较小的部分(可能以后用lambda替换其中的某些部分)功能)。

在执行此操作之前,我希望查看是否有在本地运行此类方案的支持,因此,我已在AWS管理控制台中配置了API网关并导出了OpenAPI定义。

所以我的template.yaml看起来像

AWstemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: Demo SAM API
Resources:
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: test
      DeFinitionBody:
        'Fn::Transform':
          Name: AWS::Include
          Parameters:
            Location: swagger.yaml

和swagger.yaml如下:

openapi: "3.0.1"
info:
  title: "Test.API"
  description: "Test.API"
  version: "2020-09-14T07:49:45Z"
servers:
- url: "https://dummydomain.com"
paths:
  /api/v3/Client:
    get:
      x-amazon-apigateway-integration:
        type: "http_proxy"
        uri: "https://dummydommain.com/api/v3/client"
        passthroughBehavior: "when_no_match"
        httpMethod: "GET"
        timeoutInMillis: 29000
components: {}

运行sam local start-api --debug会产生错误

Error: Template does not have any APIs connected to Lambda functions

添加了虚拟lambda函数只是为了解决错误,但是仍然没有运气-它在调试输出中有消息:

Lambda function integration not found in Swagger document at path='/api/v3/client' method='get'

并使用以下命令回复“ http:// localhost:3000 / api / v3 / client”

{"message":"Missing Authentication Token"}

这是否意味着SAM与lambda的耦合非常紧密,并且不会在本地运行API网关作为代理,或者我正在做一些愚蠢的事情?

解决方法

将Lambda集成添加到template.yaml文件:

YourLambdaFunction:
        Type: AWS::Serverless::Function 
        Properties:
            CodeUri: path/to/your/lambda/
            Role: arn:aws:iam::<ACCOUNT_NUMBER>:role/service-role/role-for-lambda
            Events:
                YourGetEvent:
                    Type: Api 
                    Properties:
                        Path: /api/v3/Client
                        Method: get
                        RestApiId:
                            Ref: MyApi