# Glue Data Connector

Connect to and query tables in an AWS Glue Data Catalog

The Glue Data Connector enables federated SQL querying on tables in an AWS Glue Data Catalog.

```yaml
datasets:
  - from: glue:tpch.lineitem
    name: lineitem
    params:
      glue_region: us-east-1
      glue_key: ${env:SPICE_AWS_KEY} # Optional.
      glue_secret: ${env:SPICE_AWS_SECRET} # Optional.
```

## Configuration

### `from`

Specify a table using the format, `glue:<database>.<table>` by replacing `<database>` with the name of the Glue database and `<table>`with the name of the table inside of the `<database>`.

### `name`

The dataset name. This will be used as the table name within Spice.

Example:

```sql
SELECT COUNT(*) FROM lineitem;
```

```shell
+----------+
| count(*) |
+----------+
| 6001215  |
+----------+
```

The dataset name cannot be a [reserved keyword](https://spiceai.org/docs/reference/spicepod/keywords).

### `params`

The following parameters are supported for configuring the connection to the Glue Data Catalog:

| Parameter Name | Definition |
| - | - |
| `glue_region` | The AWS region for the Glue Data Catalog. E.g. `us-west-2`. |
| `glue_catalog_id` | The Glue catalog ID. For Amazon S3 Tables, use the format `<account_id>:s3tablescatalog/<table_bucket_name>`. If not provided, the default catalog for the account is used. |
| `glue_key` | Access key (e.g. AWS\_ACCESS\_KEY\_ID for AWS). If not provided, credentials will be loaded from environment variables or IAM roles. |
| `glue_secret` | Secret key (e.g. AWS\_SECRET\_ACCESS\_KEY for AWS). If not provided, credentials will be loaded from environment variables or IAM roles. |
| `glue_session_token` | Session token (e.g. AWS\_SESSION\_TOKEN for AWS) for temporary credentials |
| `glue_iam_role_source` | Optional. IAM role credential source. `auto` (default) uses the default AWS credential chain, `metadata` uses only instance/container metadata (IMDS, ECS, EKS/IRSA), `env` uses only environment variables. |

The following parameters control how the embedded S3 reader fetches Parquet/ORC/CSV data files referenced by Glue table metadata. They are inherited from the [S3 data connector](https://spice.ai/docs/building-blocks/data-connectors/s3). `glue_endpoint` and `glue_url_style` also apply to Iceberg-format tables, whose object I/O is handled by the Iceberg client. The other parameters do not apply to Iceberg-format tables.

| Parameter Name | Definition |
| - | - |
| `glue_endpoint` | Optional. Custom S3-compatible endpoint URL used when reading Parquet/ORC/CSV data files and Iceberg table files, for example `https://s3.us-east-1.amazonaws.com` or `http://minio.local:9000`. Leave unset for AWS S3. |
| `glue_url_style` | Optional. S3 URL addressing style for Parquet/ORC/CSV and Iceberg files. One of `vhost` or `path`. When unset, Parquet/ORC/CSV reads use the S3 connector's [automatic detection](https://spice.ai/docs/building-blocks/data-connectors/s3#s3_url_style), and Iceberg tables use `path` when the bucket in the table's Iceberg `metadata_location` contains a dot. |
| `glue_versioning` | Optional. Enables S3 object versioning support for Parquet/ORC/CSV data files when set to `enabled`. Defaults to `enabled`. |
| `client_timeout` | Optional. Timeout for the underlying S3 client used to fetch Parquet/ORC/CSV data files, for example `30s`. |
| `allow_http` | Optional. Set to `true` to allow insecure HTTP for the S3 endpoint used to read Parquet/ORC/CSV data files. Defaults to `false`. Required when `glue_endpoint` uses an `http://` scheme. |

## Examples

### Basic Glue Table

```yaml
datasets:
  - from: glue:tpch.lineitem
    name: lineitem
    params:
      glue_region: us-east-1
      glue_key: ${env:AWS_ACCESS_KEY_ID}
      glue_secret: ${env:AWS_SECRET_ACCESS_KEY}
```

### Amazon S3 Tables

Connect to tables in [Amazon S3 Tables](https://aws.amazon.com/s3/features/tables/) using the `glue_catalog_id` parameter with the S3 Tables catalog format:

```yaml
datasets:
  - from: glue:my_namespace.orders
    name: orders
    params:
      glue_catalog_id: 123635965758:s3tablescatalog/my-table-bucket
      glue_region: us-east-2
      glue_key: ${env:AWS_ACCESS_KEY_ID}
      glue_secret: ${env:AWS_SECRET_ACCESS_KEY}
```

## Authentication

If AWS credentials are not explicitly provided in the configuration, the connector will automatically load credentials from the following sources in order. These credentials will be used to connect to the S3 bucket as well as the Glue catalog.

1. **Environment Variables**:

   - `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`
   - `AWS_SESSION_TOKEN` (if using temporary credentials)

2. **Shared AWS Config/Credentials Files**:

   - Config file: `~/.aws/config` (Linux/Mac) or `%UserProfile%\.aws\config` (Windows)

   - Credentials file: `~/.aws/credentials` (Linux/Mac) or `%UserProfile%\.aws\credentials` (Windows)

   - The `AWS_PROFILE` environment variable can be used to specify a named profile, otherwise the `[default]` profile is used.

   - Supports both static credentials and SSO sessions

   - Example credentials file:

     ```ini
     # Static credentials
     [default]
     aws_access_key_id = YOUR_ACCESS_KEY
     aws_secret_access_key = YOUR_SECRET_KEY

     # SSO profile
     [profile sso-profile]
     sso_start_url = https://my-sso-portal.awsapps.com/start
     sso_region = us-west-2
     sso_account_id = 123456789012
     sso_role_name = MyRole
     region = us-west-2
     ```

   > **Tip**
   >
   > To set up SSO authentication:
   >
   > 1. Run `aws configure sso` to configure a new SSO profile
   > 2. Use the profile by setting `AWS_PROFILE=sso-profile`
   > 3. Run `aws sso login --profile sso-profile` to start a new SSO session

3. **AWS STS Web Identity Token Credentials**:

   - Used primarily with OpenID Connect (OIDC) and OAuth
   - Common in Kubernetes environments using IAM roles for service accounts (IRSA)

4. **ECS Container Credentials**:

   - Used when running in Amazon ECS containers
   - Automatically uses the task's IAM role
   - Retrieved from the ECS credential provider endpoint
   - Relies on the environment variable `AWS_CONTAINER_CREDENTIALS_RELATIVE_URI` or `AWS_CONTAINER_CREDENTIALS_FULL_URI` which are automatically injected by ECS.

5. **AWS EC2 Instance Metadata Service (IMDSv2)**:

   - Used when running on EC2 instances.
   - Automatically uses the instance's IAM role.
   - Retrieved securely using [IMDSv2](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html).

The connector will try each source in order until valid credentials are found. If no valid credentials are found, an authentication error will be returned.

> **Note**
>
> **IAM Permissions** Regardless of the credential source, the IAM role or user must have appropriate S3/Glue permissions (e.g., `s3:ListBucket`, `glue:GetTable`) to access the tables. If the Spicepod connects to multiple different AWS services, the permissions should cover all of them.

### Required IAM Permissions

The IAM role or user needs the following permissions to access Iceberg tables in S3/Glue:

```json
{
    "Version": "2012-10-17",
    "Statement": [
      {
        "Effect": "Allow",
        "Action": ["s3:ListBucket"],
        "Resource": "arn:aws:s3:::company-bucketname-datasets"
      },
      {
        "Effect": "Allow",
        "Action": ["s3:GetObject", "s3:PutObject"],
        "Resource": "arn:aws:s3:::company-bucketname-datasets/*"
      },
      {
        "Effect": "Allow",
        "Action": [
          "glue:GetCatalog",
          "glue:GetDatabases",
          "glue:GetDatabase",
          "glue:GetTable",
          "glue:GetTables",
          "glue:UpdateTable"
        ],
        "Resource": "*"
      }
    ]
}
```

### Permission Details

| Permission | Purpose |
| - | - |
| `s3:ListBucket` | Required. Allows scanning all objects from the bucket |
| `s3:GetObject` | Required. Allows fetching objects |
| `s3:PutObject` | Required for write operations. Allows writing objects |
| `glue:GetCatalog` | Required. Retrieve metadata about the specified catalog. |
| `glue:GetDatabases` | Required. List the databases available in the current catalog. |
| `glue:GetDatabase` | Required. Retrieve metadata about the specified database. |
| `glue:GetTable` | Required. Retrieve metadata about the specified table. |
| `glue:GetTables` | Required. List the tables available in the current database. |
| `glue:UpdateTable` | Required for write operations. Commits new table snapshots. |

## ORC Data Files

Spice reads Glue tables whose `InputFormat` is ORC (`org.apache.hadoop.hive.ql.io.orc.OrcInputFormat`), with Hive partitioning enabled. Hive and Spark often write ORC data files without an extension, such as `000000_0`, so Spice reads extensionless objects as well as `.orc` objects. It skips zero-byte objects, such as S3 folder markers, job-marker files such as `_SUCCESS`, `_committed_*`, and `.crc` files, and objects under a `_temporary` staging path.

## Write Support

This connector supports writing data to Glue-managed Iceberg tables using SQL [`INSERT INTO`](https://spiceai.org/docs/reference/sql/dml#insert) statements. Writes are currently append-only: inserted data is added as new data files and registered through a new Iceberg table snapshot. Schema validation ensures inserted data matches the target table schema.

To enable writes, set `access: read_write` on the dataset:

```yaml
datasets:
  - from: glue:tpch.lineitem
    name: lineitem
    access: read_write
    params:
      glue_region: us-east-1
```

```sql
-- Insert with values
INSERT INTO lineitem (l_orderkey, l_partkey, l_quantity)
VALUES (1, 100, 5.0);

-- Insert from another table
INSERT INTO lineitem
SELECT * FROM staging_lineitem;
```

Inserting into partitioned Iceberg tables is supported. `UPDATE` and `DELETE` operations are not currently supported.

Write operations require `s3:PutObject` permission on the target S3 bucket and `glue:UpdateTable` permission to commit the new table snapshot to the Glue Data Catalog, in addition to the read permissions listed above. For more details, see [Data Ingestion](https://spiceai.org/docs/features/data-ingestion).

## Limitations

> **Warning**
>
> **Data Source/Data Format Restrictions**
>
> This catalog connector is limited to tables that use the S3 data source. Kinesis and Kafka data sources are not supported. Additionally, this catalog connector is limited to Iceberg tables and tables whose data format is Parquet, ORC, or CSV.
>
> A Hive ACID/transactional ORC table (the Glue table property `transactional` set to `true`, `yes`, or `1`) is refused at registration rather than registered and left unqueryable, because Spice does not implement Hive ACID snapshot semantics (`base_*`, `delta_*`, `delete_delta_*`):
>
> ```
> Cannot read Hive ACID/transactional ORC table '<table>', so queries against it will not resolve. Spice does not support Hive ACID snapshot semantics (`base_*`, `delta_*`, `delete_delta_*`). Export or materialize the current snapshot into a genuinely non-transactional ORC location and register that table instead. See: https://docs.spiceai.org/components/data-connectors/glue
> ```

> **Warning**
>
> **Performance Considerations**
>
> When using the Glue Data connector without acceleration, data is loaded into memory during query execution. Ensure sufficient memory is available, including overhead for queries and the runtime, especially with concurrent queries.
>
> Memory limitations can be mitigated by storing acceleration data on disk, which is supported by [`duckdb`](https://spiceai.org/docs/components/data-accelerators/duckdb) and [`sqlite`](https://spiceai.org/docs/components/data-accelerators/sqlite) accelerators by specifying `mode: file`.
>
> Each query retrieves data from the S3 source, which might result in significant network requests and bandwidth consumption. This can affect network performance and incur costs related to data transfer from S3.

## Cookbook

- A cookbook recipe to configure Glue as a data connector in Spice. [Glue Data Connector](https://github.com/spiceai/cookbook/tree/trunk/glue#readme)
