【问题标题】:ApiGateway CloudFormation without lambda没有 lambda 的 ApiGateway CloudFormation
【发布时间】:2023-03-07 11:28:01
【问题描述】:

我正在尝试创建一个模板,以便当我调用api/divide/inputvalue 时,api 会从DynamoDB 发回对应于inputvalue 映射的响应。

它非常简单,因为我直接从 db 获取值而没有任何业务逻辑,因此我不需要任何 lambda。但是我谷歌搜索的所有示例或他们使用 lambdas 的所有教程,我现在迷失了如何在没有 lambda 的情况下使其工作

这是我目前所拥有的。因为我没有在ApiGateway::Method 中提供Uri,所以这个模板现在有错误。这就是我目前所坚持的。

{

  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "Deployment": {
      "Type": "AWS::ApiGateway::Deployment",
      "Properties": {
        "RestApiId": { "Ref": "restApiName" },
        "Description": "First Deployment",
        "StageName": "StagingStage"
      },
      "DependsOn" : ["restApiMethod"]
    },
    "restApiMethod": {
      "Type": "AWS::ApiGateway::Method",
      "Properties": {
        "AuthorizationType": "NONE",
        "HttpMethod": "GET",
        "ResourceId": {"Ref": "apiRestResource"},
        "RestApiId": {"Ref": "restApiName"},
        "Integration": {
          "Type": "AWS",
          "IntegrationHttpMethod": "GET",
          "IntegrationResponses": [{"StatusCode": 200}],
          "Uri": { "Fn::Sub":"arn.aws.apigateway:${AWS::Region}:dynamodb:action/${restApiName.Arn}"}
        },
        "MethodResponses": [{"StatusCode": 200}]
      },
      "DependsOn": ["apiRestResource"]
    },
    "apiRestResource": {
      "Type": "AWS::ApiGateway::Resource",
      "Properties": {
        "RestApiId": {"Ref": "restApiName"},
        "ParentId": {
          "Fn::GetAtt": ["restApiName","RootResourceId"]
        },
        "PathPart": "divide"
      },
      "DependsOn": ["restApiName"]
    },
    "restApiName": {
      "Type": "AWS::ApiGateway::RestApi",
      "Properties": {
        "Name": "CalculationApi"
      }
    }
 }
}

【问题讨论】:

    标签: amazon-web-services amazon-dynamodb aws-api-gateway amazon-cloudformation


    【解决方案1】:

    根据文档,Uri 属性对于AWS service-proxy 集成类型的结构如下:

    如果您为 Type 属性指定 AWS,请指定遵循以下格式的 AWS 服务:arn:aws:apigateway:region:subdomain.service|service:path|action/service_api。例如,Lambda 函数 URI 遵循以下形式:arn:aws:apigateway:region:lambda:path/path。路径通常采用/2015-03-31/functions/LambdaFunctionARN/invocations 的形式。有关更多信息,请参阅 Amazon API Gateway REST API 参考中 Integration 资源的 uri 属性。

    uri API Gateway 属性参考提供了更多详细信息:

    对于 AWS 集成,URI 的格式应为 arn:aws:apigateway:{region}:{subdomain.service|service}:{path|action}/{service_api}Regionsubdomainservice 用于确定正确的端点。对于使用 Action= 查询字符串参数的 AWS 服务,service_api 应该是所需服务的有效操作。对于 RESTful AWS 服务 API,path 用于指示 URI 中剩余的子字符串应被视为资源的路径,包括初始的 /

    对于调用 Query Actiondynamodb 服务的 AWS 服务代理,Uri 应该是这样的(使用 Fn::Sub 的 YAML 短格式插入 Ref当前 AWS 区域):

    !Sub "arn:aws:apigateway:${AWS::Region}:dynamodb:action/Query"
    

    关于您在不使用 Lambda 函数的情况下使用 API Gateway 访问 DynamoDB 的更广泛用例,请参阅 Andrew Baird 的教程博客文章 "Using Amazon API Gateway as a Proxy for DynamoDB",并将指定的管理控制台步骤转换为相应的 CloudFormation 模板资源。

    【讨论】:

    • 谢谢。那很有帮助。当我看到您的回复时,我已经在查看服务网址了。我已经更新了模板。现在我坚持的部分是从 url api/divide/inputvalue 获取路径参数 inputvalue 并使用它 dynamodb 查询。我正在查看docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/… 示例以了解我该怎么做,但生成的模板给了我resource attribute restApiName.Arn 无效的错误。
    • 您应该可以在restApiMethod中定义“RequestParameters”,并在集成请求模板中引用。
    猜你喜欢
    • 2020-03-01
    • 2018-11-16
    • 2020-09-21
    • 2021-01-11
    • 2022-01-13
    • 2020-10-19
    • 2023-03-22
    • 2017-08-31
    • 2017-01-16
    相关资源
    最近更新 更多