<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Cloud_Geek on Medium]]></title>
        <description><![CDATA[Stories by Cloud_Geek on Medium]]></description>
        <link>https://medium.com/@cloud_geek?source=rss-727cd3743f56------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*sLIIEST9M8WiqtdbgPcgAg.png</url>
            <title>Stories by Cloud_Geek on Medium</title>
            <link>https://medium.com/@cloud_geek?source=rss-727cd3743f56------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 22:10:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cloud_geek/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Kafka Producer and Consumer in Python | Dockerized kafka & zookeeper]]></title>
            <link>https://medium.com/@cloud_geek/kafka-producer-and-consumer-in-python-dockerized-kafka-zookeeper-cf788fc1760e?source=rss-727cd3743f56------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf788fc1760e</guid>
            <category><![CDATA[kafka-docker]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[kafka-python]]></category>
            <category><![CDATA[kafka-producer]]></category>
            <category><![CDATA[kafka-consumer]]></category>
            <dc:creator><![CDATA[Cloud_Geek]]></dc:creator>
            <pubDate>Thu, 29 Dec 2022 09:17:08 GMT</pubDate>
            <atom:updated>2025-04-23T17:38:29.928Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, we will learn to write a producer and a consumer for Kafka Topic in python.</p><h3><strong>Prerequisites:</strong></h3><p>We should have kafka, zookeeper and kafdrop docker containers already running. You can read about it in my <a href="https://medium.com/@cloud_geek/run-kafka-cluster-zookeeper-using-docker-kafdrop-docker-compose-52309c9f9e51">previous post</a>.<br>Or, you can use below docker-compose file to spin-up these containers.</p><pre>docker-compose -f docker-compose-kafka-cluster.yaml up -d</pre><pre>version: &#39;2&#39;<br>services:<br># Zookeeper, single node<br>  zookeeper:<br>    image: wurstmeister/zookeeper:latest<br>    environment:<br>      ZOOKEEPER_CLIENT_PORT: 2181<br>      ZOOKEEPER_TICK_TIME: 2000<br>    ports:<br>      - 2181:2181<br>      - 2888:2888<br>      - 3888:3888<br> # kafka single node     <br>  kafka1:<br>    image: wurstmeister/kafka:latest<br>    restart: &quot;no&quot;<br>    links:<br>      - zookeeper<br>    ports:<br>      - 9092:9092<br>    environment:<br>      KAFKA_BROKER_ID: 1<br>      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181<br>      KAFKA_LISTENERS: INTERNAL://:29092,EXTERNAL://:9092<br>      KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka1:29092,EXTERNAL://localhost:9092<br>      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT<br>      KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL<br>      #https://github.com/wurstmeister/kafka-docker/issues/553<br>  kafka2:<br>    image: wurstmeister/kafka:latest<br>    restart: &quot;no&quot;<br>    links:<br>      - zookeeper<br>    ports:<br>      - 9093:9093<br>    environment:<br>      KAFKA_BROKER_ID: 2<br>      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181<br>      KAFKA_LISTENERS: INTERNAL://:29093,EXTERNAL://:9093<br>      KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka2:29093,EXTERNAL://localhost:9093<br>      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT<br>      KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL<br>#kafdrop for topic/msg visualization     <br>  kafdrop:<br>    image: obsidiandynamics/kafdrop<br>    restart: &quot;no&quot;<br>    environment:<br>      KAFKA_BROKERCONNECT: &quot;kafka1:29092,kafka2:29093&quot;<br>      JVM_OPTS: &quot;-Xms16M -Xmx512M -Xss180K -XX:-TieredCompilation -XX:+UseStringDeduplication -noverify&quot;<br>    ports:<br>      - 9000:9000<br>    depends_on:<br>      - kafka1<br>      - kafka2</pre><h3>Create Producer &amp; Consumer</h3><p>We will create a python project to create our producer and consumer.<br>Or if you already have python3 installed you can do <strong><em>‘pip install kafka-python’</em></strong></p><p><strong>requirements.txt:</strong></p><pre>kafka-python==2.0.2</pre><p><strong>kafka_producer.py:</strong></p><pre>from kafka import KafkaProducer<br>import json<br>if __name__ == &#39;__main__&#39;:<br>    print(&quot;starting kafka producer..&quot;)<br>    producer = KafkaProducer(<br>        bootstrap_servers = &quot;localhost:9092&quot;,<br>        value_serializer = lambda v: json.dumps(v).encode(&#39;utf-8&#39;)<br>    )<br>    producer.send(&quot;orders&quot;, &quot;order1&quot;)<br>    producer.flush()<br>    print(&quot;message produced&quot;)</pre><p><strong>kafka_consumer.py:</strong></p><pre>from kafka import KafkaConsumer<br>import json<br><br>if __name__ == &#39;__main__&#39;:<br>    consumer = KafkaConsumer(<br>        &quot;orders&quot;<br>        bootstrap_servers=&quot;localhost:9092&quot;,<br>        value_deserializer=lambda m: json.loads(m.decode(&#39;ascii&#39;)),<br>    )<br>    for message in consumer:<br>      print(&#39;Message received: &#39;)<br>      print(str(message.value))</pre><h3>Publish Messages to Kafka Topic</h3><p>Once you have written kafka_producer.py, run it ( <strong><em>python3 kafka_producer.py </em></strong>) to publish new message to <strong>orders</strong> topic.<br>Published messages can be seen in kafdrop UI</p><figure><img alt="kafka message in kafdrop UI" src="https://cdn-images-1.medium.com/max/1024/1*nblHAhHiVgeCkJmDgU8d_w.png" /><figcaption>Kafka Mesasge in Kafdrop UI</figcaption></figure><h3>Consume Messages from Kafka Topic</h3><p>Once messages have been published to kafka topic, we can run our kafka consumer( <strong><em>python3 kafka_consumer.py </em></strong>). Consumer once started it keeps on running and as soon a new message is published in kafka topic, it consumes. <br>In producer you can publish new messages and see it getting printed in consumer code.</p><p>That’s all !! Congrats you have written your kafka producer and consumer.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FyT-GYVssdEg%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DyT-GYVssdEg&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FyT-GYVssdEg%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/a7b2e5af2cd89e274ed98142eef72efc/href">https://medium.com/media/a7b2e5af2cd89e274ed98142eef72efc/href</a></iframe><p>Hope this article was helpful to you. If you liked it, please upvote, follow and share. !! Happy Learning !! ✌️</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf788fc1760e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Run Debezium Kafka Connect using Docker | Kafka | Zookeeper | Kafdrop |Kafka Connect | Docker…]]></title>
            <link>https://medium.com/@cloud_geek/run-debezium-kafka-connect-using-docker-kafka-zookeeper-kafdrop-kafka-connect-docker-2e67760ef85d?source=rss-727cd3743f56------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e67760ef85d</guid>
            <category><![CDATA[kafka-connect]]></category>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[data-engineering]]></category>
            <category><![CDATA[debezium]]></category>
            <category><![CDATA[kafka]]></category>
            <dc:creator><![CDATA[Cloud_Geek]]></dc:creator>
            <pubDate>Thu, 29 Dec 2022 05:36:09 GMT</pubDate>
            <atom:updated>2023-05-20T04:23:09.302Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Run Debezium Kafka Connect using Docker | Kafka | Zookeeper | Kafdrop |Kafka Connect | Docker Compose</strong></h3><p>In this article, we will see how Data Engineering teams Capture Data Change events and how data is published to kafka topics etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lSG6nCWRtu_mixSKQ6NrUA.png" /></figure><h3><strong>Step1: Provision Docker containers</strong></h3><p>We will provision below 6 (4+2) docker containers using a docker-compose file.</p><p><a href="https://zookeeper.apache.org/"><strong>Zookeeper</strong></a>: ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. We will bring up zookeeper node on standard 2181 port, TICK_TIME is 2 seconds which is the heartbeat for zookeeper. Ports 2888 and 3888 are used by zookeeper peers to talk to each other.</p><p><a href="https://kafka.apache.org/intro"><strong>Kafka Cluster</strong></a>: A Kafka cluster is a system that consists of several Brokers, Topics, and Partitions for both. The key objective is to distribute workloads equally among replicas and Partitions. We will provision 2 kafka nodes first node runs on port 9092 and second on 9093. Internal listener ports are 29092, 29093 and external are 9092, 9093 . This <a href="https://www.confluent.io/blog/kafka-listeners-explained/">article</a> explains more about kafka listeners.</p><p><a href="https://github.com/obsidiandynamics/kafdrop"><strong>Kafdrop</strong></a>: Kafdrop is a web UI for viewing Kafka topics and browsing consumer groups. The tool displays information such as brokers, topics, partitions, consumers, and lets you view messages. We will run kafdrop on default 9000 port.</p><p><a href="https://hub.docker.com/r/debezium/example-postgres/tags?page=1&amp;name=1.9"><strong>Postgres</strong></a>: We need a database for which we want to capture Data Change Events. We will use <em>debezium/example-postgres:1.9 </em>image<em> </em>to provision postgres database since it has got some default schema &amp; tables created. Postgres docker runs on port 5433</p><figure><img alt="Default schema &amp; tables in postgres docker image" src="https://cdn-images-1.medium.com/max/279/1*ZPz3F6TukLNjXp6_nIuCIA.png" /><figcaption>Default schema &amp; tables in postgres docker image</figcaption></figure><p><a href="https://debezium.io/documentation/reference/stable/connectors/postgresql.html"><strong>Debezium Connector/Kafka Connect</strong></a>: The Debezium PostgreSQL connector captures row-level changes in the schemas of a PostgreSQL database. This container runs on port 8083.</p><p><a href="https://github.com/skylvr1289/sample_kafka/blob/master/docker-compose-debezium.yaml">docker-compose-debezium.yaml</a>:</p><pre>version: &#39;3.3&#39;<br>services:<br>  postgres:<br>    image: debezium/example-postgres:1.9<br>    container_name: postgres<br>    ports:<br>      - 5433:5432<br>    environment:<br>      - POSTGRES_USER=postgres<br>      - POSTGRES_PASSWORD=postgres<br><br># Zookeeper, single node<br>  zookeeper:<br>    image: wurstmeister/zookeeper:latest<br>    environment:<br>      ZOOKEEPER_CLIENT_PORT: 2181<br>      ZOOKEEPER_TICK_TIME: 2000<br>    ports:<br>      - 2181:2181<br>      - 2888:2888<br>      - 3888:3888<br><br> # kafka multi node     <br>  kafka1:<br>    image: wurstmeister/kafka:latest<br>    restart: &quot;no&quot;<br>    links:<br>      - zookeeper<br>    ports:<br>      - 9092:9092<br>    environment:<br>      KAFKA_BROKER_ID: 1<br>      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181<br>      KAFKA_LISTENERS: INTERNAL://:29092,EXTERNAL://:9092<br>      KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka1:29092,EXTERNAL://localhost:9092<br>      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT<br>      KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL<br>      #https://github.com/wurstmeister/kafka-docker/issues/553<br><br>  kafka2:<br>    image: wurstmeister/kafka:latest<br>    restart: &quot;no&quot;<br>    links:<br>      - zookeeper<br>    ports:<br>      - 9093:9093<br>    environment:<br>      KAFKA_BROKER_ID: 2<br>      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181<br>      KAFKA_LISTENERS: INTERNAL://:29093,EXTERNAL://:9093<br>      KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka2:29093,EXTERNAL://localhost:9093<br>      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT<br>      KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL<br>      #https://github.com/wurstmeister/kafka-docker/issues/553<br><br>#kafdrop for topic/msg visualization<br>  kafdrop:<br>    image: obsidiandynamics/kafdrop<br>    restart: &quot;no&quot;<br>    environment:<br>      KAFKA_BROKERCONNECT: &quot;kafka1:29092,kafka2:29093&quot;<br>      JVM_OPTS: &quot;-Xms16M -Xmx512M -Xss180K -XX:-TieredCompilation -XX:+UseStringDeduplication -noverify&quot;<br>    ports:<br>      - 9000:9000<br>    depends_on:<br>      - kafka1<br>      - kafka2<br><br>  # debezium connector<br>  kconnect:<br>    image: debezium/connect:1.9<br>    ports:<br>      - 8083:8083<br>    environment:<br>      CONFIG_STORAGE_TOPIC: my_connect_configs<br>      OFFSET_STORAGE_TOPIC: my_connect_offsets<br>      STATUS_STORAGE_TOPIC: my_connect_statuses<br>      BOOTSTRAP_SERVERS: kafka1:29092,kafka2:29093<br>    links:<br>      - zookeeper<br>      - postgres<br>    depends_on:<br>      - kafka1<br>      - kafka2<br>      - zookeeper<br>      - postgres  <br><br># 1. kafka &amp; zookeeper --&gt; docker<br># 2. kafdrop --&gt; docker<br># 3. postgres --&gt; docker<br># 4. debezium connector --&gt; docker<br># 5. pgAdmin/DBeaver --&gt; local system<br># 6. Postman --&gt; local system</pre><p><strong>Spin-up dockers:</strong></p><pre>docker-compose -f docker-compose-debezium.yaml up -d</pre><h3>Step2: Register Kafka Connect</h3><p>Once all the containers are up and running, open postman and make a POST call to <a href="http://localhost:8083/connectors">http://localhost:8083/connectors</a> with below request body.<br><strong>Note:</strong> We are passing hostname as ‘<strong>postgres</strong>’ which is the container name in docker-compose file for postgres container and the port is internal port <strong>5432</strong> not the exposed one 5433.</p><pre>{<br>    &quot;name&quot;: &quot;inventory-connector&quot;,<br>    &quot;config&quot;: {<br>        &quot;connector.class&quot;: &quot;io.debezium.connector.postgresql.PostgresConnector&quot;,<br>        &quot;database.hostname&quot;: &quot;postgres&quot;,<br>        &quot;database.port&quot;: &quot;5432&quot;,<br>        &quot;database.user&quot;: &quot;postgres&quot;,<br>        &quot;database.password&quot;: &quot;postgres&quot;,<br>        &quot;database.dbname&quot;: &quot;postgres&quot;,<br>        &quot;database.server.name&quot;: &quot;dbserver1&quot;,<br>        &quot;table.include.list&quot;: &quot;inventory.customers&quot;<br>    }<br>}</pre><p>for example:</p><figure><img alt="postman POST request" src="https://cdn-images-1.medium.com/max/1024/1*Qbsn0o5OxCb1Y-GO8adfbw.png" /><figcaption>Postman POST request</figcaption></figure><p>Here we want to get all change events (INSERT, UPDATE, DELETE) for <strong><em>customers</em></strong> table in <strong><em>inventory</em></strong> <em>schema</em>. All the changes will be published to our kafka topic. <br>To learn more about postgres connector, you can read the official documentation <a href="https://debezium.io/documentation/reference/stable/connectors/postgresql.html">here</a>.</p><h3>Step3: Visualize change events</h3><p>Now that we have kafka connect successfully registered, we are good to make some changes in database. <br>We can use any database client eg. pgAdmin or DBeaver and connect to our database<strong><em> (</em>host<em>: </em></strong><em>localhost</em><strong><em>, </em>post<em>: </em></strong><em>5433</em><strong><em>, </em>user<em>: </em></strong><em>postgres</em><strong><em>, </em>password<em>: </em></strong><em>postgres</em><strong><em>)</em></strong> and we can make an <strong>update/insert</strong> to existing row in <strong><em>customers</em></strong> table.</p><pre>update inventory.customers set first_name = &#39;Cloud_Geek&#39; where id = 1001</pre><p>After the update statement is executed, we can see in kafdrop UI under topic: ‘<strong>dbserver1.inventor.customers’</strong> a new message would be published (offset 4 below):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*POf-ldiq5Le3AdtAOLting.png" /><figcaption>UPDATE change event in Kafdrop UI</figcaption></figure><p>Similarly if we make an INSERT/DELETE statement, we will see new messages getting published to the same topic.</p><h3>Conclusion</h3><p>This is how CDC (Capture Data Change) is used by data engineering teams. This was just a basic example. <br>We can write consumers for the above topic (<strong>dbserver1.inventor.customers</strong>) and trigger various actions(emails etc) based on change events (insert, update, delete). <br>This is how we get automated messages for various stages of our delivery items (Amazon, Flipkart, Delhivery)</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FyZy4QZYMUrY%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DyZy4QZYMUrY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FyZy4QZYMUrY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/0034c785e200427f03605ffed9654987/href">https://medium.com/media/0034c785e200427f03605ffed9654987/href</a></iframe><p>Hope this article was helpful to you. If you liked it, please upvote, follow and share.</p><p>!! Happy Learning !! ✌️</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e67760ef85d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Run Kafka cluster & Zookeeper using Docker | Kafdrop | docker compose]]></title>
            <link>https://medium.com/@cloud_geek/run-kafka-cluster-zookeeper-using-docker-kafdrop-docker-compose-52309c9f9e51?source=rss-727cd3743f56------2</link>
            <guid isPermaLink="false">https://medium.com/p/52309c9f9e51</guid>
            <category><![CDATA[kafka-cluster]]></category>
            <category><![CDATA[kafdrop]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[zookeeper]]></category>
            <category><![CDATA[docker-compose]]></category>
            <dc:creator><![CDATA[Cloud_Geek]]></dc:creator>
            <pubDate>Wed, 28 Dec 2022 12:09:55 GMT</pubDate>
            <atom:updated>2022-12-29T01:34:40.855Z</atom:updated>
            <content:encoded><![CDATA[<p>I always found spinning up kafka and zookeeper nodes little painful. And most of the times, if I have to see message of a particular offset, I have to remember multiple commands which was very time consuming. To solve all these, I present you a dockerized solution.</p><p>In this article, we will learn how to spin up kafka cluster (with 2 nodes) and a zookeeper node along-with kafdrop (an useful UI console to see kafka topics, partitions, messages etc ) using docker-compose.</p><p><a href="https://zookeeper.apache.org/"><strong>Zookeeper</strong></a><br>ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. We will bring up zookeeper node on standard 2181 port, TICK_TIME is 2 seconds which is the heartbeat for zookeeper. Ports 2888 and 3888 are used by zookeeper peers to talk to each other.</p><p><a href="https://kafka.apache.org/intro"><strong>Kafka Cluster </strong></a><strong><br></strong>A Kafka cluster is a system that consists of several Brokers, Topics, and Partitions for both. The key objective is to distribute workloads equally among replicas and Partitions. We will provision 2 kafka nodes first node runs on port 9092 and second on 9093. Internal listener ports are 29092, 29093 and external are 9092, 9093 . This <a href="https://www.confluent.io/blog/kafka-listeners-explained/">article</a> explains more about kafka listeners.</p><p><a href="https://github.com/obsidiandynamics/kafdrop"><strong>Kafdrop</strong></a><strong><br></strong>Kafdrop is a web UI for viewing Kafka topics and browsing consumer groups. The tool displays information such as brokers, topics, partitions, consumers, and lets you view messages. We will run kafdrop on default 9000 port.</p><p>Now that we understand our docker-compose file, we can spin-up dockers.</p><p><em>docker-compose-kafka-cluster.yaml:</em></p><pre>version: &#39;2&#39;<br>services:<br># Zookeeper, single node<br>  zookeeper:<br>    image: wurstmeister/zookeeper:latest<br>    environment:<br>      ZOOKEEPER_CLIENT_PORT: 2181<br>      ZOOKEEPER_TICK_TIME: 2000<br>    ports:<br>      - 2181:2181<br>      - 2888:2888<br>      - 3888:3888<br> # kafka single node     <br>  kafka1:<br>    image: wurstmeister/kafka:latest<br>    restart: &quot;no&quot;<br>    links:<br>      - zookeeper<br>    ports:<br>      - 9092:9092<br>    environment:<br>      KAFKA_BROKER_ID: 1<br>      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181<br>      KAFKA_LISTENERS: INTERNAL://:29092,EXTERNAL://:9092<br>      KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka1:29092,EXTERNAL://localhost:9092<br>      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT<br>      KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL<br>      #https://github.com/wurstmeister/kafka-docker/issues/553<br>  kafka2:<br>    image: wurstmeister/kafka:latest<br>    restart: &quot;no&quot;<br>    links:<br>      - zookeeper<br>    ports:<br>      - 9093:9093<br>    environment:<br>      KAFKA_BROKER_ID: 2<br>      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181<br>      KAFKA_LISTENERS: INTERNAL://:29093,EXTERNAL://:9093<br>      KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka2:29093,EXTERNAL://localhost:9093<br>      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT<br>      KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL<br>#kafdrop for topic/msg visualization     <br>  kafdrop:<br>    image: obsidiandynamics/kafdrop<br>    restart: &quot;no&quot;<br>    environment:<br>      KAFKA_BROKERCONNECT: &quot;kafka1:29092,kafka2:29093&quot;<br>      JVM_OPTS: &quot;-Xms16M -Xmx512M -Xss180K -XX:-TieredCompilation -XX:+UseStringDeduplication -noverify&quot;<br>    ports:<br>      - 9000:9000<br>    depends_on:<br>      - kafka1<br>      - kafka2</pre><p><strong>Command to spin-up dockers:</strong></p><pre>docker-compose -f docker-compose-kafka-cluster.yaml up -d</pre><p><strong>Result: </strong>Once all the dockers are up successfully, we can visit localhost:9000 and view the kafka nodes listed in kafdrop UI. We can also create topics from this kafdrop UI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Woj5w7WDYHpKKjrTXe3UMA.png" /><figcaption>kafdrop UI @ localhost:9000</figcaption></figure><p>You can also watch the hands-on video here:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F8aUZ1J7r8hM%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D8aUZ1J7r8hM&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F8aUZ1J7r8hM%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe963496254ce29456abc67ca2cc1757/href">https://medium.com/media/fe963496254ce29456abc67ca2cc1757/href</a></iframe><p>Hope this article was helpful to you. If you liked it, please upvote, follow and share.</p><p>!! Happy Learning !! ✌️</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=52309c9f9e51" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cleanup Redundant Lambda Log Groups | Automation | Save AWS Cost]]></title>
            <link>https://medium.com/@cloud_geek/cleanup-redundant-lambda-log-groups-e5a149ca6117?source=rss-727cd3743f56------2</link>
            <guid isPermaLink="false">https://medium.com/p/e5a149ca6117</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[cloudwatch]]></category>
            <category><![CDATA[aws-cost-optimization]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[aws-cost-reduction]]></category>
            <dc:creator><![CDATA[Cloud_Geek]]></dc:creator>
            <pubDate>Sat, 24 Sep 2022 06:21:07 GMT</pubDate>
            <atom:updated>2022-12-27T13:04:13.271Z</atom:updated>
            <content:encoded><![CDATA[<p>If you are working in a company which uses AWS as cloud provider, you must have a mechanism to cleanup stale lambda log groups because, some lambda log groups are never deleted and if you ignore them, they keep on accumulating, causing the compounded AWS cost over time.</p><p>To give you just an example, I have seen more than <strong>10,000</strong> to <strong>12,000</strong> stale lambda log groups in VPCs where no cleanup was implemented.</p><p>So, in this article, we are going to implement a lambda function which will delete all the redundant lambda log groups and further we will create an event rule to trigger this lambda function weekly.</p><p><strong>Lambda Function: </strong>We will use python to create the lambda function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d54624cd04f4c3bb44326c78ee75d72e/href">https://medium.com/media/d54624cd04f4c3bb44326c78ee75d72e/href</a></iframe><p><strong>Scheduled Event Rule</strong>: <br>We will create an event rule to trigger this lambda function every Sunday.</p><p><strong>Architecture:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6LRZe5FZWIoBiz_zHJrC8w.png" /></figure><p><strong>CloudFormation Template : </strong>LambdaLogCleanup.yaml</p><pre><br>AWSTemplateFormatVersion: &quot;2010-09-09&quot;<br>Description: &quot;Lambda function to delete redundant (non existing Lambda function&#39;s logs) /aws/lambda/* log groups&quot;<br><br>Parameters:<br>  Regions:<br>    Type: String<br>    Description: &quot;regions for which Cloud Watch Logs will be purged for non existing lambda functions&quot;<br>    Default: &#39;[&quot;us-east-1&quot;,&quot;us-west-2&quot;,&quot;ap-southeast-2&quot;, &quot;eu-west-2&quot;]&#39;<br>  LambdaFunctionName:<br>    Type: String<br>    Description: &quot;name of the lambda function. to avoid creating duplicate functions in same region&quot;<br>    Default: &quot;autocleanup-DeleteRedundantLambdaCWLogsFunction&quot;<br><br>Resources:<br>  #-------------------------------------------------------------------------------<br>  # LAMBDA RESOURCES<br>  #-------------------------------------------------------------------------------<br>  LambdaRole:<br>    Type: AWS::IAM::Role<br>    Properties:<br>      AssumeRolePolicyDocument:<br>        Version: 2012-10-17<br>        Statement:<br>          - Effect: Allow<br>            Principal:<br>              Service:<br>                - lambda.amazonaws.com<br>            Action:<br>              - sts:AssumeRole<br>      ManagedPolicyArns:<br>        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole<br>      Policies:<br>        - PolicyName: LambdaLogCleanupFunctionAccess<br>          PolicyDocument:<br>            Version: &quot;2012-10-17&quot;<br>            Statement:<br>              - Effect: Allow<br>                Resource: &quot;*&quot;<br>                Action:<br>                  - lambda:ListFunctions<br>                  - logs:DescribeLogGroups<br>                  - logs:DeleteLogGroup<br>                  - logs:DeleteLogStream<br><br>  LambdaFunction:<br>    Type: AWS::Lambda::Function<br>    Properties:<br>      FunctionName: !Ref LambdaFunctionName<br>      Code:<br>        ZipFile: |<br>          import json<br>          import os<br>          import boto3<br>          import logging<br>          logger = logging.getLogger()<br>          logger.setLevel(logging.INFO) <br>          <br>          def lambda_handler(event, context):<br>              print(&quot;Entered lambda_handler&quot;)<br>              regions =json.loads(os.environ.get(&#39;REGIONS&#39;, &#39;[&quot;us-west-2&quot;]&#39;))<br>              for region in regions:<br>                  delete_redundant_lambda_logs(region)<br>                  <br>          def delete_redundant_lambda_logs(region: str):<br>              print(region)<br>              lambda_client = boto3.client(&#39;lambda&#39;, region_name = region)<br>              lambda_paginator = lambda_client.get_paginator(&#39;list_functions&#39;)<br>              existing_functions = set()<br>              page_count =0<br>              count = 0<br>              for response in lambda_paginator.paginate():<br>                print(&#39;page: %s&#39;%str(page_count))<br>                page_count = page_count+1<br>                for function in response[&#39;Functions&#39;]:<br>                  count = count+1<br>                  existing_functions.add(function[&#39;FunctionName&#39;])<br>          <br>              print(&#39;Total valid existing functions: %s&#39;%str(count))<br>              # list all lambda log groups<br>              cw_client = boto3.client(&#39;logs&#39;, region_name = region)<br>              paginator = cw_client.get_paginator(&#39;describe_log_groups&#39;)<br>              count = 0<br>              deleted_logs = []<br>              for page in paginator.paginate(logGroupNamePrefix = &#39;/aws/lambda/&#39;):<br>                print(&#39;log page: &#39;+ str(count))<br>                count = count +1<br>                for group in page[&#39;logGroups&#39;]:<br>                  #&#39;/aws/lambda/function_name&#39;<br>                  log_lambda_name = group[&#39;logGroupName&#39;].split(&#39;/&#39;)[3]<br>                  if(log_lambda_name not in existing_functions):<br>                      print(&#39;deleting log group: %s&#39; %{group[&#39;logGroupName&#39;]})<br>                      cw_client.delete_log_group(logGroupName = group[&#39;logGroupName&#39;])<br>                      deleted_logs.append(group[&#39;logGroupName&#39;])<br>              print(&#39;deleted %s lambda log groups&#39;%{len(deleted_logs)})<br>      Description: !Sub &quot;Lambda function to delete redundant lambda log groups in all CDP regions&quot;<br>      Handler: index.lambda_handler<br>      Environment:<br>        Variables:<br>          REGIONS: !Ref Regions<br>      Role: !GetAtt LambdaRole.Arn<br>      Runtime: python3.9<br>      Timeout: 900<br><br>  LambdaFunctionLogGroup:<br>    Type: AWS::Logs::LogGroup<br>    Properties:<br>      LogGroupName: !Sub &quot;/aws/lambda/${LambdaFunction}&quot;<br>      RetentionInDays: 7<br>  #-------------------------------------------------------------------------------<br>  # EVENTS RESOURCES<br>  #-------------------------------------------------------------------------------<br>  LambdaScheduledRule:<br>    Type: AWS::Events::Rule<br>    Properties:<br>      Description: &quot;ScheduledRule&quot;<br>      # every sunday 3:00 am, every month (UTC time)<br>      # min hour day month day-of-week year<br>      ScheduleExpression: !Sub &#39;cron(00 3 ? 1-12 1 *)&#39;<br>      State: &quot;ENABLED&quot;<br>      Targets:<br>        - Arn:<br>            Fn::GetAtt:<br>              - &quot;LambdaFunction&quot;<br>              - &quot;Arn&quot;<br>          Id: &quot;LambdaFunction&quot;<br>          RetryPolicy:<br>            MaximumEventAgeInSeconds: 120<br>            MaximumRetryAttempts: 2<br><br>  PermissionForEventsToInvokeLambda:<br>    Type: AWS::Lambda::Permission<br>    Properties:<br>      FunctionName: !Ref &quot;LambdaFunction&quot;<br>      Action: &quot;lambda:InvokeFunction&quot;<br>      Principal: &quot;events.amazonaws.com&quot;<br>      SourceArn:<br>        Fn::GetAtt:<br>          - LambdaScheduledRule<br>          - Arn</pre><p><strong>AWS cli command to create the resources:</strong></p><pre>aws clouddformtaion create-stack\<br> --stack-name weeklycleanup-redundantLambdaCWLogs\<br> --template-body file://LambdaLogCleanup.yaml\<br> --capabilities CAPABILITY_IAM\<br> --region us-east-1</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F4tl94K9hvI0%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D4tl94K9hvI0&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F4tl94K9hvI0%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d38b48e03e08b249a4ebf225efc75001/href">https://medium.com/media/d38b48e03e08b249a4ebf225efc75001/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e5a149ca6117" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>