Connect Confluent CLI to Confluent Cloud Cluster

You can connect your Confluent CLI instance to a Confluent Cloud cluster by following this procedure.

You must have a valid user account in Confluent Cloud to connect to Confluent Cloud with the Confluent CLI.

  1. Log in to your cluster using the confluent login command using a Confluent Cloud user account.

    confluent login --save
    

    With the --save flag, you are automatically logged back in when your login credentials or SSO refresh token expires after one hour.

  2. Set the Confluent Cloud environment.

    1. Get the environment ID.

      confluent environment list
      

      Your output should resemble:

           Id    |      Name
      +----------+----------------+
        * a-542  | dev
          a-4985 | prod
          a-2345 | jdoe-gcp-env
          a-9012 | jdoe-aws-env
      
    2. Set the environment using the ID (<env-id>).

      confluent environment use <env-id>
      

      Your output should resemble:

      Now using a-4985 as the default (active) environment.
      
  3. Set the cluster to use.

    1. Get the cluster ID.

      confluent kafka cluster list
      

      Your output should resemble:

            Id      |       Name        | Provider |   Region    | Durability | Status
      +-------------+-------------------+----------+-------------+------------+--------+
          ekg-rr8v7 | dev-aws-oregon    | aws      | us-west-2   | LOW        | UP
          ekg-q2j96 | prod              | gcp      | us-central1 | LOW        | UP
      
    2. Set the cluster using the ID (<cluster-id>). This is the cluster where the commands are run.

      confluent kafka cluster use <cluster-id>
      
  4. Create an API key and secret and save them. This is required to produce or consume to your topic.

    From the Confluent CLI, type the following command:

    confluent api-key create --resource <resource-id>
    

    Your output should resemble:

    Save the API key and secret. The secret is not retrievable later.
    +---------+------------------------------------------------------------------+
    | API Key | LD35EM2YJTCTRQRM                                                 |
    | Secret  | 67JImN+9vk+Hj3eaj2/UcwUlbDNlGGC3KAIOy5JNRVSnweumPBUpW31JWZSBeawz |
    +---------+------------------------------------------------------------------+
    
  5. (Optional) Add the API secret with confluent api-key store <key> <secret>. When you create an API key with the CLI, it is automatically stored locally. However, when you create an API key using the UI, API, or with the CLI on another machine, the secret is not available for CLI use until you store it. This is required because secrets cannot be retrieved after creation.

    confluent api-key store <api-key> <api-secret> --resource <resource-id>
    
  6. Set the API key to use for the Confluent CLI commands:

    confluent api-key use <api-key> --resource <resource-id>
    

You can now run the Confluent CLI commands against the specified cluster.