2

我有一個簡單的Lambda函數,通過SES發送電子郵件。我可以使用帶有所需數據的POST請求調用它,它會發送一封電子郵件。我的問題是,我可以用什麼方法來保證這個功能?目前,任何人都可以調用該端點並使用任何數據執行該功能。如何保護AWS Lambda功能?

+2

您不能保護客戶端代碼,除非考慮模糊處理的安全措施。我想,任何基本的聯繫表格都容易受到垃圾郵件的攻擊。 –

+3

@ChrisG'aws-lambda'是一種服務器端技術 – LifeQuery

回答

5

您需要爲您的API網關設置授權人。這tutorial是一個很好的起點。

總之,你需要:

  1. Create a Cognito User Pool
  2. Create a Cognito Identity Pool that uses this User Pool
  3. Make the client to log in and retrieve Cognito credentials
  4. Make the client to send authorization headers for all requests
  5. Set an authorizer in your Lamba function

ÿ我們serverless.yml看起來像這樣與授權配置:

functions: 
    hello: 
    handler: handler.hello 
    events: 
     - http: 
      path: hello 
      method: post 
      authorizer: 
      arn: YOUR_USER_POOL_ARN 

你不需要被限制在一個Cognito授權。您可以使用配置使用Google+,Facebook等的授權人

此設置意味着蘭巴功能將只通過身份驗證的用戶觸發,你可以找出哪些是通過檢查event對象的用戶ID:

event.requestContext.authorizer.claims.sub