

# Security and permissions

## IAM permissions
<a name="athena-duckdb-permissions-iam-permissions"></a>

 DuckDB uses the same Athena API actions as engine version 3. Applications that read DuckDB results with GetQueryResultStream need athena:GetQueryResultStream, which is separate from athena:GetQueryResults. The JDBC and ODBC drivers version 4.0.0 use GetQueryResultStream by default on DuckDB workgroups that use managed query results. With query results in your own Amazon S3 bucket, they read the results directly from Amazon S3, which needs s3:GetObject on the result location. If Amazon S3 denies access, or the results use CSE-KMS, the drivers read the results through the Athena API instead. The two drivers also call athena:GetWorkGroup when they connect. If your IAM policies list Athena actions individually, allow both athena:GetQueryResultStream and athena:GetWorkGroup. The AWS managed policy AmazonAthenaFullAccess allows every Athena action and lets principals create the Athena service-linked role. For more information, see [Athena service-linked role](#athena-duckdb-permissions-service-linked-role). It does not cover your data in Amazon S3, your AWS KMS keys, or the driver download bucket. 

 Athena reads and writes your AWS Glue Data Catalog metadata and your Amazon S3 data with the credentials of the principal that runs the query. Grant that principal the AWS Glue, Amazon S3, and AWS KMS permissions for the tables and Amazon S3 paths that its queries read and write. 

 If your policies list AWS Glue actions individually, queries that read tables need glue:GetDatabase and glue:GetTable, plus glue:GetPartitions for partitioned Hive tables and glue:GetCatalog for Iceberg tables. Listing databases and tables, for example with SHOW TABLES, needs glue:GetDatabases and glue:GetTables. Reading data that is encrypted with SSE-KMS needs kms:Decrypt on the key. 

 You cannot create DuckDB secrets or attach other databases. All access to your data in Amazon S3 and to AWS Glue uses the credentials of the principal that runs the query. 

## Athena service-linked role
<a name="athena-duckdb-permissions-service-linked-role"></a>

 Athena uses the service-linked role `AWSServiceRoleForAmazonAthena` to publish query metrics for DuckDB to CloudWatch in your account. If the role doesn't exist yet, Athena creates it with the credentials of the principal that makes one of the following calls: 
+ `CreateWorkGroup` for a workgroup that uses DuckDB.
+ `UpdateWorkGroup` that changes a workgroup's engine version to DuckDB.
+ `StartQueryExecution` that runs a query on DuckDB from an Athena engine version 3 workgroup. For more information, see [Override the engine for a single query](duckdb-engine-override.md).

 Athena creates the role only once per account. To create it, the principal needs permission for `iam:CreateServiceLinkedRole`. The AWS managed policy `AmazonAthenaFullAccess` includes this permission. For more information, see [AWS managed policy: AmazonAthenaFullAccess](security-iam-awsmanpol.md#amazonathenafullaccess-managed-policy). If you use your own policies, add the following statement. It allows the principal to create only the Athena service-linked role. 

```json
{
    "Effect": "Allow",
    "Action": "iam:CreateServiceLinkedRole",
    "Resource": "arn:aws:iam::*:role/aws-service-role/ops.athena.amazonaws.com/AWSServiceRoleForAmazonAthena",
    "Condition": {
        "StringEquals": {
            "iam:AWSServiceName": "ops.athena.amazonaws.com"
        }
    }
}
```

 Alternatively, an administrator can create the role once for the account with the following command. 

```bash
aws iam create-service-linked-role --aws-service-name ops.athena.amazonaws.com
```

 If the role doesn't exist and the principal can't create it, the call fails with `InvalidRequestException` and one of the following messages: 
+ `CreateWorkGroup` and `UpdateWorkGroup` return the message `Access denied when calling CreateServiceLinkedRole operation`.
+ `StartQueryExecution` returns the error code `SLR_CREATION_ACCESS_DENIED` and a message that starts with `Running this query on DuckDB requires the Athena service-linked role`.

 If IAM hasn't finished creating the role, the call fails with the message `ServiceLinkedRole ... is not available yet. Please try again`. If IAM throttles the request, the call fails with `TooManyRequestsException` and the message `Rate exceeded while creating the Athena service-linked role. Please retry.` In both cases, retry the call. 

## Lake Formation tables
<a name="athena-duckdb-permissions-lake-formation-tables"></a>

 During preview, Amazon Athena, powered by DuckDB does not support tables whose Amazon S3 location is registered with AWS Lake Formation, including tables that grant permissions to IAMAllowedPrincipals. To see which locations are registered, in the Lake Formation console side navigation, choose **Data lake locations**. Every statement on such a table fails, whether the principal has full or filtered access, with an error message that contains "is registered with Lake Formation." This includes a query on a view whose definition reads such a table. In hybrid access mode, principals that you did not opt in to Lake Formation can still query the table if it grants permissions to IAMAllowedPrincipals. CREATE TABLE AS SELECT into a registered location can succeed, but you cannot then query, alter, or drop the new table. Do not grant IAM principals direct Amazon S3 access to a location that is registered with Lake Formation. 

## Amazon S3 Tables
<a name="athena-duckdb-permissions-amazon-s3-tables"></a>

 Tables in Amazon S3 table buckets work when the table buckets are integrated with AWS analytics services using IAM access control, which is the default. With Lake Formation access control, their location is registered with Lake Formation, and statements on their tables fail as on other tables in registered locations, as described in [Lake Formation tables](#athena-duckdb-permissions-lake-formation-tables). 

 To check which applies, in the Lake Formation console side navigation, choose **Data lake locations**. If the list includes your table buckets' location, such as s3://tables:us-west-2:111122223333:bucket/\*, it is registered with Lake Formation. 

## Reading files by Amazon S3 path
<a name="athena-duckdb-permissions-reading-files-by-amazon-s3-path"></a>

 DuckDB can read and write files by Amazon S3 path, for example with read\_parquet or COPY TO. These reads and writes do not go through the AWS Glue Data Catalog, so table permissions in AWS Glue or Lake Formation do not limit them. Only the principal's IAM permissions for Amazon S3 do. This holds even if you allow Amazon S3 access only for requests made through Athena, for example with the `aws:CalledVia` condition key, because DuckDB reads and writes by path are requests made through Athena. Only Amazon S3 paths can be read or written. Local files and HTTP or HTTPS URLs are refused. 

 The following example SQL queries show how to read files directly from Amazon S3, with a reader function or with a quoted Amazon S3 path in place of a table name: 

```sql
SELECT * FROM read_parquet('s3://bucket/path/*.parquet');
SELECT * FROM read_csv('s3://bucket/path/file.csv');
SELECT * FROM read_json('s3://bucket/path/file.json');
SELECT * FROM 's3://bucket/path/file.parquet';
```