> ## Documentation Index
> Fetch the complete documentation index at: https://anaconda.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Query data with Amazon Athena

export const Comments = ({children}) => {
  return <div class="my-4 px-5 py-4 overflow-hidden rounded-2xl flex gap-3 border border-zinc-500/20 bg-zinc-50/50 dark:border-zinc-500/30 dark:bg-zinc-500/10" data-callout-type="comments">
      <div class="w-4">
        <svg width="14" height="14" viewBox="0 0 640 640" fill="currentColor" xmlns="http://www.w3.org/2000/svg" class="w-5 h-5" aria-label="Comments">
            <path d="M320 112C434.9 112 528 205.1 528 320C528 434.9 434.9 528 320 528C205.1 528 112 434.9 112 320C112 205.1 205.1 112 320 112zM320 576C461.4 576 576 461.4 576 320C576 178.6 461.4 64 320 64C178.6 64 64 178.6 64 320C64 461.4 178.6 576 320 576zM280 400C266.7 400 256 410.7 256 424C256 437.3 266.7 448 280 448L360 448C373.3 448 384 437.3 384 424C384 410.7 373.3 400 360 400L352 400L352 312C352 298.7 341.3 288 328 288L280 288C266.7 288 256 298.7 256 312C256 325.3 266.7 336 280 336L304 336L304 400L280 400zM320 256C337.7 256 352 241.7 352 224C352 206.3 337.7 192 320 192C302.3 192 288 206.3 288 224C288 241.7 302.3 256 320 256z" />
        </svg>
      </div>
      <div class="text-sm prose min-w-0 w-full">
        {children}
      </div>
    </div>;
};

This tutorial walks you through connecting Anaconda Platform to Amazon Athena so you can run SQL queries over data in S3 from your workstations and Metaflow tasks.

<Note>
  Creating a resource integration requires an administrator role. If you do not have administrator access, ask your administrator to create the integration before you begin.
</Note>

By the end of this tutorial, you will have:

* An S3 bucket and Athena database with queryable data
* An IAM role that allows Anaconda Platform to interact with Athena
* A workstation notebook and a Metaflow flow that run Athena SQL queries

## Create Athena resources

AWS Athena runs SQL queries over data assets in S3. If you already have an Athena setup, skip to [Chain the Athena role with the platform task role](#chain-the-athena-role-with-the-platform-task-role). If you want to set up a test, follow the steps below to create an S3 bucket and an IAM role for Athena.

### Create an S3 bucket

In the AWS console, create a bucket for your data. Copy the bucket's Amazon Resource Name (ARN) or keep the console open. You need the ARN when you create the IAM role.

### Create an IAM role

Athena requires an IAM role with permissions to run queries and access the data bucket. For the full setup guide, see the [AWS Athena getting started documentation](https://docs.aws.amazon.com/athena/latest/ug/getting-started.html).

Create an IAM role with the following minimum permissions:

* The `AWSAthenaFullAccess` managed policy
* S3 bucket access for query results
* Glue Data Catalog access, if you use Glue catalogs

If you are unsure where to start, use this template as a starting point:

```json expandable IAM policy template theme={null}
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Effect": "Allow",
			"Action": [
				"s3:GetBucketLocation",
				"s3:PutObject",
				"s3:GetObject",
				"s3:ListBucket"
			],
			"Resource": [
				"arn:aws:s3:::<YOUR_BUCKET>/*",
				"arn:aws:s3:::<YOUR_BUCKET>"
			]
		},
		{
			"Effect": "Allow",
			"Action": [
				"athena:StartQueryExecution",
				"athena:GetQueryExecution",
				"athena:GetQueryResults",
				"athena:GetWorkGroup"
			],
			"Resource": "*"
		},
		{
			"Effect": "Allow",
			"Action": [
				"glue:GetTable",
				"glue:GetDatabase",
				"glue:CreateDatabase",
				"glue:CreateTable",
				"glue:UpdateDatabase",
				"glue:UpdateTable",
				"glue:DeleteTable"
			],
			"Resource": "*"
		}
	]
}
```

<Comments>
  Replace \<YOUR\_BUCKET> with your S3 bucket's name. The bucket policy Resource entries are ARNs that contain the bucket name, such as `arn:aws:s3:::my-athena-data/*`.
</Comments>

### Configure the query results location

1. Create an S3 bucket for Athena to store query results and metadata in.
2. In the Athena console, set the query result location to your S3 bucket.
3. Confirm your IAM role has access to this bucket.

## Chain the Athena role with the platform task role

To allow Anaconda Platform tasks to use your Athena role, register it as an integration in the platform:

1. Select **Integrations** in the left-hand navigation.

2. Click **AWS** in the **Add an Integration** section.

   <Note>
     Do not use the Amazon S3 integration. Athena requires IAM role chaining for the Athena API, Glue Data Catalog, and S3, which the S3 integration does not provide.
   </Note>

3. Enter a name and description for the integration.

4. Enter the ARN of the IAM role you created.

   <Note>
     If you need the ARN, expand the **Getting your IAM role ARN** section in the panel.

     This shows the trust policy statement you need to add to your role, and the tag key and value required for the platform to discover it. You can choose to use an existing target role or create a new one; the panel shows the required trust policy and tagging steps for either path.
   </Note>

5. Click **Add**.

After the integration is created, the **How to use** tab in the integration panel shows a code snippet with the exact `role_arn` value for your flows. Copy this snippet into your Metaflow steps to access Athena through the chained role.

## Download the tutorial content

Download the tutorial content to your workstation:

```bash theme={null}
outerbounds tutorials pull --url https://outerbounds-journeys-content.s3.us-west-2.amazonaws.com/main/journeys.tar.gz --destination-dir ~/learn
```

<Tip>
  This command downloads all tutorial content as a single bundle. If you've already worked through other tutorials, you likely already have this and do not need to run the command again.
</Tip>

The Athena tutorial content is in `~/learn/athena`. If you prefer a different location, replace `~/learn` with a directory of your choice.

## Load data into your bucket

Open the notebook in `00-setup` from the `~/learn/athena` directory. Before running it, update the `bucket_name` and `role_arn` variables with the S3 bucket and IAM role you created earlier. This notebook walks you through putting data into your S3 bucket so Athena can query it.

## Query Athena from a workstation notebook

Open the notebook in `01-nb` from the `~/learn/athena` directory. This notebook walks you through running SQL queries using Athena. You will:

* Connect to Athena using your configured role
* Write and execute SQL queries
* Retrieve and analyze query results

## Query Athena from a Metaflow workflow

Open the `02-flow` directory from the `~/learn/athena` directory. This directory contains a Metaflow flow that runs SQL queries using Athena. Before running it, update the `bucket_name` and `role_arn` variables in `flow.py` with the same values you used in the notebooks.

Run the flow:

```bash theme={null}
python flow.py --environment=fast-bakery run --with kubernetes
```

## Next steps

To build on this tutorial:

* Create more complex queries that combine multiple data sources.
* Build automated reporting workflows using Athena.
* Integrate Athena queries into your ML pipelines.
