Fine-grained Access with AssumeRole

So far what we have seen is kind of a static assignment of the roles and using the services accordingly. When we run our code inside Lambda, it has its own role and permissions.

  • API Gateway Authorizer to validate that we have a valid Cognito session

  • API Gateway Scope to validate that the user has the permission to invoke the API

  • But how we do make sure that the Lambda function has the exact permissions as per the request role?

In that case, we will be using Fine-grained access control. Conceptually it is a very simple practice that is used by most of the AWS services.

We will be using the AssumeRole API inside the Lambda function based on the incoming Cognito profile detail.

const AWS = require('aws-sdk');
const sts = new AWS.STS();

sts.assumeRole({
  RoleArn: '<role arn>',
  RoleSessionName: 'MySession'
}, (err, data) => {
  if (err) { // an error occurred
    console.log('Cannot assume role');
    console.log(err, err.stack);
  } else { // successful response
  
    // This is important as AssumeRole will not automatically update the existing Lambda role
    AWS.config.update({
      accessKeyId: data.Credentials.AccessKeyId,
      secretAccessKey: data.Credentials.SecretAccessKey,
      sessionToken: data.Credentials.SessionToken
    });
  }
});

Last updated

Was this helpful?