【问题标题】:AWS SAM/Cloudformation configure API Gateway to point to lambda function versionAWS SAM/Cloudformation 配置 API Gateway 指向 lambda 函数版本
【发布时间】:2022-01-13 05:43:33
【问题描述】:

我目前正在尝试为我的 AWS lambda 函数启用预置并发。我已经想到我只能在 Lambda 函数版本或 Lambda 函数别名上执行此操作。但是我很难将我的 API Gateway 指向这个版本,它似乎总是指向基本功能,而不是版本。

在 UI 中,我可以轻松地将我的 Lambda 函数版本附加到 API 网关端点,但我不知道如何在我的 SAM 模板中执行此操作。

这是我目前拥有的:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Transform": "AWS::Serverless-2016-10-31",
    "Description": "Desc.",
    "Parameters": { },
    "Resources": {
      "MyLambdaFunction": {
        "Type": "AWS::Serverless::Function",
        "Properties": {
          "Runtime": "dotnetcore3.1",
          "CodeUri": "MyCodeUri",
          "MemorySize": 1024,
          "Timeout": 30,
          "Events": {
            "HttpEvent1": {
              "Type": "Api",
              "Properties": {
                "Path": "/v1/test",
                "Method": "GET",
                "RestApiId": {
                  "Ref": "ApiGateway"
                }
              }
            }
          },
          "Handler": "MyNamespace::MyNamespace.MyFunc::RunAsync"
        }
      },
      "MyLambdaFunctionConcurrentV1": {
        "Type": "AWS::Lambda::Version",
        "Properties": {
          "FunctionName": {
            "Ref": "MyLambdaFunction"
          },
          "ProvisionedConcurrencyConfig": {
            "ProvisionedConcurrentExecutions": 1
          }
        }
      },
      "ApiGateway": {
        "Type": "AWS::Serverless::Api",
        "Properties": {
          "StageName": {
            "Ref": "ApiStageName"
          },
          "Cors": {
            "AllowCredentials": true,
            "AllowHeaders": "'*'",
            "AllowMethods": "'*'",
            "AllowOrigin": "'*'",
            "MaxAge": "'600'"
          }
        }
      }
    },
    "Outputs": {
      "ApiUrl": {
        "Description": "API Gateway Endpoint URL",
        "Value": {
          "Fn::Sub": "https://${ApiGateway}.execute-api.${AWS::Region}.${AWS::URLSuffix}/${ApiStageName}"
        },
        "Export": {
          "Name": {
            "Fn::Sub": "${AWS::StackName}-ApiUrl"
          }
        }
      }
    }
  }

所以我可以部署我的 Lambda 函数、我的 API 网关和我的版本。但我不知道如何将 API 网关链接到我的版本。

【问题讨论】:

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


    【解决方案1】:

    我刚刚发现我查看了错误的文档。因为我有一个 SAM 模板 而不是 Cloudformation 模板,所以我可以将 AutoPublishAlias 与直接附加到我的 lambda 函数的 ProvisionedConcurrencyConfig 一起使用。

    知道了这一点,解决方案就简单多了 - 该版本不是必需的,因为 AWS::Serverless::Function 的 SAM 模板版本直接支持 ProvisionedConcurrencyConfig - 只要还设置了 AutoPublishAlias

    这是我的工作模板:

    {
        "AWSTemplateFormatVersion": "2010-09-09",
        "Transform": "AWS::Serverless-2016-10-31",
        "Description": "Desc.",
        "Parameters": { },
        "Resources": {
          "MyLambdaFunction": {
            "Type": "AWS::Serverless::Function",
            "Properties": {
              "AutoPublishAlias": "V1",
              "ProvisionedConcurrencyConfig": {
                "ProvisionedConcurrentExecutions": 1
              },
              "Runtime": "dotnetcore3.1",
              "CodeUri": "MyCodeUri",
              "MemorySize": 1024,
              "Timeout": 30,
              "Events": {
                "HttpEvent1": {
                  "Type": "Api",
                  "Properties": {
                    "Path": "/v1/test",
                    "Method": "GET",
                    "RestApiId": {
                      "Ref": "ApiGateway"
                    }
                  }
                }
              },
              "Handler": "MyNamespace::MyNamespace.MyFunc::RunAsync"
            }
          },
          "ApiGateway": {
            "Type": "AWS::Serverless::Api",
            "Properties": {
              "StageName": {
                "Ref": "ApiStageName"
              },
              "Cors": {
                "AllowCredentials": true,
                "AllowHeaders": "'*'",
                "AllowMethods": "'*'",
                "AllowOrigin": "'*'",
                "MaxAge": "'600'"
              }
            }
          }
        },
        "Outputs": {
          "ApiUrl": {
            "Description": "API Gateway Endpoint URL",
            "Value": {
              "Fn::Sub": "https://${ApiGateway}.execute-api.${AWS::Region}.${AWS::URLSuffix}/${ApiStageName}"
            },
            "Export": {
              "Name": {
                "Fn::Sub": "${AWS::StackName}-ApiUrl"
              }
            }
          }
        }
      }
    

    旁注:我还尝试将相同的AutoPublishAlias 应用于同一堆栈中的多个函数 - 它可以工作,因此它不需要是唯一的。

    【讨论】:

      猜你喜欢
      • 2021-01-24
      • 2019-12-09
      • 2021-05-11
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      • 2018-04-23
      • 2019-08-11
      • 2019-01-09
      相关资源
      最近更新 更多