How to use BastionZero to set up passwordless access to AWS RDS PostgreSQL
Configure access to your RDS PostgreSQL databases without the need for traditional passwords
In this guide, we'll show you how to set up passwordless access to your RDS PostgreSQL databases without the need for traditional passwords.
There are seven basic steps to this process:
- 1.Prepare your BastionZero Account
- 2.Configure AWS IAM Role
- 3.Launch an EC2 Instance
- 4.Install BastionZero agent on EC2 Instance
- 5.Create & Configure an RDS Database
- 6.Attach RDS instance to EC2 instance
- 7.Create BastionZero access policy
For the purposes of this guide, I’lll assume you have already signed up for a BastionZero account, have your BastionZero account integrated with your IDP, and have the ZLI installed on your local machine. Once that is complete, we’ll do a few things in our BastionZero account to set ourselves up for success as outlined below:
- Create a registration key
We can grab a registration key from the BastionZero web interface at cloud.bastionzero.com. Once you sign in, select "Create" in the upper righthand corner and choose "API Key."
We'll use this registration key for installing the agent below. Hang onto it!
- Access IAM (Identity & Access Management) in the AWS console.
- Click on "Roles" then "Create role."
- Select "AWS service" as the type of trusted entity.
- For the service that will use this role, choose "EC2".
- Click on "Next".
- On the "Attach permissions policies" page, click on "Create policy" (you'll need to do this in a new tab or window - don't close the role creation process, we’ll come back to it).
- In the JSON tab of the create policy screen, replace any existing content with the following JSON object. You need to modify the Resource definition to match the database you wish to access in a password-less fashion. The general structure is:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "rds-db:*",
"Resource": "arn:aws:rds-db:RDS_DATABASE_REGION:YOUR_AWS_ACCOUNT_ID:dbuser:*/*"
}
]
}
In the example policy shown above, the service is rds-db, the region is us-east-1, the account-id should be your own account ID, and the resource-type/resource-ID is dbuser:*/*, indicating this policy applies to all database users in all databases within your RDS service.
- Click on "Next."
- Name the policy, for example, "RDS_Impersonation_Policy."
- Click on "Create policy."
- Go back to the tab or window where you are creating the role and refresh the page.
- In the search box, search for the name of the policy you just created (e.g. "RDS_Impersonation_Policy").
- Check the box next to your newly created policy.
- Click "Next: Review."
- Enter the role name, for instance, "RDS Impersonator."
- Review the role and click "Create role."
- Now you have successfully created an IAM Role User "RDS Impersonator" with a role that lets the "RDS Impersonator" authenticate as a database user.
- Navigate to the EC2 Dashboard by selecting "EC2" from the list of services.
- Click on the "Launch Instances" button.
- Give your instance a name.
- Choose an Amazon Machine Image (AMI).
- Choose an instance type (we’re using a
t.2 micro
in this case).
- When you launch your instance, you need to select a key pair that you'll use to SSH into your instance to install the BastionZero agent. If you don't have an existing key pair, you can create a new one. Be sure to download and securely store the private key file (.pem).
- In the "Advanced Details" section, look for the "IAM instance profile" field. It is in this drop-down list that you'll select the IAM role that you want to assign to the EC2 instance. Click on the "IAM instance profile" drop-down list and you should see the "RDS Impersonator" role that you created earlier. Select this role.
- After you've selected the role, continue to configure storage, add tags, and configure the security group as per your requirement.
- Review your choices and then click "Launch."
- Click on "View Instances" to go back to your EC2 Dashboard. Here, you should see your instance launching.
- It may take a few minutes for your instance to launch. Once it's running, you can connect to it using the key pair that you specified when launching the instance.
- SSH into your EC2 instance.
- Download and install the BastionZero agent according to the instructions provided by BastionZero here. You'll need your registration key from the "Preparing Your Account" section above.
- Name the target something memorable; we'll need it when we create a database target below.
- From the services list, select "RDS" to access the RDS Dashboard.
- Click on "Create database."
- Select the "Standard Create" method and choose the PostgreSQL engine.
- Choose the version of PostgreSQL that suits your needs.
- Enter the details under "Settings." Here, the Master username will be "postgres" by default, and you will be asked to enter a password. Remember this password, as you will need it to connect to the database and add your passwordless user.
- Specify the Database details as needed like DB instance identifier, instance size, storage, etc.
- Under "Connectivity," make sure your database is publicly accessible if you need to access it from outside of AWS.
- Choose the VPC, Subnet Group, Publicly Accessible settings, VPC Security Group(s), and Availability Zone based on your needs.
- After all the details have been specified, click on "Create database" at the bottom of the page.
- Your RDS instance will now be set up. This can take a few minutes.
- In the database menu, click “Actions” in the top right and select “Set up EC2 connection.”
- Select the EC2 instance from above and click “Confirm and set up.”
- After the database is available, click on the DB instance name to get the details, including the endpoint.
- Use a SQL client to connect to the database. You'll need the endpoint, port (usually 5432 for PostgreSQL), username ("postgres"), and the password you created earlier.
CREATE USER db_userx; GRANT rds_iam TO db_userx;
- Click the "Create" button in the top right and select "Database."
- Give your target a name (e.g., rds-psql).
- Select the Proxy Target from the dropdown menu (this should be the target you installed the BastionZero agent on).
- Set “SplitCert” to “Yes.”
- Set the Database Type to “Postgres.”
- In the "Remote Host" field, enter the instance connection name from AWS RDS prepended by
rds://
(e.g.,rds://database-1.cdmphnzvju0l.us-east-1.rds.amazonaws.com
). - In the "Remote Port" field, enter the remote database port (e.g.,
5432
). - In the “Local Host” field, enter
localhost
. - Set the “Local Port” to your desired local port (e.g., “1501”). Unless there is a specific need to open this connection on the same port each time, we suggest you leave this field blank and BastionZero will choose an open port for you at time of connection.
- Click "Add" to create the database target.
- Click on "Create" in the top right and select "Policy."
- In the “Policy Type” select “Proxy.”
- In the "Policy Name" field, enter a name for your policy.
- In the "Users" section, add users from your IdP who you’d like to be able to access the remote database.
- In the "Targets" section, select the database target you created prior.
- In the “Allowed Target Users” section, add the database user from AWS (e.g.,
db_userx
) - Click "Save" to create the policy.
- From your terminal using the zli, run:
zli connect {database_username}@{database target name}
zli connect db_userx@rds-psql
The command line will provide a port that can then be used to connect to the database.
In your database client, you’ll need to specify only your username.
Congratulations! You have successfully completed all the necessary steps to set up your passwordless access to AWS RDS PostgreSQL. You should now be able to establish a secure connection to your database using the ZLI and your specified username. This marks the completion of your setup and configuration process.