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

# Create an Amazon Redshift Connection in Airflow

[Amazon Redshift](https://aws.amazon.com/redshift/) is a data warehouse product from AWS. Integrating Redshift with Airflow allows you to automate, schedule and monitor a variety of tasks. These tasks include creating, deleting, and resuming a cluster; ingesting or exporting data to and from Redshift; and running SQL queries against Redshift.

This document covers two different methods to connect Airflow to Amazon Redshift:

* Using database (DB) user credentials
* Using IAM credentials
* Using IAM role

<Tip>
  If you're an Astro user, Astronomer recommends using workload identity to authorize to your Deployments to Redshift. This eliminates the need to specify secrets in your Airflow connections or copying credentials file to your Airflow project. See [Authorize Deployments to your cloud](/docs/astro/authorize-deployments-to-your-cloud).
</Tip>

## Prerequisites

* The [Astro CLI](/docs/cli/v1.43/overview).
* A locally running [Astro project](/docs/cli/v1.43/get-started-cli).
* Permissions to access to your Redshift cluster. See [Using IAM authentication](https://docs.aws.amazon.com/redshift/latest/mgmt/generating-user-credentials.html) and [Authorizing Amazon Redshift to access other AWS services](https://docs.aws.amazon.com/redshift/latest/mgmt/authorizing-redshift-service.html).

## Get connection details

<details>
  <summary>DB Credentials</summary>

  DB user credentials can be used to establish a connection to an Amazon Redshift cluster. While straightforward to use, this approach lacks the strong security and user access controls provided by identity and access management (IAM). Connecting this way requires the following information:

  * Cluster identifier
  * Database name
  * Port
  * User
  * Password

  Complete the following steps to retrieve these values:

  1. In your AWS console, select the region that contains your Redshift cluster, open the Redshift cluster dashboard, then open your cluster.

  2. From the **General information** section, copy the **Cluster identifier** and **Endpoint**.

  3. Open the **Properties** tab and copy the **Database name** and **Port**.

  4. [Create a Redshift user](https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_USER.html) and [grant a role](https://docs.aws.amazon.com/redshift/latest/dg/r_GRANT.html) so that Airflow can access Redshift through the user. Copy the username and password.
</details>

<details>
  <summary>IAM Credentials</summary>

  You can use IAM credentials to connect Airflow to Redshift. This approach lets you use IAM credentials and limits Airflow's permissions. The limitation of this method is that you must include an AWS credentials file in your Airflow project.

  This approach requires the following information:

  * Cluster identifier
  * Database name
  * Port
  * Region
  * IAM user
  * AWS credentials file

  Complete the following steps to retrieve these values:

  1. In your AWS console, select the region that contains your Redshift cluster, open the Redshift cluster dashboard, then open your cluster.

  2. Open the **General information** tab, then copy the **Cluster identifier** and **Endpoint**.

  3. Open the **Properties** tab and copy the **Database name** and **Port**.

  4. Open your IAM dashboard, go to **Users** and select your user. Then, go to **Permissions** and follow the [AWS documentation](https://docs.aws.amazon.com/redshift/latest/mgmt/redshift-iam-access-control-identity-based.html) to ensure that the IAM user is authorized to connect to Redshift and perform SQL operations.

  5. [Generate a new access key ID and secret access key](https://docs.aws.amazon.com/powershell/latest/userguide/pstools-appendix-sign-up.html).
</details>

<details>
  <summary>IAM Role</summary>

  You can use AWS's [Assume Role](https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html) method to automatically generate temporary credentials to connect to Redshift. This is useful to grant temporary access to limited-privilege IAM users or roles without storing any credentials on disk. Creating the connection requires the following information:

  * Cluster identifier
  * Database name
  * Port
  * Region
  * IAM role ARN

  Complete the following steps to retrieve these values:

  1. In your AWS console, select the region that contains your Redshift cluster, open the Redshift cluster dashboard, then open your cluster.

  2. Open the **General information** tab, then copy the **Cluster identifier** and **Endpoint**.

  3. Open the **Properties** tab and copy the **Database name** and **Port**.

  4. Open your IAM dashboard, and [follow the AWS documentation](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_job-functions_create-policies.html) to create an IAM role and attach an IAM Policy to access the required services, for example AWS Redshift.

  5. Edit the trust relationship of the role created in Step 4 to add a trust policy that allows the IAM role to assume your new role.

     ```json wrap theme={null}

     {
         "Version": "2012-10-17",
         "Statement": [
             {
                 "Effect": "Allow",
                 "Principal": {
                     "AWS": "arn:aws:iam::<your-aws-account>:role/<your-role-name>"
                 },
                 "Action": "sts:AssumeRole"
             }
         ]
     }

     ```

  6. Copy the **ARN** of the role.
</details>

## Create your connection

<Info>
  Astro users can also create connections using the [Astro Environment Manager](/docs/astro/manage-connections-variables#astro-environment-manager), which stores connections in an Astro-managed secrets backend. These connections can be shared across multiple deployed and local Airflow environments. See [Create Airflow connections in the Astro UI](/docs/astro/create-and-link-connections).
</Info>

<details>
  <summary>DB Credentials</summary>

  1. Open your Astro project and add the following line to your `requirements.txt` file:

     ```text wrap theme={null}
     apache-airflow-providers-amazon
     ```

     This will install the Amazon provider package, which makes the Amazon Redshift connection type available in Airflow.

  2. Run `astro dev restart` to restart your local Airflow environment and apply your changes in `requirements.txt`.

  3. In the Airflow UI for your local Airflow environment, go to **Admin** > **Connections**. Click **+** to add a new connection, then select the connection type as **Amazon Redshift**.

  4. Fill out the following connection fields using the information you retrieved from [Get connection details](#get-connection-details):

     * **Connection Id**: Enter a name for the connection.
     * **Host**: Enter the cluster **Endpoint**.
     * **Database**: Enter the **Database name**.
     * **User**: Enter the DB user username.
     * **Password**: Enter the DB user password.
     * **Port**: Enter the **Port**.

  5. Click **Test**. After the connection test succeeds, click **Save**.

       <Frame>
         <img src="https://mintcdn.com/astronomer/Dj3lmYK-LeP4OB1k/images/img/examples/connection-aws-redshift.png?fit=max&auto=format&n=Dj3lmYK-LeP4OB1k&q=85&s=6e6c1f28c3069f3c42577dccff261dfb" alt="aws-connection-db-creds" width="1754" height="1110" data-path="images/img/examples/connection-aws-redshift.png" />
       </Frame>
</details>

<details>
  <summary>IAM Credentials</summary>

  1. Open your Astro project and add the following line to your `requirements.txt` file:

     ```text wrap theme={null}
     apache-airflow-providers-amazon
     ```

     This will install the Amazon provider package, which makes the Amazon Redshift connection type available in Airflow.

  2. Copy the `aws` credentials file to the `include` directory of your Astro project. It should have the following format:

     ```yaml wrap theme={null}
     # ~/.aws/credentials
     [<your-profile-name>]
     aws_access_key_id="your_aws_access_key_id"
     aws_secret_access_key="your_aws_secret_access_key"
     ```

  3. Run `astro dev restart` to restart your local Airflow environment and apply your changes in `requirements.txt`.

  4. In the Airflow UI for your local Airflow environment, go to **Admin** > **Connections**. Click **+** to add a new connection, then select the connection type as **Amazon Redshift**.

  5. Enter a name for the connection in the **Connection Id** field.

  6. Copy the following JSON template into the **Extra** field, then replace the placeholder values with the information you retrieved in [Get connection details](#get-connection-details).

     ```json wrap theme={null}

     {
         "iam": true, 
         "cluster_identifier": "<your-cluster-identifier>", 
         "port": 5439, 
         "region": "<your-region>",
         "db_user": "<your-user>", 
         "database": "<your-database>", 
         "profile": "<your-profile-name>"
     }

     ```

  7. Click **Test**. After the connection test succeeds, click **Save**.

       <Frame>
         <img src="https://mintcdn.com/astronomer/Dj3lmYK-LeP4OB1k/images/img/examples/connection-aws-redshift-iam.png?fit=max&auto=format&n=Dj3lmYK-LeP4OB1k&q=85&s=dba22e61058f10d3746ff08970b999d1" alt="aws-connection-iam-creds" width="2696" height="1332" data-path="images/img/examples/connection-aws-redshift-iam.png" />
       </Frame>
</details>

<details>
  <summary>IAM Role</summary>

  1. Open your Astro project and add the following line to your `requirements.txt` file:

     ```text wrap theme={null}
     apache-airflow-providers-amazon
     ```

     This will install the Amazon provider package, which makes the Amazon Redshift connection type available in Airflow.

  2. Run `astro dev restart` to restart your local Airflow environment and apply your changes in `requirements.txt`.

  3. In the Airflow UI for your local Airflow environment, go to **Admin** > **Connections**. Click **+** to add a new connection, then select the connection type as **Amazon Redshift**.

  4. Complete the following connection fields using the information you retrieved from [Get connection details](#get-connection-details):

     * **Connection Id**: Enter a name for the connection.
     * **Host**: Enter the cluster **Endpoint**.
     * **Database**: Enter the **Database name**.
     * **Port**: Enter the **Port**.
     * **Extra**:
       ```json wrap theme={null}

           {
           "role_arn": "<your-role-arn>",
           "region_name": "<your-region>"
           }

       ```

  5. Click **Test**. After the connection test succeeds, click **Save**.
</details>

## How it works

Airflow uses the [Amazon Redshift Python Connector](https://docs.aws.amazon.com/redshift/latest/mgmt/python-configuration-options.html) to connect to Redshift through the [`RedshiftSQLHook`](https://airflow.apache.org/docs/apache-airflow-providers-amazon/stable/_api/airflow/providers/amazon/aws/hooks/redshift_sql/index.html).

## See also

* [Apache Airflow Amazon provider package documentation](https://airflow.apache.org/docs/apache-airflow-providers-amazon/stable/connections/redshift.html)
* Redshift modules in the [Airflow Registry](https://airflow.apache.org/registry/)
* [Import and export Airflow connections using Astro CLI](/docs/astro/import-export-connections-variables#using-the-astro-cli-local-environments-only)
