> For the complete documentation index, see [llms.txt](https://ai-security-docs.akto.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ai-security-docs.akto.io/akto-argus-agentic-ai-security-for-homegrown-ai/connectors/others/kafka-amazon-msk-iam.md).

# Connect Akto to Amazon MSK (IAM)

Connect the Akto mini-runtime and traffic collectors to Amazon MSK using IAM authentication

## Overview

Akto can use an [Amazon MSK](https://docs.aws.amazon.com/msk/latest/developerguide/what-is-msk.html) cluster with [IAM access control](https://docs.aws.amazon.com/msk/latest/developerguide/iam-access-control.html) as its Kafka. The traffic collectors send API traffic to MSK, and the Akto mini-runtime reads it from there.

Once connected:

* **No passwords** - the mini-runtime and the traffic collectors authenticate with the AWS identity they run as, such as an EKS pod role, an ECS task role or an EC2 instance role.
* **Encrypted** - all connections use TLS.
* **Topics are created automatically** - Amazon MSK does not create topics on first use (`auto.create.topics.enable=false`), so the mini-runtime checks the Akto topics when it starts and creates any that are missing.
* **No collector changes** - the traffic collectors only write to topics that already exist, whatever type of collector you use.

## How It Works

```mermaid
flowchart LR
    TC["Traffic collectors"] -->|"write API traffic<br/>(IAM, TLS, port 9098)"| MSK[("Amazon MSK<br/>akto.* topics")]
    MSK -->|"read API traffic"| MR["Akto mini-runtime"]
    MR -->|"create missing topics<br/>on startup"| MSK
    MR --> D["Akto dashboard"]
```

When the mini-runtime starts:

```mermaid
sequenceDiagram
    autonumber
    participant MR as Akto mini-runtime
    participant MSK as Amazon MSK
    participant TC as Traffic collector

    MR->>MSK: Connect with IAM and list topics
    alt Topics missing
        MR->>MSK: Create the missing akto.* topics
        MSK-->>MR: Created
    else All topics present
        MSK-->>MR: Nothing to create
    end
    MR->>MSK: Start reading akto.api.logs
    TC->>MSK: Write API traffic and heartbeats
    MSK-->>MR: Deliver API traffic
```

1. **Check** - the mini-runtime lists the topics on the cluster.
2. **Create** - it creates only the missing Akto topics, using the cluster's default partitions and replication factor. Existing topics are never modified.
3. **Retry** - if a topic cannot be created, it retries 3 times, then logs the reason with the fix and keeps running.
4. **Read** - the mini-runtime reads the traffic the collectors send.

## Prerequisites

### 1. Amazon MSK cluster

An Amazon MSK cluster with **IAM role-based authentication** enabled.

### 2. Network access

The mini-runtime and the traffic collectors run in a network that can reach the MSK brokers on port `9098`, the IAM listener.

### 3. AWS identity

The mini-runtime and the traffic collectors run with an AWS identity. Credentials are picked up automatically from the [default AWS credential chain](https://docs.aws.amazon.com/sdkref/latest/guide/standardized-credentials.html):

* **Amazon EKS** - [IAM role for the service account (IRSA)](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html) or [EKS Pod Identity](https://docs.aws.amazon.com/eks/latest/userguide/pod-identities.html)
* **Amazon ECS** - [task role](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html)
* **Amazon EC2** - [instance profile role](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html)
* or `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY` / `AWS_SESSION_TOKEN` environment variables

### 4. Start order

Start the **mini-runtime before the traffic collectors**, so the topics exist before the collectors send traffic. Traffic sent while the topics are missing is not stored.

## Steps to Connect

Replace `<region>`, `<account-id>`, `<cluster-name>` and `<cluster-uuid>` with your MSK cluster's values. They are part of the cluster ARN.

{% stepper %}
{% step %}
**Grant IAM permissions to the mini-runtime**

Attach this policy to the IAM role the mini-runtime runs as:

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["kafka-cluster:Connect"],
      "Resource": "arn:aws:kafka:<region>:<account-id>:cluster/<cluster-name>/<cluster-uuid>"
    },
    {
      "Effect": "Allow",
      "Action": [
        "kafka-cluster:DescribeTopic",
        "kafka-cluster:CreateTopic",
        "kafka-cluster:ReadData",
        "kafka-cluster:WriteData"
      ],
      "Resource": "arn:aws:kafka:<region>:<account-id>:topic/<cluster-name>/<cluster-uuid>/akto.*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "kafka-cluster:DescribeGroup",
        "kafka-cluster:AlterGroup"
      ],
      "Resource": "arn:aws:kafka:<region>:<account-id>:group/<cluster-name>/<cluster-uuid>/*"
    }
  ]
}
```

| Permission                                                | Why it is needed                                                               |
| --------------------------------------------------------- | ------------------------------------------------------------------------------ |
| `kafka-cluster:Connect`                                   | Connect to the MSK cluster                                                     |
| `kafka-cluster:DescribeTopic`                             | Check whether the Akto topics exist                                            |
| `kafka-cluster:CreateTopic`                               | Create the Akto topics if they are missing. **Optional** - see the note below. |
| `kafka-cluster:ReadData`                                  | Read the traffic sent by the collectors                                        |
| `kafka-cluster:WriteData`                                 | Write to the Akto topics, for example settings updates for the collectors      |
| `kafka-cluster:DescribeGroup`, `kafka-cluster:AlterGroup` | Use a consumer group to read the traffic                                       |

{% hint style="info" %}
**Do I need to add `kafka-cluster:CreateTopic` explicitly?**

* If the role already has broader MSK access that includes it, such as `kafka-cluster:*` or administrator access, nothing else is needed.
* Policies that only grant `kafka:*`, for example `AmazonMSKFullAccess`, cover managing MSK clusters, **not** creating topics inside them. Topic creation needs `kafka-cluster:CreateTopic`.
* If you prefer not to grant `CreateTopic`, [create the topics manually](#create-the-topics-manually) instead.
* An explicit `Deny`, a service control policy (SCP) or a permissions boundary that blocks `kafka-cluster:CreateTopic` prevents topic creation even for an administrator role.
  {% endhint %}
  {% endstep %}

{% step %}
**Grant IAM permissions to the traffic collector**

The traffic collector only sends data, so it needs fewer permissions. Attach this policy to the IAM role the traffic collector runs as:

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["kafka-cluster:Connect"],
      "Resource": "arn:aws:kafka:<region>:<account-id>:cluster/<cluster-name>/<cluster-uuid>"
    },
    {
      "Effect": "Allow",
      "Action": [
        "kafka-cluster:DescribeTopic",
        "kafka-cluster:WriteData"
      ],
      "Resource": "arn:aws:kafka:<region>:<account-id>:topic/<cluster-name>/<cluster-uuid>/akto.*"
    }
  ]
}
```

