【问题标题】:Limiting Lambda Permissions using AWS SAM and DynamoDB使用 AWS SAM 和 DynamoDB 限制 Lambda 权限
【发布时间】:2020-07-29 20:57:29
【问题描述】:

我一直在尝试使用 Cloudformation/SAM 将堆栈放在一起。下面的 lambda 函数是在 DynamoDB 中创建联系人的处理程序,所以我认为给它必要的最少权限是谨慎的做法。

我注意到 DynamoDBWritePolicy 允许的不仅仅是 PutItem,所以我想知道进一步锁定事物的最佳方法是什么。我明白这可能有点过头了,但这是出于学习目的。

这是我的 Lambda 定义:

CreateContact:
  Type: AWS::Serverless::Function
  Properties:
    Handler: index.handler
    Runtime: nodejs12.x
    Policies:
      - AWSLambdaBasicExecutionRole
      - DynamoDBWritePolicy:
          TableName: contacts
    CodeUri: ./src/handlers/create-contact
    Events:
      ApiEvent:
        Type: Api
        Properties:
          Path: /
          Method: post
          RestApiId:
            Ref: APIGateway

所以我的问题是:我应该只为 PutItem 创建一个新策略还是依赖于权限边界之类的东西,还是忽略任何其他选项?

【问题讨论】:

    标签: amazon-web-services aws-lambda


    【解决方案1】:

    你的方法没有错。您可以创建一个新策略来“允许”对特定资源的某些特定操作。

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "SpecificTable",
                "Effect": "Allow",
                "Action": [
                    "dynamodb:PutItem"
                ],
                "Resource": "arn:aws:dynamodb:*:*:table/MyTable"
            }
        ]
    }
    

    【讨论】:

      【解决方案2】:

      是的,SAM 提供的预定义策略模板包含的权限可能超出您的 lambda 函数所需的权限。如果您需要更细粒度的权限(这是最佳实践),您始终可以在 lambda 函数的 Policies 对象的 SAM 模板中设置它们,如下所示:

      CreateContact:
          Type: AWS::Serverless::Function
          Properties:
            Handler: index.handler
            Runtime: nodejs12.x
            Policies:
              - Version: 2012-10-17
                Statement:
                  - Effect: Allow
                    Action: dynamodb:PutItem
                    Resource: !Sub "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/contacts"
            CodeUri: ./src/handlers/create-contact
            Events:
              ApiEvent:
                Type: Api
                Properties:
                  Path: /
                  Method: post
                  RestApiId:
                    Ref: APIGateway
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-14
        • 1970-01-01
        • 2016-11-21
        • 2020-09-01
        • 2020-12-16
        • 2022-06-30
        • 2021-11-14
        • 2017-03-09
        相关资源
        最近更新 更多