AWS Big Data Blog
Effective data lakes using AWS Lake Formation, Part 2: Securing data lakes with row-level access control
Apr 2023: This post was updated with the latest dataset and the updated CloudFormation template.
July 2023: This post was reviewed for accuracy.
Increasingly, customers are looking at data lakes as a core part of their strategy to democratize data access across the organization. Data lakes enable you to handle petabytes and exabytes of data coming from a multitude of sources in varying formats, and gives users the ability to access it from their choice of analytics and machine learning tools. Fine-grained access controls are needed to ensure data is protected and access is granted to only those who require it.
AWS Lake Formation is a fully managed service that helps you build, secure, and manage data lakes, and provide access control for data in the data lake. Lake Formation row-level permissions allow you to restrict access to specific rows based on data compliance and governance policies. Lake Formation also provides centralized auditing and compliance reporting by identifying which principals accessed what data, when, and through which services.
Effective data lakes using AWS Lake Formation
|
This post demonstrates how row-level access controls work in Lake Formation, and how to set them up.
If you have large fact tables storing billions of records, you need a way to enable different users and teams to access only the data they’re allowed to see. Row-level access control is a simple and performant way to protect data, while giving users access to the data they need to perform their job. In the retail industry for instance, you may want individual departments to only see their own transactions, but allow regional managers access to transactions from every department.
Traditionally you can achieve row-level access control in a data lake through two common approaches:
- Duplicate the data, redact sensitive information, and grant coarse-grained permissions on the redacted dataset
- Load data into a database or a data warehouse, create a view with a WHERE clause to select only specific records, and grant permission on the resulting view
These solutions work well when dealing with a small number of tables, principals, and permissions. However, they make it difficult to audit and maintain because access controls are spread across multiple systems and methods. To make it easier to manage and enforce fine-grained access controls in a data lake, we announced a general availability of Lake Formation row-level access controls at re:Invent 2021. With the governed tables, you can create row-level filters and attach them to tables to restrict access to data for AWS Identity and Access Management (IAM) and SAMLv2 federated identities.
How data filters work for row-level security
Granting permissions on a table with row-level security (row filtering) restricts access to only specific rows in the table. The filtering is based on the values of one or more columns. For example, a salesperson analyzing sales opportunities should only be allowed to see those opportunities in their assigned territory and not others. We can define row-level filters to restrict access where the value of the territory column matches the assigned territory of the user.
With row-level security, we introduced the concept of data filters. Data filters make it simpler to manage and assign a large number of fine-grained permissions. You can specify the row filter expression using the WHERE clause syntax described in the PartiQL dialect.
Example use case
In this post, a fictional ecommerce company sells many different products, like books, toys, and jewelry. Customers can leave reviews and star ratings for each product, so other customers can make informed decisions about what they should buy. We use the a sample synthetic review dataset, which includes different products and customer reviews.
To illustrate the different roles and responsibilities of a data owner and a data consumer, we assume two personas: a data lake administrator and a data analyst. The administrator is responsible for setting up the data lake, creating data filters, and granting permissions to data analysts. Data analysts residing in different countries (for our use case, the US and Mexico) can only analyze product reviews for customers located in their own country and for compliance reasons, shouldn’t be able to see data for customers located in other countries. We have two data analysts: one responsible for the US marketplace and another for the Maxican marketplace. Each analyst uses Amazon Athena to analyze customer reviews for their specific marketplace only.
Set up resources with AWS CloudFormation
This post includes an AWS CloudFormation template for a quick setup. You can review and customize it to suit your needs.
The CloudFormation template generates the following resources:
- An AWS Lambda function (for Lambda-backed AWS CloudFormation custom resources). We use the function to copy sample data files from the public S3 bucket to your Amazon Simple Storage Service (Amazon S3) bucket.
- An S3 bucket to serve as our data lake.
- IAM users and policies:
DataLakeAdmin
DataAnalystUS
DataAnalystMX
- An AWS Glue Data Catalog database, table, and partition.
- Lake Formation data lake settings and permissions.
Before launching the CloudFormation template, you need to ensure that you disabled Use only IAM access control for new databases/tables by following steps:
- Sign in to the Lake Formation console in the
us-east-1
orus-west-2
Region. - Under Data catalog, choose Settings.
- Deselect Use only IAM access control for new databases and Use only IAM access control for new tables in new databases.
- Choose Save.
To launch the CloudFormation stack, complete the following steps:
- Sign in to the CloudFormation console in the same Region.
- Choose Launch Stack:
- Choose Next.
- For DatalakeAdminUserName and DatalakeAdminUserPassword, enter the user name and password you want for the data lake admin IAM user.
- For DataAnalystUsUserName and DataAnalystUsUserPassword, enter the user name and password you want for the data analyst user who is responsible for the US marketplace.
- For DataAnalystMxUserName and DataAnalystMxUserPassword, enter the user name and password you want for the data analyst user who is responsible for the Mexican marketplace.
- For DataLakeBucketName, enter the name of your data lake bucket.
- For DatabaseName and TableName, leave as the default.
- Choose Next.
- On the next page, choose Next.
- Review the details on the final page and select I acknowledge that AWS CloudFormation might create IAM resources.
- Choose Create.
Stack creation can take about 1 minute.
Query without data filters
After you set up the environment, you can query the product reviews table. Let’s first query the table without row-level access controls to make sure we can see the data. If you’re running queries in Athena for the first time, you need to configure the query result location.
Sign in to the Athena console using the DatalakeAdmin
user, and run the following query:
The following screenshot shows the query result. This table has only one partition, product_category=Video
, so each record is a review comment for a video product.
Let’s run an aggregation query to retrieve the total number of records per marketplace
:
The following screenshot shows the query result. The marketplace
column has five different values. In the subsequent steps, we set up row-based filters using the marketplace
column.
Set up data filters
Let’s start by creating two different data filters, one for the analyst responsible for the US marketplace, and another for the one responsible for the Mexican marketplace. The we grant the users their respective permissions.
Create a filter for the US marketplace data
Let’s first set up a filter for the US marketplace data.
- As the
DatalakeAdmin
user, open the Lake Formation console. - Choose Data filters.
- Choose Create new filter.
- For Data filter name, enter
reviews_US
. - For Target database, choose the database
lakeformation_tutorial_row_security
. - For Target table, choose the table
reviews
. - For Column-level access, leave as the default.
- For Row-level access, choose Filter rows, and enter
marketplace='US'
for Row filter expression. - Choose Create data filter.
Create a filter for the Mexican marketplace data
Let’s create another data filter to restrict access to the Mexican marketplace data.
- On the Data filters page, choose Create new filter.
- For Data filter name, enter
reviews_MX
. - For Target database, choose the database
lakeformation_tutorial_row_security
. - For Target table, choose the table
reviews
. - For Column-level access, leave as the default.
- For Row-level access, choose Filter rows, and enter
marketplace='MX'
for Row filter expression. - Choose Create data filter.
Grant permissions to the US data analyst
Now we have two data filters. Next, we need to grant permissions using these data filters to our analysts. We start by granting permissions to the DataAnalystUS
user.
- On the Data lake permissions page, choose Grant.
- For Principals, choose IAM users and roles, and choose the user
DataAnalystUS
. - For LF tags or catalog resources, choose Named data catalog resources.
- For Database, choose the database
lakeformation_tutorial_row_security
. - For Table – optional, choose the table
reviews
. - For Data filters – optional¸ select reviews_US.
- For Data filter permissions, select Select.
- Choose Grant.
The following screenshot show the available data filters you can attach to a table when configuring permissions.
Grant permissions to the Mexican data analyst
Next, complete the following steps to configure permissions for the user DataAnalystMX
:
- On the Data lake permissions page, choose Grant.
- For Principals, choose IAM users and roles, and choose the user
DataAnalystMX
. - For LF tags or catalog resources, choose Named data catalog resources.
- For Database, choose the database
lakeformation_tutorial_row_security
. - For Table – optional, choose the table
reviews
. - For Data filters – optional¸ select reviews_MX.
- For Data filter permissions, select Select.
- Choose Grant.
Query with data filters
With the data filters attached to the product reviews table, we’re ready to run some queries and see how permissions are enforced by Lake Formation.
Sign in to the Athena console using the DataAnalystUS
user. Run the following query to retrieve a few records, which are filtered based on the row-level permissions we defined:
The following screenshot shows the query result.
Similarly, run a query to count the total number of records per marketplace:
The following screenshot shows the query result. Only the marketplace US
shows in the results. This is because our user is only allowed to see rows where the marketplace
column value is equal to US
.
Switch to the DataAnalystMX
user and run the same query:
The following screenshot shows the query result. All of the records belong to the MX
marketplace.
Run the query to count the total number of records per marketplace:
The following screenshot shows the query result. Again, only the row belonging to the MX
marketplace is returned.
Clean up
Now to the final step, cleaning up the resources.
Conclusion
In this post, we covered how row-level security in Lake Formation enables you to control data access without needing to duplicate it or manage complicated alternatives such as views. We demonstrated how Lake Formation data filters can make creating, managing, and enforcing row-level permissions simple and easy.
When you want to grant permission on specific cell, you can include or exclude columns in the data filters in addition to the row filter expression. You can learn more about the cell filters in Part 1: Implementing cell-level and row-level security.
About the Authors
Noritaka Sekiyama is a Senior Big Data Architect on the AWS Glue and AWS Lake Formation team. He has 11 years of experience working in the software industry. Based in Tokyo, Japan, he is responsible for implementing software artifacts, building libraries, troubleshooting complex issues and helping guide customer architectures.
Sanjay Srivastava is a Principal Product Manager for AWS Lake Formation. He is passionate about building products, in particular products that help customers get more out of their data. During his spare time, he loves to spend time with his family and engage in outdoor activities including hiking, running, and gardening.