

# Turning on the lineage system table

## To turn on the system table (console)
<a name="catalog-lineage-system-table-enable-console"></a>

1. Open the AWS Glue console at [https://console.aws.amazon.com/glue/](https://console.aws.amazon.com/glue/).

1. In the navigation pane, under **Data Catalog**, choose **Catalog settings**.

1. Under **System Table**, select **Lineage System Table**.

1. Choose **Save**.

## To turn on the system table (AWS CLI)
<a name="catalog-lineage-system-table-enable-cli"></a>

```bash
aws glue put-data-catalog-export-configuration \
  --lineage S3TablesExport=ENABLED \
  --region us-east-1
```

To check the setting:

```bash
aws glue get-data-catalog-export-configuration --region us-east-1
```

In the response, `Lineage.S3TablesExport` is the setting and `Lineage.S3TablesExportState.Status` is the status. The status is one of `ENABLING`, `ENABLED`, `DISABLING`, `DISABLED`, or `FAILED`.

To turn it off, set `--lineage S3TablesExport=DISABLED`.

Settings that you leave out of the request don't change. For example, the preceding command doesn't change the asset export setting (`--assets`).

To use a customer managed key, add `--s3-tables-configuration 'EncryptionConfiguration={SseAlgorithm=aws:kms,KmsKeyArn={{key-ARN}}}'`. Without it, the table bucket uses SSE-S3 (`SseAlgorithm=AES256`).

The system table and the asset export share one `aws-catalog` table bucket, so they share one encryption configuration. If the bucket already exists with a different encryption configuration, the request fails. To change the encryption, turn off both exports, delete the `aws-catalog` table bucket, and turn them on again.

The calling identity needs `glue:PutDataCatalogExportConfiguration`, `glue:GetDataCatalogExportConfiguration`, `s3tables:CreateTableBucket`, `s3tables:GetTableBucket`, `s3tables:CreateTable`, `s3tables:PutTableBucketEncryption`, and `s3tables:PutTableBucketPolicy`. AWS Glue creates the `aws-catalog` table bucket with the caller's credentials. If you use a customer managed key, the caller also needs `kms:DescribeKey` on that key.

## To turn off the system table (AWS CLI)
<a name="catalog-lineage-system-table-enable-cli-off"></a>

```bash
aws glue put-data-catalog-export-configuration \
  --lineage S3TablesExport=DISABLED \
  --region us-east-1
```

To check, run `aws glue get-data-catalog-export-configuration`. `Lineage.S3TablesExportState.Status` is `DISABLED` when the system table is off.

When you turn off the system table:
+ No new lineage events are written to the table.
+ Existing records stay in the table and you can still query them. The `aws-catalog` table bucket and the `events` table aren't deleted.
+ The asset export setting doesn't change.
+ When you turn it on again, the table also receives the events posted while it was off that are still within the raw event retention period. The table can then contain duplicate rows; to count events exactly, deduplicate on `event_subject_id` and `event_time`.

To remove the lineage records, delete the `events` table in the `lineage` namespace of the `aws-catalog` table bucket. If you turn the system table on again later, only events still within the raw event retention period are written to the new table.

## Encryption
<a name="catalog-lineage-system-table-enable-encryption"></a>

Amazon S3 Tables encrypts the system table with SSE-S3 by default. If you use a customer managed AWS KMS key, its key policy must allow the `systemtables.catalog.amazonaws.com` and `maintenance.s3tables.amazonaws.com` service principals to use `kms:GenerateDataKey`, `kms:Decrypt`, and `kms:DescribeKey`. Only symmetric keys are supported.

## Grant read access
<a name="grant-read-access"></a>

1. Turn on integration with AWS analytics services for the `aws-catalog` table bucket. For instructions, see [Integrating Amazon S3 Tables with AWS analytics services](https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-tables-integrating-aws.html) in the Amazon S3 User Guide.

1. In AWS Lake Formation, grant `SELECT` on the `events` table, or on all tables in the `lineage` database, to each principal that queries lineage.

1. Attach the following policy to each querying principal, or to the IAM role of the engine it uses.

   ```json
   {
     "Version": "2012-10-17",
     "Statement": [
       {
         "Sid": "S3TablesReadAccess",
         "Effect": "Allow",
         "Action": [
           "s3tables:GetTableBucket", "s3tables:ListTableBuckets",
           "s3tables:GetNamespace", "s3tables:ListNamespaces",
           "s3tables:GetTable", "s3tables:ListTables",
           "s3tables:GetTableMetadataLocation", "s3tables:GetTableData"
         ],
         "Resource": [
           "arn:aws:s3tables:us-east-1:111122223333:bucket/aws-catalog",
           "arn:aws:s3tables:us-east-1:111122223333:bucket/aws-catalog/*"
         ]
       },
       {
         "Sid": "GlueDataCatalogReadAccess",
         "Effect": "Allow",
         "Action": ["glue:GetCatalog", "glue:GetDatabase", "glue:GetDatabases", "glue:GetTable", "glue:GetTables"],
         "Resource": [
           "arn:aws:glue:us-east-1:111122223333:catalog",
           "arn:aws:glue:us-east-1:111122223333:catalog/s3tablescatalog",
           "arn:aws:glue:us-east-1:111122223333:catalog/s3tablescatalog/aws-catalog",
           "arn:aws:glue:us-east-1:111122223333:database/s3tablescatalog/aws-catalog/*",
           "arn:aws:glue:us-east-1:111122223333:table/s3tablescatalog/aws-catalog/*/*"
         ]
       },
       {
         "Sid": "LakeFormationDataAccess",
         "Effect": "Allow",
         "Action": "lakeformation:GetDataAccess",
         "Resource": "*"
       }
     ]
   }
   ```

Queries fail without `s3tables:GetTableData`, even when the table is visible in the catalog. If the table uses a customer managed key, the principal also needs `kms:Decrypt` on that key.

## Query the table
<a name="catalog-lineage-system-table-enable-query"></a>
+ **Amazon Athena.** The table appears under the `s3tablescatalog/aws-catalog` catalog, with `lineage` as the database. Query it as `"s3tablescatalog/aws-catalog"."lineage"."events"`.
+ **Amazon Redshift.** The S3 Tables catalog is mounted as an external database. Use three-part names.
+ **Amazon EMR.** Read the table through the AWS Glue Iceberg REST endpoint or the Amazon S3 Tables catalog client.

## Set a retention period
<a name="catalog-lineage-system-table-enable-retention"></a>

By default, lineage events stay in the table until you delete them. To remove older events automatically, turn on record expiration for the `events` table. A record expires based on its `event_time`.

For example, to keep 90 days of lineage events:

```bash
aws s3tables put-table-record-expiration-configuration \
  --table-arn arn:aws:s3tables:us-east-1:111122223333:bucket/aws-catalog/table/{{table-id}} \
  --value '{"status":"enabled","settings":{"days":90}}' \
  --region us-east-1
```

Replace {{table-id}} with the ID of the `events` table, which you can get with the following command:

```bash
aws s3tables get-table \
  --table-bucket-arn arn:aws:s3tables:us-east-1:111122223333:bucket/aws-catalog \
  --namespace lineage \
  --name events
```

An event with an `event_time` of January 1 becomes eligible to expire on April 1, and Amazon S3 removes it within 24 to 48 hours after that. Expired records can't be recovered. To stop expiring records, set the value to `{"status":"disabled"}`.

The calling identity needs `s3tables:PutTableRecordExpirationConfiguration`. For the console steps and for monitoring expiration, see [Record expiration for tables](https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-tables-record-expiration.html) in the Amazon S3 User Guide.