<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Ssm on The Network Stack</title>
    <link>https://thenetworkstack.com/tags/ssm/</link>
    <description>Recent content in Ssm on The Network Stack</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Mon, 16 Mar 2020 00:10:04 +0000</lastBuildDate>
    <atom:link href="https://thenetworkstack.com/tags/ssm/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>AWS Session Manager - Zero Trust Access</title>
      <link>https://thenetworkstack.com/aws-session-manager-zero-trust-access/</link>
      <pubDate>Mon, 16 Mar 2020 00:10:04 +0000</pubDate>
      <guid>https://thenetworkstack.com/aws-session-manager-zero-trust-access/</guid>
      <description>&lt;p&gt;When working with AWS, there are plenty of features that make your life easier, but it&amp;rsquo;s rare to find a feature that feels like magic&amp;hellip;&lt;/p&gt;
&lt;p&gt;Before we dive into the magic, what&amp;rsquo;s the problem we&amp;rsquo;re trying to solve? When working with servers anywhere, usually there needs to be some sort of remote access to SSH or RDP and install/troubleshoot things as needed. Worst case, your server has a public IP with port 22 (SSH) open to the world, which means theoretically anyone, anywhere in the world could break in. As you get more advanced, you set up VPN tunnels or bastion hosts to secure your remote access. This can increase in complexity very quickly with multiple VPCs across multiple regions, multiplied by each AWS account. And with the rise in &amp;ldquo;Zero Trust&amp;rdquo; networking, how do you securely provide access without blindly trusting network CIDRs?&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>When working with AWS, there are plenty of features that make your life easier, but it&rsquo;s rare to find a feature that feels like magic&hellip;</p>
<p>Before we dive into the magic, what&rsquo;s the problem we&rsquo;re trying to solve? When working with servers anywhere, usually there needs to be some sort of remote access to SSH or RDP and install/troubleshoot things as needed. Worst case, your server has a public IP with port 22 (SSH) open to the world, which means theoretically anyone, anywhere in the world could break in. As you get more advanced, you set up VPN tunnels or bastion hosts to secure your remote access. This can increase in complexity very quickly with multiple VPCs across multiple regions, multiplied by each AWS account. And with the rise in &ldquo;Zero Trust&rdquo; networking, how do you securely provide access without blindly trusting network CIDRs?</p>
<p>Enter AWS Systems Manager Session Manager. While not a very magical name, it does provide a magical experience. Session Manager gives you remote access to your servers without opening any ports in security groups, building any VPN tunnels, or using bastion hosts. All access is based on IAM permissions, and sessions are proxied by AWS Systems Manager. You can either use the AWS Console for web access, or the AWS CLI for a native &ldquo;terminal&rdquo; experience.</p>
<h3 id="requirements">Requirements</h3>
<p>The <a href="https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-getting-started.html">official documentation</a> has the most detail, but the short answer is:</p>
<ol>
<li>Verify SSM agent is running on your EC2 instances with an Instance Role that gives access to Systems Manager</li>
<li>If you&rsquo;re using a local terminal:
<ul>
<li>Verify <a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html">AWS CLI 1.16.12</a> or later is installed</li>
<li>Install the <a href="https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html">Session Manager Plugin for AWS CLI</a></li>
</ul>
</li>
</ol>
<p>Once the requirements are met, you can view your managed instances by going to <em>Systems Manager</em> -&gt; <em>Managed Instances</em> in the AWS Console. You can easily start a web-based session by clicking an Instance and then clicking <em>Actions</em> -&gt; <em>Start Session</em>. The same thing can be accomplished from the EC2 console by clicking your instance and then <em>Connect</em> -&gt; <em>Connection Method: Session Manager</em>. While this feels similar to other tools (like a VMware console), the real magic is using the terminal on your local computer.</p>
<p>You can start a remote &ldquo;SSH&rdquo; style session with your instances that have zero inbound access with this command:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">aws ssm start-session --target instance_id
</span></span></code></pre></div><p>At this point, you have SSH access to your instance, and can go through all your normal installation or troubleshooting steps. The first time you do this feels like magic, as you realize all the time you no longer need to spend with VPN tunnels, bastion hosts, and opening up security groups for remote access.</p>
<p><strong>But wait, there&rsquo;s more!</strong> Not only can you initiate remote access sessions for SSH or PowerShell, but SSM can also do <a href="https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-sessions-start.html">port-forwarding</a> from your local computer to the remote server! Want to see what a particular web server is showing on port 80? Just forward a random local port (e.g. 9999) to your web server port 80, and then navigate to <code>http://localhost:9999</code> in your web browser. How about Windows RDP access? Session Manager only supports SSH or PowerShell for remote sessions, but with the port-forwarding feature, we can also get remote RDP access:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">aws ssm start-session --target instance_id --document-name AWS-StartPortForwardingSession --parameters <span class="nv">portNumber</span><span class="o">=</span><span class="s2">&#34;3389&#34;</span>,localPortNumber<span class="o">=</span><span class="s2">&#34;33389&#34;</span>
</span></span></code></pre></div><p>Since the &ldquo;localPortNumber&rdquo; is set to 33389 above, you would just RDP to 127.0.0.1:33389 to port-forward to your remote instance.</p>
<p>OK, at this point AWS has provided zero trust remote access, but the AWS CLI SSM commands (especially port forwarding) aren&rsquo;t the most intuitive to remember. Also, getting the EC2 instance ID requires going back and forth between your terminal and the AWS web console. I decided to write a Python script that abstracts some of the complexity while making it easy to start remote sessions and port forwarding.</p>
<h3 id="aws-remote">AWS Remote</h3>
<p>I&rsquo;ve called this script <strong>AWS Remote</strong>: <a href="https://github.com/dkuchenski/aws-remote">https://github.com/dkuchenski/aws-remote</a></p>
<p>Some of the main features of AWS Remote:</p>
<ul>
<li>Listing all EC2 instance IDs, important attributes, and SSM management status</li>
<li>Start remote sessions via instance ID or instance name</li>
<li>Friendly port-forward syntax via instance ID or instance name</li>
</ul>
<p>So instead of remembering the AWS CLI syntax and finding instance IDs, you can do this:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">aws-remote session instance2-example.domain.com
</span></span></code></pre></div><p>or</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">aws-remote port-forward <span class="m">8080</span> <span class="m">80</span> instance2-example.domain.com
</span></span></code></pre></div><p><em>Note: The instance &ldquo;name&rdquo; is referencing the Name tag of the instance</em></p>
<p>This script uses a combination of the Python Boto3 SDK and the AWS CLI to get information from AWS and start sessions based on your parameters. This is also the first time I&rsquo;ve used <a href="https://click.palletsprojects.com/en/7.x/">Click</a> to build the command line interface. Click is great for building easy-to-use CLIs, and therefore you can use <code>--help</code> at any point to see what your available options are. I&rsquo;ve also mimicked the AWS CLI <code>--profile</code> and <code>--region</code> options for a familiar setup.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ aws-remote --help
</span></span><span class="line"><span class="cl">Usage: aws-remote [OPTIONS] COMMAND [ARGS]...
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  AWS Remote is a simple, command line tool to view and interact with AWS
</span></span><span class="line"><span class="cl">  instances via SSM. Requires the AWS CLI and Session Manager Plugin to be
</span></span><span class="line"><span class="cl">  installed locally.
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">Options:
</span></span><span class="line"><span class="cl">  --profile TEXT  Specify AWS profile
</span></span><span class="line"><span class="cl">  --region TEXT   Specify AWS region
</span></span><span class="line"><span class="cl">  --help          Show this message and exit.
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">Commands:
</span></span><span class="line"><span class="cl">  list          List EC2 instances and SSM management status
</span></span><span class="line"><span class="cl">  port-forward  Start SSM port forward to instance id/name
</span></span><span class="line"><span class="cl">  session       Start SSM session with instance id/name
</span></span></code></pre></div><p>I&rsquo;m still figuring out the best way to distribute scripts like this, but for now there are a couple of installation steps that can be found here: <a href="https://github.com/dkuchenski/aws-remote/blob/master/README.md">https://github.com/dkuchenski/aws-remote/blob/master/README.md</a></p>
<p>Hope AWS Remote is a useful tool, and if you have any feedback, please let me know!</p>
<h3 id="references">References</h3>
<ul>
<li><a href="https://aws.amazon.com/blogs/aws/new-session-manager/">AWS News Blog: New – AWS Systems Manager Session Manager</a></li>
<li><a href="https://dbader.org/blog/mastering-click-advanced-python-command-line-apps">Mastering Click: advanced Python command-line apps</a> (dbader.org)</li>
<li><a href="https://stackoverflow.com/questions/23626972/how-do-i-pass-variables-to-other-methods-using-pythons-click-command-line-inte">How do I pass variables to other methods using Python&rsquo;s Click?</a> (Stack Overflow)</li>
</ul>
]]></content:encoded>
    </item>
  </channel>
</rss>
