<?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: Kit Sunde</title>
    <description>The latest articles on DEV Community by Kit Sunde (@kitsunde).</description>
    <link>https://dev.to/kitsunde</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%2F1701%2F8bc9b7d4-2dc8-40cf-a743-ab6ce69c7337.jpg</url>
      <title>DEV Community: Kit Sunde</title>
      <link>https://dev.to/kitsunde</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kitsunde"/>
    <language>en</language>
    <item>
      <title>Rename your terraform workspace on s3.</title>
      <dc:creator>Kit Sunde</dc:creator>
      <pubDate>Fri, 08 Jun 2018 09:38:50 +0000</pubDate>
      <link>https://dev.to/kitsunde/rename-your-terraform-workspace-on-s3-kg9</link>
      <guid>https://dev.to/kitsunde/rename-your-terraform-workspace-on-s3-kg9</guid>
      <description>&lt;p&gt;There's always a workspace &lt;strong&gt;default&lt;/strong&gt; in terraform as it's natural to start with it, then create a second workspace &lt;strong&gt;production&lt;/strong&gt;. But &lt;strong&gt;default&lt;/strong&gt; isn't a good name for staging and it doesn't map onto branches.&lt;/p&gt;

&lt;p&gt;It's rather simple to move the workspace though if you look into an s3 bucket with the workspace:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;aws s3 &lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;--recursive&lt;/span&gt; s3://mediapop-tfstate
2018-06-08 17:28:47      92004 &lt;span class="nb"&gt;env&lt;/span&gt;:/production/terraform
2018-06-02 14:08:00      44605 terraform
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These two files are the &lt;code&gt;.tfstate&lt;/code&gt; files you're used to seeing locally, it's pretty obvious by the name which file belongs to what workspace. So lets rename it into &lt;strong&gt;staging&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;aws s3 &lt;span class="nb"&gt;mv &lt;/span&gt;s3://mediapop-tfstate/terraform s3://mediapop-tfstate/env:/staging/terraform
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We've now renamed the default workspace to the staging workspace and can do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;terraform workspace change staging
&lt;span class="nv"&gt;$ &lt;/span&gt;terraform plan

...

No changes. Infrastructure is up-to-date.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're using DynamoDB for locking you'll also need to rename the lock. Or you'll receive this message:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Error refreshing state: state data in S3 does not have the expected content.&lt;/p&gt;

&lt;p&gt;This may be caused by unusually long delays in S3 processing a previous state&lt;br&gt;
update.  Please wait for a minute or two and try again. If this problem&lt;br&gt;
persists, and neither S3 nor DynamoDB are experiencing an outage, you may need&lt;br&gt;
to manually verify the remote state and update the Digest value stored in the&lt;br&gt;
DynamoDB table to the following value&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Get the digest of the old lock:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="nx"&gt;aws&lt;/span&gt; &lt;span class="nx"&gt;dynamodb&lt;/span&gt; &lt;span class="nx"&gt;get&lt;/span&gt;&lt;span class="err"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;  &lt;span class="err"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="s1"&gt;'{"LockID": {"S": "mediapop-tfstate/terraform-md5"}}'&lt;/span&gt; &lt;span class="err"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="err"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="nx"&gt;mediapop&lt;/span&gt;&lt;span class="err"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;tfstate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Outputs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Item"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"Digest"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"S"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"9a9098da0fd7e3a11d57fc5d9718e095"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"LockID"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"S"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"mediapop-tfstate/terraform-md5"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a new file &lt;code&gt;staging.json&lt;/code&gt; and rename the lock:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Digest"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"S"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"9a9098da0fd7e3a11d57fc5d9718e095"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"LockID"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"S"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"mediapop-tfstate/env:/staging/terraform-md5"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then push the item to dynamodb:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;aws dynamodb put-item &lt;span class="nt"&gt;--item&lt;/span&gt; file://staging.json &lt;span class="nt"&gt;--table-name&lt;/span&gt; mediapop-tfstate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Delete the old default lock:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;aws dynamodb remove-item  &lt;span class="nt"&gt;--key&lt;/span&gt; &lt;span class="s1"&gt;'{"LockID": {"S": "mediapop-tfstate/terraform-md5"}}'&lt;/span&gt; &lt;span class="nt"&gt;--table-name&lt;/span&gt; mediapop-tfstate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>terraform</category>
    </item>
  </channel>
</rss>
