View a markdown version of this page

Getting started with Aurora data groups - Amazon Aurora

Getting started with Aurora data groups

In this tutorial, you create a data group named tenant42 on a DB cluster named orders-cluster-1, add a database with a table to it, and move the data group to a second DB cluster named orders-cluster-2. The steps use the AWS CLI and a SQL client such as psql. Each step links to the full topic.

You can also create, view, and move data groups in the Amazon RDS console. In the navigation pane, choose Data Groups. Each linked topic has the console procedure.

To keep this example simple, the data group uses the same KMS key as its DB cluster. To encrypt a data group with its own KMS key, see Encrypting data groups in Amazon Aurora.

  1. Set up two DB clusters. You need a source DB cluster and a target DB cluster in the same AWS account and AWS Region. Both must run an engine version that supports data groups, use Aurora I/O-Optimized storage, have a writer DB instance, and have a cluster admin user with the same name. You can use existing DB clusters that meet these requirements. For the full list, see Requirements for Aurora data groups. For the IAM permissions that you need, see Prerequisites for using Aurora data groups.

    To find the engine versions that support data groups, run the following command.

    aws rds describe-db-engine-versions \ --engine aurora-postgresql \ --query "DBEngineVersions[?SupportsDataGroups].EngineVersion" \ --output text

    To create the two DB clusters, run the following commands once for orders-cluster-1 and once for orders-cluster-2.

    Important

    The DB clusters incur charges until you delete them. To delete them, follow the clean-up steps at the end of this tutorial.

    Use the same engine version for both, down to the minor and patch level, and the same admin user name.

    aws rds create-db-cluster \ --db-cluster-identifier orders-cluster-1 \ --engine aurora-postgresql \ --engine-version 19.0 \ --storage-type aurora-iopt1 \ --master-username postgres \ --manage-master-user-password aws rds create-db-instance \ --db-instance-identifier orders-cluster-1-instance-1 \ --db-cluster-identifier orders-cluster-1 \ --db-instance-class db.r8g.large \ --engine aurora-postgresql

    The --manage-master-user-password option stores the admin user password in AWS Secrets Manager. For more information, see Password management with Amazon Aurora and AWS Secrets Manager.

    Wait until both DB clusters are available. For more information, see Creating an Amazon Aurora DB cluster.

  2. Create a data group. The following command creates the tenant42 data group on orders-cluster-1.

    aws rds create-data-group \ --db-cluster-identifier orders-cluster-1 \ --data-group-name tenant42

    Save the DataGroupIdentifier from the output, such as dg-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111. You use it in the later steps.

    Run the following command until the status is active.

    aws rds describe-data-groups \ --data-group-identifier dg-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111 \ --query "DataGroups[0].Status"

    For other options, such as a data group endpoint, see Creating a data group.

  3. Add a database and map the admin user. Connect to the writer endpoint of orders-cluster-1 as postgres, the cluster admin user that you set with --master-username in step 1. For more information, see Connecting to an Amazon Aurora DB cluster.

    Run the following SQL. It maps postgres to the data group as EXPECT_NAME_MATCH, which a move needs, because postgres creates and owns the database in the data group. Because you created both DB clusters with postgres as the admin user, the role already exists on the target DB cluster, and you don't need to create another role. Then the SQL creates the orders database in the data group and adds a table with 1,000 rows.

    You can also create a role for an application and map it to the data group. For more information, see Managing roles for data groups.

    Aurora PostgreSQL

    When you created the data group in step 2, Aurora also created a tablespace named tenant42, so it already exists. The TABLESPACE clause creates the database in the data group.

    SELECT * FROM aurora_datagroup_add_role_access('tenant42', 'EXPECT_NAME_MATCH', 'postgres'); CREATE DATABASE orders TABLESPACE tenant42; \c orders CREATE TABLE customer_orders ( order_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, customer_id bigint NOT NULL, order_total numeric(10,2) NOT NULL, created_at timestamptz NOT NULL DEFAULT now() ); INSERT INTO customer_orders (customer_id, order_total) SELECT (random() * 1000)::bigint + 1, round((random() * 500 + 5)::numeric, 2) FROM generate_series(1, 1000);

    For more information about creating databases in a data group, see Creating or migrating databases in a data group.

  4. Move the data group. Prepare the move to orders-cluster-2. Prepare checks that the target DB cluster can take the data group. If the move is refused, see Before you move a data group.

    aws rds move-data-group \ --data-group-identifier dg-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111 \ --move-operation prepare-move \ --target-db-cluster-identifier orders-cluster-2

    Run the describe-data-groups command from step 2 until the status is ready-to-move. Then execute the move.

    aws rds move-data-group \ --data-group-identifier dg-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111 \ --move-operation execute-move \ --target-db-cluster-identifier orders-cluster-2

    Run the describe-data-groups command again until the status is active. For more information, see Moving a data group to another DB cluster.

  5. Check the result. The data group is now on orders-cluster-2.

    aws rds describe-data-groups \ --data-group-identifier dg-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111 \ --query "DataGroups[0].[Status,DBClusterIdentifier]" \ --output text

    The output resembles the following example.

    active orders-cluster-2

    Connect to the writer endpoint of orders-cluster-2 and count the rows. The query returns 1,000. On orders-cluster-1, the orders database no longer exists.

    Aurora PostgreSQL
    \c orders SELECT count(*) FROM customer_orders;

    For the other checks to run after a move, see After you move a data group.

Next, try the following:

Clean up. When you no longer need the data group, drop its database, or migrate it to another data group. Then delete the data group. A delete fails if the data group still holds databases. Connect to the writer endpoint of orders-cluster-2 and run the following SQL.

Aurora PostgreSQL

You can't drop the database that you're connected to, so connect to the postgres database first.

\c postgres DROP DATABASE orders;

Then delete the data group. For more information, see Deleting a data group.

aws rds delete-data-group \ --data-group-identifier dg-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111

To avoid ongoing charges, delete the DB clusters that you created for this tutorial. Run the following commands once for orders-cluster-1 and once for orders-cluster-2. Wait until the DB instance is deleted before you delete the DB cluster. For more information, see Deleting Aurora DB clusters and DB instances.

aws rds delete-db-instance \ --db-instance-identifier orders-cluster-1-instance-1 aws rds delete-db-cluster \ --db-cluster-identifier orders-cluster-1 \ --skip-final-snapshot