

# Credentials and required permissions

The extension signs requests using Amazon credentials from a DuckDB secret. Use a secret of type `aws` or `s3`. The `Secret` option on `ATTACH` selects the secret by name.

The Athena extension obtains signing credentials from those secrets; it does not read profile files or environment credentials on its own. Configure the credential source through the `aws` extension's secret provider. An Athena `ATTACH` does not start a browser or SAML sign-in flow. Complete any required sign-in for the credential source before creating the secret.

An explicitly created secret is not the only credential source. When the loaded `aws` extension supplies a usable default `__aws_default` secret, the Athena extension can use it without a new `CREATE SECRET`. The explicit-secret procedure in this guide makes the signer clear and supports a named or refreshable credential chain.

<a name="connect-with-duckdb-extension-authentication-sources"></a>**Credential sources**

The `aws` extension's `credential_chain` provider supports the following `CHAIN` values. Configure the credentials required by the selected source. For named profiles, follow the recipes in [Use a named profile](#connect-with-duckdb-extension-authentication-profile).


**Credential sources**  

| Source | CHAIN value | 
| --- | --- | 
| Environment variables | env | 
| Profile configuration and credentials files | config | 
| Amazon EC2 instance profile | instance | 
| Container credentials | container | 
| STS role assumption | sts | 
| Web identity tokens | web\_identity | 
| IAM Identity Center | sso | 
| External credential process | process | 

`CHAIN 'sts'` requires `ASSUME_ROLE_ARN`. For role assumption configured in a profile, use `CHAIN 'config'`.

## Use refreshable credentials
<a name="connect-with-duckdb-extension-authentication-refresh"></a>

For a long session that uses temporary credentials, create a credential-chain secret with `REFRESH 'auto'`. In a new session, run the following commands and replace the Region and workgroup with your values.

```sql
LOAD athena FROM aws.amazon.com;
CREATE SECRET aws_identity (TYPE aws, PROVIDER credential_chain, REFRESH 'auto');
ATTACH 'athena://{{us-west-2}}/{{my-duckdb-workgroup}}' AS ath
    (TYPE athena, Secret 'aws_identity');
```

## Use a named profile
<a name="connect-with-duckdb-extension-authentication-profile"></a>

Set `PROFILE` on the secret to select a profile. For an IAM Identity Center profile, add `CHAIN 'sso'`; for a `credential_process` profile, add `CHAIN 'process'`. Run one of the following `CREATE SECRET` commands in a new session after loading the extension, then run the `ATTACH` command. Replace {{my-profile}} with your profile name.

```sql
CREATE SECRET profile_creds
    (TYPE aws, PROVIDER credential_chain, PROFILE '{{my-profile}}', REFRESH 'auto');
```

```sql
CREATE SECRET profile_creds
    (TYPE aws, PROVIDER credential_chain, PROFILE '{{my-profile}}',
     CHAIN 'sso', REFRESH 'auto');
```

```sql
CREATE SECRET profile_creds
    (TYPE aws, PROVIDER credential_chain, PROFILE '{{my-profile}}',
     CHAIN 'process', REFRESH 'auto');
```

```sql
ATTACH 'athena://{{us-west-2}}/{{my-duckdb-workgroup}}' AS ath
    (TYPE athena, Secret 'profile_creds');
```

## Choose a signing secret
<a name="connect-with-duckdb-extension-authentication-selection"></a>

If more than one equally ranked signing secret is eligible and `ATTACH` does not name one, the extension reports an ambiguous-secret error. Name the intended secret with `Secret`. The extension refuses `ProfileName`, `AwsProfile`, and access-key options on `ATTACH`; configure credentials in the secret instead.

```sql
SELECT name, type, provider FROM duckdb_secrets() ORDER BY name;
```

## More ways to sign in and attach
<a name="connect-with-duckdb-extension-authentication-more-ways"></a>

Complete [Install the extension](connect-with-duckdb-extension-download-and-install.md) to install the extension before these examples. Run steps 1 and 2 in the same fresh session, and step 3 in another fresh session. Configure usable credentials for the credential chain first. Replace the Regions, workgroups, databases, and tables with your values. The first workgroup uses managed query results; the second writes results to S3.

### Step 1: Use the default signing secret
<a name="connect-with-duckdb-extension-authentication-default-secret"></a>

With the `aws` extension loaded and default credentials enabled, `__aws_default` can sign an attachment without an explicit `CREATE SECRET`. The default secret resolves the credential chain on its first lookup. Inspect its provider before and after attaching with `duckdb_secrets()`.

