<?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>DEV Community: Bodo Schönfeld</title>
    <description>The latest articles on DEV Community by Bodo Schönfeld (@niftycode).</description>
    <link>https://dev.to/niftycode</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%2F337568%2F7db356e4-ce19-404b-8e78-794777085b17.jpeg</url>
      <title>DEV Community: Bodo Schönfeld</title>
      <link>https://dev.to/niftycode</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/niftycode"/>
    <language>en</language>
    <item>
      <title>Start your AWS EC2 instance using Python</title>
      <dc:creator>Bodo Schönfeld</dc:creator>
      <pubDate>Thu, 20 Feb 2020 08:57:17 +0000</pubDate>
      <link>https://dev.to/niftycode/start-your-aws-ec2-instance-using-python-3n25</link>
      <guid>https://dev.to/niftycode/start-your-aws-ec2-instance-using-python-3n25</guid>
      <description>&lt;p&gt;Amazon EC2 (Elastic Compute Cloud) is a cloud service that allows you to bring up virtual machines (instances) online. To do so you can use the AWS Dashboard. But there is another way: Python (or another programming language supported by &lt;a href="https://aws.amazon.com/"&gt;Amazon Web Services&lt;/a&gt;). You can find an overview on this &lt;a href="https://aws.amazon.com/tools/"&gt;website&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you never used Amazon Web Services with Python before, you have to install two additional modules first:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install boto3 botocore (Windows)
pip3 install boto3 botocore (macOS/Linux)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the next step save your &lt;strong&gt;AWS Credentials&lt;/strong&gt; in a folder named ".aws" located in your home (Linux)/Users (macOS)/%UserProfile% (Windows) folder. You can do so manually as explained in this &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html"&gt;AWS documentation&lt;/a&gt; or you make it the easy way by using the &lt;strong&gt;AWS Command Line Interface&lt;/strong&gt; (AWS CLI). For more information see the AWS documentation &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html"&gt;"Installing the AWS CLI version 2"&lt;/a&gt;. The installer files (for Linux, Windows and macOS) are also provided on that website.&lt;/p&gt;

&lt;p&gt;After you've installed the AWS CLI open the PowerShell (or the Command Prompt) on Windows. On UNIX-like systems open a Shell. Then enter the following command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws configure
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You will be asked for the &lt;em&gt;AWS Access Key ID&lt;/em&gt; and the &lt;em&gt;AWS Secret Access Key&lt;/em&gt;. As &lt;em&gt;default region name&lt;/em&gt; enter your Availability Zone (AZ). And finally the default output format should be "json". As you can imagine, your credentials will be saved in the ".aws" folder.&lt;/p&gt;

&lt;p&gt;Now you can use a Python script to start and stop an EC2 instance. As starting point you can use &lt;a href="https://github.com/niftycode/aws-ec2-start-stop"&gt;the script from my Github repository&lt;/a&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/niftycode/aws-ec2-start-stop.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This script needs a txt file named "instance_id.txt". Create such a file and save it in your home directory:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/home/[username]/instance_id.txt (Linux)
/Users/[username]/instance_id.txt (macOS/Windows)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As the name indicates, this text file should contain the ID of your EC2 instance (Check your EC2 dashboard for the available IDs.)&lt;/p&gt;

&lt;p&gt;You're done! Now start your EC2 instance with the following command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 start_stop_ec2.py -u
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And to stop the instance run:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 start_stop_ec2.py -d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After an instance is up and running, the public IP address will be shown. Use this address to establish a remote connection via a Shell or Microsoft Remote Desktop.&lt;/p&gt;

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