<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-us"><generator uri="https://gohugo.io/" version="0.156.0">Hugo</generator><title type="html">Cloud on Marcin Jasion - Pragmatic DevOps</title><link href="https://b58f7780.mjasion.pages.dev/posts/cloud/" rel="alternate" type="text/html" title="html"/><link href="https://b58f7780.mjasion.pages.dev/posts/cloud/index.xml" rel="alternate" type="application/rss+xml" title="rss"/><updated>2022-06-29T00:00:00+02:00</updated><id>https://b58f7780.mjasion.pages.dev/posts/cloud/</id><entry><title type="html">How to setup AWS Site-to-Site VPN with Unifi UDM 🔒</title><link href="https://b58f7780.mjasion.pages.dev/posts/cloud/how-to-setup-aws-site-to-site-vpn-with-unifi-udm/?utm_source=atom_feed" rel="alternate" type="text/html"/><link href="https://b58f7780.mjasion.pages.dev/posts/kubernetes/how-to-debug-istio-upstream-reset/?utm_source=atom_feed" rel="related" type="text/html" title="How to debug Istio Upstream Reset 502 UPE (old 503 UC)"/><id>https://b58f7780.mjasion.pages.dev/posts/cloud/how-to-setup-aws-site-to-site-vpn-with-unifi-udm/</id><author><name>Marcin Jasion</name></author><published>2022-06-29T00:00:00+02:00</published><updated>2022-06-29T00:00:00+02:00</updated><content type="html"><![CDATA[<blockquote>Site-to-Site VPN allows connecting local network with AWS VPC. This blog is a step-by-step instruction to establish connection</blockquote><p>By default resources, you launch on the cloud (EC2, RDS, and others) cannot communicate with your local networks like home or office. To allow this you can create a Site-to-Site VPN. This VPN connection will be established between your router and AWS VPC.</p>
<p>Creating VPN between networks is <a href="https://docs.aws.amazon.com/vpn/latest/s2svpn/SetUpVPNConnections.html" target="_blank" rel="noopener">well documented</a>. However, you can have issues configuring your home router. At home, I have a Unifi Dream Machine router, which is designed for small networks, but has features that match advanced routers for offices. One of them is a Site-to-Site VPN using the IPSec protocol.</p>
<h2 id="setup-vpn-on-aws-">Setup VPN on AWS ☁️</h2>
<p>The first step is to create a VPN connection on AWS. For this blog I will use&hellip; the default VPC 🙂. To configure it AWS requires to define 3 components: Customer Gateway, Virtual Private Gateway and VPN connection.</p>
<p>The Customer Gateway is basically just an entity that holds the information about your home router - public IP.
The Virtual Private Gateway is the virtual entity on the VPC side, that allows configuring routing to that gateway. That VPG is attached to the VPN. So the last entity is the VPN connection which brings it all together and establishes the VPN tunnels between your home or office and VPC.</p>
<h3 id="create-base-components-">Create base components 🏗️</h3>
<p>To start your VPN connection start by defining Customer Gateway.
Go to the <strong>VPC</strong> tab, find the <strong>Customer Gateway</strong> panel and click the button to <strong>Create</strong>. The required field is IP address. Write here your public IP address where your router is running. Also, it is good to name this gateway. I named mine <code>home</code>.</p>
<blockquote>
<p>To quickly check you public IP you can open  <a href="https://ifconfig.co" target="_blank" rel="noopener">https://ifconfig.co</a></p>
</blockquote>
<p>The next step is to create a Virtual Private Gateway. Go to the <strong>Virtual Private Gateway</strong> panel and click <strong>Create</strong>. It just asks for a name. Let&rsquo;s name it also <code>home</code>. The VPG state will be detached. I will come back to it later.
<img src="/posts/cloud/how-to-setup-aws-site-to-site-vpn-with-unifi-udm/virtual_gateway_notassigned.png" alt=""></p>
<h3 id="create-a-vpn-connection-">Create a VPN connection 🔒</h3>
<p>This is the time to start defining VPN. Open the <strong>Site-to-Site VPN connection</strong> panel and click <strong>Create VPN Connection</strong>. The form will have 3 panels: details and tunnel options.</p>
<p><strong>Details</strong> start from defining the gateway on the VPC side. Choose <strong>Virtual private gateway</strong> and in the form select your VPG.</p>
<p>Next select <strong>Customer gateway</strong>. Here you define with which router the VPN will be established.</p>
<p>There is the last configuration to set: routing. It is a section where you can define which local networks the VPN will be used for. On the screenshot, I marked this as a point <strong>1</strong>.</p>
<p>AWS allows for two options: dynamic routing based on BGP, and statically defined. In my case, I am using static.
In the prefixes, I am putting my local network prefixes(like <code>192.168.1.0/24</code>). You are allowed to put multiple networks here.</p>
<p><img src="/posts/cloud/how-to-setup-aws-site-to-site-vpn-with-unifi-udm/create_vpn.png" alt=""></p>
<h3 id="configure-tunnel-options-">Configure Tunnel Options ⚙️</h3>
<p>Tunnel options allow defining the IPSec parameters. AWS creates 2 tunnels, to which you can connect and each of them you can configure differently. Or the same 🙂.</p>
<p>What I am always choosing is:</p>
<ul>
<li><strong>Encryption algorithms</strong>: AES-256 (for both phases)</li>
<li><strong>Integrity algorithms</strong>: SHA2-256, SHA2-384, SHA2-512</li>
<li><strong>DH groups</strong>: all above 14</li>
<li><strong>IKE Version</strong>: ikev2</li>
</ul>
<p>Those parameters will be crucial for setting up our Unifi router.</p>
<p>When you finish these changes you can create the VPN and wait a few seconds. The VPN connection should be ready.
<img src="/posts/cloud/how-to-setup-aws-site-to-site-vpn-with-unifi-udm/vpn_ready.png" alt=""></p>
<h3 id="configure-vpc-routing-">Configure VPC Routing 🛣️</h3>
<p>Previously I mentioned that the Virtual Private Gateway state is not attached to any VPC. We can reassign the VPN connection between VPCs by changing attachments.</p>
<p>To attach go back to <strong>Virtual Private Gateway</strong> and select your VPG and in <strong>Actions</strong>, button find <strong>Attach to VPC</strong>. Select your VPC and your VPG should be available to configure routing on VPC.
<img src="/posts/cloud/how-to-setup-aws-site-to-site-vpn-with-unifi-udm/virtual_gateway_assigned.png.png" alt=""></p>
<p>On VPC the last thing to configure is routes. As we have assigned VPG, and networks from our home are known (configured on VPN with static routes), we can configure automated propagation of those rules to VPC route tables. To configure this perform:</p>
<ol>
<li>Go to <strong>Route tables</strong></li>
<li>Select routes assigned to VPC, or those which should have access to your home.</li>
<li>Select <strong>Actions</strong> button, and choose <strong>Edit route propagation</strong>,</li>
<li>Select Virtual private gateway</li>
</ol>
<p>After a few seconds, the new route should be added
<img src="/posts/cloud/how-to-setup-aws-site-to-site-vpn-with-unifi-udm/vpc_routes.png" alt="">
As you can see, the last route is &ldquo;Propagated&rdquo;, and its target is my virtual private gateway.</p>
<h2 id="setup-vpn-on-unifi-">Setup VPN on Unifi 🏠</h2>
<p>Having configured AWS VPC, the last part is to configure our router. In my home, I have Unifi Dream Machine, with the latest software (Network 7.1).</p>
<p>To create a VPN connection:</p>
<ul>
<li>Go to <strong>Settings</strong> &gt; <strong>Teleport &amp; VPN</strong>,</li>
<li>Scroll down to <strong>Site-to-Site VPN</strong> and click  <strong>Create</strong>,
<img src="/posts/cloud/how-to-setup-aws-site-to-site-vpn-with-unifi-udm/udm-ipsec.png" alt=""></li>
</ul>
<p>Start filling out the form. The Pre-Shared Key you could configure in Tunnel Options. If you have skipped this, go to the AWS VPN tab, and click <strong>Download Configuration</strong>. In this file you will find the PSK to fill in point 1 and the <strong>Remote IP Address</strong> (point 4).
In the <strong>Remote Gateway/Subnets</strong>(point 3) put AWS VPC network addressing. In my case, it was <code>172.31.0.0/16</code>.</p>
<p>To align encryption options with <strong>Tunnel Options</strong> on AWS, select <strong>Manual</strong> in Advanced configuration and customize. Configure your parameters according to how you have configured them on AWS. I recommend using <strong>AES-256</strong> and higher DH Groups and use the above image as an example.</p>



    


