使用 AWS Lake Formation 標籤式存取控制管理數百個資料表
Use Lake Formation Tag-Based Access Control to Manage Hundreds of Tables
AWS Lake Formation 的標籤式存取控制(TBAC)以 LF-Tags 管理數百個資料表的權限,讓用戶按標籤授予 SELECT 權限,而毋須逐一為資料表設定權限。新資料表套用相應標籤後,便可按標籤政策取得存取權限。文章範圍限於 AWS Glue Data Catalog 資源,不涵蓋跨帳戶共享或資料列層級安全性篩選器。
Managing access permissions for a data lake with hundreds of tables using individual GRANT statements leads to significant operational friction. When you use resource-based policies in AWS Lake Formation, you often find yourself writing complex scripts to update permissions every time a new table is added to the catalog. This manual overhead creates a "permission drift" where analysts lose access to new data, or worse, over-privileged roles accumulate permissions that are never cleaned up.
Tag-based access control (TBAC) solves this by decoupling the permission logic from the physical location of the data. Instead of granting access to database.table_a, you assign a metadata tag like Classification=Confidential to the table and grant the user access to any resource carrying that tag. This article explains how to implement this architecture using AWS Lake Formation, focusing on scaling your authorization model without increasing your administrative burden. The scope is limited to AWS Glue Data Catalog resources; it does not cover cross-account sharing or row-level security filters.
What you'll build
- A centralized tagging taxonomy in AWS Lake Formation.
- Automated permission assignments using tag-based policies.
- A scalable framework for onboarding new tables by simply applying a tag.
flowchart TD
Admin["IAM User / Admin"] -->|Define Tags| LF["AWS Lake Formation"]
LF -->|Assign Tags| Cat["AWS Glue Data Catalog"]
Cat -->|Protect| Tables["S3 Tables / Iceberg Tables"]
User["Data Analyst"] -->|Query via| Athena["Amazon Athena"]
Athena -->|Check Policy| LF
LF -->|Authorize| CatPrerequisites
- An AWS account with the Lake Formation service initialized.
- The AWS CLI v2 installed and configured.
- Existing AWS Glue databases and tables (e.g., Apache Iceberg tables).
- Permissions to create LF-Tags and modify Lake Formation settings.
Step 1: Define your tagging taxonomy
Before applying tags, you must define the key-value pairs that represent your data classification. In Lake Formation, these are called LF-Tags. I recommend keeping the taxonomy simple: use keys for dimensions like Environment, Sensitivity, or BusinessUnit.
Run the following AWS CLI command to create a tag:
aws lakeformation create-lf-tag \
--tag-key "Sensitivity" \
--tag-values "Public" "Confidential" "Restricted"
Verify the tag exists:
aws lakeformation get-lf-tag --tag-key "Sensitivity"
Step 2: Tag your existing resources
Once the tags exist, you associate them with your Glue databases or tables. You can tag an entire database, and by default, tables inside that database can inherit those tags, or you can tag tables individually for granular control.
To tag a specific table:
aws lakeformation add-lf-tags-to-resource \
--resource '{ "Table": { "DatabaseName": "<your-database>", "Name": "<your-table>" } }' \
--lf-tags TagKey="Sensitivity",TagValues=["Confidential"]
Check the association:
aws lakeformation get-resource-lf-tags \
--resource '{ "Table": { "DatabaseName": "<your-database>", "Name": "<your-table>" } }'
Step 3: Grant permissions via tags
Instead of granting SELECT on a specific table, you now grant SELECT on the tag. This is a one-time setup; any future table assigned the Confidential tag will automatically be accessible to the principal without further configuration.
aws lakeformation grant-permissions \
--principal DataLakePrincipalIdentifier="arn:aws:iam::<account-id>:role/<analyst-role>" \
--permissions "SELECT" \
--resource '{ "LFTagPolicy": { "ResourceType": "TABLE", "Expression": [{"TagKey": "Sensitivity", "TagValues": ["Confidential"]}] } }'
Verify that the principal has the permission:
aws lakeformation list-permissions \
--principal DataLakePrincipalIdentifier="arn:aws:iam::<account-id>:role/<analyst-role>"
Design decisions
- Tag-based access, not resource-based grants. Use tags when your environment exceeds a few dozen tables. Resource-based grants create a "N*M" management problem where N users and M tables require N*M individual policy entries. Tags allow you to manage access as a set (e.g., all tables with
Project=Alpha). - Database-level tagging, not table-level tagging. Tagging at the database level is easier to maintain. Reserve table-level tagging for exceptions where a specific table has a different sensitivity than the rest of the database.
- Deny-by-default, not implicit allow. Lake Formation enforces a "no-access" policy by default. Ensure your "Data Lake Administrators" have the necessary permissions to manage tags, but keep standard users restricted to only what their assigned tags permit.
Troubleshooting
- Symptom: User receives an "Access Denied" error when querying a table that clearly has the right tag.
-
Cause: The user might be missing the
lakeformation:GetDataAccesspermission in their IAM policy, or the database itself does not have the necessary LF-Tag permissions to allow inheritance. -
Fix: Ensure the user's IAM role has the
lakeformation:GetDataAccessaction, and verify theLF-Tagis applied to the parent database if you are relying on tag inheritance.
-
Cause: The user might be missing the
- Symptom: A new table is not visible despite having the correct tag.
-
Cause: The Lake Formation "Catalog Settings" might be set to
IAM access controlfor new tables rather thanLake Formation permissions. -
Fix: Check your database settings in the Glue Data Catalog. If the database is set to
IAM access control, Lake Formation will ignore your tags.
-
Cause: The Lake Formation "Catalog Settings" might be set to
- Symptom: Unable to create a new tag.
-
Cause: You lack the
lakeformation:CreateLFTagpermission. - Fix: Ensure your administrator user or role has the appropriate Lake Formation permissions enabled in the Lake Formation console under "Administrative roles and tasks."
-
Cause: You lack the
Clean up
If you are testing this in a sandbox account, ensure you clean up the resources to avoid ongoing maintenance:
- Remove the grant:
aws lakeformation revoke-permissions \
--principal DataLakePrincipalIdentifier="arn:aws:iam::<account-id>:role/<analyst-role>" \
--permissions "SELECT" \
--resource '{ "LFTagPolicy": { "ResourceType": "TABLE", "Expression": [{"TagKey": "Sensitivity", "TagValues": ["Confidential"]}] } }'
- Remove the tag from the resource:
aws lakeformation remove-lf-tags-from-resource \
--resource '{ "Table": { "DatabaseName": "<your-database>", "Name": "<your-table>" } }' \
--lf-tags TagKey="Sensitivity",TagValues=["Confidential"]
- Delete the LF-Tag definition:
aws lakeformation delete-lf-tag --tag-key "Sensitivity"
Conclusion
By moving from resource-specific grants to tag-based access control, you decouple your security posture from your schema growth. This architecture ensures that your security scales linearly with your data volume, rather than exponentially with your permission management effort. Your next step is to integrate LF-Tag application into your CI/CD pipeline—when your infrastructure-as-code (IAC) scripts deploy a new table, have them automatically apply the relevant Sensitivity or BusinessUnit tags as part of the deployment process.
Try it: Identify your most sensitive database and attempt to move its access control from individual table grants to a single tag-based policy for a test IAM role.
References
- Lake Formation Tag-Based Access Control
- Managing LF-Tags in Lake Formation
- Lake Formation Granting Permissions
About the examples: automated checks confirmed that 9 AWS CLI commands match the AWS service API models (operation and parameter names). The examples have not been run end to end, so test them in a non-production environment before relying on them.
Cover photo by Luca Volpe on Unsplash.
This article was drafted with AI assistance and is intended for technical review and fact-checking before publication.
來源:DEV Community · dev.to