📔
ServerlessDocs - Serverless File Service
  • Introduction
  • Why Serverless!!
  • Prerequisites
  • User Management
    • Authentication
    • Amazon Cognito
    • Create the User Pool
    • Create an App Client
    • Integrate the app with Cognito User Pool
    • Create the Identity Pool
    • Validate the setup
    • Troubleshooting
  • Manage Documents with AWS S3
    • Authorization
    • Update Cognito Identity Pool Auth Role
    • S3 Documents Bucket Folder Structure
    • Validate the Access Permissions
    • Public and Private files access
    • Upload Files to S3
    • Download Files
    • Delete Files
    • Share Files
  • More Security Configurations
    • Cognito user emails
    • Password policies
    • Allow only Organization and Whitelisted users to Signup
    • Allow admin users to upload Public files
    • Notify Admin users when a new user signup
    • Enable Multi-factor Authentication (MFA)
  • User Operations
    • Password Reset / Forgot Password
    • Resend Verification Code
  • Serverless APIs
    • APIs
    • Secure APIs using API Gateway Authorizer
    • Access Cognito values in Lambda function
    • Authorize APIs with OAuth 2 Scope
    • Fine-grained Access with AssumeRole
    • Notify Admin users on large file uploads
    • Generate a Month-To-Date Usage report
  • Source Code and Setup
    • Source Code
    • IAM Policies
    • S3 Bucket Policies
    • Suggestions / Feedback
    • More References
  • Deployment
    • AWS SAM
    • Serverless Framework
  • Contributors
    • Team
Powered by GitBook
On this page

Was this helpful?

  1. Serverless APIs

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.

In this context, the executing Lambda by default has only the AssumeRole permission so that it can transition to the new role. The target role must have all the policies to execute the function's logic correctly.

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
    });
  }
});

PreviousAuthorize APIs with OAuth 2 ScopeNextNotify Admin users on large file uploads

Last updated 4 years ago

Was this helpful?