<div class="alert success">
    <span><i data-feather="check-circle"></i></span>
    <span><strong>The VPN connection should be established ❤️</strong></span>
</div>

<h2 id="last-step---testing-">Last step - testing 🪛</h2>
<p>To check if you have a working VPN connection create an EC2 instance on this VPC.</p>



    


<div class="alert warning">
    <span><i data-feather="alert-triangle"></i></span>
    <span><strong>Ensure your Security group allows for your home network. You can allow access to a single port, protocol, or whole traffic.</strong></span>
</div>

<p>I have created an instance with IP <code>172.31.34.95</code>. This instance has a Security Group rule allowing for <strong>All Traffic</strong> from my home network.
When the VPN works and instance is up, a simple <code>ping</code> can prove that everything is configured:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>$ ping 172.31.34.95 -c <span style="color:#ae81ff">5</span>
</span></span><span style="display:flex;"><span>PING 172.31.34.95 <span style="color:#f92672">(</span>172.31.34.95<span style="color:#f92672">)</span> 56<span style="color:#f92672">(</span>84<span style="color:#f92672">)</span> bytes of data.
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">64</span> bytes from 172.31.34.95: icmp_seq<span style="color:#f92672">=</span><span style="color:#ae81ff">1</span> ttl<span style="color:#f92672">=</span><span style="color:#ae81ff">63</span> time<span style="color:#f92672">=</span>38.1 ms
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">64</span> bytes from 172.31.34.95: icmp_seq<span style="color:#f92672">=</span><span style="color:#ae81ff">2</span> ttl<span style="color:#f92672">=</span><span style="color:#ae81ff">63</span> time<span style="color:#f92672">=</span>38.2 ms
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">64</span> bytes from 172.31.34.95: icmp_seq<span style="color:#f92672">=</span><span style="color:#ae81ff">3</span> ttl<span style="color:#f92672">=</span><span style="color:#ae81ff">63</span> time<span style="color:#f92672">=</span>36.4 ms
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">64</span> bytes from 172.31.34.95: icmp_seq<span style="color:#f92672">=</span><span style="color:#ae81ff">4</span> ttl<span style="color:#f92672">=</span><span style="color:#ae81ff">63</span> time<span style="color:#f92672">=</span>38.3 ms
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">64</span> bytes from 172.31.34.95: icmp_seq<span style="color:#f92672">=</span><span style="color:#ae81ff">5</span> ttl<span style="color:#f92672">=</span><span style="color:#ae81ff">63</span> time<span style="color:#f92672">=</span>38.1 ms
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>--- 172.31.34.95 ping statistics ---
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">5</span> packets transmitted, <span style="color:#ae81ff">5</span> received, 0% packet loss, time 4005ms
</span></span><span style="display:flex;"><span>rtt min/avg/max/mdev <span style="color:#f92672">=</span> 36.380/37.810/38.285/0.718 ms
</span></span></code></pre></div><blockquote>
<p>If you are not sure that you are pinging EC2 take a look on ping response times. On local networks they are much lower.</p>
</blockquote>
<h2 id="summary-">Summary ☑️</h2>
<p>Setting up VPN allows making your infrastructure secure. You don&rsquo;t have to expose ports on the public internet to have access to cloud machines.</p>
<h2 id="faq-">FAQ ❓</h2>
<p><strong>I have two network VLANs at home, should I configure some firewall rules if I don&rsquo;t want to allow access from one of them?</strong></p>
<p>Let&rsquo;s assume you have two networks: <code>home</code> and <code>guest</code>.  If the <code>guest</code> network should not have access to resources to VPN, on AWS VPN,  in <strong>Static IP Prefixes</strong> configuration you have to set only <code>home</code> network subnet.</p>
<p>Another thing are Security Groups, where we define allowed networks. If you will not set too wide network range, then it will also block access.</p>
]]></content><category scheme="https://b58f7780.mjasion.pages.dev/tags/aws" term="aws" label="aws"/><category scheme="https://b58f7780.mjasion.pages.dev/tags/vpn" term="vpn" label="vpn"/><category scheme="https://b58f7780.mjasion.pages.dev/tags/unifi" term="unifi" label="unifi"/><category scheme="https://b58f7780.mjasion.pages.dev/tags/networking" term="networking" label="networking"/></entry><entry><title type="html">How to configure parallelism in Terraform Cloud</title><link href="https://b58f7780.mjasion.pages.dev/posts/cloud/how-to-set-parallelism-in-terraform-cloud/?utm_source=atom_feed" rel="alternate" type="text/html"/><link href="https://b58f7780.mjasion.pages.dev/posts/cloud/how-to-enable-debug-in-terraform-cloud/?utm_source=atom_feed" rel="related" type="text/html" title="How to enable debug and trace logs in Terraform Cloud and Enterprise"/><id>https://b58f7780.mjasion.pages.dev/posts/cloud/how-to-set-parallelism-in-terraform-cloud/</id><author><name>Marcin Jasion</name></author><published>2022-06-14T00:00:00+02:00</published><updated>2022-06-14T00:00:00+02:00</updated><content type="html"><![CDATA[<blockquote>Remote terraform execution runs on default parallelism value. There is a simple way to improve execution speed 4 times</blockquote><p>In my previous <a href="../how-to-enable-debug-in-terraform-cloud/">post</a> I showed how to enable debug logs. Today I want to present how to improve <code>terraform plan</code> and <code>terraform apply</code> speed by configuring <strong>parallelism</strong>.</p>
<p><a href="https://www.terraform.io/" target="_blank" rel="noopener">Terraform</a> by default runs <code>10</code> concurrent operations. To reduce execution time on plan or apply operation we can increase this parameter.</p>
<blockquote>
<p>By increasing <strong>parallelism</strong> you can hit the rate limit of your provider. Some cloud providers (like <a href="https://developers.cloudflare.com/terraform/advanced-topics/provider-customization/#increase-the-frequency-of-api-requests" target="_blank" rel="noopener">Cloudflare</a>) inform about the number of API requests allowed in a period of time. Hitting the limit can impact your deployments.</p>
</blockquote>
<h2 id="tfe_parallelism-variable"><code>TFE_PARALLELISM</code> variable</h2>
<p>The easiest way to increase parallelism in Terraform Cloud for Remote Execution is the <code>TFE_PARALLELISM</code> variable. It just requires a number. To set this you need to perform those steps:</p>
<ul>
<li>Select your workspace,</li>
<li>Go to <strong>Variables</strong> tab,</li>
<li>Add variable in <strong>Workspace variables</strong> panel and create <code>TFE_PARALLELISM</code> variable:
<img src="/posts/cloud/how-to-set-parallelism-in-terraform-cloud/workspace_tfe_parallelism.png" alt="Workspace TFE_PARALLELISM variable">
<blockquote>
<p>Ensure you have selected <strong>Environment variable</strong> button</p>
</blockquote>
</li>
</ul>
<p>The change should be available on next execution.</p>
<h2 id="manage-parallelism-for-each-stage-of-execution">Manage parallelism for each stage of execution</h2>
<p>Terraform CLI allows configuring parallelism differently per command (<code>terraform plan</code>, <code>terraform apply</code> or <code>terraform destroy</code>). In Terraform Cloud we can also do this. In these cases, use <code>TF_CLI_ARGS_plan=&quot;-parallelism=&lt;N&gt;&quot;</code> or <code>TF_CLI_ARGS_apply=&quot;-parallelism=&lt;N&gt;&quot;</code> environment variables instead of <code>TFE_PARALLELISM</code>.</p>
<p>I prefer this way because it allows being more granular. I want to run plan fast because it makes a request about every resource.</p>
<p>To set <code>TF_CLI_ARGS_plan=&quot;-parallelism=&lt;N&gt;&quot;</code> or <code>TF_CLI_ARGS_apply=&quot;-parallelism=&lt;N&gt;&quot;</code> parameters perform same steps as in instruction written above for <code>TFE_PARALLELISM</code>.</p>
<h2 id="manage-the-variables-in-a-single-place">Manage the variables in a single place</h2>
<p>I showed how to configure a variable per workspace. Terraform Cloud allows configuring a <strong>Variable set</strong> which can be attached to each workspace, so we don&rsquo;t need to repeat ourselves for each workspace.</p>
<p>To configure <strong>Variable set</strong> do:</p>
<ul>
<li>Go to your organization <strong>Settings</strong></li>
<li>Select <strong>Variable set</strong> tab and click button <strong>Create variable set</strong></li>
<li>In <strong>Variables</strong> panel you need to define your variables</li>
</ul>
<p>What is left is to attach the variable set to your workspace, or you can enable this set for all workspaces in the organization.</p>
<p><strong>Variables set</strong> has lower precedence than workspace variables. Definition of the same variable in workspace will be used in execution. <a href="https://www.terraform.io/cloud-docs/workspaces/variables#precedence" target="_blank" rel="noopener">Here</a> you can read more.</p>
]]></content><category scheme="https://b58f7780.mjasion.pages.dev/tags/terraform" term="terraform" label="terraform"/><category scheme="https://b58f7780.mjasion.pages.dev/tags/terraform-cloud" term="terraform-cloud" label="terraform-cloud"/><category scheme="https://b58f7780.mjasion.pages.dev/tags/performance" term="performance" label="performance"/></entry><entry><title type="html">How to enable debug and trace logs in Terraform Cloud and Enterprise</title><link href="https://b58f7780.mjasion.pages.dev/posts/cloud/how-to-enable-debug-in-terraform-cloud/?utm_source=atom_feed" rel="alternate" type="text/html"/><link href="https://b58f7780.mjasion.pages.dev/posts/kubernetes/how-to-debug-istio-upstream-reset/?utm_source=atom_feed" rel="related" type="text/html" title="How to debug Istio Upstream Reset 502 UPE (old 503 UC)"/><id>https://b58f7780.mjasion.pages.dev/posts/cloud/how-to-enable-debug-in-terraform-cloud/</id><author><name>Marcin Jasion</name></author><published>2022-06-10T00:00:00+02:00</published><updated>2022-06-10T00:00:00+02:00</updated><content type="html"><![CDATA[<blockquote>Remote terraform execution does not forward CLI parameters. This post covers how to do this.</blockquote><p><a href="https://cloud.hashicorp.com/products/terraform" target="_blank" rel="noopener">Terraform Cloud</a> is an application that helps teams use Terraform together. I am using it for side projects like my cloud infrastructure. Last time I had to see trace logs to find an issue with one of the managed resources.</p>
<p><a href="https://www.terraform.io/" target="_blank" rel="noopener">Terraform</a> has detailed logs which can be enabled by setting the <code>TF_LOG</code> environment variable to any value. This will cause detailed logs to appear on execution.</p>
<h2 id="enabling-verbose-logging-in-cli">Enabling verbose logging in CLI</h2>
<p>You can set <code>TF_LOG</code> to one of the log levels <code>TRACE</code>, <code>DEBUG</code>, <code>INFO</code>, <code>WARN</code> or <code>ERROR</code> to change the verbosity of the logs. You can set this variable in two ways. First option is to set variable for shell session:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>$ export TF_LOG<span style="color:#f92672">=</span>TRACE
</span></span><span style="display:flex;"><span>$ terraform plan
</span></span></code></pre></div><p>Second option is to change variable before command execution</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>$ TF_LOG<span style="color:#f92672">=</span>TRACE terraform plan
</span></span></code></pre></div><h2 id="enabling-logging-in-terraform-cloud">Enabling logging in Terraform Cloud</h2>
<p>If this run is in Terraform Cloud or Terraform Enterprise with Remote Execution, perform these steps:</p>
<ul>
<li>Select your workspace,</li>
<li>Go to <strong>Variables</strong> tab,</li>
<li>Add variable in <strong>Workspace variables</strong> panel and create <code>TF_LOG</code> variable:
<img src="/posts/cloud/how-to-enable-debug-in-terraform-cloud/workspace_variables.png" alt="Workspace Variables">
<blockquote>
<p>Ensure you have selected <strong>Environment variable</strong> button</p>
</blockquote>
</li>
<li>Perform the run that you want to trace</li>
</ul>
<h3 id="another-option-is-to-enable-local-execution">Another option is to enable local execution</h3>
<p>If you are working on your own project, it will be much more convenient to disable Remote Execution and execute the run locally. Go to workspace <strong>Settings</strong> and in <strong>Execution Mode</strong> panel switch the button to <strong>Local</strong>. Then you can run plan from your local machine.</p>
<hr>
<p>Once the issue is resolved, unset the <code>TF_LOG</code> environment variable to disable the enhanced logging.</p>
]]></content><category scheme="https://b58f7780.mjasion.pages.dev/tags/terraform" term="terraform" label="terraform"/><category scheme="https://b58f7780.mjasion.pages.dev/tags/terraform-cloud" term="terraform-cloud" label="terraform-cloud"/><category scheme="https://b58f7780.mjasion.pages.dev/tags/debugging" term="debugging" label="debugging"/></entry></feed>