The topic permissions apply only to topics whose names start with `akto.`. Neither the mini-runtime nor the collector can create, change or delete any other topic in your cluster.
{% endstep %}

{% step %}
**Configure the mini-runtime**

Add the following environment variables to the mini-runtime.

**Kubernetes** - add these to the `env` section of the mini-runtime container:

```yaml
env:
  - name: AKTO_KAFKA_BROKER_URL
    value: "<bootstrap-brokers-iam>"
  - name: KAFKA_AUTH_ENABLED
    value: "true"
  - name: AKTO_KAFKA_SASL_MECHANISM
    value: "AWS_MSK_IAM"
  - name: AWS_REGION
    value: "<region>"
```

**Docker:**

```bash
-e AKTO_KAFKA_BROKER_URL=<bootstrap-brokers-iam> \
-e KAFKA_AUTH_ENABLED=true \
-e AKTO_KAFKA_SASL_MECHANISM=AWS_MSK_IAM \
-e AWS_REGION=<region>
```

| Variable                    | Value                                                                                                                                                                                                                                                                                                                                                                 |
| --------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `AKTO_KAFKA_BROKER_URL`     | The MSK IAM bootstrap brokers, for example `b-1.mycluster.abc123.c2.kafka.us-east-1.amazonaws.com:9098,b-2.mycluster.abc123.c2.kafka.us-east-1.amazonaws.com:9098`. Get them from the MSK console (**View client information** → **Private endpoint (IAM)**), or with `aws kafka get-bootstrap-brokers --cluster-arn <cluster-arn>` (`BootstrapBrokerStringSaslIam`). |
| `KAFKA_AUTH_ENABLED`        | `true`                                                                                                                                                                                                                                                                                                                                                                |
| `AKTO_KAFKA_SASL_MECHANISM` | `AWS_MSK_IAM`                                                                                                                                                                                                                                                                                                                                                         |
| `AWS_REGION`                | The region of the MSK cluster                                                                                                                                                                                                                                                                                                                                         |

