【问题标题】:Is it possible to configure different API Gateway stages with different lambda versions using AWS SAM是否可以使用 AWS SAM 配置具有不同 lambda 版本的不同 API Gateway 阶段
【发布时间】:2019-12-09 21:02:30
【问题描述】:

我的应用程序有一个 SAM 模板。每次我使用新的 API Gateway 阶段名称部署 SAM 模板时,它都会替换之前创建的阶段。

所以,发现这篇文章可以帮助我发布指向不同 lambda 版本的不同版本。 https://aws.amazon.com/blogs/compute/using-api-gateway-stage-variables-to-manage-lambda-functions/

但是,为此,我必须在部署后手动更改 API 网关。 那么,有什么方法可以使用 AWS SAM 做到这一点?

例如,考虑以下 CloudFormation 模板提取:

  ProxyMethod:
    Type: AWS::ApiGateway::Method
    Properties:
      HttpMethod: ANY
      RestApiId: !Ref Api # AWS::ApiGateway::RestApi defined elsewhere
      ResourceId: !Ref ProxyResource # AWS::ApiGateway::Resource defined elsewhere
      AuthorizationType: NONE #auth is done at the public API layer
      Integration:
  # client request passed through as-is. "Lambda proxy integration"
        Type: AWS_PROXY
        Uri: !Join
          - ''
          - - 'arn:aws:apigateway:'
            - !Sub ${AWS::Region}
            - ':lambda:path/2015-03-31/functions/${!stageVariables.FunctionArn}/invocations'
        IntegrationHttpMethod: ANY
        PassthroughBehavior: WHEN_NO_MATCH

这应该允许我使用在运行时确定的 Lambda 函数创建一个方法,就像在 example 中一样。但是,当我这样做时,在部署模板时出现以下错误:

ProxyMethod CREATE_FAILED   Invalid lambda function (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; ....

我如何通过 CloudFormation 定义目标 Lambda 函数由阶段变量确定的 API 网关方法?

【问题讨论】:

  • 请将您的模板代码添加到您的问题中。

标签: amazon-web-services aws-lambda amazon-cloudformation aws-api-gateway aws-sam


【解决方案1】:

这里的问题可以通过包含大部分完整的 lambda ARN 并仅对名称进行标记来解决。 CloudFormation 可能通过正则表达式运行整个字符串,当它看到 ${!stageVariables.FunctionArn} 而不是 arn:aws:lambda:blablabla 时,它会失败。

【讨论】:

    猜你喜欢
    • 2022-01-13
    • 1970-01-01
    • 2015-10-28
    • 2022-11-29
    • 2015-11-09
    • 1970-01-01
    • 2011-11-04
    • 2022-10-03
    • 1970-01-01
    相关资源
    最近更新 更多