```sql
LOAD athena FROM aws.amazon.com;
INSTALL AND LOAD aws;
SELECT name, type, provider FROM duckdb_secrets() ORDER BY name;
ATTACH 'athena://{{us-west-2}}/{{my-duckdb-workgroup}}' AS ath (TYPE athena);
SELECT count(*) AS n FROM ath.{{my_database}}.{{my_table}};
SELECT name, type, provider FROM duckdb_secrets() ORDER BY name;
```

### Step 2: Create a refreshable signing secret
<a name="connect-with-duckdb-extension-authentication-create-refreshable-secret"></a>

The `aws` provider can renew expired default credentials when the secret is looked up. For a long session using temporary credentials, create a credential-chain secret with `REFRESH 'auto'` to supply refresh information for extension recovery after credential errors. In this example, `aws_identity` is the only eligible explicit secret, so it signs the new attachment; the default secret is not counted as a second candidate. If multiple explicit secrets are eligible, select one with `Secret`.

```sql
CREATE SECRET aws_identity (TYPE aws, PROVIDER credential_chain, REFRESH 'auto');
ATTACH 'athena://{{us-west-2}}/{{my-s3-results-workgroup}}' AS bkt (TYPE athena);
SELECT count(*) AS n FROM bkt.{{my_database}}.{{my_table}};
```

### Step 3: Attach with the service ARN
<a name="connect-with-duckdb-extension-authentication-service-arn"></a>

Start another fresh session for this example. Supply the Region and workgroup as options on the service ARN. When neither the path nor a `Region` option supplies the Region, the signing secret must supply it. A full workgroup ARN can name both Region and workgroup; see [Connect to Athena with ATTACH](connect-with-duckdb-extension-attach.md).

```sql
LOAD athena FROM aws.amazon.com;
CREATE SECRET athena_creds (TYPE aws, PROVIDER credential_chain, REFRESH 'auto');
ATTACH 'arn:aws:athena' AS by_arn
    (TYPE athena, Region '{{us-west-2}}',
     WorkGroup '{{my-duckdb-workgroup}}', Secret 'athena_creds');
SELECT count(*) AS n FROM by_arn.{{my_database}}.{{my_table}};
```

## Athena API permissions
<a name="connect-with-duckdb-extension-authentication-iam"></a>

The following table lists Athena API actions used by the extension. Which calls are made depends on the query, metadata lookup, and result fetcher. These actions do not replace the Glue, S3, or KMS permissions required by your queries. For those requirements, see [Security and permissions](duckdb-permissions.md).



| Use | Athena IAM actions | 
| --- | --- | 
| Workgroup configuration | athena:GetWorkGroup | 
| Submit, poll, and cancel queries | athena:StartQueryExecution, athena:GetQueryExecution, athena:StopQueryExecution | 
| Read results | athena:GetQueryResultStream, athena:GetQueryResults | 
| Read prepared statements | athena:GetPreparedStatement | 
| Read catalog metadata | athena:GetDatabase, athena:GetTableMetadata, athena:ListDatabases, athena:ListTableMetadata | 
| List data catalogs | athena:ListDataCatalogs | 

The following example allows the Athena calls for one workgroup and one data catalog. Replace the Region, account ID, and resource names. It covers Athena API permissions only; add the data and result-storage permissions required for your workload.

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Workgroup",
      "Effect": "Allow",
      "Action": [
        "athena:GetWorkGroup",
        "athena:StartQueryExecution",
        "athena:GetQueryExecution",
        "athena:StopQueryExecution",
        "athena:GetQueryResults",
        "athena:GetQueryResultStream",
        "athena:GetPreparedStatement",
        "athena:GetDatabase",
        "athena:GetTableMetadata",
        "athena:ListDatabases",
        "athena:ListTableMetadata"
      ],
      "Resource": "arn:aws:athena:us-west-2:111122223333:workgroup/my-duckdb-workgroup"
    },
    {
      "Sid": "CatalogMetadata",
      "Effect": "Allow",
      "Action": [
        "athena:GetDatabase",
        "athena:GetTableMetadata",
        "athena:ListDatabases",
        "athena:ListTableMetadata"
      ],
      "Resource": "arn:aws:athena:us-west-2:111122223333:datacatalog/AwsDataCatalog"
    },
    {
      "Sid": "CatalogList",
      "Effect": "Allow",
      "Action": "athena:ListDataCatalogs",
      "Resource": "*"
    }
  ]
}
```

When you set `S3Tables` to `'*'` to list table buckets, the extension also calls Glue and needs `glue:GetCatalogs`. Direct reads of customer-bucket result objects need `s3:GetObject` for those objects. Your data and encryption configuration can require additional permissions.