Before you begin
You’ll need:- An AWS account.
- A CockroachDB self-hosted cluster hosted on AWS. You can set up a cluster using . You must create instances in the same VPC that the MSK Serverless cluster will use in order for the changefeed to authenticate successfully.
- A Kafka client to consume the changefeed messages. You must ensure that your client machine is in the same VPC as the MSK Serverless cluster. This tutorial uses a client set up following the AWS MSK Serverless guide.
- The
CHANGEFEEDprivilege in order to create and manage changefeed jobs. Refer to for more details.
Step 1. Create an MSK Serverless cluster
- In the AWS Management Console, go to the Amazon MSK console and click Create cluster.
- If you select Quick create, AWS will provision the cluster in the default VPC for the account. To modify the VPC or network settings, select Custom create. (The VPC selection is important because the MSK Serverless cluster must be in the same VPC as the CockroachDB instance and Kafka client machine.)
- Ensure you name the cluster and select Serverless as the cluster type. Click Create cluster.
- Once the cluster is running, click View client information in the Cluster summary box. Copy the endpoint address, which will be similar to
boot-vab1abab.c1.kafka-serverless.us-east-1.amazonaws.com:9098. Click Done to return to the cluster’s overview page.
Step 2. Create an IAM policy and role to access the MSK Serverless cluster
MSK Serverless clusters only support IAM authentication. In this step, you’ll create an IAM policy that contains the permissions to interact with the MSK Serverless cluster. Then, you’ll create an IAM role, which you’ll associate with the IAM policy. In a later step, both the CockroachDB cluster and Kafka client machine will use this role to work with the MSK Serverless cluster.- In the AWS Management Console, go to the IAM console, select Policies from the navigation, and then Create Policy.
-
Using the JSON tab option, update the policy with the following JSON. These permissions will allow you to connect to the cluster, manage topics, and consume messages. You may want to adjust the permissions to suit your permission model. For more details on the available permissions, refer to the AWS documentation on IAM Access Control for MSK.
Replace the instances of
arn:aws:kafka:{region}:{account ID}:cluster/{msk-cluster-name}with the MSK ARN from your cluster’s summary page and add/*to the end, like the following: -
Once you have added your policy, add a policy name (for example,
msk-policy), click Next, and Create policy. - Return to the IAM console, select Roles from the navigation, and then Create role.
- Select AWS service for the Trusted entity type. For Use case, select EC2 from the dropdown. Click Next.
-
On the Add permissions page, search for the IAM policy (
msk-policy) you just created. Click Next. -
Name the role (for example,
msk-role) and click Create role.
Step 3. Set up the CockroachDB cluster role
In this step, you’ll create a role, which contains thests:AssumeRole permission, for the EC2 instance that is running your CockroachDB cluster. The sts:AssumeRole permission will allow the EC2 instance to obtain temporary security credentials to access the MSK Serverless cluster according to the msk-policy permissions. To achieve this, you’ll add the EC2 role to the trust relationship of the msk-role you created in the previous step.
- Navigate to the IAM console, select Roles from the navigation, and then Create role.
- Select AWS service for the Trusted entity type. For Use case, select EC2 from the dropdown. Click Next.
- On the Add permissions page, click Next.
-
Name the role (for example,
ec2-role) and click Create role. -
Once the role has finished creating, copy the ARN in the Summary section. Click on the Trust relationships tab. You’ll find a Trusted entities policy:
-
Navigate to the IAM console and search for the role (
msk-role) you created in Step 2 that contains the MSK policy. Select the role, which will take you to its summary page. -
Click on the Trust relationships tab, and click Edit trust policy. Add the ARN of the EC2 IAM role (
ec2-role) to the JSON policy:Once you’ve updated the policy, click Update policy.
Step 4. Connect the client to the MSK Serverless cluster
In this step, you’ll prepare the client to connect to the MSK Serverless cluster and create a Kafka topic.- Ensure that your client can connect to the MSK Serverless cluster. This tutorial uses an EC2 instance running Kafka as the client. Navigate to the summary page for the client EC2 instance. Click on the Actions dropdown. Click Security, and then select Modify IAM role.
-
On the Modify IAM role page, select the role you created for the MSK Serverless cluster (
msk-role) that contains the policy created in Step 2. Click Update IAM role. -
Open a terminal and connect to your Kafka client. Check that your
client.propertiesfile contains the correct SASL and security configuration, like the following:If you need further detail on setting up the Kafka client, refer to the AWS setup guide. -
Move to the directory of your Kafka installation:
-
It is necessary to create topics manually for MSK Serverless clusters. To create a topic, run the following:
Replace:
- with your endpoint copied in Step 1.4.
- with your topic name. This tutorial will use the CockroachDB
movrworkload and will run a changefeed on themovr.userstable. - with the number of partitions you require.
Step 5. Start the changefeed
In this step, you’ll prepare your CockroachDB cluster to start the changefeed.-
(Optional) On the EC2 instance running CockroachDB, run the application workload to set up some data for your changefeed.
Create the schema for the workload:
Then run the workload:
-
Start a SQL session. For details on the available flags, refer to the page.
To set your Enterprise license, refer to the page.
-
Enable the
kv.rangefeed.enabled: -
To connect the changefeed to the MSK Serverless cluster, the URI must contain the following parameters:
- The MSK Serverless cluster endpoint prefixed with the
kafka://scheme, for example:kafka://boot-vab1abab.c1.kafka-serverless.us-east-1.amazonaws.com:9098. tls_enabledset totrue.sasl_enabledset totrue.sasl_mechanismset toAWS_MSK_IAM.sasl_aws_regionset to the region of the MSK Serverless cluster.sasl_aws_iam_role_arnset to the ARN for the IAM role (msk-role) that has the permissions outlined in Step 2.2.sasl_aws_iam_session_nameset to a string that you specify to identify the session in AWS.
You can either specify the Kafka URI in theCREATE CHANGEFEEDstatement directly. Or, create an for the MSK Serverless URI. External connections define a name for an external connection while passing the provider URI and query parameters: - The MSK Serverless cluster endpoint prefixed with the
-
Use the statement to start the changefeed using either the external connection (
external://) or fullkafka://URI:To view a changefeed job, use .
Step 6. Consume the changefeed messages on the client
-
Return to the terminal that is running the Kafka client. Move to the Kafka installation directory:
-
Run the following command to start a consumer. Set
--topicto the topic you created in Step 4.5:
See also
For more resources, refer to the following:- page for details on parameters that sinks support.
- Configuration for serverless clusters in the AWS documentation for details on topic-level configuration options.
- Monitoring serverless clusters in the AWS documentation for details on monitoring the MSK Serverless cluster.
- for details on monitoring the changefeed job.

