Run-time monitoring via AWS X-Ray can help with achieving Least Privilege IAM Permissions. We recently released a new tool to monitor X-Ray traces and generate Least Privilege IAM Policies.

I’m pleased to announce that we’ve recently released a new open source project “AWS Least Privilege”. The project aims to streamline the process of collecting run-time resource usage information from X-Ray and reaching a Least Privilege IAM Policy for a given application. Project is available on GitHub at: https://github.com/functionalone/aws-least-privilege.

AWS X-Ray provides in-depth information about service API calls executed via the AWS SDK. Using this information, it is possible to build a profile of the AWS resources and actions that are actually used by an application (or part of an application, such as a Lambda function) and generate a precise policy document reflecting what resources are used and how they are accessed.

In this article, I’d like to take the chance to walk through the process of enabling X-Ray and using our new open source tool to reach a least privilege IAM policy. We’ll start with a demo Serverless application from Serverless Stack, which uses Lambda and DynamoDB for the back-end. The application with a full tutorial is available at: serverless-stack.com. I won’t go into the full setup process as this is documented very nicely at serverless-stack.com.

Once setup. you will have 5 lambda functions defined each using a single shared IAM role named: notes-app-api-prod-us-east-1-lambdaRole. The role has an in-line policy which uses the following allow statement to grant DynamoDB access:

This IAM configuration has a few issues. First the allow statement grants “*” access to all DynamoDB tables and second the role is shared between multiple Lambda functions. Creating a shared role is the default option when using the serverless framework, but is not best practice when trying to achieve a least privilege setup. With least privilege, the goal is to run each Lambda function with a role granting only the minimal permissions required for the function to operate and avoiding a wide set of permissions that is common to all functions. Let’s see how we can improve this using run-time information from X-Ray.

Enabling AWS X-Ray

First step for collecting run-time information is to configure AWS X-Ray. Enabling X-Ray is a two step process:

First, enable “active tracing” configuration on the Lambda functions. This can be done via the AWS Console UI as documented here: https://docs.aws.amazon.com/lambda/latest/dg/enabling-x-ray.html. But, since the project is using the Serverless Framework, I suggest doing this as part of the serverless.yml setup. You will need to add to the serverless.yml file the permission statement:

Then there is need to add a Resource configuration for each function to enable active tracing:

After doing these changes there is of course need to re-deploy the functions using:

serverless deploy

We now have X-Ray configured and tracing the back-end application. After using the application we can go to the X-Ray console and see the service map which is generated:

We can also view specific traces. For example, here is the trace for the “update” function:

From looking at the trace it is obvious that the update function uses only the “UpdateItem” action on the “notes” table and doesn’t require the full permissions granted in the serverless.yml file. Now let’s see how we can use this information to create least privilege IAM policies.

Scanning X-Ray to achieve Least Privilege

First we will need to install the aws-least-privilige npm:

npm install -g aws-least-privilege

This will install the command line tool: xray-privilege-scan.

We can now run the tool to scan X-Ray traces and generate IAM policy documents per Lambda function. We can run the tool with no parameters and it will scan the last 60 minutes of X-Ray traces (for a full list of options run: xray-privilege-scan -h). Running the tool will produce output of the form:

The tool will create a policy doc named <unique-id>.policy.json per Lambda function. We can take a look at the policy doc for the “update” function:

We now have a policy document which describes the resources and actions used by the Lambda “update” function. The policy document can be used directly in the AWS console, but take note that it contains an extra Description field which needs to be removed if doing a copy-paste. Since our sample application is using the Serverless Framework, it is probably best to incorporate the generate policies in to the serverless.yml file. You can check out the following serverless.yml file which I’ve done this for here: https://github.com/glicht/serverless-stack-demo-api/blob/least-privilege/serverless.yml.

A few notes about how the xray-privilege-scan tool works:

The tool takes into account multiple traces for the same Lambda function and will merge the resources accessed (and associated actions) into the generated policy.

The tool will try to group together resources of the same type which are accessed by the same set of actions. For example, if two S3 buckets are both accessed with actions GetObject and PutObject a policy statement of the following form will be generated: