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

# Add an RDS database

> Add your AWS RDS databases to Opal to allow your developers to request temporary access.

<Info>
  This guide assumes you've already [configured your AWS organization in
  Opal](/integrations/setting-up-your-aws-organization-in-opal).
</Info>

The following diagram illustrates how Opal connects to AWS RDS. Use this guide to learn how you can add RDS databases to Opal.

<img src="https://mintcdn.com/opalsecurity/CCjTTkaW-43B4efd/images/docs/f83f3cd-1.png?fit=max&auto=format&n=CCjTTkaW-43B4efd&q=85&s=585af0a2ab1b1573bc8f2a6c28eb399b" alt="1920" width="1920" height="1080" data-path="images/docs/f83f3cd-1.png" />

You can add RDS databases to Opal easily using [RDS IAM database authentication](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.html).

## Supported RDS engines

Amazon IAM database authentication is available for the following database engines:

* MySQL 8.0, minor version 8.0.16 or higher
* MySQL 5.7, minor version 5.7.16 or higher
* MySQL 5.6, minor version 5.6.34 or higher
* PostgreSQL 14, 13, 12, and 11, all minor versions
* PostgreSQL 10, minor version 10.6 or higher
* PostgreSQL 9.6, minor version 9.6.11 or higher
* PostgreSQL 9.5, minor version 9.5.15 or higher Don't see what you want here? Let us know by suggesting an edit or [reaching out](mailto:support@opal.dev) and we'll make it happen.

## Add an RDS database

### 1. Enable IAM authentication

You'll need to enable IAM database authentication for your RDS instance. You can do this using the AWS Console and CLI. You can read more on how to do this [here in the AWS official documentation](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.Enabling.html).

#### AWS Console

<Frame caption="Modify your RDS instance">
  <img src="https://mintcdn.com/opalsecurity/lwwIeFbsleftxaXx/images/docs/d1be813-Screen_Shot_2020-12-03_at_3.50.06_PM.png?fit=max&auto=format&n=lwwIeFbsleftxaXx&q=85&s=6a849507b03f02209ae04d5515df6731" width="1768" height="508" data-path="images/docs/d1be813-Screen_Shot_2020-12-03_at_3.50.06_PM.png" />
</Frame>

<Frame caption="Enable IAM database authentication.">
  <img src="https://mintcdn.com/opalsecurity/E-CmJXh0QNjZUl4g/images/docs/662ccc5-Screen_Shot_2020-12-03_at_3.50.41_PM.png?fit=max&auto=format&n=E-CmJXh0QNjZUl4g&q=85&s=e522d93952fa8aeb0649cc85119fec73" width="1494" height="706" data-path="images/docs/662ccc5-Screen_Shot_2020-12-03_at_3.50.41_PM.png" />
</Frame>

#### AWS CLI

<CodeGroup>
  ```shell shell theme={null}
  aws rds modify-db-instance \
  --db-instance-identifier mydbinstance \
  --apply-immediately \
  --enable-iam-database-authentication
  ```
</CodeGroup>

<Warning>
  Enabling IAM Authentication **requires a brief outage** for your RDS database.
</Warning>

### 2. Create database users

Connect to your database and create a database user with read-only access. Here is a snippet you can use for Postgres and MySQL.

<CodeGroup>
  ```pgsql pgsql theme={null}
  -- Create a group
  CREATE ROLE readaccess;

  -- Grant access to existing tables
  GRANT USAGE ON SCHEMA public TO readaccess;
  GRANT SELECT ON ALL TABLES IN SCHEMA public TO readaccess;

  -- Grant access to future tables
  ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readaccess;

  -- Create a user with IAM authentication
  CREATE USER readonly; -- NOTE: THIS USERNAME IS REQUIRED
  GRANT readaccess TO readonly;
  GRANT rds_iam TO readonly;

  CREATE USER fullaccess; -- NOTE: THIS USERNAME IS REQUIRED
  GRANT rds_superuser TO fullaccess;
  GRANT rds_iam TO fullaccess;

  ```

  ```mysql mysql theme={null}
  -- NOTE: The usernames readonly and fullaccess are required
  GRANT SELECT ON *.* TO 'readonly'@'%' IDENTIFIED WITH AWSAuthenticationPlugin AS 'RDS';
  GRANT SELECT ON *.* TO 'readonly'@'%' WITH GRANT OPTION;

  CREATE USER 'fullaccess'@'%' IDENTIFIED WITH AwsAuthenticationPlugin AS 'RDS';
  GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, PROCESS, REFERENCES,
  INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, LOCK TABLES,
  EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW,
  CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT,
  TRIGGER ON *.* TO 'fullaccess'@'%' WITH GRANT OPTION;

  FLUSH PRIVILEGES;
  ```
</CodeGroup>

#### Custom Database Users

If you'd like to instead define your own database users with custom amounts of access, you can do that easily in one of two ways: through Opal UI, or with tags.

