<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Gen Benedict Casio</title>
    <description>The latest articles on Forem by Gen Benedict Casio (@c1lc1l).</description>
    <link>https://forem.com/c1lc1l</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2533115%2F96050895-c450-40ff-8798-4c49da6389e3.JPG</url>
      <title>Forem: Gen Benedict Casio</title>
      <link>https://forem.com/c1lc1l</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/c1lc1l"/>
    <language>en</language>
    <item>
      <title>Deploy your Discord Bot using Amazon EC2</title>
      <dc:creator>Gen Benedict Casio</dc:creator>
      <pubDate>Wed, 18 Dec 2024 16:24:45 +0000</pubDate>
      <link>https://forem.com/c1lc1l/deploy-your-discord-bot-using-amazon-ec2-2mdm</link>
      <guid>https://forem.com/c1lc1l/deploy-your-discord-bot-using-amazon-ec2-2mdm</guid>
      <description>&lt;p&gt;&lt;strong&gt;Ready to host your first application on the cloud?&lt;/strong&gt; ☁️ In this article, we’ll explore how to deploy your Discord bot using &lt;strong&gt;Amazon EC2&lt;/strong&gt; 💻. While this guide offers an overview, my &lt;a href="https://github.com/c1lc1l/word-bot" rel="noopener noreferrer"&gt;Word Bot Github Repo&lt;/a&gt; provides a step-by-step walkthrough to get your bot up and running 🚀.  &lt;/p&gt;




&lt;h2&gt;
  
  
  Story Time 📖
&lt;/h2&gt;

&lt;p&gt;I was debating on what application to code and which service to use for my mentorship assignment when I decided to sift through my &lt;code&gt;pythonpythonpython&lt;/code&gt; folder. That’s when I rediscovered my old Discord bot from 2021! 🎉  &lt;/p&gt;

&lt;p&gt;Excited, I booted it up... but it didn’t work 😅. Discord had updated their API, and my bot used deprecated code 🔧. It was the perfect reminder of how quickly tech evolves 🌱. So, I revamped it, and what better way to host it than on the cloud with Amazon EC2? 🌥️  &lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs7jogjpz2of3wxa21oyh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs7jogjpz2of3wxa21oyh.png" alt="python banner" width="800" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Python? 🐍
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Versatility&lt;/strong&gt;: Python offers an extensive range of libraries, making it ideal for various development use cases 🔄.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ease of Use&lt;/strong&gt;: Its simple and readable syntax makes coding efficient and beginner-friendly 👨‍💻👩‍💻.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rich Ecosystem&lt;/strong&gt;: With libraries like &lt;a href="https://discordpy.readthedocs.io/en/stable/" rel="noopener noreferrer"&gt;discord.py&lt;/a&gt;, it’s easy to interact with APIs 🌐.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security&lt;/strong&gt;: Modules like &lt;a href="https://pypi.org/project/python-dotenv/" rel="noopener noreferrer"&gt;dotenv&lt;/a&gt; help manage sensitive environment variables securely 🔒.
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4czwvv0y0lwd1pc3jzuo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4czwvv0y0lwd1pc3jzuo.png" alt="ec2 banner" width="800" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why EC2? 🌍
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: Amazon EC2 scales easily to meet the demands of different workloads, from small projects to enterprise-level applications 📈.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reliability&lt;/strong&gt;: Running your bot 24/7 is effortless with AWS's robust infrastructure ⚡.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility&lt;/strong&gt;: EC2 supports a wide variety of operating systems and configurations 🖥️.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ease of Configuration&lt;/strong&gt;: Setting up an EC2 instance is straightforward, even for beginners 🛠️.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Prepare Your Bot&lt;/strong&gt; 🤖
&lt;/h3&gt;

&lt;p&gt;If you already have a bot, make sure it’s updated with the latest &lt;a href="https://discordpy.readthedocs.io/en/stable/" rel="noopener noreferrer"&gt;discord.py&lt;/a&gt; version 🔄. If you don’t, you can use my &lt;a href="https://github.com/c1lc1l/word-bot" rel="noopener noreferrer"&gt;Word Bot&lt;/a&gt; as a starting point! 🚀  &lt;/p&gt;

&lt;p&gt;One of the simplest and most engaging features of my Word Bot is responding to a user with a friendly "Hello!" 👋 when they send a message. Here's a snippet from the repository:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Bot setup
&lt;/span&gt;&lt;span class="n"&gt;bot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;commands&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Bot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;command_prefix&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;$&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;intents&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;intents&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Simple command that responds with a random hello message
&lt;/span&gt;&lt;span class="nd"&gt;@bot.command&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hello_command&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;typing&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="n"&gt;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;choice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hello_messages&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;display_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function listens for messages 📩, checks if the content is "$hello," and responds with a friendly message in return 🗨️.&lt;/p&gt;




&lt;h2&gt;
  
  
  Deploying Your Bot
&lt;/h2&gt;

&lt;p&gt;Here’s a quick overview of the deployment process. Detailed instructions are in the repo!&lt;/p&gt;

