You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用SAM模板为Lambda事件创建非代理API Gateway的配置咨询

解决方案

要实现Lambda + API Gateway的Non-proxy集成,需要在SAM模板中显式声明API Gateway的全量资源配置,替换默认隐式生成的代理集成API,配置自定义集成响应支持XML返回。修改后的完整template.yaml如下:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  callforward

  Sample SAM Template for callforward

Globals:
  Function:
    Timeout: 3

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.8

  # 显式声明API Gateway
  CallForwardApi:
    Type: AWS::ApiGateway::RestApi
    Properties:
      Name: CallForwardApi

  # 定义/hello路径资源
  HelloResource:
    Type: AWS::ApiGateway::Resource
    Properties:
      RestApiId: !Ref CallForwardApi
      ParentId: !GetAtt CallForwardApi.RootResourceId
      PathPart: hello

  # 定义GET方法,配置非代理集成
  HelloGetMethod:
    Type: AWS::ApiGateway::Method
    Properties:
      RestApiId: !Ref CallForwardApi
      ResourceId: !Ref HelloResource
      HttpMethod: GET
      AuthorizationType: NONE
      Integration:
        Type: AWS
        IntegrationHttpMethod: POST
        Uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HelloWorldFunction.Arn}/invocations
        IntegrationResponses:
          - StatusCode: 200
            ResponseParameters:
              method.response.header.Content-Type: "'application/xml'"
            ResponseTemplates:
              application/xml: $input.path('$')
      MethodResponses:
        - StatusCode: 200
          ResponseParameters:
            method.response.header.Content-Type: true

  # 授权API Gateway调用Lambda
  LambdaInvokePermission:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !GetAtt HelloWorldFunction.Arn
      Action: lambda:InvokeFunction
      Principal: apigateway.amazonaws.com
      SourceArn: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${CallForwardApi}/*/GET/hello

  # 部署API到Prod环境
  ApiDeployment:
    Type: AWS::ApiGateway::Deployment
    DependsOn: HelloGetMethod
    Properties:
      RestApiId: !Ref CallForwardApi
      StageName: Prod

Outputs:
  HelloWorldApi:
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${CallForwardApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  HelloWorldFunction:
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt HelloWorldFunctionRole.Arn

关键配置说明

  • 显式声明AWS::ApiGateway::RestApi资源,完全自主控制API的所有配置规则,替代SAM默认生成的隐式API
  • 定义AWS::ApiGateway::Method时将Integration的Type设置为AWS,而非默认的AWS_PROXY,开启非代理集成模式
  • 新增AWS::ApiGateway::IntegrationResponse配置,自定义响应头Content-Type为application/xml,同时可自定义响应映射模板,将Lambda返回的内容转换为符合要求的XML格式返回给客户端
  • 新增AWS::Lambda::Permission资源,授权API Gateway服务调用你的Lambda函数
  • 显式配置API部署和阶段资源,确保配置变更后自动部署到对应环境

注意事项

Lambda侧不需要返回代理集成要求的statusCode/headers/body格式的结构体,直接返回XML内容或者可被映射的结构化数据即可,集成响应会按照你配置的规则完成最终响应的拼接转换。

内容的提问来源于stack exchange,提问作者ananthu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 22:09:03