Why do we need grantReadWriteData when I already attach a role to my lambda allowing all dynamoDB action?

huangapple go评论108阅读模式
英文:

Why do we need grantReadWriteData when I already attach a role to my lambda allowing all dynamoDB action?

问题

我正在查看一个示例项目,它定义了一个具有允许对 DynamoDB goals 表执行任何操作的角色的 Lambda 函数。

    const dynamoDbRole = new iam.Role(this, 'DynamoDbRole', {
      assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
    });

    const goalsPolicy = new Policy(this, 'GoalsPolicy', {
      policyName: 'GoalsPolicy',
      roles: [dynamoDbRole],
      statements: [
        new iam.PolicyStatement({
          effect: iam.Effect.ALLOW,
          actions: ['dynamodb:*'],
          resources: [goalsTable.tableArn],
        })
      ]
    });


     const functionUpdateGoal = new lambda.Function(this, 'FunctionUpdateGoal', {
      functionName: `${this.ProjectName}-UpdateGoal`,
      runtime: lambda.Runtime.NODEJS_12_X,
      description: 'Update goal for user id',
      handler: 'UpdateGoal.handler',
      memorySize: 256,
      timeout: cdk.Duration.seconds(120),
      role: dynamoDbRole,
      environment: { TABLE_NAME: goalsTable.tableName },
      code: lambda.Code.fromAsset(path.dirname('../functions/UpdateGoal.js')),
    });

但是我还看到下面这行代码:

goalsTable.grantReadWriteData(functionUpdateGoal);

我对为什么需要这行代码感到困惑,将角色附加到函数上不足以让它对 goals 表进行操作吗?为什么我们还需要让表授予读写权限?为什么在这种情况下需要双向操作?在授予资源权限时,是否总是这样的?

英文:

I am looking at a sample project, it defines a lambda that has a role that allow it to do anything to the dynamoDB goals table

    const dynamoDbRole = new iam.Role(this, 'DynamoDbRole', {
      assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
    });

    const goalsPolicy = new Policy(this, 'GoalsPolicy', {
      policyName: 'GoalsPolicy',
      roles: [dynamoDbRole],
      statements: [
        new iam.PolicyStatement({
          effect: iam.Effect.ALLOW,
          actions: ['dynamodb:*'],
          resources: [goalsTable.tableArn],
        })
      ]
    });


     const functionUpdateGoal = new lambda.Function(this, 'FunctionUpdateGoal', {
      functionName: `${this.ProjectName}-UpdateGoal`,
      runtime: lambda.Runtime.NODEJS_12_X,
      description: 'Update goal for user id',
      handler: 'UpdateGoal.handler',
      memorySize: 256,
      timeout: cdk.Duration.seconds(120),
      role: dynamoDbRole,
      environment: { TABLE_NAME: goalsTable.tableName },
      code: lambda.Code.fromAsset(path.dirname('../functions/UpdateGoal.js')),
    });

But I also see

goalsTable.grantReadWriteData(functionUpdateGoal);

under it. I am confused as to why we need this line, isn't it sufficient to attach the role to the function to let it operate on the goals table? why do we need to also let the table grant the read and write? Why do we need to do it both ways in this case? Is it always like that when we grant right to resources?

答案1

得分: 1

不,只需要一种方式。使用角色可以让您拥有更精细的权限级别,而grantReadWriteData允许您在一行代码中创建一个适用于几乎大多数用例的有效策略。

英文:

No, only a single way is required. Using a role allows you to have more fine grained permission levels, whereas grantReadWriteData allows you to create a policy in a single line of code which is effective for almost most use-cases.

huangapple
  • 本文由 发表于 2023年8月9日 06:45:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定