<?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 Jean-Paul Muzama Mulopo on Medium]]></title>
        <description><![CDATA[Stories by Jean-Paul Muzama Mulopo on Medium]]></description>
        <link>https://medium.com/@jeanpaulmuzama88?source=rss-38cb030cc014------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*F6oi2zIwsOTZmMKiZwxi-w.jpeg</url>
            <title>Stories by Jean-Paul Muzama Mulopo on Medium</title>
            <link>https://medium.com/@jeanpaulmuzama88?source=rss-38cb030cc014------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 11:47:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jeanpaulmuzama88/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[Automating AWS EC2 Instance Management with Python: A Comprehensive Guide]]></title>
            <link>https://medium.com/@jeanpaulmuzama88/automating-aws-ec2-instance-management-with-python-a-comprehensive-guide-151a45a5371d?source=rss-38cb030cc014------2</link>
            <guid isPermaLink="false">https://medium.com/p/151a45a5371d</guid>
            <dc:creator><![CDATA[Jean-Paul Muzama Mulopo]]></dc:creator>
            <pubDate>Sat, 11 Jan 2025 17:19:25 GMT</pubDate>
            <atom:updated>2025-01-12T17:14:57.309Z</atom:updated>
            <content:encoded><![CDATA[<p>Managing cloud infrastructure manually can be time-consuming and error-prone. With AWS EC2 and Python, you can automate tasks like creating, stopping, and terminating instances, saving time and ensuring consistency. In this article, we’ll walk you through a Python script that automates the entire lifecycle of an EC2 instance, from creating a key pair and security group to launching, stopping, and optionally terminating the instance. This guide is designed to be beginner-friendly while providing enough depth for experienced users.</p><h3>Why Automate EC2 Management?</h3><p>Automating EC2 management offers several advantages:</p><ul><li><strong>Efficiency</strong>: Automate repetitive tasks like launching and stopping instances.</li><li><strong>Cost Savings</strong>: Ensure instances are only running when needed.</li><li><strong>Consistency</strong>: Reduce human error by scripting your workflows.</li><li><strong>Scalability</strong>: Easily manage multiple instances programmatically.</li></ul><p>By the end of this guide, you’ll have a Python script that:</p><ol><li>Creates a key pair for SSH access.</li><li>Creates a security group with necessary rules.</li><li>Launches an EC2 instance with a custom name.</li><li>Waits for the instance to be in a “running” state.</li><li>Stops the instance after 5 minutes.</li><li>Optionally terminates the instance.</li></ol><h3>Prerequisites</h3><p>Before diving into the script, ensure you have the following:</p><ol><li><strong>An AWS Account</strong>: <em>If you don’t have one, sign up at aws.amazon.com.</em></li><li><strong>IAM User with EC2 Full Access</strong>:</li></ol><ul><li><em>Create an IAM user with the </em><strong><em>AmazonEC2FullAccess</em></strong><em> policy attached. This policy grants the user full access to EC2 services, including creating, stopping, and terminating instances, as well as managing key pairs and security groups.</em></li><li><em>Save the </em><strong><em>Access Key ID</em></strong><em> and </em><strong><em>Secret Access Key</em></strong><em> for authentication.</em></li></ul><p><strong>3. AWS CLI Configuration</strong>:</p><ul><li><em>Install the AWS CLI and configure it with your credentials:</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/615/1*5pHENpiQgWM_jSMlJwQ__A.png" /></figure><ul><li>Provide your Access Key ID, Secret Access Key, default region (e.g., us-east-1), and output format (e.g., json).</li></ul><p>4. <strong>Python Environment</strong>:</p><ul><li>Install Python (version 3.6 or higher).</li><li>Install the boto3 library, the AWS SDK for Python:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/1*K066BSz_Ww4OpaNuB3g4Lg.png" /></figure><h3>Step-by-Step Guide</h3><h3>Step 1: Import Required Libraries</h3><p>Start by importing the necessary Python libraries:</p><ul><li>boto3: To interact with AWS services.</li><li>time: To handle delays.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/692/1*OYU10aA1NIysq3bcNCYn9A.png" /></figure><h3>Step 2: Initialize the EC2 Client</h3><p>Use boto3 to create an EC2 client. This client will allow you to interact with the EC2 service.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/705/1*xJtP7k9jGC2xPmAdFINBHA.png" /></figure><h3>Step 3: Create a Key Pair</h3><p>A key pair is required for SSH access to your EC2 instance. The script creates a key pair and saves the private key to a file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*GXer0Zg27o37ZWUgzNPT_Q.png" /></figure><h3>Step 4: Create a Security Group</h3><p>A security group acts as a virtual firewall for your instance, controlling inbound and outbound traffic. The script creates a security group and adds a rule to allow all inbound traffic (for demonstration purposes). This is not recommended for production environments, where you should restrict access to specific IP addresses and ports.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*fXZZy7tg8lOTWqepZBcJoQ.png" /></figure><h3>Step 5: Launch the EC2 Instance</h3><p>Define the parameters for launching the EC2 instance, including the AMI, instance type, key pair, security group, and tags. The script then launches the instance and retrieves its ID.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*Pruf01IQYlHfoHrwTpEDTA.png" /></figure><h3>Step 6: Wait for the Instance to Be Running</h3><p>Use an EC2 waiter to ensure the instance is in a “running” state before proceeding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/595/1*K14eH2irR54a3IhArpvoLQ.png" /></figure><h3>Step 7: Wait for 5 Minutes</h3><p>Pause the script for 5 minutes to simulate usage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/597/1*jwMdCC6EvmYViB15y27caQ.png" /></figure><h3>Step 8: Stop the Instance</h3><p>Stop the instance and wait for it to be in a “stopped” state.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*P0kCgC47bAHs64xO87r8MQ.png" /></figure><h3>Step 9: Optional — Terminate the Instance</h3><p>If you want to terminate the instance instead of stopping it, uncomment the following lines:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*itphrCB-T3CJQz6QgthppA.png" /></figure><h3>Full Script</h3><p>Here’s the complete script for reference:</p><p><a href="https://github.com/JPMuzama/aws-exercise/blob/f884622876b67e2fe46969b8c15aeefc5b6be3f9/first-step-ec2-python.py">https://github.com/JPMuzama/aws-exercise/blob/f884622876b67e2fe46969b8c15aeefc5b6be3f9/first-step-ec2-python.py</a></p><h3>Why No AWS Console Screenshots?</h3><p>This guide avoids including AWS Management Console screenshots because the interface changes frequently. Instead, we’ve focused on providing clear, step-by-step instructions that remain relevant regardless of UI updates.</p><h3>Alternative Script: Using Existing Key Pair and Security Group</h3><p>If you already have a key pair and security group, or if you prefer not to create them programmatically, you can use the following simplified script. This script assumes you have an existing key pair and security group, and it uses their names or IDs directly.</p><p><a href="https://github.com/JPMuzama/aws-exercise/blob/f884622876b67e2fe46969b8c15aeefc5b6be3f9/second-step-ec2-pythin.py">https://github.com/JPMuzama/aws-exercise/blob/f884622876b67e2fe46969b8c15aeefc5b6be3f9/second-step-ec2-pythin.py</a></p><h3>Conclusion</h3><p>Automating AWS EC2 instance management with Python is a powerful way to streamline your cloud operations. By following this guide, you’ve learned how to:</p><ul><li>Create a key pair and security group.</li><li>Launch an EC2 instance with a custom name.</li><li>Stop or terminate the instance after a set period.</li></ul><p>This script is just the beginning. You can expand it to include additional features like monitoring, scaling, or integrating with other AWS services. Happy coding, and enjoy your journey into AWS automation!</p><p>If you found this guide helpful, feel free to share it with others. For questions or feedback, I’ll be pleased to see your comment below! your dear @Jean-paul Muzama Mulopo</p><h3>Acknowledgments</h3><p>I would like to extend my heartfelt gratitude to my instructor, <a href="https://medium.com/u/9047f985461a">Mbula Mboma Jean Gilbert</a>, for their invaluable guidance and expertise in teaching me the concepts of AWS EC2 management and Python automation. Their clear explanations and hands-on approach made this learning journey both enjoyable and insightful. Thank you for inspiring me to explore and share this knowledge with others!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=151a45a5371d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>