【问题标题】:How to secure an AWS Lambda function?如何保护 AWS Lambda 函数?
【发布时间】:2017-11-29 09:41:21
【问题描述】:

我有一个简单的 Lambda 函数,它通过 SES 发送电子邮件。我可以使用带有所需数据的 POST 请求调用它,它会发送一封电子邮件。我的问题是,我可以使用哪些方法来保护此功能?目前,任何人都可以调用该端点并使用任何数据执行函数。

【问题讨论】:

  • 您无法保护客户端代码,除非将混淆视为一种安全措施。我猜,任何基本的联系表格都容易被垃圾邮件发送。
  • @ChrisG aws-lambda 是一种服务器端技术

标签: javascript aws-lambda aws-api-gateway serverless-framework serverless-architecture


【解决方案1】:

您需要为您的 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 使用 authorizer 配置将如下所示:

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

您无需受限于 Cognito 授权方。您可以使用为 Google+、Facebook 等配置授权者。

此设置意味着 Lamba 功能将仅由经过身份验证的用户触发,您可以通过检查 event 对象来识别用户 ID:

event.requestContext.authorizer.claims.sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 2019-11-03
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多