When `AKTO_KAFKA_SASL_MECHANISM=AWS_MSK_IAM` is set, `KAFKA_USERNAME` and `KAFKA_PASSWORD` are not used. TLS is enabled automatically.
{% endstep %}

{% step %}
**Configure the traffic collector**

Add the following environment variables to the traffic collector.

**Kubernetes (DaemonSet)** - add these to the `env` section of the collector container:

```yaml
env:
  - name: AKTO_KAFKA_BROKER_MAL
    value: "<msk-broker-host>:9098"
  - name: AKTO_KAFKA_SASL_MECHANISM
    value: "AWS_MSK_IAM"
  - name: AWS_REGION
    value: "<region>"
```

**Docker:**

```bash
-e AKTO_KAFKA_BROKER_MAL=<msk-broker-host>:9098 \
-e AKTO_KAFKA_SASL_MECHANISM=AWS_MSK_IAM \
-e AWS_REGION=<region>
```

| Variable                    | Value                                                                                                                                                                                                                                                      |
| --------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `AKTO_KAFKA_BROKER_MAL`     | **One** MSK broker address with the IAM port, for example `b-1.mycluster.abc123.c2.kafka.us-east-1.amazonaws.com:9098`. Use a single broker from the bootstrap list, not the full comma-separated list. The collector discovers the other brokers from it. |
| `AKTO_KAFKA_SASL_MECHANISM` | `AWS_MSK_IAM`                                                                                                                                                                                                                                              |
| `AWS_REGION`                | The region of the MSK cluster                                                                                                                                                                                                                              |

{% hint style="info" %}
TLS is enabled automatically for MSK IAM. MSK uses publicly trusted certificates, so no CA certificate file is needed, and `USE_TLS` / `TLS_CA_CERT_PATH` are not required. `IS_AUTH_IMPLEMENTED`, `KAFKA_USERNAME` and `KAFKA_PASSWORD` are ignored.
{% endhint %}
{% endstep %}

{% step %}
**Start the services and verify**

Start the mini-runtime first, then the traffic collectors. Check the mini-runtime logs:

| Log message                                      | Meaning                                              |
| ------------------------------------------------ | ---------------------------------------------------- |
| `Created Kafka topics: [...]`                    | The listed topics were missing and have been created |
| `Kafka topics are present: [...]`                | All topics exist                                     |
| `Kafka init: ping successful, producer is ready` | The mini-runtime can write to MSK                    |
| `Consumer subscribed to topic: akto.api.logs`    | The mini-runtime is ready to read traffic            |

The traffic collector logs should show `Configuring AWS MSK IAM authentication` and no Kafka write errors. API traffic then appears in the Akto dashboard under **API Collections**.
{% endstep %}
{% endstepper %}

## Kafka Topics

When it starts, the mini-runtime checks that the following topics exist and creates any that are missing:

| Topic                                | Contents                                         |
| ------------------------------------ | ------------------------------------------------ |
| `akto.api.logs`                      | API traffic from the traffic collectors          |
| `akto.api.logs2`                     | API traffic for threat detection                 |
| `akto.api.producer.logs`             | Logs                                             |
| `akto.daemonset.producer.heartbeats` | Traffic collector heartbeats                     |
| `akto.config.updates`                | Settings updates for the traffic collectors      |
| `akto.misc`                          | A test message the mini-runtime sends on startup |