#### Opal UI

1. In the **Inventory**, go to the **Roles** tab of the RDS instance, then select **+ Create**.

<img src="https://mintcdn.com/opalsecurity/TlQj9FwRe9HHNEYB/images/docs/11aa57f4cc9aefcc2863e8a80be6e2db6df86470f39676c4fe793ef434a3edc9-rds-roles.png?fit=max&auto=format&n=TlQj9FwRe9HHNEYB&q=85&s=7708ef04c0321d8090d9d6402c32eb4d" alt="" width="2693" height="1291" data-path="images/docs/11aa57f4cc9aefcc2863e8a80be6e2db6df86470f39676c4fe793ef434a3edc9-rds-roles.png" />

2. Add a role name, and the database username as the **Role Remote ID**, then select **Create**.

<img src="https://mintcdn.com/opalsecurity/odnvD_MsXBxTor9u/images/docs/953cb7e566d45bf12d6c1e5b2febff77b95d9d5112a2e18df3a88eb9871279ff-Screenshot_2024-10-10_at_10.27.19_AM.png?fit=max&auto=format&n=odnvD_MsXBxTor9u&q=85&s=3b7a4ef91a9d3eee0b4fb671a27e2bb6" alt="" width="888" height="636" data-path="images/docs/953cb7e566d45bf12d6c1e5b2febff77b95d9d5112a2e18df3a88eb9871279ff-Screenshot_2024-10-10_at_10.27.19_AM.png" />

#### Tags

1. Create the user with any username that you like and grant them `rds_iam` with the following queries:

<CodeGroup>
  ```pgsql pgsql theme={null}
  GRANT rds_iam TO myuser;
  ```

  ```mysql mysql theme={null}
  CREATE USER myuser IDENTIFIED WITH AwsAuthenticationPlugin AS 'RDS';
  ```
</CodeGroup>

2. Tag the RDS database with key `opal:user:1` and value `myuser`. You can manage multiple levels of access using more tags! Example: `opal:user:2` => `myseconduser`. To see how to tag your database check out Step 3.

Now the custom database users will show up in Opal as access levels.

<Warning>AWS supports a maximum of 50 tags on a resource.</Warning>

### 3. Tag the database

Finally, tag the RDS database with `opal` so Opal knows which RDS databases you'd like Opal to automatically import:

#### AWS Console

<Frame caption="Tagging an RDS database with  in the AWS Console.">
  <img src="https://mintcdn.com/opalsecurity/KunPWigry5GIeB5g/images/docs/441854a-Screen_Shot_2020-12-03_at_3.54.50_PM.png?fit=max&auto=format&n=KunPWigry5GIeB5g&q=85&s=4bf537cf0203551322055ad7e033c12e" width="2076" height="1176" data-path="images/docs/441854a-Screen_Shot_2020-12-03_at_3.54.50_PM.png" />

  `opal`
</Frame>

#### AWS CLI

<CodeGroup>
  ```shell shell theme={null}
  aws rds add-tags-to-resource \
    --resource-name arn:aws:rds:<REGION>:<AWS_ACCOUNT_ID>:db:<DB_INSTANCE_NAME> \
    --tags "Key=opal,Value="
  ```
</CodeGroup>

#### Terraform

Add the following `tags` argument to your AWS RDS instance:

```
tags = {
  opal = ""
  opal:database-name="my_db" // This is optional. By default, Opal will pull the database name from RDS's configuration
}
```

## Access an RDS database in Opal

Once you've successfully tagged an RDS database, it will appear on the Resources page in the **Catalog**. If you followed the steps above, you'll be able to **Connect** with read-only and full access to your database.

Access to RDS databases in Opal is session-based, meaning your developers won't have access unless they initiate a session.

<Frame caption="Start a session to an RDS database.">
  <img src="https://mintcdn.com/opalsecurity/4Xj9diJ3E3kX-9Xd/images/docs/df18ccf-RDS-Connect.png?fit=max&auto=format&n=4Xj9diJ3E3kX-9Xd&q=85&s=7e73cd430dad0fc0c3f3dfcde2d9c3a4" width="2958" height="1658" data-path="images/docs/df18ccf-RDS-Connect.png" />
</Frame>

You can instantly open a connection to the database using your favorite viewer, like Postico or TablePlus, or connect manually. Your credentials can only initiate connections for 15 minutes.

<Frame caption="Automatically connect to your database or use manual configuration.">
  <img src="https://mintcdn.com/opalsecurity/fu-nWazMe1LxLhxi/images/docs/32e8a67-Screen_Shot_2020-12-03_at_4.11.28_PM.png?fit=max&auto=format&n=fu-nWazMe1LxLhxi&q=85&s=d505d7a56f0d1d080de3ec79a874c655" width="2420" height="1366" data-path="images/docs/32e8a67-Screen_Shot_2020-12-03_at_4.11.28_PM.png" />
</Frame>
