|
| 1 | +--- |
| 2 | +title: "Self-managed Kafka cluster" |
| 3 | +tags: ["kafka", "self-managed"] |
| 4 | +categories: [] |
| 5 | +weight: 11 |
| 6 | +description: > |
| 7 | + Using Localstack lambda with self-managed Kafka cluster |
| 8 | +--- |
| 9 | + |
| 10 | +Localstack does not currently support AWS MSK out of the box, but you can run your own self-managed Kafka cluster and integrate it with your own applications. |
| 11 | + |
| 12 | +## Running self-managed Kafka |
| 13 | + |
| 14 | +You can find the [example Docker Compose](docker-compose.yml) file which contains a single-noded ZooKeeper and a Kafka cluster and a simple LocalStack setup as well as [Kowl](https://github.com/cloudhut/kowl), an Apache Kafka Web UI. |
| 15 | + |
| 16 | +1. Run Docker Compose: |
| 17 | + |
| 18 | +{{< command >}} |
| 19 | +$ docker-compose up -d |
| 20 | +{{< / command >}} |
| 21 | + |
| 22 | +2. Create the Lambda function: |
| 23 | + |
| 24 | +{{< command >}} |
| 25 | +$ awslocal lambda create-function \ |
| 26 | + --function-name fun1 \ |
| 27 | + --handler lambda.handler \ |
| 28 | + --runtime python3.8 \ |
| 29 | + --role r1 \ |
| 30 | + --zip-file fileb://lambda.zip |
| 31 | +{ |
| 32 | + "FunctionName": "fun1", |
| 33 | + "FunctionArn": "arn:aws:lambda:us-east-1:000000000000:function:fun1", |
| 34 | + "Runtime": "python3.8", |
| 35 | + "Role": "r1", |
| 36 | + "Handler": "lambda.handler", |
| 37 | + "CodeSize": 294, |
| 38 | + "Description": "", |
| 39 | + "Timeout": 3, |
| 40 | + "LastModified": "2021-05-19T02:01:06.617+0000", |
| 41 | + "CodeSha256": "/GPsiNXaq4tBA4QpxPCwgpeVfP7j+1tTH6zdkJ3jiU4=", |
| 42 | + "Version": "$LATEST", |
| 43 | + "VpcConfig": {}, |
| 44 | + "TracingConfig": { |
| 45 | + "Mode": "PassThrough" |
| 46 | + }, |
| 47 | + "RevisionId": "d85469d2-8558-4d75-bc0e-5926f373e12c", |
| 48 | + "State": "Active", |
| 49 | + "LastUpdateStatus": "Successful", |
| 50 | + "PackageType": "Zip" |
| 51 | +} |
| 52 | +{{< / command >}} |
| 53 | + |
| 54 | +3. Create an example secret: |
| 55 | + |
| 56 | +{{< command >}} |
| 57 | +$ awslocal secretsmanager create-secret --name localstack |
| 58 | +{ |
| 59 | + "ARN": "arn:aws:secretsmanager:us-east-1:000000000000:secret:localstack-TDIuI", |
| 60 | + "Name": "localstack", |
| 61 | + "VersionId": "32bbb8e2-46ee-4322-b3d5-b6459d54513b" |
| 62 | +} |
| 63 | +{{< / command >}} |
| 64 | + |
| 65 | +4. Create an example Kafka topic: |
| 66 | + |
| 67 | +{{< command >}} |
| 68 | +$ docker exec -ti kafka kafka-topics --zookeeper zookeeper:2181 --create --replication-factor 1 --partitions 1 --topic t1 |
| 69 | +Created topic t1. |
| 70 | +{{< / command >}} |
| 71 | + |
| 72 | +5. Create the event source mapping to your local kafka cluster: |
| 73 | + |
| 74 | +{{< command >}} |
| 75 | +$ awslocal lambda create-event-source-mapping \ |
| 76 | + --topics t1 \ |
| 77 | + --source-access-configuration Type=SASL_SCRAM_512_AUTH,URI=arn:aws:secretsmanager:us-east-1:000000000000:secret:localstack-TDIuI \ |
| 78 | + --function-name arn:aws:lambda:us-east-1:000000000000:function:fun1 \ |
| 79 | + --self-managed-event-source '{"Endpoints":{"KAFKA_BOOTSTRAP_SERVERS":["localhost:9092"]}}' |
| 80 | +{ |
| 81 | + "UUID": "4a2b0ea6-960c-4847-8684-465876dd6dbd", |
| 82 | + "BatchSize": 100, |
| 83 | + "FunctionArn": "arn:aws:lambda:us-east-1:000000000000:function:fun1", |
| 84 | + "LastModified": "2021-05-19T04:02:49+02:00", |
| 85 | + "LastProcessingResult": "OK", |
| 86 | + "State": "Enabled", |
| 87 | + "StateTransitionReason": "User action", |
| 88 | + "Topics": [ |
| 89 | + "t1" |
| 90 | + ], |
| 91 | + "SourceAccessConfigurations": [ |
| 92 | + { |
| 93 | + "Type": "SASL_SCRAM_512_AUTH", |
| 94 | + "URI": "arn:aws:secretsmanager:us-east-1:000000000000:secret:localstack-TDIuI" |
| 95 | + } |
| 96 | + ], |
| 97 | + "SelfManagedEventSource": { |
| 98 | + "Endpoints": { |
| 99 | + "KAFKA_BOOTSTRAP_SERVERS": [ |
| 100 | + "localhost:9092" |
| 101 | + ] |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | +{{< / command >}} |
| 106 | + |
| 107 | +6. Aditionally visit `http://localhost:8080` for Kowl's UI. |
0 commit comments