### Create the topics manually

If you do not grant `kafka-cluster:CreateTopic`, create the topics yourself before starting the mini-runtime. For example, use the Kafka CLI with the [Amazon MSK IAM authentication library](https://github.com/aws/aws-msk-iam-auth):

```bash
# client.properties
security.protocol=SASL_SSL
sasl.mechanism=AWS_MSK_IAM
sasl.jaas.config=software.amazon.msk.auth.iam.IAMLoginModule required;
sasl.client.callback.handler.class=software.amazon.msk.auth.iam.IAMClientCallbackHandler
```

```bash
for topic in akto.api.logs akto.api.logs2 akto.api.producer.logs akto.daemonset.producer.heartbeats akto.config.updates akto.misc; do
  bin/kafka-topics.sh --bootstrap-server <bootstrap-brokers-iam> \
    --command-config client.properties --create --topic $topic
done
```

### Optional mini-runtime settings

| Variable                              | Default | Description                                                                                                                                                                                             |
| ------------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `AKTO_KAFKA_CREATE_TOPICS`            | not set | Not set: topics are checked and created when using Amazon MSK IAM. `false`: never create topics. `true`: also check and create topics for other Kafka clusters, for example Amazon MSK with SASL/SCRAM. |
| `AKTO_KAFKA_TOPIC_PARTITIONS`         | `-1`    | Number of partitions for topics the mini-runtime creates. `-1` uses the cluster's `num.partitions`.                                                                                                     |
| `AKTO_KAFKA_TOPIC_REPLICATION_FACTOR` | `-1`    | Replication factor for topics the mini-runtime creates. `-1` uses the cluster's `default.replication.factor`.                                                                                           |

## Troubleshooting

**Mini-runtime: `Kafka topics may be missing, reads and writes to them will fail: {...}. Fix: grant the IAM role kafka-cluster:Connect, kafka-cluster:DescribeTopic and kafka-cluster:CreateTopic on these topics, or create them manually`**

The mini-runtime could not create the topics. It is usually preceded by `Could not ensure Kafka topics exist ... TopicAuthorizationException`, which means the mini-runtime's IAM role is not allowed to create topics. The mini-runtime keeps running, but traffic is not processed until the topics exist. To fix it, either:

* add `kafka-cluster:CreateTopic` (and `kafka-cluster:DescribeTopic`) on `topic/<cluster-name>/<cluster-uuid>/akto.*` to the mini-runtime's role, then restart the mini-runtime, or
* [create the topics manually](#create-the-topics-manually). The mini-runtime picks them up without a restart.

IAM changes can take a minute to take effect.

**Mini-runtime: `Kafka init: ping failed ... Topic akto.misc not present in metadata`**

The topics do not exist. See the previous issue.

**Traffic collector: `Kafka write ... failed ... Unknown Topic Or Partition`**

The topic does not exist on the MSK cluster. Make sure the mini-runtime has started and created the topics. The collector keeps running and starts sending successfully as soon as the topics exist; traffic sent before that is not stored.

**`Failed to load AWS config for MSK IAM authentication`, or authentication errors**

The service cannot find AWS credentials, or its role is missing `kafka-cluster:Connect`. Make sure it runs with an IAM role (see [Prerequisites](#prerequisites)), the policies from the steps above are attached, and `AWS_REGION` is set.

## Get Support

If you need assistance connecting Akto to Amazon MSK:

* **In-app Chat** - Use the chat widget in your Akto dashboard for instant support.
* **Discord Community** - Join our community at [discord.gg/Wpc6xVME4s](https://discord.gg/Wpc6xVME4s).
* **Email Support** - Contact us at <help@akto.io>.
* **Contact Form** - Submit a support request at <https://www.akto.io/contact-us>.

Our team is available 24/7 to help with setup, troubleshooting, and best practices.
