簡體   English   中英

AWS SAM - HttpApi 與 Lambda Function 集成的簡單示例

[英]AWS SAM - Simple example of HttpApi with Lambda Function Integration

我是 AWS SAM 的新手,我正在嘗試修改 Hello World 示例模板以使用打開的 api swagger 文件來定義 API 和 lambda function 集成。 當我運行“sam build”和“sam deploy --guided”時,一切都會順利進行。 但是,當我檢查 api-gateway 時,我發現路由 /helloGet 沒有附加集成(應該是 lambda 函數)。 似乎無法弄清楚問題是什么。 這是我的 sam template.yaml 和 openapi swagger.yaml。

#template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Sample SAM Template

Globals:
  Function:
    Timeout: 3

Resources:
  MyHttpApi:
    Type: AWS::Serverless::HttpApi
    Properties:
      DefinitionUri: ./swagger.yaml
      Domain:
        DomainName: apitest.mydomain.com
        CertificateArn: arn:aws:acm:*******:*********:certificate/*****-****-***-****-**********
        EndpointConfiguration: REGIONAL
        Route53:
          HostedZoneId: ********************

  HelloWorldFunction:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures: ['x86_64']
      Events:
        HelloWorldEvent1:
          Type: HttpApi 
          Properties:
            Path: /helloGet
            Method: get
            ApiId:
              Ref: MyHttpApi

還有我的其他文件:

#swagger.yaml
openapi: "3.0.1"
info:
  title: "Test SAM API"
  version: "1.0.0"

paths:
  /helloGet:
    get:
      responses:
        default:
          description: "Default response for GET /helloGet"
      x-amazon-apigateway-integration:
        payloadFormatVersion: "2.0"
        type: "aws_proxy"
        httpMethod: "GET"
        uri: 
          Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HelloWorldFunction.Arn}/invocations"
        connectionType: "INTERNET"

x-amazon-apigateway-cors:
  allowMethods:
  - "*"
  allowHeaders:
  - "*"
  exposeHeaders:
  - "*"
  maxAge: 0
  allowCredentials: false
  allowOrigins:
  - "https://1.0.0.127:4443"
x-amazon-apigateway-importexport-version: "1.0"

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions-integration.html

集成請求中使用的 HTTP 方法。 對於 Lambda function 次調用,該值必須為 POST。

因此,為了使其正常工作,我將 httpMethod: "GET" 更改為 httpMethod: "POST"。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM