簡體   English   中英

是否可以從 AWS 無服務器 API 中的其他 lambda 函數調用 lambda function?

[英]Is it possible to call lambda function from other lambda functions in AWS serverless API?

我正在使用 AWS SAM 模板和 ASP.Net Core 創建無服務器 API。 我想知道是否可以從多個 lambda 函數調用一個公共的 lamnda function?

我有 2 個用於用戶身份驗證的 API。

  1. /用戶/認證
  2. /管理員/認證

現在,當用戶調用這些 API 端點時,我想調用一個公共的 lambda function,如下所示:

public AuthResponse Authenticate(AuthInfo info, int role);

我獲得了一個用戶角色,基於該角色調用 API 端點。 例如,如果/user/authetication被調用,則role=1否則role=0 然后我希望 Authenticate() lambda 根據AuthInfo + Role執行用戶身份驗證。

我想這樣做是因為我的所有用戶都存儲在同一個表中,我想交叉驗證用戶是否具有訪問該功能的正確角色。

我還將分享用於上述 API 的serverless.template的一部分。

/管理員/認證

"Handler": "Server::API.Admin::Authenticate",
        "Description" : "Allows admin to authenticate",
        "Runtime": "dotnetcore2.1",
        "CodeUri": "",
        "MemorySize": 256,
        "Timeout" : 300,
        "Role": {"Fn::GetAtt" : [ "LambdaExecutionRole", "Arn"]},
        "FunctionName" : "AdminAuthenticate",
        "Events": 
        {
          "PutResource": 
          {
            "Type": "Api",
            "Properties": 
            {
              "Path": "/v1/admin/authenticate",
              "Method": "POST"
            }
          }
        }
      }
    }

/用戶/認證

"Handler": "Server::API.User::Authenticate",
        "Description" : "Allows user to authenticate",
        "Runtime": "dotnetcore2.1",
        "CodeUri": "",
        "MemorySize": 256,
        "Timeout" : 300,
        "Role": {"Fn::GetAtt" : [ "LambdaExecutionRole", "Arn"]},
        "FunctionName" : "UserAuthenticate",
        "Events": 
        {
          "PutResource": 
          {
            "Type": "Api",
            "Properties": 
            {
              "Path": "/v1/user/authenticate",
              "Method": "GET"
            }
          }
        }
      }
    }

正如您在上面看到的,創建了 2 個 lambda 函數AdminAuthenticateUserAuthentication 我希望這些 lambda 函數共享公共代碼。

有誰知道該怎么做?

感謝致敬。

我可以考慮 2 個選項來實現您的目標。 在第一個選項中,您使用多個 Lambda 函數,每個端點一個,都指向相同的代碼庫。 在第二個選項中,您有一個 Lambda function 來處理所有身份驗證需求。

單一代碼庫,多種功能

在這種情況下,您可以使用 2 個函數定義模板文件,但使用CodeUri屬性指向相同的代碼庫。

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Transform": "AWS::Serverless-2016-10-31",
    "Resources": {
        "AdminFunction": {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "Handler": "Server::API.Admin::Authenticate",
                "Description": "Allows admin to authenticate",
                "Runtime": "dotnetcore2.1",
                "CodeUri": "./codebase_path/",
                "MemorySize": 256,
                "Timeout": 300,
                "FunctionName": "AdminAuthenticate",
                "Events": {
                    "PutResource": {
                        "Type": "Api",
                        "Properties": {
                            "Path": "/v1/admin/authenticate",
                            "Method": "POST"
                        }
                    }
                }
            }
        },
        "UserFunction": {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "Handler": "Server::API.User::Authenticate",
                "Description": "Allows user to authenticate",
                "Runtime": "dotnetcore2.1",
                "CodeUri": "./codebase_path/",
                "MemorySize": 256,
                "Timeout": 300,
                "FunctionName": "UserAuthenticate",
                "Events": {
                    "PutResource": {
                        "Type": "Api",
                        "Properties": {
                            "Path": "/v1/user/authenticate",
                            "Method": "POST"
                        }
                    }
                }
            }
        }
    }
}

單一代碼庫,單一 function

在這種情況下,您將在 API 網關上公開 2 個端點,但它們將被定向到 function 上的同一個處理程序。因此,您需要在代碼中編寫一些邏輯以正確處理登錄。 event object 傳遞給您的 Lambda function 將在path屬性中包含有關原始 URL 的信息( 參考——即使這是針對 Lambda 代理,仍然適用)。

這種情況下的模板文件類似於(注意我將 Admin/User 術語替換為“Any”,因為這將處理任何形式的身份驗證):

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Transform": "AWS::Serverless-2016-10-31",
    "Resources": {
        "AnyFunction": {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "Handler": "Server::API.Any::Authenticate",
                "Description": "Allows any to authenticate",
                "Runtime": "dotnetcore2.1",
                "CodeUri": "./hello_world/",
                "MemorySize": 256,
                "Timeout": 300,
                "Events": {
                    "UserEndpoint": {
                        "Type": "Api",
                        "Properties": {
                            "Path": "/v1/user/authenticate",
                            "Method": "POST"
                        }
                    },
                    "AdminEndpoint": {
                        "Type": "Api",
                        "Properties": {
                            "Path": "/v1/admin/authenticate",
                            "Method": "POST"
                        }
                    }
                }
            }
        }
    }
}

您可以通過任何 lambda function調用lambda 函數,在您選擇的語言中使用 aws sdk,並且您已經定義了調用 function。 作為參考,這里是boto3 invoke definition 的鏈接

您使用通用代碼庫進行身份驗證的方法不正確。

如果您需要 lambda function 來檢查或驗證特定請求,您可以為此設置自定義授權方,用您的話說,共享 lambda 代碼或在調用您為特定端點設置的 lambda 之前先調用它,有可能如果需要,傳遞自定義數據。

Lambda 授權方(或作為自定義授權方)API 網關功能,它使用 Lambda function 來控制對您的 API 的訪問。

在此處輸入圖像描述

如果這仍然不能解決您的問題並且您想要通用代碼庫,您可以將多達 api 個端點指向相同的 lambda function。然后您必須在代碼庫中處理事件 ['resources'] 這不是推薦的方式,但您可以使用它。

您可以參考aws 示例來設置自定義授權方,或者文檔足夠公平以消除您的所有疑慮。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM