Run federated queries on Amazon Redshift (IAM authentication)
Configure Databricks Lakehouse Federation to run federated queries on Amazon Redshift using AWS Identity and Access Management (IAM) authentication instead of a username and password. Authentication uses a Unity Catalog service credential that references an AWS IAM role. For general Redshift federation setup with username and password, see Run federated queries on Amazon Redshift.
IAM authentication for Amazon Redshift is available on Databricks on AWS only, because it relies on an AWS IAM role referenced by a service credential.
Before you begin
Before you run federated queries on Amazon Redshift using IAM authentication, you must meet the following requirements.
Databricks requirements
- A workspace enabled for Unity Catalog.
- Databricks compute that uses Databricks Runtime 19 or above and Standard or Dedicated access mode.
- The
CREATE CONNECTIONprivilege on the Unity Catalog metastore attached to the workspace. - The
CREATE SERVICE CREDENTIALprivilege on the metastore.
AWS requirements
- An Amazon Redshift provisioned cluster or serverless workgroup.
- Network connectivity from your Databricks workspace to the cluster or workgroup. See Networking recommendations for Lakehouse Federation.
- The ability to create IAM roles and policies.
Choose an authentication mode
Redshift IAM authentication supports three modes. Choose the one that matches your Redshift deployment, because it determines the IAM action you grant and whether you create a database user:
- DB User (
redshift:GetClusterCredentials): For provisioned clusters. Requires an explicit database user that you create in Redshift. - Group Federation (
redshift:GetClusterCredentialsWithIAM): For provisioned clusters. Databricks derives the database identity from the IAM role, so you don't create a user. - Serverless (
redshift-serverless:GetCredentials): For Redshift Serverless. The database identity is derived automatically, like Group Federation.
Configure AWS IAM authentication
Complete the following steps in AWS to create the database user, IAM policy, and IAM role that Unity Catalog uses to connect.
Step 1: Set up the Redshift database identity
For DB User mode, connect to your Redshift cluster and create a database user with password authentication disabled, then grant it the privileges it needs. You can run these commands from the cluster's Query data > Query in query editor page.
CREATE USER iam_user PASSWORD DISABLE;
GRANT ALL ON DATABASE dev TO iam_user;
GRANT USAGE ON SCHEMA public TO iam_user;
GRANT ALL ON ALL TABLES IN SCHEMA public TO iam_user;
For Group Federation and Serverless modes, skip the CREATE USER statement. Databricks derives the database identity automatically. The derived identity still needs in-database privileges so grant the privileges your queries require to that identity, or to a Redshift group or role it belongs to. Without them, the connection authenticates but federated queries fail with a permission error.
Step 2: Set up the AWS IAM policy
Create an IAM policy that authorizes the credential-fetching action for your authentication mode.
- DB User
- Group Federation
- Serverless
For provisioned clusters, authorize redshift:GetClusterCredentials on the cluster, database user, and database:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "RedshiftDbUser",
"Effect": "Allow",
"Action": ["redshift:GetClusterCredentials"],
"Resource": [
"arn:aws:redshift:<region>:<account-id>:cluster:<cluster-name>",
"arn:aws:redshift:<region>:<account-id>:dbuser:<cluster-name>/<db-user>",
"arn:aws:redshift:<region>:<account-id>:dbname:<cluster-name>/<database>"
]
}
]
}
For provisioned clusters, authorize redshift:GetClusterCredentialsWithIAM on the cluster and database. The database identity comes from the IAM role, so no dbuser ARN is needed:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "RedshiftGroupFederation",
"Effect": "Allow",
"Action": ["redshift:GetClusterCredentialsWithIAM"],
"Resource": [
"arn:aws:redshift:<region>:<account-id>:cluster:<cluster-name>",
"arn:aws:redshift:<region>:<account-id>:dbname:<cluster-name>/<database>"
]
}
]
}
For Redshift Serverless, authorize redshift-serverless:GetCredentials on the workgroup. This action is in the redshift-serverless namespace and acts on a workgroup ARN, not the provisioned-cluster resources used by the other modes:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "RedshiftServerless",
"Effect": "Allow",
"Action": ["redshift-serverless:GetCredentials"],
"Resource": ["arn:aws:redshift-serverless:<region>:<account-id>:workgroup/<workgroup-id>"]
}
]
}
For more information, see Using IAM authentication to generate database user credentials and Security and connections in Amazon Redshift Serverless in the AWS documentation.
Step 3: Create the AWS IAM role
Create an IAM role that Unity Catalog can assume to authenticate to Redshift:
-
In the AWS IAM console, go to Roles and click Create role.
-
For Trusted entity type, select Custom trust policy.
-
In the Custom trust policy editor, paste the following policy. This is a placeholder. After you create the service credential in Databricks, replace it with the trust policy that Databricks generates. The
PrincipalandActionare required. TheConditionblock is optional at this stage.JSON{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": ["arn:aws:iam::414351767826:role/unity-catalog-prod-UCMasterRole-14S5ZJVKOTYTL"]
},
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": {
"sts:ExternalId": "0000"
}
}
}
]
} -
Click Next. On the Add permissions page, select the IAM policy you created in Set up the AWS IAM policy.
-
Click Next, enter a Role name, and click Create role.
-
Note the role's ARN. You provide it when you create the service credential.
For more information about IAM roles with custom trust policies, see Creating a role using custom trust policies in the AWS documentation.
Step 4: Create the Databricks service credential
Create a Unity Catalog service credential that references the IAM role ARN from the previous step. For detailed steps, see Create service credentials.
After you create the service credential, finish configuring the IAM role's trust policy:
- In the Service credential created dialog, copy the entire trust policy shown, then click Done.
- Return to your IAM role in the AWS console, open the Trust relationships tab, and replace the placeholder trust policy with the one you copied. This adds your service credential's external ID and makes the role self-assuming.
Note the name of the service credential. You reference it when you create the connection.
Create a connection
A connection specifies a path and credentials for accessing an external database system. To create a connection, you can use Catalog Explorer or the CREATE CONNECTION SQL command in a Databricks notebook or the Databricks SQL query editor.
You can also use the Databricks REST API or the Databricks CLI to create a connection. See POST /api/2.1/unity-catalog/connections and Unity Catalog commands.
Permissions required: Metastore admin or user with the CREATE CONNECTION privilege.
- Catalog Explorer
- SQL
- In your Databricks workspace, click
Catalog.
- At the top of the Catalog pane, click the
Add icon and select Create a connection from the menu.
- On the Connection basics page of the Set up connection wizard, enter a user-friendly Connection name.
- Select a Connection type of Redshift.
- For Auth type, select Service Credential.
- (Optional) Add a comment.
- Click Next.
- On the Authentication page, enter the following connection properties for your Redshift cluster or workgroup:
- Host: For example,
redshift-demo.us-west-2.redshift.amazonaws.com - Port: For example,
5439 - User: For DB User mode, the database user you created. For example,
iam_user. Leave this empty for Group Federation and Serverless modes.
- Host: For example,
- Click Next.
- On the Connection details page, select the Authentication mode (DB User, Group Federation, or Serverless), and for Service credential, select the service credential you created in Create the service credential.
- Click Create connection.
- On the Catalog basics page, enter a name for the foreign catalog. A foreign catalog mirrors a database in an external data system so that you can query and manage access to data in that database using Databricks and Unity Catalog.
- (Optional) Click Test connection to confirm that it works.
- Click Create catalog.
- On the Access page, select the workspaces in which users can access the catalog you created. You can select All workspaces have access, or click Assign to workspaces, select the workspaces, and then click Assign.
- Change the Owner who can manage access to all objects in the catalog. Start typing a principal in the text box, and then click the principal in the returned results.
- Grant Privileges on the catalog. Click Grant:
- Specify the Principals who will have access to objects in the catalog. Start typing a principal in the text box, and then click the principal in the returned results.
- Select the Privilege presets to grant to each principal. All account users are granted
BROWSEby default.- Select Data Reader from the drop-down menu to grant
readprivileges on objects in the catalog. - Select Data Editor from the drop-down menu to grant
readandmodifyprivileges on objects in the catalog. - Manually select the privileges to grant.
- Select Data Reader from the drop-down menu to grant
- Click Grant.
- Click Next.
- On the Metadata page, specify tags key-value pairs. For more information, see Apply tags to Unity Catalog securable objects.
- (Optional) Add a comment.
- Click Save.
Run the following command in a notebook or the Databricks SQL query editor. Providing the credential option instead of a password creates an IAM-authenticated connection. Set auth_mode to match your Redshift deployment, and set user only for db_user mode.
-- db_user mode (provisioned cluster)
CREATE CONNECTION <connection-name> TYPE redshift
OPTIONS (
host '<hostname>',
port '<port>',
user '<iam-user>',
auth_mode 'db_user',
credential '<service-credential-name>'
);
-- group_federation mode (provisioned cluster)
CREATE CONNECTION <connection-name> TYPE redshift
OPTIONS (
host '<hostname>',
port '<port>',
auth_mode 'group_federation',
credential '<service-credential-name>'
);
-- serverless mode (Redshift Serverless)
CREATE CONNECTION <connection-name> TYPE redshift
OPTIONS (
host '<hostname>',
port '<port>',
auth_mode 'serverless',
credential '<service-credential-name>'
);
Create a foreign catalog
If you use the UI to create a connection to the data source, foreign catalog creation is included and you can skip this step.
If you use the UI to create a connection to the data source, foreign catalog creation is included and you can skip this step.
A foreign catalog mirrors a database in an external data system so that you can query and manage access to data in that database using Databricks and Unity Catalog. To create a foreign catalog, you use a connection to the data source that has already been defined.
To create a foreign catalog, you can use Catalog Explorer or the CREATE FOREIGN CATALOG SQL command in a Databricks notebook or the SQL query editor. You can also use the Databricks REST API or the Databricks CLI to create a catalog. See POST /api/2.1/unity-catalog/catalogs and Unity Catalog commands.
Permissions required: CREATE CATALOG permission on the metastore and either ownership of the connection or the CREATE FOREIGN CATALOG privilege on the connection.
- Catalog Explorer
- SQL
-
In your Databricks workspace, click
Catalog to open Catalog Explorer.
-
At the top of the Catalog pane, click the
Add icon and select Add a catalog from the menu.
Alternatively, from the Quick access page, click the Catalogs button, and then click the Create catalog button.
-
Follow the instructions for creating foreign catalogs in Create catalogs.
Run the following SQL command in a notebook or SQL query editor. Items in brackets are optional. Replace the placeholder values:
<catalog-name>: Name for the catalog in Databricks.<connection-name>: The connection object that specifies the data source, path, and access credentials.<database-name>: Name of the database you want to mirror as a catalog in Databricks.
CREATE FOREIGN CATALOG [IF NOT EXISTS] <catalog-name> USING CONNECTION <connection-name>
OPTIONS (database '<database-name>');