&lt;p&gt;1) &lt;strong&gt;Launch an EC2 Instance&lt;/strong&gt; 🚀:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sign in to AWS and go to the EC2 Dashboard.
&lt;/li&gt;
&lt;li&gt;Click "Launch Instance" and select &lt;strong&gt;Amazon Linux 2023 AMI&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;Choose an instance type (e.g., &lt;code&gt;t2.micro&lt;/code&gt; for the free tier).
&lt;/li&gt;
&lt;li&gt;Configure your instance settings, ensuring SSH access is enabled in the security group.
&lt;/li&gt;
&lt;li&gt;Download the &lt;code&gt;.pem&lt;/code&gt; key file to SSH into your instance.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;2) &lt;strong&gt;Connect to Your Instance&lt;/strong&gt; 🔌: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open your terminal or Git Bash and navigate to the folder where your &lt;code&gt;.pem&lt;/code&gt; key is located.
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;SSH into your EC2 instance:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; ssh &lt;span class="nt"&gt;-i&lt;/span&gt; your-key-name.pem ec2-user@your-ec2-public-ip
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;3) &lt;strong&gt;Set Up Dependencies&lt;/strong&gt; ⚙️:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Update the package manager and install Python 3 and the necessary packages(Discord and DotEnv):&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; &lt;span class="nb"&gt;sudo &lt;/span&gt;yum update &lt;span class="nt"&gt;-y&lt;/span&gt;
 &lt;span class="nb"&gt;sudo &lt;/span&gt;yum &lt;span class="nb"&gt;install &lt;/span&gt;python3 python3-pip &lt;span class="nt"&gt;-y&lt;/span&gt;
 pip3 &lt;span class="nb"&gt;install &lt;/span&gt;discord.py python-dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;4) &lt;strong&gt;Install Git in the EC2 Instance&lt;/strong&gt; 🛠️:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Ensure that Git is installed:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; &lt;span class="nb"&gt;sudo &lt;/span&gt;yum &lt;span class="nb"&gt;install &lt;/span&gt;git &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;5) &lt;strong&gt;Clone the Repository&lt;/strong&gt; 📂:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Use the clone command and navigate into the project directory:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; git clone https://github.com/yourusername/word-bot.git
 &lt;span class="nb"&gt;cd &lt;/span&gt;word-bot
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;6) &lt;strong&gt;Set Up Environment Variables&lt;/strong&gt; 🗝️:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Create a &lt;code&gt;.env&lt;/code&gt; file in the root directory and add your bot’s token:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"DISCORD_BOT_TOKEN=your-discord-token"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; .env
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;7) &lt;strong&gt;Run the Bot&lt;/strong&gt; ▶️:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Start the bot on your EC2 instance:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; python3 discord-bot.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;8) &lt;strong&gt;Keep the Bot Running in the Background&lt;/strong&gt; 🕒:&lt;br&gt;&lt;br&gt;
   To keep the bot running after you close the terminal, use &lt;code&gt;screen&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Install &lt;code&gt;screen&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt; &lt;span class="nb"&gt;sudo &lt;/span&gt;yum &lt;span class="nb"&gt;install &lt;/span&gt;screen &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Start a new screen session:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt; screen &lt;span class="nt"&gt;-S&lt;/span&gt; discord-bot
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Run the bot inside the screen session:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt; python3 discord-bot.py
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Detach from the screen session by pressing &lt;code&gt;Ctrl + A&lt;/code&gt;, then &lt;code&gt;D&lt;/code&gt;.  &lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Reattach to the session later:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt; screen &lt;span class="nt"&gt;-r&lt;/span&gt; discord-bot
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  Typical Interaction with the Bot 🤖💬
&lt;/h2&gt;

&lt;p&gt;Once your bot is up and running, here’s what a typical interaction in your Discord server might look like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fipxgsgxadg3smrjqjlxj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fipxgsgxadg3smrjqjlxj.png" alt="sample interaction with the bot" width="710" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yep, my bot's name is Wordie! 😄 But hey, I'm always open to fun suggestions!&lt;/p&gt;




&lt;h2&gt;
  
  
  You made it to the end! 🎉🎉
&lt;/h2&gt;

&lt;p&gt;Deploying your Discord bot on Amazon EC2 is a great way to bring your projects to life on the cloud ☁️. With the simplicity of &lt;strong&gt;Python&lt;/strong&gt; 🐍 and the flexibility of &lt;strong&gt;EC2&lt;/strong&gt; 💻, you can easily set up and scale your bot, ensuring it’s running 24/7 ⏰. By following the steps outlined in this guide, you’ve learned how to get your bot up and running with minimal hassle.&lt;/p&gt;

&lt;p&gt;Remember, the beauty of cloud computing 🌐 is that your bot can grow with you! Whether you're adding new features, improving performance, or just experimenting 🎮, EC2 provides the resources to support your journey.&lt;/p&gt;

&lt;p&gt;So, go ahead—give your bot some personality and functionality, and watch it thrive in the cloud! 🚀 If you encounter any bumps along the way, don't forget to check the &lt;a href="https://github.com/c1lc1l/word-bot?tab=readme-ov-file#troubleshooting" rel="noopener noreferrer"&gt;troubleshooting section&lt;/a&gt; or refer to the &lt;a href="https://discordpy.readthedocs.io/en/stable/" rel="noopener noreferrer"&gt;Discord API documentation&lt;/a&gt; 📚. &lt;/p&gt;




&lt;p&gt;Happy coding! 👨‍💻👩‍💻&lt;/p&gt;

</description>
      <category>python</category>
      <category>discord</category>
      <category>aws</category>
      <category>ec2</category>
    </item>
  </channel>
</rss>
