简体   繁体   中英

AWS SAM - Simple example of HttpApi with Lambda Function Integration

I am new to AWS SAM, and I am trying to modify the Hello World example template to use an open api swagger file to define an API with lambda function integrations. When I run "sam build" and "sam deploy --guided" everything goes through. However, when I check the api-gateway I see that the route /helloGet has no integration attached to it (should be a lambda function). Can't seem to figure out what the problem is. Here are my sam template.yaml and 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

And my other file:

#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

The HTTP method used in the integration request. For Lambda function invocations, the value must be POST.

So to make it work I changed httpMethod: "GET" to httpMethod: "POST".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM