<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Davidlu&#39;s Blog</title>
  
  <subtitle>SRE | DevOps</subtitle>
  <link href="/atom.xml" rel="self"/>
  
  <link href="http://davidlu1001.github.io/"/>
  <updated>2021-03-17T10:51:32.000Z</updated>
  <id>http://davidlu1001.github.io/</id>
  
  <author>
    <name>David Lu</name>
    
  </author>
  
  <generator uri="https://hexo.io/">Hexo</generator>
  
  <entry>
    <title>Terraform - Add dependency for modules</title>
    <link href="http://davidlu1001.github.io/2021/03/17/Terraform-Add-dependency-for-modules/"/>
    <id>http://davidlu1001.github.io/2021/03/17/Terraform-Add-dependency-for-modules/</id>
    <published>2021-03-17T10:51:32.000Z</published>
    <updated>2021-03-17T10:51:32.000Z</updated>
    
    <content type="html"><![CDATA[<h4 id="Error"><a href="#Error" class="headerlink" title="Error"></a>Error</h4><blockquote><p>Error creating application autoscaling target: ValidationException: ECS service doesn’t exist: service/AAA</p></blockquote><p>Encountered this type of dependency issue before, but re-run terraform plan/apply usually can fix it, but not lucky this time :-)</p><p>After having a quick look, and found out the module <code>depends_on</code> (dependency between terraform modules) only officially supported for Terraform version 0.13 - <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2hhc2hpY29ycC90ZXJyYWZvcm0vaXNzdWVzLzEwNDYy">hashicorp/terraform#10462<i class="fa fa-external-link-alt"></i></span> - but we’re on version 0.12 unfortunately.</p><h4 id="Workaround"><a href="#Workaround" class="headerlink" title="Workaround"></a>Workaround</h4><p>Found the workaround <span class="exturl" data-url="aHR0cHM6Ly9kaXNjdXNzLmhhc2hpY29ycC5jb20vdC90aXBzLWhvd3RvLWltcGxlbWVudC1tb2R1bGUtZGVwZW5kcy1vbi1lbXVsYXRpb24vMjMwNQ==">here<i class="fa fa-external-link-alt"></i></span>:</p><blockquote><p>The key insight here is that variables are nodes in the dependency graph too, and so can use them as a “hub” for passing dependencies across the module boundary.</p></blockquote><h4 id="Example"><a href="#Example" class="headerlink" title="Example"></a>Example</h4><p>e.g.</p><p>In <strong>module A</strong> (e.g. with resource <code>aws_appautoscaling_target</code>)</p><ol><li>Add the following in <code>variables.yml</code>:</li></ol><figure class="highlight fsharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">variable <span class="string">&quot;mod_depends_on&quot;</span> &#123;</span><br><span class="line">  <span class="class"><span class="keyword">type</span>    </span>= any</span><br><span class="line">  <span class="keyword">default</span> = <span class="keyword">null</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><ol start="2"><li>Add <code>depends_on</code> for resource A <code>aws_appautoscaling_target</code> (as depends_on can refer directly to variables in Terraform version 0.12)</li></ol><figure class="highlight lasso"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">resource <span class="string">&quot;aws_appautoscaling_target&quot;</span> <span class="string">&quot;target&quot;</span> &#123;</span><br><span class="line">  <span class="params">...</span></span><br><span class="line">  depends_on = <span class="meta">[</span><span class="built_in">var</span>.mod_depends_on<span class="meta">]</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>In <strong>module B</strong> (e.g. with resource <code>aws_ecs_service</code>):</p><p>Just need to add the following:</p><figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">mod_depends_on</span> = [module.A]</span><br></pre></td></tr></table></figure><h4 id="Reference"><a href="#Reference" class="headerlink" title="Reference:"></a>Reference:</h4><p><span class="exturl" data-url="aHR0cHM6Ly9kaXNjdXNzLmhhc2hpY29ycC5jb20vdC90aXBzLWhvd3RvLWltcGxlbWVudC1tb2R1bGUtZGVwZW5kcy1vbi1lbXVsYXRpb24vMjMwNQ==">https://discuss.hashicorp.com/t/tips-howto-implement-module-depends-on-emulation/2305<i class="fa fa-external-link-alt"></i></span></p>]]></content>
    
    <summary type="html">
    
      
      
        &lt;h4 id=&quot;Error&quot;&gt;&lt;a href=&quot;#Error&quot; class=&quot;headerlink&quot; title=&quot;Error&quot;&gt;&lt;/a&gt;Error&lt;/h4&gt;&lt;blockquote&gt;
&lt;p&gt;Error creating application autoscaling target
      
    
    </summary>
    
    
      <category term="SRE" scheme="http://davidlu1001.github.io/categories/SRE/"/>
    
      <category term="Terraform" scheme="http://davidlu1001.github.io/categories/Terraform/"/>
    
    
      <category term="SRE" scheme="http://davidlu1001.github.io/tags/SRE/"/>
    
      <category term="Terraform" scheme="http://davidlu1001.github.io/tags/Terraform/"/>
    
  </entry>
  
  <entry>
    <title>ElasticSearch backup with AWS S3</title>
    <link href="http://davidlu1001.github.io/2020/12/12/ElasticSearch-backup-with-AWS-S3/"/>
    <id>http://davidlu1001.github.io/2020/12/12/ElasticSearch-backup-with-AWS-S3/</id>
    <published>2020-12-11T11:08:50.000Z</published>
    <updated>2020-12-11T11:08:50.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="TL-DR"><a href="#TL-DR" class="headerlink" title="TL;DR"></a>TL;DR</h1><ul><li><p>ElasticSearch Backup (snapshot / restore) on AWS S3</p></li><li><p>Steps / Configrations for ES snapshot / restore</p></li><li><p>Use elastic <code>curator</code> to manage snapshots (create / remove)</p></li><li><p>Docker image for ES Curator to manage Elasticsearch snapshots - <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2RhdmlkbHUxMDAxL2RvY2tlci1jdXJhdG9y">davidlu1001/docker-curator<i class="fa fa-external-link-alt"></i></span></p></li></ul><h1 id="Overview"><a href="#Overview" class="headerlink" title="Overview"></a>Overview</h1><p>The purpose of this blog is to investigate the possible solutions to backup and restore ES indices. So that in the event of a failure, the cluster data can be quickly restored and minimized the business impact.</p><a id="more"></a><h2 id="Backup-content"><a href="#Backup-content" class="headerlink" title="Backup content"></a>Backup content</h2><p>Generally the backup content would include:</p><ul><li><p>Cluster data</p></li><li><p>State configuration: includes cluster / index / shard settings</p></li></ul><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># index settings</span></span><br><span class="line"><span class="built_in">curl</span> <span class="literal">-X</span> GET <span class="string">&quot;localhost:9200/_all/_settings?pretty&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># cluster settings</span></span><br><span class="line"><span class="built_in">curl</span> <span class="literal">-s</span> <span class="literal">-XGET</span> <span class="string">&quot;http://localhost:9200/_cluster/settings?pretty&amp;flat_settings&amp;filter_path=persistent&quot;</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;persistent&quot;</span> : &#123;</span><br><span class="line">    <span class="string">&quot;cluster.routing.allocation.cluster_concurrent_rebalance&quot;</span> : <span class="string">&quot;12&quot;</span>,</span><br><span class="line">    <span class="string">&quot;cluster.routing.allocation.node_concurrent_recoveries&quot;</span> : <span class="string">&quot;6&quot;</span>,</span><br><span class="line">    <span class="string">&quot;indices.recovery.max_bytes_per_sec&quot;</span> : <span class="string">&quot;320mb&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>P.S.</p><ul><li><p>Transient settings are not considered for backup</p></li><li><p>Also can capture these cluster settings in a data backup snapshot by specifying the <code>include_global_state: true</code> (default) parameter for the snapshot API.</p></li></ul><h2 id="Snapshot-Repository"><a href="#Snapshot-Repository" class="headerlink" title="Snapshot Repository"></a>Snapshot Repository</h2><p>In order to enable backup for ES cluster data, a snapshot repository must be registered before performing snapshot and restore operations.</p><p>Snapshots can be stored in:</p><ul><li><p>Local shared filesystem: NFS</p></li><li><p>Remote repositories: backed by the cloud (AWS / Azure / GCP) or by distributed file systems (HDFS) with <span class="exturl" data-url="aHR0cHM6Ly93d3cuZWxhc3RpYy5jby9ndWlkZS9lbi9lbGFzdGljc2VhcmNoL3BsdWdpbnMvNy4xMC9yZXBvc2l0b3J5Lmh0bWw=">repository plugins<i class="fa fa-external-link-alt"></i></span></p></li></ul><p>Considering that if use shared filesystem NFS, additional setup / configuration is required, and the cluster needs rolling restart to take effect. As all nodes must have access to the shared storage to be able to store the snapshot data. Therefore, from the perspective of complexity and operational safety, will not consider it (even the cluster only uses a little of the disk space).</p><p>And luckily we’ve got <code>repository-s3</code> plugin installed on all nodes in the ES cluster, so rolling restart is not needed if using S3 as snapshot repository.</p><figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ ES_PATH_CONF=<span class="regexp">/etc/</span>elasticsearch <span class="regexp">/usr/</span>share<span class="regexp">/elasticsearch/</span>bin/elasticsearch-plugin list</span><br><span class="line"></span><br><span class="line">discovery-ec2</span><br><span class="line">repository-s3</span><br></pre></td></tr></table></figure><p>So would prefer to use AWS S3 as a remote snapshot repository with existing S3 repository plugin.</p><p>Backup retention strategy<br>Based on backup granularity and retention time, we can choose the following strategies based on different scenario:</p><ul><li><p>Takes daily snapshots (during the hour we specify) and retains up to 14 of them for 30 days.</p></li><li><p>Takes hourly snapshots and retains up to 336 of them for 14 days.</p></li></ul><p>Can carefully start with daily backup (the initial backup may take relatively longer time) and keep monitoring, and then could try hourly backup if it’s feasible, in order to provide more granular recovery points.</p><blockquote><p>Incremental snapshot mechanism</p><p>The snapshot functionality will simply remove the data that was only referenced by the deleted snapshot but will leave the data that is still required for other snapshots in the repository.</p><p>So it would be safe to delete the first initial snapshot without affecting the latter snapshots.</p><p>Ref: <span class="exturl" data-url="aHR0cHM6Ly93d3cuZWxhc3RpYy5jby9ibG9nL2ZvdW5kLWVsYXN0aWNzZWFyY2gtc25hcHNob3QtYW5kLXJlc3RvcmU=">https://www.elastic.co/blog/found-elasticsearch-snapshot-and-restore<i class="fa fa-external-link-alt"></i></span></p></blockquote><h1 id="Backup-cluster’s-data"><a href="#Backup-cluster’s-data" class="headerlink" title="Backup cluster’s data"></a>Backup cluster’s data</h1><p>Will take snapshot per index, instead of backup the whole cluster, which can potentially bring the flexibility for the restore step.</p><p>The snapshots are taken incrementally. This enables us to take frequent snapshots with minimal overhead (the initial backup may take relatively longer time, based on the amount of data). The more frequently we take snapshots, the less time it will take to complete the backup.</p><p>The snapshot process is executed in non-blocking fashion. All indexing and searching operations can continue to run against the index that is being snapshotted.</p><h2 id="Snapshot-Prerequisites"><a href="#Snapshot-Prerequisites" class="headerlink" title="Snapshot Prerequisites"></a>Snapshot Prerequisites</h2><ul><li>Check repository-s3 plugin is installed</li></ul><figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ ES_PATH_CONF=<span class="regexp">/etc/</span>elasticsearch <span class="regexp">/usr/</span>share<span class="regexp">/elasticsearch/</span>bin/elasticsearch-plugin list</span><br><span class="line"></span><br><span class="line">discovery-ec2</span><br><span class="line">repository-s3</span><br></pre></td></tr></table></figure><ul><li><p>Create an S3 bucket to store the snapshot (e.g. <code>es-backup</code>)</p></li><li><p>Check current iam instance profile role on ES cluster</p></li><li><p>Update existing IAM role and setup proper S3 Permissions</p></li></ul><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="attr">&quot;Version&quot;</span>: <span class="string">&quot;2012-10-17&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;Statement&quot;</span>: [</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">&quot;Effect&quot;</span>: <span class="string">&quot;Allow&quot;</span>,</span><br><span class="line">            <span class="attr">&quot;Action&quot;</span>: [</span><br><span class="line">                <span class="string">&quot;s3:ListAllMyBuckets&quot;</span>,</span><br><span class="line">                <span class="string">&quot;s3:GetBucketLocation&quot;</span></span><br><span class="line">            ],</span><br><span class="line">            <span class="attr">&quot;Resource&quot;</span>: <span class="string">&quot;arn:aws:s3:::*&quot;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">&quot;Effect&quot;</span>: <span class="string">&quot;Allow&quot;</span>,</span><br><span class="line">            <span class="attr">&quot;Action&quot;</span>: [</span><br><span class="line">                <span class="string">&quot;s3:ListBucket&quot;</span>,</span><br><span class="line">                <span class="string">&quot;s3:ListBucketMultipartUploads&quot;</span>,</span><br><span class="line">                <span class="string">&quot;s3:ListBucketVersions&quot;</span></span><br><span class="line">            ],</span><br><span class="line">            <span class="attr">&quot;Resource&quot;</span>: [</span><br><span class="line">                <span class="string">&quot;arn:aws:s3:::es-backup&quot;</span></span><br><span class="line">            ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">&quot;Effect&quot;</span>: <span class="string">&quot;Allow&quot;</span>,</span><br><span class="line">            <span class="attr">&quot;Action&quot;</span>: [</span><br><span class="line">                <span class="string">&quot;s3:GetObject&quot;</span>,</span><br><span class="line">                <span class="string">&quot;s3:GetObjectAcl&quot;</span>,</span><br><span class="line">                <span class="string">&quot;s3:PutObject&quot;</span>,</span><br><span class="line">                <span class="string">&quot;s3:PutObjectAcl&quot;</span>,</span><br><span class="line">                <span class="string">&quot;s3:DeleteObject&quot;</span>,</span><br><span class="line">                <span class="string">&quot;s3:AbortMultipartUpload&quot;</span>,</span><br><span class="line">                <span class="string">&quot;s3:ListMultipartUploadParts&quot;</span></span><br><span class="line">            ],</span><br><span class="line">            <span class="attr">&quot;Resource&quot;</span>: [</span><br><span class="line">                <span class="string">&quot;arn:aws:s3:::es-backup/*&quot;</span></span><br><span class="line">            ]</span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><ul><li>Need to update the role permission for both ES master / data node</li></ul><blockquote><p>Note:</p><p>Otherwise will get the repository_verification_exception error when trying to register the snapshot repository</p></blockquote><figure class="highlight inform7"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;root_cause&quot;</span> : <span class="comment">[</span></span><br><span class="line"><span class="comment">  &#123;</span></span><br><span class="line"><span class="comment">    &quot;type&quot; : &quot;repository_verification_exception&quot;,</span></span><br><span class="line"><span class="comment">    &quot;reason&quot; : &quot;<span class="comment">[snapshot-s3-repo]</span> <span class="comment">[<span class="comment">[zMHw44eLS7q0ItSfYmh2Ug, &#x27;RemoteTransportException<span class="comment">[<span class="comment">[elastic-data-warm-0db25600a6f728fe9]</span><span class="comment">[172.17.24.56:9300]</span><span class="comment">[internal:admin/repository/verify]</span>]</span>; nested: BlobStoreException<span class="comment">[Failed to check if blob <span class="comment">[master.dat]</span> exists]</span>; nested: NotSerializableExceptionWrapper<span class="comment">[amazon_s3_exception: Forbidden (Service: Amazon S3; Status Code: 403; Error Code: 403 Forbidden; Request ID: 425B663C880BBD13; S3 Extended Request ID: huXmNMJSK+Lh4vW5LIqJsYUfu9s3ZhPa2knSeblOVBbNnoffvYaZXhMz4sgfT8ZGTAAC+3yjUNc=)]</span>;&#x27;]</span></span></span></span><br><span class="line"><span class="comment"><span class="comment">    ...</span></span></span><br><span class="line"><span class="comment"><span class="comment">    ]</span>&quot;</span></span><br><span class="line"><span class="comment">  &#125;</span></span><br></pre></td></tr></table></figure><ul><li>Register snapshot repository per index with base_path (prefix) in S3 bucket for ES cluster (one-time operation)</li></ul><figure class="highlight autohotkey"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"># use `base_path` under S3 for `service-A`</span><br><span class="line"></span><br><span class="line">curl -X PUT <span class="string">&quot;localhost:9200/_snapshot/snapshot-s3-repo?pretty&quot;</span> -H &#x27;Content-Type: application/json&#x27; -d&#x27;</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;type&quot;</span>: <span class="string">&quot;s3&quot;</span>,</span><br><span class="line">  <span class="string">&quot;settings&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;bucket&quot;</span>: <span class="string">&quot;es-backup&quot;</span>,</span><br><span class="line">    <span class="string">&quot;region&quot;</span>: <span class="string">&quot;us-west-2&quot;</span>,</span><br><span class="line">    <span class="string">&quot;base_path&quot;</span>: <span class="string">&quot;service-A&quot;</span>,</span><br><span class="line">    <span class="string">&quot;max_snapshot_bytes_per_sec&quot;</span>: <span class="string">&quot;500mb&quot;</span>,</span><br><span class="line">    <span class="string">&quot;max_restore_bytes_per_sec&quot;</span>: <span class="string">&quot;1gb&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&#x27;</span><br></pre></td></tr></table></figure><ul><li>Register snapshot repository for each index (with base_path: same S3 bucket, but different directory)</li></ul><figure class="highlight sml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"># e.g. for .elastichq</span><br><span class="line"></span><br><span class="line">curl -<span class="type">X</span> <span class="type">PUT</span> <span class="string">&quot;localhost:9200/_snapshot/snapshot-s3-elastichq?pretty&quot;</span> -<span class="type">H</span> <span class="symbol">&#x27;Content</span>-<span class="type">Type</span>: application/json&#x27; -d&#x27;</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;type&quot;</span>: <span class="string">&quot;s3&quot;</span>,</span><br><span class="line">  <span class="string">&quot;settings&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;bucket&quot;</span>: <span class="string">&quot;es-backup&quot;</span>,</span><br><span class="line">    <span class="string">&quot;base_path&quot;</span>: <span class="string">&quot;elastichq&quot;</span>,</span><br><span class="line">    <span class="string">&quot;region&quot;</span>: <span class="string">&quot;us-west-2&quot;</span>,</span><br><span class="line">    <span class="string">&quot;max_snapshot_bytes_per_sec&quot;</span>: <span class="string">&quot;500mb&quot;</span>,</span><br><span class="line">    <span class="string">&quot;max_restore_bytes_per_sec&quot;</span>: <span class="string">&quot;1gb&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="string">&#x27;</span></span><br></pre></td></tr></table></figure><ul><li>Check or Verify snapshot repository<figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">curl</span> localhost:<span class="number">9200</span>/_snapshot?pretty</span><br></pre></td></tr></table></figure></li></ul><h1 id="Backup-operation"><a href="#Backup-operation" class="headerlink" title="Backup operation"></a>Backup operation</h1><p>The snapshots lifecycle management (SLM) feature is introduced and natively supported in ES version <code>7.5.0</code>, so for lower version need to self-manage the snapshots / retention policy.</p><p>Can either use existing tool curator (with cronjob) on EC2 instance (e.g. gateway / master node), or can consider using ECS cronjob (scheduled task) to schedule the backup to avoid single point of failure.</p><p>For curator version compatibility, should be safe to choose the latest version <code>5.8.3</code> to support ES <code>6.X</code>.</p><p>Backup operation with ES snapshot API:</p><figure class="highlight applescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># e.g. backup specified indice</span></span><br><span class="line"></span><br><span class="line">curl -X PUT <span class="string">&quot;localhost:9200/_snapshot/snapshot-s3-repo/elastichq-20201125?wait_for_completion=false&amp;pretty&quot;</span> -H &#x27;Content-Type: <span class="built_in">application</span>/json&#x27; -d&#x27;</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;indices&quot;</span>: <span class="string">&quot;.elastichq&quot;</span>,</span><br><span class="line">  <span class="string">&quot;ignore_unavailable&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">  <span class="string">&quot;include_global_state&quot;</span>: <span class="literal">false</span></span><br><span class="line">&#125;</span><br><span class="line">&#x27;</span><br><span class="line"></span><br><span class="line"><span class="comment"># check snapshots</span></span><br><span class="line"></span><br><span class="line">curl localhost:<span class="number">9200</span>/_snapshot/snapshot-s3-repo/_all?pretty</span><br><span class="line"></span><br><span class="line"><span class="comment"># delete snapshot</span></span><br><span class="line"></span><br><span class="line">curl -X DELETE <span class="string">&quot;localhost:9200/_snapshot/snapshot-s3-repo/elastichq-20201125?pretty&quot;</span></span><br></pre></td></tr></table></figure><blockquote><p>Useful Config for Snapshot / Restore Process</p><p><code>max_restore_bytes_per_sec</code></p><p>Throttles per node restore rate. Defaults to unlimited. Note that restores are also throttled through <span class="exturl" data-url="aHR0cHM6Ly93d3cuZWxhc3RpYy5jby9ndWlkZS9lbi9lbGFzdGljc2VhcmNoL3JlZmVyZW5jZS83LjEwL3JlY292ZXJ5Lmh0bWw=">recovery settings<i class="fa fa-external-link-alt"></i></span>.</p><p><code>max_snapshot_bytes_per_sec</code></p><p>Throttles per node snapshot rate. Defaults to <code>40mb</code> per second.</p><p><code>chunk_size</code></p><p>Big files can be broken down into chunks during snapshotting if needed. Specify the chunk size as a value and unit, for example: 1GB, 10MB, 5KB, 500B. Defaults to <code>1GB</code>.</p><p><code>buffer_size</code></p><p>Minimum threshold below which the chunk is uploaded using a single request. Beyond this threshold, the S3 repository will use the <span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL0FtYXpvblMzL2xhdGVzdC9kZXYvdXBsb2Fkb2JqdXNpbmdtcHUuaHRtbA==">AWS Multipart Upload API<i class="fa fa-external-link-alt"></i></span> to split the chunk into several parts, each of buffer_size length, and to upload each part in its own request. Should between 5mb to 5gb. Defaults to the minimum between 100mb and 5% of the heap size.</p></blockquote><h2 id="Restore-cluster’s-data"><a href="#Restore-cluster’s-data" class="headerlink" title="Restore cluster’s data"></a>Restore cluster’s data</h2><h3 id="Restore-Prerequisites"><a href="#Restore-Prerequisites" class="headerlink" title="Restore Prerequisites"></a>Restore Prerequisites</h3><ul><li><p>Confirm the target index has same number of shards as the index in the snapshot</p></li><li><p>Close the target index need to be restored</p></li></ul><blockquote><p>Reference</p><p>By default, the cluster state is not restored. To include the global cluster state, need to set <code>include_global_state</code> to <code>true</code> in the restore request body (if include the cluster state in the previous backup operation), but usually we don’t want to backup the cluster state.</p><p>An existing index can be only restored if it’s closed and has the same number of shards as the index in the snapshot. </p><p>The restore operation automatically opens restored indices if they were closed, and creates new indices if they didn’t exist in the cluster.</p></blockquote><h3 id="Restore-operation"><a href="#Restore-operation" class="headerlink" title="Restore operation"></a>Restore operation</h3><p>In order to restore the index from a snapshot:</p><ul><li>Update index settings to speed up restore process:</li></ul><figure class="highlight ada"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"># before restore</span><br><span class="line">curl -XPUT <span class="string">&quot;localhost:9200/&#123;index_name&#125;/_settings&quot;</span> -d&#x27; &#123;</span><br><span class="line">        <span class="string">&quot;number_of_replicas&quot;</span> : 0,</span><br><span class="line">        <span class="string">&quot;refresh_interval&quot;</span> : &quot;-1&quot;</span><br><span class="line">&#125;&#x27;</span><br></pre></td></tr></table></figure><ul><li>Restore snapshot from S3 (Set the index settings back at the same time)</li></ul><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># e.g restore from specific snapshot service-A</span></span><br><span class="line"></span><br><span class="line">curl -XPOST <span class="string">&quot;localhost:9200/_snapshot/snapshot-s3-repo/service-A-2020112416/_restore?pretty&quot;</span> -H &#x27;Content-<span class="keyword">Type</span>: application/json&#x27; -d&#x27;</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;indices&quot;</span>: <span class="string">&quot;service-A&quot;</span>,</span><br><span class="line">  <span class="string">&quot;ignore_unavailable&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">  <span class="string">&quot;include_global_state&quot;</span>: <span class="literal">false</span>,</span><br><span class="line">  <span class="string">&quot;include_aliases&quot;</span>: <span class="literal">false</span>,</span><br><span class="line">  <span class="string">&quot;index_settings&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;index.number_of_replicas&quot;</span>: <span class="number">2</span>,</span><br><span class="line">    <span class="string">&quot;index.refresh_interval&quot;</span>: <span class="string">&quot;1s&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&#x27;</span><br></pre></td></tr></table></figure><p>P.S.</p><ul><li><p>Use the <code>index_settings</code> parameter to override index settings during the restore process</p></li><li><p>Set <code>include_aliases</code> to <code>false</code> to prevent aliases from being restored together with associated indices</p></li></ul><h1 id="Curator-Usage"><a href="#Curator-Usage" class="headerlink" title="Curator Usage"></a>Curator Usage</h1><h2 id="use-curator-cli"><a href="#use-curator-cli" class="headerlink" title="use curator_cli"></a>use <code>curator_cli</code></h2><p>Examples:</p><p>Here the variable <code>&quot;$&#123;DRY_RUN&#125;&quot;</code> can be either <code>&quot;--dry-run&quot;</code> (for dry-run mode) or <code>&quot;&quot;</code> (empty, means without dry-run)</p><ul><li>create snapshot</li></ul><figure class="highlight dsconfig"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># create snapshot for index(es) in the same repo</span></span><br><span class="line"></span><br><span class="line">/<span class="string">usr</span>/<span class="string">local</span>/<span class="string">bin</span>/<span class="string">curator_cli</span> \</span><br><span class="line">$&#123;<span class="string">DRY_RUN</span>&#125; \</span><br><span class="line"><span class="built_in">--host</span> <span class="string">&quot;$&#123;ELASTICSEARCH_HOST&#125;&quot;</span> \</span><br><span class="line"><span class="built_in">--port</span> <span class="string">9200</span> \</span><br><span class="line"><span class="string">snapshot</span> \</span><br><span class="line"><span class="built_in">--repository</span> <span class="string">&quot;$&#123;REPO_NAME&#125;&quot;</span> \</span><br><span class="line"><span class="built_in">--name</span> <span class="string">&quot;$&#123;INDEX_PREFIX&#125;-$&#123;curr_date&#125;&quot;</span> \</span><br><span class="line"><span class="built_in">--wait_for_completion</span> <span class="built_in">--skip_repo_fs_check</span> \</span><br><span class="line"><span class="built_in">--filter_list</span> <span class="string">&quot;&#123;\&quot;</span><span class="string">filtertype</span>\<span class="string">&quot;:\&quot;</span><span class="string">pattern</span>\<span class="string">&quot;,\&quot;</span><span class="string">kind</span>\<span class="string">&quot;:\&quot;</span><span class="string">prefix</span>\<span class="string">&quot;,\&quot;</span><span class="string">value</span>\<span class="string">&quot;:\&quot;</span>$&#123;<span class="string">INDEX_PREFIX</span>&#125;\<span class="string">&quot;&#125;&quot;</span></span><br></pre></td></tr></table></figure><ul><li><p>remove snapshot</p><figure class="highlight lsl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">/usr/local/bin/curator_cli \</span><br><span class="line">$&#123;DRY_RUN&#125; \</span><br><span class="line">--host <span class="string">&quot;$&#123;ELASTICSEARCH_HOST&#125;&quot;</span> \</span><br><span class="line">--port <span class="number">9200</span> \</span><br><span class="line">delete_snapshots \</span><br><span class="line">--repository <span class="string">&quot;$&#123;REPO_NAME&#125;&quot;</span> \</span><br><span class="line">--ignore_empty_list \</span><br><span class="line">--filter_list <span class="string">&quot;[&#123;<span class="subst">\&quot;</span>filtertype<span class="subst">\&quot;</span>:<span class="subst">\&quot;</span>age<span class="subst">\&quot;</span>,<span class="subst">\&quot;</span>source<span class="subst">\&quot;</span>:<span class="subst">\&quot;</span>creation_date<span class="subst">\&quot;</span>,<span class="subst">\&quot;</span>direction<span class="subst">\&quot;</span>:<span class="subst">\&quot;</span>older<span class="subst">\&quot;</span>,<span class="subst">\&quot;</span>unit<span class="subst">\&quot;</span>:<span class="subst">\&quot;</span>$&#123;UNIT&#125;<span class="subst">\&quot;</span>,<span class="subst">\&quot;</span>unit_count<span class="subst">\&quot;</span>:<span class="subst">\&quot;</span>$&#123;UNIT_COUNT&#125;<span class="subst">\&quot;</span>&#125;,&#123;<span class="subst">\&quot;</span>filtertype<span class="subst">\&quot;</span>:<span class="subst">\&quot;</span>pattern<span class="subst">\&quot;</span>,<span class="subst">\&quot;</span>kind<span class="subst">\&quot;</span>:<span class="subst">\&quot;</span>prefix<span class="subst">\&quot;</span>,<span class="subst">\&quot;</span>value<span class="subst">\&quot;</span>:<span class="subst">\&quot;</span>$&#123;INDEX_PREFIX&#125;<span class="subst">\&quot;</span>&#125;]&quot;</span></span><br></pre></td></tr></table></figure></li><li><p>restore snapshot</p></li></ul><figure class="highlight lsl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"># close first</span><br><span class="line"></span><br><span class="line">/usr/local/bin/curator_cli \</span><br><span class="line">$&#123;DRY_RUN&#125; \</span><br><span class="line">--host <span class="string">&quot;$&#123;ELASTICSEARCH_HOST&#125;&quot;</span> \</span><br><span class="line">--port <span class="number">9200</span> \</span><br><span class="line">close \</span><br><span class="line">--ignore_empty_list \</span><br><span class="line">--filter_list <span class="string">&quot;&#123;<span class="subst">\&quot;</span>filtertype<span class="subst">\&quot;</span>:<span class="subst">\&quot;</span>pattern<span class="subst">\&quot;</span>,<span class="subst">\&quot;</span>kind<span class="subst">\&quot;</span>:<span class="subst">\&quot;</span>prefix<span class="subst">\&quot;</span>,<span class="subst">\&quot;</span>value<span class="subst">\&quot;</span>:<span class="subst">\&quot;</span>$&#123;INDEX_PREFIX&#125;<span class="subst">\&quot;</span>&#125;&quot;</span></span><br><span class="line"></span><br><span class="line"># restore</span><br><span class="line"></span><br><span class="line">/usr/local/bin/curator_cli \</span><br><span class="line">$&#123;DRY_RUN&#125; \</span><br><span class="line">--host <span class="string">&quot;$&#123;ELASTICSEARCH_HOST&#125;&quot;</span> \</span><br><span class="line">--port <span class="number">9200</span> \</span><br><span class="line">restore \</span><br><span class="line">--repository <span class="string">&quot;$&#123;REPO_NAME&#125;&quot;</span> \</span><br><span class="line">--wait_for_completion --skip_repo_fs_check --ignore_empty_list \</span><br><span class="line">--filter_list <span class="string">&quot;[&#123;<span class="subst">\&quot;</span>filtertype<span class="subst">\&quot;</span>:<span class="subst">\&quot;</span>state<span class="subst">\&quot;</span>&#125;,&#123;<span class="subst">\&quot;</span>filtertype<span class="subst">\&quot;</span>:<span class="subst">\&quot;</span>pattern<span class="subst">\&quot;</span>,<span class="subst">\&quot;</span>kind<span class="subst">\&quot;</span>:<span class="subst">\&quot;</span>prefix<span class="subst">\&quot;</span>,<span class="subst">\&quot;</span>value<span class="subst">\&quot;</span>:<span class="subst">\&quot;</span>$&#123;INDEX_PREFIX&#125;<span class="subst">\&quot;</span>&#125;]&quot;</span></span><br></pre></td></tr></table></figure><h2 id="use-curator"><a href="#use-curator" class="headerlink" title="use curator"></a>use <code>curator</code></h2><p>Examples:</p><p>Here the variable <code>&quot;$&#123;DRY_RUN&#125;&quot;</code> can be either <code>&quot;--dry-run&quot;</code> (for dry-run mode) or <code>&quot;&quot;</code> (empty, means without dry-run)</p><figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="regexp">/usr/</span>local<span class="regexp">/bin/</span>curator --config <span class="regexp">/etc/</span>curator<span class="regexp">/config.yml &quot;$&#123;DRY_RUN&#125;&quot; /</span>etc<span class="regexp">/curator/</span>actions.yml</span><br></pre></td></tr></table></figure><p><code>actions.yml</code> for <code>snapshot</code>:</p><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># snapshot</span></span><br><span class="line"></span><br><span class="line"><span class="attr">actions:</span></span><br><span class="line">  <span class="attr">1:</span></span><br><span class="line">    <span class="attr">action:</span> <span class="string">snapshot</span></span><br><span class="line">    <span class="attr">description:</span> <span class="string">&gt;-</span></span><br><span class="line">      <span class="string">Snapshot</span> <span class="string">company-index-A-*</span> <span class="string">prefixed</span> <span class="string">indices</span> <span class="string">with</span> <span class="string">the</span> <span class="string">default</span> <span class="string">snapshot</span> <span class="string">name</span> <span class="string">pattern</span> <span class="string">of</span></span><br><span class="line">      <span class="string">&#x27;company-index-A-%Y%m%d%H%M&#x27;</span><span class="string">.</span>  <span class="string">Wait</span> <span class="string">for</span> <span class="string">the</span> <span class="string">snapshot</span> <span class="string">to</span> <span class="string">complete.</span>  <span class="string">Skip</span></span><br><span class="line">      <span class="string">the</span> <span class="string">repository</span> <span class="string">filesystem</span> <span class="string">access</span> <span class="string">check.</span>  <span class="string">Use</span> <span class="string">the</span> <span class="string">other</span> <span class="string">options</span> <span class="string">to</span> <span class="string">create</span></span><br><span class="line">      <span class="string">the</span> <span class="string">snapshot.</span></span><br><span class="line">    <span class="attr">options:</span></span><br><span class="line">      <span class="attr">repository:</span> <span class="string">snapshot-s3-index-A</span></span><br><span class="line">      <span class="comment"># Leaving name blank will result in the default &#x27;curator-%Y%m%d%H%M&#x27;</span></span><br><span class="line">      <span class="attr">name:</span> <span class="string">company-index-A-%Y%m%d%H%M</span></span><br><span class="line">      <span class="attr">ignore_unavailable:</span> <span class="literal">False</span></span><br><span class="line">      <span class="attr">include_global_state:</span> <span class="literal">False</span></span><br><span class="line">      <span class="attr">partial:</span> <span class="literal">False</span></span><br><span class="line">      <span class="attr">wait_for_completion:</span> <span class="literal">True</span></span><br><span class="line">      <span class="attr">skip_repo_fs_check:</span> <span class="literal">True</span></span><br><span class="line">      <span class="attr">disable_action:</span> <span class="literal">False</span></span><br><span class="line">    <span class="attr">filters:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="attr">filtertype:</span> <span class="string">pattern</span></span><br><span class="line">        <span class="attr">kind:</span> <span class="string">prefix</span></span><br><span class="line">        <span class="attr">value:</span> <span class="string">company-index-A</span></span><br><span class="line">  <span class="attr">2:</span></span><br><span class="line">    <span class="attr">action:</span> <span class="string">delete_snapshots</span></span><br><span class="line">    <span class="attr">description:</span> <span class="string">&gt;-</span></span><br><span class="line">      <span class="string">Delete</span> <span class="string">snapshots</span> <span class="string">from</span> <span class="string">the</span> <span class="string">selected</span> <span class="string">repository</span> <span class="string">older</span> <span class="string">than</span> <span class="number">14</span> <span class="string">days</span></span><br><span class="line">      <span class="string">(based</span> <span class="string">on</span> <span class="string">creation_date),</span> <span class="string">for</span> <span class="string">&#x27;company-index-A-*&#x27;</span> <span class="string">prefixed</span> <span class="string">snapshots.</span></span><br><span class="line">    <span class="attr">options:</span></span><br><span class="line">      <span class="attr">repository:</span> <span class="string">snapshot-s3-index-A</span></span><br><span class="line">      <span class="comment"># Leaving name blank will result in the default &#x27;curator-%Y%m%d%H%M&#x27;</span></span><br><span class="line">      <span class="attr">disable_action:</span> <span class="literal">False</span></span><br><span class="line">      <span class="attr">ignore_empty_list:</span> <span class="literal">True</span></span><br><span class="line">    <span class="attr">filters:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="attr">filtertype:</span> <span class="string">pattern</span></span><br><span class="line">        <span class="attr">kind:</span> <span class="string">prefix</span></span><br><span class="line">        <span class="attr">value:</span> <span class="string">company-index-A</span></span><br><span class="line">      <span class="bullet">-</span> <span class="attr">filtertype:</span> <span class="string">age</span></span><br><span class="line">        <span class="attr">source:</span> <span class="string">creation_date</span></span><br><span class="line">        <span class="attr">direction:</span> <span class="string">older</span></span><br><span class="line">        <span class="attr">unit:</span> <span class="string">days</span></span><br><span class="line">        <span class="attr">unit_count:</span> <span class="number">14</span></span><br></pre></td></tr></table></figure><p><code>actions.yml</code> for <code>restore</code>:</p><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">actions:</span></span><br><span class="line">  <span class="attr">1:</span></span><br><span class="line">    <span class="attr">action:</span> <span class="string">close</span></span><br><span class="line">    <span class="attr">description:</span> <span class="string">&quot;Close selected indices company-index-A before restoring snapshot&quot;</span></span><br><span class="line">    <span class="attr">options:</span></span><br><span class="line">      <span class="attr">continue_if_exception:</span> <span class="literal">True</span></span><br><span class="line">      <span class="attr">ignore_empty_list:</span> <span class="literal">True</span></span><br><span class="line">    <span class="attr">filters:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="attr">filtertype:</span> <span class="string">pattern</span></span><br><span class="line">        <span class="attr">kind:</span> <span class="string">prefix</span></span><br><span class="line">        <span class="attr">value:</span> <span class="string">company-index-A</span></span><br><span class="line">  <span class="attr">2:</span></span><br><span class="line">    <span class="attr">action:</span> <span class="string">restore</span></span><br><span class="line">    <span class="attr">description:</span> <span class="string">&gt;-</span></span><br><span class="line">      <span class="string">Restore</span> <span class="string">all</span> <span class="string">indices</span> <span class="string">in</span> <span class="string">the</span> <span class="string">most</span> <span class="string">recent</span> <span class="string">company-index-A-*</span> <span class="string">snapshot</span> <span class="string">with</span> <span class="string">state</span></span><br><span class="line">      <span class="string">SUCCESS.</span>  <span class="string">Wait</span> <span class="string">for</span> <span class="string">the</span> <span class="string">restore</span> <span class="string">to</span> <span class="string">complete</span> <span class="string">before</span> <span class="string">continuing.</span>  <span class="string">Skip</span></span><br><span class="line">      <span class="string">the</span> <span class="string">repository</span> <span class="string">filesystem</span> <span class="string">access</span> <span class="string">check.</span>  <span class="string">Use</span> <span class="string">the</span> <span class="string">other</span> <span class="string">options</span> <span class="string">to</span> <span class="string">define</span></span><br><span class="line">      <span class="string">the</span> <span class="string">index/shard</span> <span class="string">settings</span> <span class="string">for</span> <span class="string">the</span> <span class="string">restore.</span></span><br><span class="line">    <span class="attr">options:</span></span><br><span class="line">      <span class="attr">repository:</span> <span class="string">snapshot-s3-index-A</span></span><br><span class="line">      <span class="comment"># Leaving name blank will result in restoring the most recent snapshot by age</span></span><br><span class="line">      <span class="attr">name:</span></span><br><span class="line">      <span class="comment"># Leaving indices blank will result in restoring all indices in the snapshot</span></span><br><span class="line">      <span class="attr">indices:</span></span><br><span class="line">      <span class="attr">include_aliases:</span> <span class="literal">False</span></span><br><span class="line">      <span class="attr">ignore_unavailable:</span> <span class="literal">False</span></span><br><span class="line">      <span class="attr">include_global_state:</span> <span class="literal">False</span></span><br><span class="line">      <span class="attr">partial:</span> <span class="literal">False</span></span><br><span class="line">      <span class="attr">extra_settings:</span></span><br><span class="line">        <span class="attr">index_settings:</span></span><br><span class="line">          <span class="attr">number_of_replicas:</span> <span class="number">2</span></span><br><span class="line">      <span class="attr">wait_for_completion:</span> <span class="literal">True</span></span><br><span class="line">      <span class="attr">skip_repo_fs_check:</span> <span class="literal">True</span></span><br><span class="line">      <span class="attr">disable_action:</span> <span class="literal">False</span></span><br><span class="line">    <span class="attr">filters:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="attr">filtertype:</span> <span class="string">pattern</span></span><br><span class="line">        <span class="attr">kind:</span> <span class="string">prefix</span></span><br><span class="line">        <span class="attr">value:</span> <span class="string">company-index-A</span></span><br><span class="line">        <span class="attr">exclude:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="attr">filtertype:</span> <span class="string">state</span></span><br><span class="line">        <span class="attr">state:</span> <span class="string">SUCCESS</span></span><br><span class="line">        <span class="attr">exclude:</span></span><br></pre></td></tr></table></figure><h2 id="docker-curator"><a href="#docker-curator" class="headerlink" title="docker-curator"></a>docker-curator</h2><p>The Docker image for ES Curator (to manage ES <code>snapshots</code>) could be found here - <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2RhdmlkbHUxMDAxL2RvY2tlci1jdXJhdG9y">davidlu1001/docker-curator<i class="fa fa-external-link-alt"></i></span></p><p>This image keeps up to date with curator releases <code>5.8.3</code>. It is also based on minimal alpine image.</p><h3 id="Features"><a href="#Features" class="headerlink" title="Features"></a>Features</h3><ul><li>Upgrade curator to version <code>5.8.3</code></li><li>Add support for snapshot / restore (use <code>curator_cli</code> for single index scenario)</li><li>Add support for snapshot / restore <code>ALL</code> indexes for ES using <code>curator</code> with actions rules. This would be useful when:<ul><li>too many indexes (can not match with <code>prefix / regex</code> pattern) for <code>curator_cli</code></li><li>if use different snapshot repository per index</li><li>for accident recovery scenario to restore ALL indexes</li></ul></li><li>Add <code>DRY_RUN</code> mode</li><li>Rewrite Dockerfile and use <code>alpine</code> to reduce image size (with <code>python3</code>)</li></ul><h3 id="Usage"><a href="#Usage" class="headerlink" title="Usage"></a>Usage</h3><p>Image <code>entrypoint</code> is set to customized script, need to pass paremeters to <code>CMD</code>, can also support override <code>ENV</code></p><p>Default ENV value:</p><figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">TYPE</span>=snapshot</span><br><span class="line"><span class="attr">INDEX_PREFIX</span>=.kibana</span><br><span class="line"><span class="attr">REPO_NAME</span>=snapshot-repo</span><br><span class="line"><span class="attr">DRY_RUN</span>=<span class="literal">True</span></span><br></pre></td></tr></table></figure><p>e.g.</p><figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"># <span class="keyword">Snapshot</span> single <span class="keyword">index</span> <span class="keyword">with</span> DRY_RUN mode, <span class="keyword">and</span> <span class="keyword">delete</span> snapshots <span class="number">14</span> days ago</span><br><span class="line"></span><br><span class="line">docker-compose run <span class="comment">--rm es-curator snapshot .monitoring-es-7-2020.12.04 snapshot-repo True</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># Restore single <span class="keyword">index</span> (<span class="keyword">with</span> latest <span class="keyword">snapshot</span>) <span class="keyword">without</span> DRY_RUN mode</span><br><span class="line"></span><br><span class="line">docker-compose run <span class="comment">--rm es-curator restore .monitoring-es-7-2020.12.04 snapshot-repo False</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># <span class="keyword">Snapshot</span> <span class="keyword">ALL</span> indexes <span class="keyword">for</span> ES <span class="keyword">without</span> DRY_RUN mode, <span class="keyword">and</span> <span class="keyword">delete</span> snapshots <span class="number">14</span> days ago</span><br><span class="line"></span><br><span class="line">docker-compose run <span class="comment">--rm es-curator snapshot ALL snapshot-repo False</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># Restore <span class="keyword">ALL</span> indexes <span class="keyword">for</span> ES (<span class="keyword">with</span> latest <span class="keyword">snapshot</span>) <span class="keyword">without</span> DRY_RUN mode</span><br><span class="line"></span><br><span class="line">docker-compose run <span class="comment">--rm es-curator restore ALL snapshot-repo False</span></span><br></pre></td></tr></table></figure><p>Pass <code>ENV</code>:</p><figure class="highlight d"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">- ELASTICSEARCH_HOST: <span class="keyword">default</span> <span class="keyword">is</span> <span class="string">`elasticsearch`</span></span><br><span class="line"></span><br><span class="line">- UNIT: <span class="keyword">default</span> <span class="keyword">is</span> days, support <span class="string">`seconds | minutes | hours | days | weeks | months | years`</span></span><br><span class="line"></span><br><span class="line">- UNIT_COUNT: <span class="keyword">default</span> <span class="keyword">is</span> <span class="number">14</span></span><br></pre></td></tr></table></figure><p>e.g.</p><figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Snapshot single index without DRY_RUN mode, and delete snapshots older than 1 minutes ago</span></span><br><span class="line"></span><br><span class="line"><span class="attribute">UNIT</span>=minutes <span class="attribute">UNIT_COUNT</span>=1 docker-compose <span class="builtin-name">run</span> --rm es-curator snapshot .monitoring-es-7-2020.12.04 snapshot-repo <span class="literal">False</span></span><br></pre></td></tr></table></figure><h1 id="References"><a href="#References" class="headerlink" title="References"></a>References</h1><ul><li><p><span class="exturl" data-url="aHR0cHM6Ly93d3cuZWxhc3RpYy5jby9ndWlkZS9lbi9lbGFzdGljc2VhcmNoL3JlZmVyZW5jZS9jdXJyZW50L2JhY2t1cC1jbHVzdGVyLmh0bWw=">Elastic - Backup Cluster<i class="fa fa-external-link-alt"></i></span></p></li><li><p><span class="exturl" data-url="aHR0cHM6Ly93d3cuZWxhc3RpYy5jby9ndWlkZS9lbi9lbGFzdGljc2VhcmNoL3BsdWdpbnMvNy4xMC9yZXBvc2l0b3J5LXMzLmh0bWw=">S3 repository plugin<i class="fa fa-external-link-alt"></i></span></p></li><li><p><span class="exturl" data-url="aHR0cHM6Ly93d3cuZWxhc3RpYy5jby9ndWlkZS9lbi9lbGFzdGljc2VhcmNoL3BsdWdpbnMvY3VycmVudC9yZXBvc2l0b3J5LXMzLXJlcG9zaXRvcnkuaHRtbA==">Repository settings<i class="fa fa-external-link-alt"></i></span></p></li><li><p><span class="exturl" data-url="aHR0cHM6Ly93d3cuZWxhc3RpYy5jby9ibG9nL2ZvdW5kLWVsYXN0aWNzZWFyY2gtc25hcHNob3QtYW5kLXJlc3RvcmU=">ES - Snapshot and Restore (incremental snapshot mechanism)<i class="fa fa-external-link-alt"></i></span></p></li><li><p><span class="exturl" data-url="aHR0cHM6Ly93d3cuZWxhc3RpYy5jby9ndWlkZS9lbi9lbGFzdGljc2VhcmNoL2NsaWVudC9jdXJhdG9yLzUuOC9pbmRleC5odG1s">Elastic Curator<i class="fa fa-external-link-alt"></i></span></p></li></ul>]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;TL-DR&quot;&gt;&lt;a href=&quot;#TL-DR&quot; class=&quot;headerlink&quot; title=&quot;TL;DR&quot;&gt;&lt;/a&gt;TL;DR&lt;/h1&gt;&lt;ul&gt;
&lt;li&gt;&lt;p&gt;ElasticSearch Backup (snapshot / restore) on AWS S3&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Steps / Configrations for ES snapshot / restore&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use elastic &lt;code&gt;curator&lt;/code&gt; to manage snapshots (create / remove)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Docker image for ES Curator to manage Elasticsearch snapshots - &lt;a href=&quot;https://github.com/davidlu1001/docker-curator&quot;&gt;davidlu1001/docker-curator&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&quot;Overview&quot;&gt;&lt;a href=&quot;#Overview&quot; class=&quot;headerlink&quot; title=&quot;Overview&quot;&gt;&lt;/a&gt;Overview&lt;/h1&gt;&lt;p&gt;The purpose of this blog is to investigate the possible solutions to backup and restore ES indices. So that in the event of a failure, the cluster data can be quickly restored and minimized the business impact.&lt;/p&gt;
    
    </summary>
    
    
      <category term="SRE" scheme="http://davidlu1001.github.io/categories/SRE/"/>
    
      <category term="ElasticSearch" scheme="http://davidlu1001.github.io/categories/ElasticSearch/"/>
    
      <category term="Curator" scheme="http://davidlu1001.github.io/categories/Curator/"/>
    
    
      <category term="ElasticSearch" scheme="http://davidlu1001.github.io/tags/ElasticSearch/"/>
    
      <category term="SRE" scheme="http://davidlu1001.github.io/tags/SRE/"/>
    
      <category term="Backup" scheme="http://davidlu1001.github.io/tags/Backup/"/>
    
      <category term="DR" scheme="http://davidlu1001.github.io/tags/DR/"/>
    
      <category term="S3" scheme="http://davidlu1001.github.io/tags/S3/"/>
    
      <category term="curator" scheme="http://davidlu1001.github.io/tags/curator/"/>
    
  </entry>
  
  <entry>
    <title>Terraform Upgrade to 0.12</title>
    <link href="http://davidlu1001.github.io/2020/06/22/Terraform-Upgrade-to-0-12/"/>
    <id>http://davidlu1001.github.io/2020/06/22/Terraform-Upgrade-to-0-12/</id>
    <published>2020-06-22T01:29:03.000Z</published>
    <updated>2020-06-22T01:29:03.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Steps"><a href="#Steps" class="headerlink" title="Steps"></a>Steps</h1><h2 id="1-Check-existing-terraform-version"><a href="#1-Check-existing-terraform-version" class="headerlink" title="1. Check existing terraform version"></a>1. Check existing terraform version</h2><figure class="highlight ebnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">terraform -v</span></span><br></pre></td></tr></table></figure><h2 id="2-Upgrade-to-Terraform-0-11-first-if-applicable"><a href="#2-Upgrade-to-Terraform-0-11-first-if-applicable" class="headerlink" title="2. Upgrade to Terraform 0.11 first (if applicable)"></a>2. Upgrade to Terraform 0.11 first (if applicable)</h2><p>If the current version is not <code>0.11</code> then upgrade it to <code>0.11.14</code> first. If you are on 0.11.x, please make sure you are on 0.11.14.</p><h2 id="3-Pre-upgrade-Checklist"><a href="#3-Pre-upgrade-Checklist" class="headerlink" title="3. Pre-upgrade Checklist"></a>3. Pre-upgrade Checklist</h2><p>Terraform <code>v0.11.14</code> introduced a temporary helper command <code>terraform 0.12checklist</code>, which analyses the configuration to detect any required steps that will be easier to perform before upgrading.</p><h2 id="4-Initialisation-in-0-11"><a href="#4-Initialisation-in-0-11" class="headerlink" title="4. Initialisation in 0.11"></a>4. Initialisation in 0.11</h2><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">terraform <span class="keyword">init</span></span><br></pre></td></tr></table></figure><h2 id="5-Plan-and-make-sure-no-errors-are-shown"><a href="#5-Plan-and-make-sure-no-errors-are-shown" class="headerlink" title="5. Plan and make sure no errors are shown"></a>5. <code>Plan</code> and make sure no errors are shown</h2><figure class="highlight ebnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">terraform plan</span></span><br></pre></td></tr></table></figure><h2 id="6-Apply"><a href="#6-Apply" class="headerlink" title="6. Apply"></a>6. <code>Apply</code></h2><p>to ensure that your real infrastructure and Terraform state are consistent with the current configuration.</p><figure class="highlight coq"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">terraform <span class="built_in">apply</span></span><br></pre></td></tr></table></figure><a id="more"></a><h2 id="7-Do-the-checklist-check"><a href="#7-Do-the-checklist-check" class="headerlink" title="7. Do the checklist check"></a>7. Do the <code>checklist</code> check</h2><p>to see if there are any pre-upgrade steps in the checklist</p><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">terraform</span> <span class="number">0</span>.<span class="number">12</span>checklist</span><br></pre></td></tr></table></figure><h2 id="8-Resolve-suggestions-from-checklist"><a href="#8-Resolve-suggestions-from-checklist" class="headerlink" title="8. Resolve suggestions from checklist"></a>8. Resolve suggestions from checklist</h2><p>Depending upon the suggestions above, take the steps &amp; change the tf scripts.</p><p>If you are using templates, then you need to update the template provider as well.</p><p>Run above command until there are no suggestions.</p><h2 id="9-Switch-to-terraform-0-12"><a href="#9-Switch-to-terraform-0-12" class="headerlink" title="9. Switch to terraform 0.12"></a>9. Switch to terraform 0.12</h2><p>Switch to terraform 0.12 (choose one of the following methods, ranked in order of recommendation)</p><ul><li><p>Using <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL3dhcnJlbnNib3gvdGVycmFmb3JtLXN3aXRjaGVy">tfswitch<i class="fa fa-external-link-alt"></i></span> (recommend) / <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL3RmdXRpbHMvdGZlbnY=">tfenv<i class="fa fa-external-link-alt"></i></span> to switch to 0.12</p></li><li><p>Using docker-terraform</p></li><li><p>Unpin the old 0.11 version (if applicable) and upgrade to Terraform 0.12: <code>brew upgrade terraform</code></p></li></ul><h2 id="10-Initialisation-in-0-12"><a href="#10-Initialisation-in-0-12" class="headerlink" title="10. Initialisation in 0.12"></a>10. Initialisation in 0.12</h2><p>For those repos that ref terraform-modules, need to:</p><ul><li><p>Make sure use git module to refer terraform-modules:</p><p>  e.g.</p><figure class="highlight dust"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml">source = &quot;git@github.com:</span><span class="template-variable">&#123;user&#125;</span><span class="xml">/terraform-modules.git//</span><span class="template-variable">&#123;module_name&#125;</span><span class="xml">?ref=</span><span class="template-variable">&#123;branch_name&#125;</span><span class="xml">&quot;</span></span><br></pre></td></tr></table></figure></li><li><p>replace branch name to refer testing branch in <code>terraform-modules</code> (if applicable)</p></li><li><p>update <code>.gitmodules</code> first to use testing branch:</p><p>  e.g.</p><figure class="highlight nix"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">cat .gitmodules</span><br><span class="line"> </span><br><span class="line">[submodule <span class="string">&quot;common/modules&quot;</span>]</span><br><span class="line"> </span><br><span class="line">    <span class="attr">path</span> = common/modules</span><br><span class="line"> </span><br><span class="line">    <span class="attr">url</span> = git@github.com:&#123;user&#125;/terraform-modules.git</span><br><span class="line"> </span><br><span class="line">    <span class="attr">branch</span> = &#123;branch_name&#125;</span><br></pre></td></tr></table></figure></li></ul><p>Then run command to update submodules:</p><figure class="highlight brainfuck"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">git</span> <span class="comment">submodule</span> <span class="comment">update</span> --<span class="comment">recursive</span> --<span class="comment">remote</span></span><br></pre></td></tr></table></figure><blockquote><p>P.S.</p><p>Re-running init with modules already installed will install the sources for any modules that were added to configuration since the last init, but will not change any already-installed modules.</p><p>Use <code>-upgrade</code> to override this behavior, updating all modules to the latest available source code..</p><p>So use this command when there’re changes from terraform-modules:</p></blockquote><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">terraform <span class="keyword">init</span> -upgrade</span><br></pre></td></tr></table></figure><h2 id="11-Check-for-errors"><a href="#11-Check-for-errors" class="headerlink" title="11. Check for errors"></a>11. Check for errors</h2><figure class="highlight ebnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">terraform validate</span></span><br></pre></td></tr></table></figure><h2 id="12-Auto-update-the-code"><a href="#12-Auto-update-the-code" class="headerlink" title="12. Auto update the code"></a>12. Auto update the code</h2><p>with below command. Terraform v0.12 includes a new command <code>terraform 0.12upgrade</code> that will read the configuration files for a module written for Terraform 0.11 and update them in-place to use the cleaner Terraform 0.12 syntax and also adjust for use of features that have changed behaviour in the 0.12 Terraform language.</p><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">terraform</span> <span class="number">0</span>.<span class="number">12</span>upgrade</span><br></pre></td></tr></table></figure><h3 id="12-1-Symlink"><a href="#12-1-Symlink" class="headerlink" title="12.1. Symlink"></a>12.1. Symlink</h3><p>If there is a symlink under the directory, the directory of the actual file pointed by the symlink needs to be upgraded in the last, to avoid the situation where 0.11/0.12 codes coexist. This will terminate the execution of the <code>0.12upgrade</code> command.</p><p>And also need to delete symlink first, before running <code>0.12upgrade</code>, then restore the symlink files.</p><p>The logic for each directory would be something like this:</p><p>e.g.</p><figure class="highlight sqf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"># init</span></span><br><span class="line">terraform init -upgrade</span><br><span class="line"></span><br><span class="line"><span class="meta"># clean</span></span><br><span class="line">rm -rf .terraform</span><br><span class="line"></span><br><span class="line"><span class="meta"># record symlink</span></span><br><span class="line"><span class="built_in">find</span> . -<span class="built_in">type</span> l -<span class="built_in">name</span> <span class="string">&quot;*.tf&quot;</span> -ls | awk -F<span class="string">&#x27; ./&#x27;</span> <span class="string">&#x27;&#123;print $NF&#125;&#x27;</span> | awk -F<span class="string">&#x27; -&gt; &#x27;</span> <span class="string">&#x27;&#123;print &quot;ln -s &quot;$2,$1&#125;&#x27;</span> &gt; /tmp/all_ln.txt</span><br><span class="line"></span><br><span class="line"><span class="meta"># remove</span></span><br><span class="line"><span class="built_in">find</span> . -<span class="built_in">type</span> l -<span class="built_in">name</span> <span class="string">&quot;*.tf&quot;</span> | sed -e <span class="string">&quot;s#\./##g&quot;</span> | xargs <span class="built_in">echo</span> <span class="string">&quot;rm&quot;</span> | bash</span><br><span class="line"></span><br><span class="line"><span class="meta"># upgrade</span></span><br><span class="line">terraform <span class="number">0.12</span>upgrade -yes</span><br><span class="line"></span><br><span class="line"><span class="meta"># restore symlink</span></span><br><span class="line">cat /tmp/all_ln.txt | bash</span><br><span class="line"></span><br><span class="line"><span class="meta"># plan</span></span><br><span class="line">terraform plan</span><br></pre></td></tr></table></figure><h3 id="12-2-Error-on-batch-upgrade"><a href="#12-2-Error-on-batch-upgrade" class="headerlink" title="12.2. Error on batch upgrade"></a>12.2. Error on batch upgrade</h3><p>According to the official doc, run the following command for batch upgrade:</p><figure class="highlight perl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">gfind . -name <span class="string">&#x27;*.tf&#x27;</span> -<span class="keyword">printf</span> <span class="string">&quot;%h\n&quot;</span> | <span class="keyword">sort</span> | uni<span class="string">q | xargs -n1 terraform init</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">gfind . -name &#x27;*.tf&#x27; -printf &quot;%h\n&quot; |</span> <span class="keyword">sort</span> | uni<span class="string">q | xargs -n1 terraform 0.12upgrade -yes</span></span><br></pre></td></tr></table></figure><p>but got bunch of errors:</p><figure class="highlight subunit"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">Error: </span>error resolving providers:</span><br><span class="line"></span><br><span class="line">- provider.template: no suitable version installed</span><br><span class="line">  version requirements: &quot;(any version)&quot;</span><br><span class="line">  versions installed: none</span><br></pre></td></tr></table></figure><p>Turned out the init command will <strong><em>overwrite</em></strong> the <code>.terraform/plugins</code> dir each time:</p><p>e.g.</p><p>First init module <code>./A</code>:</p><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">ls</span> -rthl .terraform/plugins/darwin_amd<span class="number">64</span>/</span><br><span class="line"><span class="attribute">lock</span>.json*                              terraform-provider-aws_v<span class="number">2</span>.<span class="number">63</span>.<span class="number">0</span>_x<span class="number">4</span>*      terraform-provider-template_v<span class="number">2</span>.<span class="number">1</span>.<span class="number">2</span>_x<span class="number">4</span>*</span><br></pre></td></tr></table></figure><p>Then init module <code>./B</code>:</p><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">ls</span> -rthl .terraform/plugins/darwin_amd<span class="number">64</span>/</span><br><span class="line"><span class="attribute">lock</span>.json*                          terraform-provider-aws_v<span class="number">2</span>.<span class="number">63</span>.<span class="number">0</span>_x<span class="number">4</span>*</span><br></pre></td></tr></table></figure><p>So need to create script to process each directory one by one (first <code>init</code> then <code>upgrade</code>) instead of using <code>xargs</code> for batch upgrade.</p><h2 id="13-Fix-syntax-issues"><a href="#13-Fix-syntax-issues" class="headerlink" title="13. Fix syntax issues"></a>13. Fix syntax issues</h2><p>Might need to manual fix syntax issues (that can not be auto-upgraded):</p><ul><li>Invalid expression value: number required</li></ul><figure class="highlight subunit"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">Error: </span>Incorrect value type</span><br><span class="line"> </span><br><span class="line">  on .terraform/modules/service-crayola/service/iam_db_auth.tf line 3, in data &quot;template_file&quot; &quot;iam-db-auth-policy&quot;:</span><br><span class="line">   3:   count    = var.enable_iam_db_auth</span><br><span class="line"> </span><br><span class="line">Invalid expression value: number required.</span><br></pre></td></tr></table></figure><p>Use a conditional expression to select the count based on the variable:</p><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">count</span> = var.enable_iam_db_auth ? <span class="number">1</span> : <span class="number">0</span></span><br></pre></td></tr></table></figure><ul><li>Incorrect attribute value type</li></ul><figure class="highlight vhdl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="literal">Error</span>: Incorrect <span class="keyword">attribute</span> value <span class="keyword">type</span></span><br><span class="line"> </span><br><span class="line">  <span class="keyword">on</span> .terraform/modules/container-crayola/container/main.tf <span class="literal">line</span> <span class="number">38</span>, <span class="keyword">in</span> resource <span class="string">&quot;aws_iam_policy_attachment&quot;</span> <span class="string">&quot;ci-attach&quot;</span>:</span><br><span class="line">  <span class="number">38</span>:   users      = [var.username, var.extra_ci_users]</span><br><span class="line">    |<span class="comment">----------------</span></span><br><span class="line">    | var.extra_ci_users <span class="keyword">is</span> empty tuple</span><br><span class="line">    | var.username <span class="keyword">is</span> <span class="string">&quot;ci-travis-crayola&quot;</span></span><br><span class="line"> </span><br><span class="line">Inappropriate value <span class="keyword">for</span> <span class="keyword">attribute</span> <span class="string">&quot;users&quot;</span>: element <span class="number">1</span>: <span class="built_in">string</span> required.</span><br></pre></td></tr></table></figure><p>Referring to <span class="exturl" data-url="aHR0cHM6Ly93d3cudGVycmFmb3JtLmlvL3VwZ3JhZGUtZ3VpZGVzLzAtMTIuaHRtbCNyZWZlcnJpbmctdG8tbGlzdC12YXJpYWJsZXM=">https://www.terraform.io/upgrade-guides/0-12.html#referring-to-list-variables<i class="fa fa-external-link-alt"></i></span></p><p>The solution is just <code>remove the redundant list brackets</code> or add a <code>flatten</code> function.</p><ul><li>Unsupported attribute for lifecycle/ignore_changes</li></ul><figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">Error: Unsupported <span class="keyword">attribute</span></span><br><span class="line"> </span><br><span class="line">  <span class="keyword">on</span> .terraform/modules/balances-api_alb_target_group/alb-target-<span class="keyword">group</span>/target-<span class="keyword">group</span>.tf <span class="type">line</span> <span class="number">46</span>, <span class="keyword">in</span> resource &quot;aws_alb_target_group&quot; &quot;main&quot;:</span><br><span class="line">  <span class="number">46</span>:       healthy_threshold,</span><br><span class="line"> </span><br><span class="line">This <span class="keyword">object</span> has <span class="keyword">no</span> argument, nested block, <span class="keyword">or</span> exported <span class="keyword">attribute</span> named</span><br><span class="line">&quot;healthy_threshold&quot;.</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">Error: Unsupported <span class="keyword">attribute</span></span><br><span class="line"> </span><br><span class="line">  <span class="keyword">on</span> .terraform/modules/balances-api_task_definition/task-def-volumeless/main.tf <span class="type">line</span> <span class="number">8</span>, <span class="keyword">in</span> resource &quot;aws_ecs_task_definition&quot; &quot;main&quot;:</span><br><span class="line">   <span class="number">8</span>:     ignore_changes        = [image]</span><br><span class="line"> </span><br><span class="line">This <span class="keyword">object</span> has <span class="keyword">no</span> argument, nested block, <span class="keyword">or</span> exported <span class="keyword">attribute</span> named</span><br><span class="line">&quot;image&quot;.</span><br></pre></td></tr></table></figure><p>Ref: <span class="exturl" data-url="aHR0cHM6Ly93d3cudGVycmFmb3JtLmlvL2RvY3MvY29uZmlndXJhdGlvbi9yZXNvdXJjZXMuaHRtbCNsaWZlY3ljbGUtbGlmZWN5Y2xlLWN1c3RvbWl6YXRpb25z">https://www.terraform.io/docs/configuration/resources.html#lifecycle-lifecycle-customizations<i class="fa fa-external-link-alt"></i></span></p><p><code>ignore_changes</code> can only support list of attribute names, but <code>image</code> is the sub-attribute name for <code>container_definitions</code></p><ul><li>Deperacated condition for lb_listener_rule</li></ul><figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Warning</span>: &quot;condition.0.values&quot;: [DEPRECATED] use <span class="string">&#x27;host_header&#x27;</span> <span class="keyword">or</span> <span class="string">&#x27;path_pattern&#x27;</span> <span class="keyword">attribute</span> <span class="keyword">instead</span></span><br><span class="line"> </span><br><span class="line">  <span class="keyword">on</span> crafty-microservice-main.tf <span class="type">line</span> <span class="number">30</span>, <span class="keyword">in</span> resource &quot;aws_lb_listener_rule&quot; &quot;crafty_rule_0&quot;:</span><br><span class="line">  <span class="number">30</span>: resource &quot;aws_lb_listener_rule&quot; &quot;crafty_rule_0&quot; &#123;</span><br><span class="line"> </span><br><span class="line">(<span class="keyword">and</span> one more <span class="keyword">similar</span> <span class="built_in">warning</span> elsewhere)</span><br><span class="line"> </span><br><span class="line"><span class="built_in">Warning</span>: &quot;condition.0.field&quot;: [DEPRECATED] use <span class="string">&#x27;host_header&#x27;</span> <span class="keyword">or</span> <span class="string">&#x27;path_pattern&#x27;</span> <span class="keyword">attribute</span> <span class="keyword">instead</span></span><br><span class="line"> </span><br><span class="line">  <span class="keyword">on</span> crafty-microservice-main.tf <span class="type">line</span> <span class="number">30</span>, <span class="keyword">in</span> resource &quot;aws_lb_listener_rule&quot; &quot;crafty_rule_0&quot;:</span><br><span class="line">  <span class="number">30</span>: resource &quot;aws_lb_listener_rule&quot; &quot;crafty_rule_0&quot; &#123;</span><br><span class="line"> </span><br><span class="line">(<span class="keyword">and</span> one more <span class="keyword">similar</span> <span class="built_in">warning</span> elsewhere)</span><br></pre></td></tr></table></figure><p>Just need to switch the format as follows:</p><p><strong>From</strong>:</p><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">condition</span> &#123;</span><br><span class="line">  <span class="attribute">field</span>  = <span class="string">&quot;path-pattern&quot;</span></span><br><span class="line">  values = [<span class="string">&quot;*&quot;</span>]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>To</strong>: </p><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">condition</span> &#123;</span><br><span class="line">  <span class="section">path_pattern</span> &#123;</span><br><span class="line">    <span class="attribute">values</span> = [<span class="string">&quot;*&quot;</span>]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>From</strong>:</p><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">condition</span> &#123;</span><br><span class="line">  <span class="attribute">field</span>  = <span class="string">&quot;host-header&quot;</span></span><br><span class="line">  values = [<span class="string">&quot;abc.hq.com&quot;</span>]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>To</strong>: </p><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">condition</span> &#123;</span><br><span class="line">  <span class="section">host_header</span> &#123;</span><br><span class="line">    <span class="attribute">values</span> = [<span class="string">&quot;abc.hq.com&quot;</span>]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>This was a change introduced in terraform-providers/terraform-provider-aws#8268, which was released in AWS Provider 2.42.0.</p><p>Ref: <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2JyaWtpczk4L3RlcnJhZm9ybS11cC1hbmQtcnVubmluZy1jb2RlL2lzc3Vlcy80MyNpc3N1ZWNvbW1lbnQtNTY5NDQxNzc3">https://github.com/brikis98/terraform-up-and-running-code/issues/43#issuecomment-569441777<i class="fa fa-external-link-alt"></i></span></p><p>Doc: <span class="exturl" data-url="aHR0cHM6Ly93d3cudGVycmFmb3JtLmlvL2RvY3MvcHJvdmlkZXJzL2F3cy9yL2xiX2xpc3RlbmVyX3J1bGUuaHRtbA==">https://www.terraform.io/docs/providers/aws/r/lb_listener_rule.html<i class="fa fa-external-link-alt"></i></span></p><ul><li>After fix<br>After fix syntax issues in terraform-modules, need to update modules and re-run init / upgrade when necessary:</li></ul><figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"># git <span class="keyword">update</span> submodules</span><br><span class="line">git submodule <span class="keyword">update</span> <span class="comment">--recursive --remote</span></span><br><span class="line"> </span><br><span class="line"># upgrade modules <span class="keyword">to</span> <span class="keyword">local</span> .terraform/modules</span><br><span class="line">terraform init -upgrade</span><br></pre></td></tr></table></figure><h2 id="14-Plan"><a href="#14-Plan" class="headerlink" title="14. Plan"></a>14. Plan</h2><figure class="highlight ebnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">terraform plan</span></span><br></pre></td></tr></table></figure><p>Be aware of using correct aws role to execute plan / apply for different directories.</p><p>And might also need to fix syntax errors manually for those files can not be upgraded automatically (e.g. templates)</p><h2 id="15-Apply"><a href="#15-Apply" class="headerlink" title="15. Apply"></a>15. Apply</h2><figure class="highlight coq"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">terraform <span class="built_in">apply</span></span><br></pre></td></tr></table></figure><h2 id="16-Post-Actions"><a href="#16-Post-Actions" class="headerlink" title="16.Post Actions"></a>16.Post Actions</h2><h3 id="IDE-support-VSCode"><a href="#IDE-support-VSCode" class="headerlink" title="IDE support (VSCode)"></a>IDE support (VSCode)</h3><p>When using vscode extension for Terraform (<code>v1.x</code>), it might show syntax error as follow:</p><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">Unknown</span> token: <span class="number">2</span>:<span class="number">26</span> IDENT var.cidr</span><br><span class="line"> </span><br><span class="line"><span class="attribute">Peek</span> Problem (⌥F<span class="number">8</span>)</span><br><span class="line"><span class="attribute">No</span> quick fixes available</span><br></pre></td></tr></table></figure><p>Need to add the following configs in VSCode <code>settings.json</code> to enable support for terraform 0.12</p><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">&quot;terraform.languageServer&quot;:</span> &#123;</span><br><span class="line">        <span class="attr">&quot;enabled&quot;:</span> <span class="literal">true</span>,</span><br><span class="line">        <span class="attr">&quot;args&quot;:</span> []</span><br><span class="line">    &#125;<span class="string">,</span></span><br><span class="line"><span class="attr">&quot;terraform.indexing&quot;:</span> &#123;</span><br><span class="line">    <span class="attr">&quot;enabled&quot;:</span> <span class="literal">false</span>,</span><br><span class="line">    <span class="attr">&quot;liveIndexing&quot;:</span> <span class="literal">false</span></span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure><p>Unfortunately after installing the latest VSCdoe extension (e.g. <code>2.0.1</code> - which officially support Terraform 0.12) it seems NOT working properly.</p><p>Found bunch of errors:</p><ul><li><span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2hhc2hpY29ycC92c2NvZGUtdGVycmFmb3JtL2lzc3Vlcy8zMjk=">Workspace not initialized<i class="fa fa-external-link-alt"></i></span></li><li><span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2hhc2hpY29ycC92c2NvZGUtdGVycmFmb3JtL2lzc3Vlcy8zODY=">Nothing works after update to 2.x<i class="fa fa-external-link-alt"></i></span></li><li><span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL21pY3Jvc29mdC92c2NvZGUvaXNzdWVzLzk5Njk5">“Server return 404” when trying to install an old version<i class="fa fa-external-link-alt"></i></span></li></ul><p>The old version 1.40 used to work, so the <strong><em>workaround</em></strong>:</p><ul><li>Remove 2.0.1 extension from VS Code<br>Download 1.4.0 of the vsix from <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2hhc2hpY29ycC92c2NvZGUtdGVycmFmb3JtL3JlbGVhc2VzL2Rvd25sb2FkL3YxLjQuMC9tYXV2ZS50ZXJyYWZvcm0tdjEuNC4wLnZzaXg=">here<i class="fa fa-external-link-alt"></i></span></li><li>Manually install it: <code>code --install-extension mauve.terraform-v1.4.0.vsix</code></li><li>Restart VS Code</li><li>When prompted install language server, choose: <code>v.0.0.11-beta2</code></li><li>If no prompt can install manually: <code>⇧⌘P</code>, then type <code>Terraform: Enable/Disable Language Server</code> and install <code>v.0.0.11-beta2</code></li></ul><h2 id="Reference"><a href="#Reference" class="headerlink" title="Reference"></a>Reference</h2><ul><li><span class="exturl" data-url="aHR0cHM6Ly93d3cudGVycmFmb3JtLmlvL3VwZ3JhZGUtZ3VpZGVzLzAtMTIuaHRtbA==">Upgrade Guilds<i class="fa fa-external-link-alt"></i></span></li><li><span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2hhc2hpY29ycC90ZXJyYWZvcm0tZ3VpZGVzL3RyZWUvbWFzdGVyL2luZnJhc3RydWN0dXJlLWFzLWNvZGUvdGVycmFmb3JtLTAuMTItZXhhbXBsZXM=">Useful 0.12 code examples<i class="fa fa-external-link-alt"></i></span></li><li><span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2hhc2hpY29ycC92c2NvZGUtdGVycmFmb3JtL2lzc3Vlcy8xOTUjaXNzdWVjb21tZW50LTU3ODE5NDE2MA==">Terraform v0.12 - Syntax check broken: Unknown token IDENT<i class="fa fa-external-link-alt"></i></span></li><li>Fix syntax issues<ul><li><span class="exturl" data-url="aHR0cHM6Ly9kaXNjdXNzLmhhc2hpY29ycC5jb20vdC9zeW50YXgtaXNzdWVzLXdoaWxlLXVwZ3JhZGluZy1mcm9tLTExLTE0LXRvLXZlcnNpb24tMTIvNDM1Ng==">syntax issue while upgrading from 11 to 12<i class="fa fa-external-link-alt"></i></span></li><li><span class="exturl" data-url="aHR0cHM6Ly93d3cudGVycmFmb3JtLmlvL3VwZ3JhZGUtZ3VpZGVzLzAtMTIuaHRtbCNtYXAtdmFyaWFibGVzLW5vLWxvbmdlci1tZXJnZS13aGVuLW92ZXJyaWRkZW4=">map var no longer merge when overriden<i class="fa fa-external-link-alt"></i></span></li><li><span class="exturl" data-url="aHR0cHM6Ly93d3cudGVycmFmb3JtLmlvL3VwZ3JhZGUtZ3VpZGVzLzAtMTIuaHRtbCNyZWZlcnJpbmctdG8tbGlzdC12YXJpYWJsZXM=">refering to list var<i class="fa fa-external-link-alt"></i></span></li></ul></li></ul>]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;Steps&quot;&gt;&lt;a href=&quot;#Steps&quot; class=&quot;headerlink&quot; title=&quot;Steps&quot;&gt;&lt;/a&gt;Steps&lt;/h1&gt;&lt;h2 id=&quot;1-Check-existing-terraform-version&quot;&gt;&lt;a href=&quot;#1-Check-existing-terraform-version&quot; class=&quot;headerlink&quot; title=&quot;1. Check existing terraform version&quot;&gt;&lt;/a&gt;1. Check existing terraform version&lt;/h2&gt;&lt;figure class=&quot;highlight ebnf&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;1&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;attribute&quot;&gt;terraform -v&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;

&lt;h2 id=&quot;2-Upgrade-to-Terraform-0-11-first-if-applicable&quot;&gt;&lt;a href=&quot;#2-Upgrade-to-Terraform-0-11-first-if-applicable&quot; class=&quot;headerlink&quot; title=&quot;2. Upgrade to Terraform 0.11 first (if applicable)&quot;&gt;&lt;/a&gt;2. Upgrade to Terraform 0.11 first (if applicable)&lt;/h2&gt;&lt;p&gt;If the current version is not &lt;code&gt;0.11&lt;/code&gt; then upgrade it to &lt;code&gt;0.11.14&lt;/code&gt; first. If you are on 0.11.x, please make sure you are on 0.11.14.&lt;/p&gt;
&lt;h2 id=&quot;3-Pre-upgrade-Checklist&quot;&gt;&lt;a href=&quot;#3-Pre-upgrade-Checklist&quot; class=&quot;headerlink&quot; title=&quot;3. Pre-upgrade Checklist&quot;&gt;&lt;/a&gt;3. Pre-upgrade Checklist&lt;/h2&gt;&lt;p&gt;Terraform &lt;code&gt;v0.11.14&lt;/code&gt; introduced a temporary helper command &lt;code&gt;terraform 0.12checklist&lt;/code&gt;, which analyses the configuration to detect any required steps that will be easier to perform before upgrading.&lt;/p&gt;
&lt;h2 id=&quot;4-Initialisation-in-0-11&quot;&gt;&lt;a href=&quot;#4-Initialisation-in-0-11&quot; class=&quot;headerlink&quot; title=&quot;4. Initialisation in 0.11&quot;&gt;&lt;/a&gt;4. Initialisation in 0.11&lt;/h2&gt;&lt;figure class=&quot;highlight csharp&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;1&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;terraform &lt;span class=&quot;keyword&quot;&gt;init&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;

&lt;h2 id=&quot;5-Plan-and-make-sure-no-errors-are-shown&quot;&gt;&lt;a href=&quot;#5-Plan-and-make-sure-no-errors-are-shown&quot; class=&quot;headerlink&quot; title=&quot;5. Plan and make sure no errors are shown&quot;&gt;&lt;/a&gt;5. &lt;code&gt;Plan&lt;/code&gt; and make sure no errors are shown&lt;/h2&gt;&lt;figure class=&quot;highlight ebnf&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;1&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;attribute&quot;&gt;terraform plan&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;

&lt;h2 id=&quot;6-Apply&quot;&gt;&lt;a href=&quot;#6-Apply&quot; class=&quot;headerlink&quot; title=&quot;6. Apply&quot;&gt;&lt;/a&gt;6. &lt;code&gt;Apply&lt;/code&gt;&lt;/h2&gt;&lt;p&gt;to ensure that your real infrastructure and Terraform state are consistent with the current configuration.&lt;/p&gt;
&lt;figure class=&quot;highlight coq&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;1&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;terraform &lt;span class=&quot;built_in&quot;&gt;apply&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;
    
    </summary>
    
    
      <category term="DevOps" scheme="http://davidlu1001.github.io/categories/DevOps/"/>
    
      <category term="SRE" scheme="http://davidlu1001.github.io/categories/SRE/"/>
    
      <category term="Terraform" scheme="http://davidlu1001.github.io/categories/Terraform/"/>
    
    
      <category term="AWS" scheme="http://davidlu1001.github.io/tags/AWS/"/>
    
      <category term="DevOps" scheme="http://davidlu1001.github.io/tags/DevOps/"/>
    
      <category term="SRE" scheme="http://davidlu1001.github.io/tags/SRE/"/>
    
      <category term="Terraform" scheme="http://davidlu1001.github.io/tags/Terraform/"/>
    
  </entry>
  
  <entry>
    <title>AWS Savings Plans Overview</title>
    <link href="http://davidlu1001.github.io/2020/04/28/AWS-Savings-Plans/"/>
    <id>http://davidlu1001.github.io/2020/04/28/AWS-Savings-Plans/</id>
    <published>2020-04-28T07:57:35.000Z</published>
    <updated>2020-04-28T07:57:35.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="What-are-AWS-Savings-Plans"><a href="#What-are-AWS-Savings-Plans" class="headerlink" title="What are AWS Savings Plans"></a>What are AWS Savings Plans</h1><p>AWS Savings Plans is launched in November 2019, and allows customers to save up to 72% on Amazon EC2 / AWS Fargate in exchange for making a commitment (on how much they will spend per hour) to a consistent amount of compute usage for a 1 or 3-year term. </p><p>Customers can choose how much they wish to commit to (minimum <code>$0.001</code> per hour) and layer Savings Plans on top of one another.</p><p>The major difference between Reserved Instances and AWS Savings Plans are that, rather than committing to a specific instance type in return for a discount, you are committing to a specific spend per hour.</p><blockquote><p><strong><em>It’s important to note that at this time, AWS doesn’t allow customers to change their Savings Plans contract once purchased or sell unused discounts in the AWS Marketplace. Once customers commit to a Savings Plan price, they are locked in for the one or three years they committed to.</em></strong> </p></blockquote><p>There are two types of AWS Savings Plans:</p><ul><li><p><strong>EC2 Instance Saving Plans</strong>: like Standard RIs</p></li><li><p><strong>Compute Savings Plans</strong>: shares attributes with Convertible RIs with the added bonus that discounts can be applied to the Fargate container service.</p></li></ul><p>These two types provide the choice between maximising financial benefit and sacrificing flexibility or maximising flexibility while benefiting from a smaller discount. </p><p>Here’s a quick comparison of the two types:</p><p><img data-src="https://raw.githubusercontent.com/davidlu1001/davidlu1001.github.io/hexo/uPic/Gx8Hj0.png" alt=""></p><a id="more"></a><h2 id="EC2-Instance-Savings-Plans"><a href="#EC2-Instance-Savings-Plans" class="headerlink" title="EC2 Instance Savings Plans"></a>EC2 Instance Savings Plans</h2><p>The main differences between EC2 Instance Saving Plans and Standard RIs is that Savings Plan discounts are applied automatically to any EC2 instance (within the same Family and Region) regardless of the Operating System or Tenancy.</p><p>At this stage it’s important to note AWS Savings Plans cannot yet be applied to RDS instances, AWS Redshift, or ElastiCache services. Customers using these services will have to continue using Reserved Instances.</p><h2 id="Compute-Savings-Plans"><a href="#Compute-Savings-Plans" class="headerlink" title="Compute Savings Plans"></a>Compute Savings Plans</h2><p>Commit to and pay for these in the same way as EC2 Instance Savings Plans, but can only get discounts equal to those offered by Convertible Reserved Instances.</p><p>The big selling point for Compute Savings Plans is that the discounts are automatically applied to EC2 instances of any <strong>Family</strong>, Size, <strong>AZ</strong>, <strong>Region</strong>, OS or Tenancy, and also apply to Fargate and Lambda usage.</p><p>A diagram says more than a thousand words.</p><p><img data-src="https://raw.githubusercontent.com/davidlu1001/davidlu1001.github.io/hexo/uPic/1Zwfom.jpg" alt=""></p><h1 id="How-it-works"><a href="#How-it-works" class="headerlink" title="How it works"></a>How it works</h1><p><img data-src="https://raw.githubusercontent.com/davidlu1001/davidlu1001.github.io/hexo/uPic/GhwBkA.jpg" alt=""></p><p><img data-src="https://raw.githubusercontent.com/davidlu1001/davidlu1001.github.io/hexo/uPic/qkNOIj.jpg" alt=""></p><h1 id="Findings"><a href="#Findings" class="headerlink" title="Findings"></a>Findings</h1><ul><li><p>There is no direct financial benefit of purchasing AWS Savings Plans to replace fully-optimized and effectively-managed RIs when reservations expire. However, it is anticipated many customers will opt for the new discount program due to its increased flexibility and reduced management overhead.</p></li><li><p>Similarly to Reservations, unused discounts do not accumulate. Therefore it’s critical that need to keep track of any Savings Plan waste in order to minimize it in the future.</p></li><li><p>In order to calculate whether or not layering a Savings Plan on top of an RI is financially viable, it’s important to have complete visibility of utilisation metrics (or accurately forecast demand) to avoid wasted Savings Plans discounts.</p></li><li><p>Every hour, AWS will assess the possible discount programs that usage qualifies for, and will apply them in the following order: Standard Reservation, Convertible Reservation, Savings Plan.</p></li></ul><p>So we can choose:</p><ul><li><p>either use the recommendations directly from AWS Cost Exploreer (e.g, a commitment of $6.80/hour)</p></li><li><p>or as the minimum commitment is <code>$0.001</code> per hour. This means only have to spend $8.76 per year ($0.001 x 24 hours x 365 days) in order to qualify for a discount - which might be safer? And if that works as expected then can add another AWS Savings Plans on top of it (due to the discount products applied in the following order: RIs -&gt; Savings Plans -&gt; On Demand).</p></li></ul><h1 id="FAQ"><a href="#FAQ" class="headerlink" title="FAQ"></a>FAQ</h1><blockquote><p><strong>Can I continue to purchase EC2 RIs?</strong></p><p>Yes. You can continue purchasing RIs to maintain compatibility with your existing cost management processes, and your RIs will work along-side Savings Plans to reduce your overall bill. However as your RIs expire we encourage you to sign up for Savings Plans as they offer the same savings as RIs, but with additional flexibility.</p><p><strong>Do I have to choose between the two types of savings plans?</strong></p><p>In the same way as customers can purchase both Standard and Convertible RIs, it’s possible to purchase both EC2 Instance Savings Plans and Compute Savings Plans.</p><p><strong>What happens if I don’t spend my minimum monetary commitment?</strong></p><p>If you don’t meet the minimum monetary commitment in any given hour, that Savings Plan benefit is forfeited. For example, if you commit to $10 per hour, but only consumes $8 of services in any hour, you’ll lose the remaining $2. However, if you’re using linked or consolidated accounts, it’s likely that the unused Savings Plan commitment would have “floated” to benefit another account.</p></blockquote><h1 id="References"><a href="#References" class="headerlink" title="References"></a>References</h1><ul><li><p><span class="exturl" data-url="aHR0cHM6Ly9hd3MuYW1hem9uLmNvbS9ibG9ncy9hd3MvbmV3LXNhdmluZ3MtcGxhbnMtZm9yLWF3cy1jb21wdXRlLXNlcnZpY2VzLw==">AWS - New Savings Plans for AWS compute services<i class="fa fa-external-link-alt"></i></span></p></li><li><p><span class="exturl" data-url="aHR0cHM6Ly9hd3MuYW1hem9uLmNvbS9zYXZpbmdzcGxhbnMvcHJpY2luZy8=">AWS Savings Plans - Pricing<i class="fa fa-external-link-alt"></i></span></p></li><li><p><span class="exturl" data-url="aHR0cHM6Ly9hd3MuYW1hem9uLmNvbS9zYXZpbmdzcGxhbnMvZmFxLw==">AWS Savings Plans - FAQ<i class="fa fa-external-link-alt"></i></span></p></li><li><p><span class="exturl" data-url="aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj11UTlyeS05dVV2byZ0PTE4Mjlz">AWS re:Invent 2019: Dive deep on how to save with AWS Savings Plans<i class="fa fa-external-link-alt"></i></span></p></li></ul>]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;What-are-AWS-Savings-Plans&quot;&gt;&lt;a href=&quot;#What-are-AWS-Savings-Plans&quot; class=&quot;headerlink&quot; title=&quot;What are AWS Savings Plans&quot;&gt;&lt;/a&gt;What are AWS Savings Plans&lt;/h1&gt;&lt;p&gt;AWS Savings Plans is launched in November 2019, and allows customers to save up to 72% on Amazon EC2 / AWS Fargate in exchange for making a commitment (on how much they will spend per hour) to a consistent amount of compute usage for a 1 or 3-year term. &lt;/p&gt;
&lt;p&gt;Customers can choose how much they wish to commit to (minimum &lt;code&gt;$0.001&lt;/code&gt; per hour) and layer Savings Plans on top of one another.&lt;/p&gt;
&lt;p&gt;The major difference between Reserved Instances and AWS Savings Plans are that, rather than committing to a specific instance type in return for a discount, you are committing to a specific spend per hour.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;It’s important to note that at this time, AWS doesn’t allow customers to change their Savings Plans contract once purchased or sell unused discounts in the AWS Marketplace. Once customers commit to a Savings Plan price, they are locked in for the one or three years they committed to.&lt;/em&gt;&lt;/strong&gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;There are two types of AWS Savings Plans:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;EC2 Instance Saving Plans&lt;/strong&gt;: like Standard RIs&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Compute Savings Plans&lt;/strong&gt;: shares attributes with Convertible RIs with the added bonus that discounts can be applied to the Fargate container service.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These two types provide the choice between maximising financial benefit and sacrificing flexibility or maximising flexibility while benefiting from a smaller discount. &lt;/p&gt;
&lt;p&gt;Here’s a quick comparison of the two types:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/davidlu1001/davidlu1001.github.io/hexo/uPic/Gx8Hj0.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
    
    </summary>
    
    
      <category term="AWS" scheme="http://davidlu1001.github.io/categories/AWS/"/>
    
      <category term="SRE" scheme="http://davidlu1001.github.io/categories/SRE/"/>
    
      <category term="Cost" scheme="http://davidlu1001.github.io/categories/Cost/"/>
    
    
      <category term="AWS" scheme="http://davidlu1001.github.io/tags/AWS/"/>
    
      <category term="Savings Plan" scheme="http://davidlu1001.github.io/tags/Savings-Plan/"/>
    
      <category term="Reserved Instances" scheme="http://davidlu1001.github.io/tags/Reserved-Instances/"/>
    
      <category term="SRE" scheme="http://davidlu1001.github.io/tags/SRE/"/>
    
      <category term="Cost Optimization" scheme="http://davidlu1001.github.io/tags/Cost-Optimization/"/>
    
  </entry>
  
  <entry>
    <title>Send metric to StatsD (Datadog)</title>
    <link href="http://davidlu1001.github.io/2020/04/22/Send-metric-to-StatsD-Datadog/"/>
    <id>http://davidlu1001.github.io/2020/04/22/Send-metric-to-StatsD-Datadog/</id>
    <published>2020-04-22T03:10:41.000Z</published>
    <updated>2020-04-22T03:10:41.000Z</updated>
    
    <content type="html"><![CDATA[<p>We can simply send custom metric to Datadog without any of the <span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRhdGFkb2docS5jb20vZGV2ZWxvcGVycy9saWJyYXJpZXMvI2FwaS1hbmQtZG9nc3RhdHNkLWNsaWVudC1saWJyYXJpZXM=">DogStatsD client libraries<i class="fa fa-external-link-alt"></i></span>)</p><p>Generally DogStatsD creates a message that contains information about your metric, event, or service check and sends it to a locally installed Agent as a collector. The destination IP address is <code>127.0.0.1</code> and the collector port over <code>UDP</code> is <code>8125</code>.</p><p>Based on the <span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRhdGFkb2docS5jb20vZGV2ZWxvcGVycy9kb2dzdGF0c2QvZGF0YWdyYW1fc2hlbGwvP3RhYj1tZXRyaWNzI3NlbmRpbmctbWV0cmljcw==">official doc<i class="fa fa-external-link-alt"></i></span> of Datadog, here’s the raw datagram format for metrics, events, and service checks that DogStatsD accepts:</p><figure class="highlight elixir"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;METRIC_NAME&gt;<span class="symbol">:&lt;VALUE&gt;|&lt;TYPE&gt;|</span>@&lt;SAMPLE_RATE&gt;|<span class="comment">#&lt;TAG_KEY_1&gt;:&lt;TAG_VALUE_1&gt;,&lt;TAG_2&gt;</span></span><br></pre></td></tr></table></figure><p>So could use <code>nc</code> and <code>socat</code> on indivial host:</p><p>e.g.</p><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># use nc</span></span><br><span class="line"></span><br><span class="line"><span class="attribute">echo</span> -n <span class="string">&quot;kafka.partition_size:123|g|#topic_name:test,partition_number:1,broker_id:28041,hostname:kafka-12345&quot;</span> | nc -w <span class="number">1</span> -cu localhost <span class="number">8125</span></span><br></pre></td></tr></table></figure><a id="more"></a><p>To speed up the send period, can reduce the <code>-w timeout</code> to <code>0</code> for <code>nc</code> command, but sometimes found that <code>nc -w 0</code> hung, so started to pipe into <code>socat</code> as an alternative:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># use socat</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$&#123;METRICS_NAME&#125;</span>:<span class="variable">$&#123;PARTITION_SIZE&#125;</span>|g|#topic_name:<span class="variable">$&#123;TOPIC_NAME&#125;</span>,partition_number:<span class="variable">$&#123;PARTITION_NUMBER&#125;</span>,broker_id:<span class="variable">$&#123;BROKER_ID&#125;</span>,hostname:<span class="variable">$&#123;HOSTNAME&#125;</span>&quot;</span> | socat -t 0 STDIN UDP:localhost:8125</span><br></pre></td></tr></table></figure><h1 id="Code-example"><a href="#Code-example" class="headerlink" title="Code example"></a>Code example</h1><figure class="highlight reasonml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"># <span class="keyword">function</span> <span class="keyword">to</span> send metric <span class="keyword">with</span> puppet tags</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> send<span class="constructor">_dd_metric()</span> &#123;</span><br><span class="line">    METRICS_NAME=<span class="string">&quot;$1&quot;</span></span><br><span class="line">    value=<span class="string">&quot;$2&quot;</span></span><br><span class="line"></span><br><span class="line">    HOSTNAME=<span class="constructor">$(<span class="params">hostname</span> -<span class="params">f</span>)</span></span><br><span class="line"></span><br><span class="line">    # Pool</span><br><span class="line">    <span class="keyword">if</span> <span class="literal">[[ -<span class="identifier">f</span> <span class="operator">/</span><span class="identifier">etc</span><span class="operator">/</span><span class="identifier">pool</span> ]</span>]; <span class="keyword">then</span></span><br><span class="line">        PUPPET_POOL=<span class="constructor">$(<span class="params">cat</span> <span class="operator">/</span><span class="params">etc</span><span class="operator">/</span><span class="params">pool</span>)</span></span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">        PUPPET_POOL=&#x27;unknown&#x27;</span><br><span class="line">    fi</span><br><span class="line"></span><br><span class="line">    # Role</span><br><span class="line">    <span class="keyword">if</span> <span class="literal">[[ -<span class="identifier">f</span> <span class="operator">/</span><span class="identifier">etc</span><span class="operator">/</span><span class="identifier">role</span> ]</span>]; <span class="keyword">then</span></span><br><span class="line">        PUPPET_ROLE=<span class="constructor">$(<span class="params">cat</span> <span class="operator">/</span><span class="params">etc</span><span class="operator">/</span><span class="params">role</span>)</span></span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">        PUPPET_ROLE=&#x27;unknown&#x27;</span><br><span class="line">    fi</span><br><span class="line"></span><br><span class="line">    # Datacenter</span><br><span class="line">    <span class="keyword">if</span> <span class="literal">[[ -<span class="identifier">f</span> <span class="operator">/</span><span class="identifier">etc</span><span class="operator">/</span><span class="identifier">dc</span> ]</span>]; <span class="keyword">then</span></span><br><span class="line">        PUPPET_DC=<span class="constructor">$(<span class="params">cat</span> <span class="operator">/</span><span class="params">etc</span><span class="operator">/</span><span class="params">dc</span>)</span></span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">        PUPPET_DC=&#x27;unknown&#x27;</span><br><span class="line">    fi</span><br><span class="line"></span><br><span class="line">    AMI_ID=<span class="constructor">$(<span class="params">curl</span> --<span class="params">connect</span>-<span class="params">timeout</span> 2 --<span class="params">max</span>-<span class="params">time</span> 4 --<span class="params">silent</span> <span class="params">http</span>:<span class="operator">/</span><span class="operator">/</span>169.254.169.254<span class="operator">/</span><span class="params">latest</span><span class="operator">/</span><span class="params">meta</span>-<span class="params">data</span><span class="operator">/</span><span class="params">ami</span>-<span class="params">id</span>)</span></span><br><span class="line"></span><br><span class="line">    echo <span class="string">&quot;$&#123;METRICS_NAME&#125;:$&#123;value&#125;|g|host:$&#123;HOSTNAME&#125;,puppet_pool:$&#123;PUPPET_POOL&#125;,puppet_role:$&#123;PUPPET_ROLE&#125;,puppet_dc:$&#123;PUPPET_DC&#125;,ami_id:$&#123;AMI_ID&#125;&quot;</span> <span class="pattern-match">| socat -t 0 <span class="constructor">STDIN</span> <span class="constructor">UDP</span>:localhost:8125</span></span><br><span class="line"><span class="pattern-match">&#125;</span></span><br><span class="line"><span class="pattern-match"></span></span><br><span class="line"><span class="pattern-match"># send metric <span class="keyword">to</span> datadog </span></span><br><span class="line"><span class="pattern-match">send<span class="constructor">_dd_metric</span> &quot;test.service&quot; 1</span></span><br><span class="line"><span class="pattern-match"></span></span><br><span class="line"><span class="pattern-match"># remember also <span class="keyword">to</span> send metric <span class="keyword">when</span> no data</span></span><br><span class="line"><span class="pattern-match">send<span class="constructor">_dd_metric</span> &quot;test.service&quot; 0</span></span><br></pre></td></tr></table></figure><h1 id="Check-from-Datadog"><a href="#Check-from-Datadog" class="headerlink" title="Check from Datadog"></a>Check from Datadog</h1><p>When metric reporting successfully, we can open Datadog console:<br><img data-src="https://raw.githubusercontent.com/davidlu1001/davidlu1001.github.io/hexo/uPic/aNCKGQ.png" alt=""></p><p>and then search for the metric:<br><img data-src="https://raw.githubusercontent.com/davidlu1001/davidlu1001.github.io/hexo/uPic/rBrQpy.png" alt=""></p><p>After that we can also create Dashboard and Monitors (with Datadog API or existing Github repo <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2NvZGVuaXplLXRvb2xzL2Jhcmtkb2c=">barkdog<i class="fa fa-external-link-alt"></i></span>) when necessary.</p><h1 id="Reference"><a href="#Reference" class="headerlink" title="Reference"></a>Reference</h1><ul><li><p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRhdGFkb2docS5jb20vZGV2ZWxvcGVycy9kb2dzdGF0c2QvZGF0YWdyYW1fc2hlbGwvI3NlbmRpbmctbWV0cmljcw==">Datadog - sending-metrics<i class="fa fa-external-link-alt"></i></span></p></li><li><p><span class="exturl" data-url="aHR0cHM6Ly9naXN0LmdpdGh1Yi5jb20vbnN0aWVsYXUvOTY2ODM1">https://gist.github.com/nstielau/966835<i class="fa fa-external-link-alt"></i></span></p></li></ul>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;We can simply send custom metric to Datadog without any of the &lt;a href=&quot;https://docs.datadoghq.com/developers/libraries/#api-and-dogstatsd-client-libraries&quot;&gt;DogStatsD client libraries&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;Generally DogStatsD creates a message that contains information about your metric, event, or service check and sends it to a locally installed Agent as a collector. The destination IP address is &lt;code&gt;127.0.0.1&lt;/code&gt; and the collector port over &lt;code&gt;UDP&lt;/code&gt; is &lt;code&gt;8125&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Based on the &lt;a href=&quot;https://docs.datadoghq.com/developers/dogstatsd/datagram_shell/?tab=metrics#sending-metrics&quot;&gt;official doc&lt;/a&gt; of Datadog, here’s the raw datagram format for metrics, events, and service checks that DogStatsD accepts:&lt;/p&gt;
&lt;figure class=&quot;highlight elixir&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;1&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;&amp;lt;METRIC_NAME&amp;gt;&lt;span class=&quot;symbol&quot;&gt;:&amp;lt;VALUE&amp;gt;|&amp;lt;TYPE&amp;gt;|&lt;/span&gt;@&amp;lt;SAMPLE_RATE&amp;gt;|&lt;span class=&quot;comment&quot;&gt;#&amp;lt;TAG_KEY_1&amp;gt;:&amp;lt;TAG_VALUE_1&amp;gt;,&amp;lt;TAG_2&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;

&lt;p&gt;So could use &lt;code&gt;nc&lt;/code&gt; and &lt;code&gt;socat&lt;/code&gt; on indivial host:&lt;/p&gt;
&lt;p&gt;e.g.&lt;/p&gt;
&lt;figure class=&quot;highlight apache&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;1&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;2&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;3&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;comment&quot;&gt;# use nc&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;attribute&quot;&gt;echo&lt;/span&gt; -n &lt;span class=&quot;string&quot;&gt;&amp;quot;kafka.partition_size:123|g|#topic_name:test,partition_number:1,broker_id:28041,hostname:kafka-12345&amp;quot;&lt;/span&gt; | nc -w &lt;span class=&quot;number&quot;&gt;1&lt;/span&gt; -cu localhost &lt;span class=&quot;number&quot;&gt;8125&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;
    
    </summary>
    
    
      <category term="SRE" scheme="http://davidlu1001.github.io/categories/SRE/"/>
    
      <category term="Monitoring" scheme="http://davidlu1001.github.io/categories/Monitoring/"/>
    
    
      <category term="SRE" scheme="http://davidlu1001.github.io/tags/SRE/"/>
    
      <category term="StatsD" scheme="http://davidlu1001.github.io/tags/StatsD/"/>
    
      <category term="Datadog" scheme="http://davidlu1001.github.io/tags/Datadog/"/>
    
      <category term="Monitoring" scheme="http://davidlu1001.github.io/tags/Monitoring/"/>
    
  </entry>
  
  <entry>
    <title>Real-life AWS infrastructure Cost Optimization cases</title>
    <link href="http://davidlu1001.github.io/2020/04/20/Cost-Optimization-cases-on-AWS/"/>
    <id>http://davidlu1001.github.io/2020/04/20/Cost-Optimization-cases-on-AWS/</id>
    <published>2020-04-20T09:08:44.000Z</published>
    <updated>2020-04-20T09:08:44.000Z</updated>
    
    <content type="html"><![CDATA[<p><strong><em>Slide uploaded on 01/Jul/2020</em></strong></p><iframe src="https://docs.google.com/presentation/d/e/2PACX-1vSopB1AL7XcOuGbqZxoTlNxnRE4ZKOcHOeIkvTAeIHQ5gvCQs69M8NV49u4dGgOR8fB34ryf2QuAHF_/embed?start=false&loop=false&delayms=3000" frameborder="0" width="960" height="569" allowfullscreen="true" mozallowfullscreen="true" webkitallowfullscreen="true"></iframe><p><strong><em>Updated on 01/May/2020</em></strong></p><p>There are other two areas where it is possible to reduce the AWS cost:</p><ol><li><p>Considering migrate from Classic Load Balancer to Application Load Balancer (technical debt):</p><ul><li><code>ALB</code> is about <code>10%</code> cheaper than <code>ELB</code> (<span class="exturl" data-url="aHR0cHM6Ly9hd3MuYW1hem9uLmNvbS9lbGFzdGljbG9hZGJhbGFuY2luZy9wcmljaW5nLw==">AWS Load Balancing pricing<i class="fa fa-external-link-alt"></i></span>)</li><li>Convenient to perform Canary Depoyments in CI / CD</li></ul></li><li><p>Using AWS Savings Plans: please refer to my article about <a href="https://davidlu1001.github.io/2020/04/28/AWS-Savings-Plans/">AWS Savings Plans Overview</a></p></li></ol><hr><p>Rather than write a big, manual-style cost optimization guide, I’d like to share a few pits I’ve encountered during the process. </p><h1 id="Tools"><a href="#Tools" class="headerlink" title="Tools"></a>Tools</h1><p>Common tools for AWS cost optimization are as follows:</p><ol><li>AWS Cost Explorer</li><li>Cost Reports in S3</li><li>AWS Trusted Advisor - Cost Optimization</li></ol><h1 id="Strategies"><a href="#Strategies" class="headerlink" title="Strategies"></a>Strategies</h1><p>Based on <code>AWS Cost Optimization Best Practice</code>, the main measures are probably the following aspects:</p><ol><li><strong>Right Sizing</strong>: Use a more appropriate (convenient) Instance Type / Family (for EC2 / RDS) </li><li><strong>Price models</strong>: leverage Reserved Instances (RI) and Spot Instances (SI)</li><li><strong>Delete / Stop unused resources</strong>: e.g. EBS Volume / Snapshot, EC2 / RDS / EIP / ELB, etc.</li><li><strong>Storage Tier / Backup Policy</strong>: Move cold data to cheaper storage tiers like Glacier; Review EBS Snapshot / RDS backup policy</li><li><strong>Right Tagging</strong>: Enforce allocation tagging, while improving Tag coverage and accuracy</li><li><strong>Scheduling On / Off times</strong>: Review existing Auto Scaling policies; Stop instances used in Dev and Prod when not in use and start them again when needed</li></ol><a id="more"></a><p>Corresponding to the specific situation of the company, mainly want to talk about 3 and 4 mentioned above.</p><h1 id="About-Backup"><a href="#About-Backup" class="headerlink" title="About Backup"></a>About Backup</h1><h2 id="AWS-Backup"><a href="#AWS-Backup" class="headerlink" title="AWS Backup"></a>AWS Backup</h2><p>At first, the company used Lambda Function (Python script) to handle EBS / RDS backup. Later, because of the release of AWS Backup Service, it was decided to migrate backup management to AWS Backup.</p><p>However, for Database backup, because AWS Backup Service does not support <code>Aurora</code>, so we still need to keep the original Lambda.</p><blockquote><p>AWS Backup currently supports all Amazon RDS database engines except Amazon Aurora.</p></blockquote><p>For AWS Backup, it can support multiple AWS Services / Resources, such as:</p><ul><li><p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL2Vmcy9sYXRlc3QvdWcvYXdzYmFja3VwLmh0bWw=">Amazon EFS<i class="fa fa-external-link-alt"></i></span></p></li><li><p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL2FtYXpvbmR5bmFtb2RiL2xhdGVzdC9kZXZlbG9wZXJndWlkZS9CYWNrdXBSZXN0b3JlLmh0bWw=">DynamoDB<i class="fa fa-external-link-alt"></i></span></p></li><li><p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL0FXU0VDMi9sYXRlc3QvVXNlckd1aWRlL0VCU1NuYXBzaG90cy5odG1s">Amazon EBS Snapshots<i class="fa fa-external-link-alt"></i></span></p></li><li><p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL0FtYXpvblJEUy9sYXRlc3QvVXNlckd1aWRlL0NIQVBfQ29tbW9uVGFza3MuQmFja3VwUmVzdG9yZS5odG1s">Amazon RDS DB Instances<i class="fa fa-external-link-alt"></i></span></p></li><li><p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL3N0b3JhZ2VnYXRld2F5L2xhdGVzdC91c2VyZ3VpZGUvYmFja2luZy11cC12b2x1bWVzLmh0bWwjYmFja3VwLXZvbHVtZXMtY3J5bw==">AWS Storage Gateway<i class="fa fa-external-link-alt"></i></span></p></li></ul><p>Unfortunately, AWS Backup does not support the <code>Wildcard (*)</code> to match resources.</p><p>For example:</p><p>Internally we use <code>Terraform</code> as IaC (<span class="exturl" data-url="aHR0cHM6Ly9lbi53aWtpcGVkaWEub3JnL3dpa2kvSW5mcmFzdHJ1Y3R1cmVfYXNfY29kZQ==">Infrastructure as code<i class="fa fa-external-link-alt"></i></span>) to automate the management of resources on AWS.</p><p>Examples of corresponding AWS Backup codes:</p><figure class="highlight nix"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">  <span class="attr">resources</span>                       = [</span><br><span class="line">    <span class="comment"># RDS</span></span><br><span class="line">    <span class="string">&quot;arn:aws:rds:<span class="subst">$&#123;data.aws_region.current.name&#125;</span>:<span class="subst">$&#123;data.aws_caller_identity.current.account_id&#125;</span>:db:redash&quot;</span>,</span><br><span class="line">    <span class="comment"># DynamoDB</span></span><br><span class="line">    <span class="string">&quot;arn:aws:dynamodb:<span class="subst">$&#123;data.aws_region.current.name&#125;</span>:<span class="subst">$&#123;data.aws_caller_identity.current.account_id&#125;</span>:table/crm&quot;</span>,</span><br><span class="line">    <span class="comment"># EBS</span></span><br><span class="line">    <span class="string">&quot;arn:aws:ec2:<span class="subst">$&#123;data.aws_region.current.name&#125;</span>:<span class="subst">$&#123;data.aws_caller_identity.current.account_id&#125;</span>:volume/</span></span><br><span class="line"><span class="string">vol-054cfe133afc1dc10&quot;</span>,</span><br><span class="line">    <span class="comment"># EFS</span></span><br><span class="line">    <span class="string">&quot;arn:aws:elasticfilesystem:<span class="subst">$&#123;data.aws_region.current.name&#125;</span>:<span class="subst">$&#123;data.aws_caller_identity.current.account_id&#125;</span>:file-system/<span class="subst">$&#123;filesystem-id&#125;</span>&quot;</span>,</span><br><span class="line">    <span class="comment"># StorageGateway</span></span><br><span class="line">    <span class="string">&quot;arn:aws:storagegateway:<span class="subst">$&#123;data.aws_region.current.name&#125;</span>:<span class="subst">$&#123;data.aws_caller_identity.current.account_id&#125;</span>:gateway/<span class="subst">$&#123;gateway-id&#125;</span>/volume/<span class="subst">$&#123;volume-id&#125;</span>&quot;</span>,</span><br><span class="line">  ]</span><br><span class="line"></span><br><span class="line">  <span class="attr">tags</span> = [</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="attr">type</span>  = <span class="string">&quot;STRINGEQUALS&quot;</span></span><br><span class="line">    <span class="attr">key</span>   = <span class="string">&quot;Environment&quot;</span></span><br><span class="line">    <span class="attr">value</span> = <span class="string">&quot;Production&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="attr">type</span>  = <span class="string">&quot;STRINGEQUALS&quot;</span></span><br><span class="line">    <span class="attr">key</span>   = <span class="string">&quot;Cost Center&quot;</span></span><br><span class="line">    <span class="attr">value</span> = <span class="string">&quot;Platform&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="attr">type</span>  = <span class="string">&quot;STRINGEQUALS&quot;</span></span><br><span class="line">    <span class="attr">key</span>   = <span class="string">&quot;MakeSnapshot&quot;</span></span><br><span class="line">    <span class="attr">value</span> = <span class="string">&quot;True&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  ]</span><br></pre></td></tr></table></figure><p>Therefore, the corresponding Backup solution is:</p><ul><li><p>Either specify the specific <code>Resource ID</code> (use <code>resources</code> to specify, but cannot support something like <code>arn:aws:rds:$&#123;region&#125;:$&#123;account_id&#125;:db:*</code>)</p></li><li><p>Either use <code>Tags</code> to mark the resources to be backed up by AWS Backup Service</p></li></ul><p>Of course, the former is certainly not realistic, which means:</p><p><strong><em>The feature of AWS Backup determins (well, I believe this is not a bug), in fact you can only use <code>Tags</code>, and need to set <code>Resources</code> to empty <code>[]</code> (by default it’s for all Resource types supported by AWS Backup). Thus laying a hidden danger.</em></strong></p><blockquote><p>Note: The relationship between <code>Tags</code> is <code>OR</code>, so as long as  one <code>Tag</code> get matched, AWS Backup will back it up.</p></blockquote><p>The foreplay is over, and the pit is coming!</p><p>At the beginning of March, it was found that the AWS bill was obviously abnormal. After analysis, it was found that the cost of EBS snapshots increased too fast.</p><p><img data-src="https://raw.githubusercontent.com/davidlu1001/davidlu1001.github.io/hexo/uPic/02_ebs_cost.png" alt=""></p><p>As you can see in the chart, it started to grow slowly from around <code>January 15th</code>, so what happened at this time?</p><p>The internal search was fruitless. After googling, I found a suspicious <span class="exturl" data-url="aHR0cHM6Ly9hd3MuYW1hem9uLmNvbS9hYm91dC1hd3Mvd2hhdHMtbmV3LzIwMjAvMDEvYXdzLWJhY2t1cC1hZGRzLXN1cHBvcnQtYW1hem9uLWVsYXN0aWMtY2xvdWQtY29tcHV0ZS1pbnN0YW5jZS1iYWNrdXAv">What’s New<i class="fa fa-external-link-alt"></i></span> message from AWS official website.</p><p>Hmmmmmm, perfectly matched…</p><p><img data-src="https://raw.githubusercontent.com/davidlu1001/davidlu1001.github.io/hexo/uPic/03_aws_what_new.png" alt=""></p><p>Okay! Looks like AWS help me to back up all the EC2 instances by default… Thank you!</p><p>According to the documentation, for AWS Backup, in addition to EBS Snapshot itself, it will also make an AMI copy for each EC2 instance, so that’s why there are a lot of AMIs.</p><blockquote><p>When backing up an Amazon EC2 instance, AWS Backup takes a snapshot of the root Amazon EBS storage volume, the launch configurations, and all associated EBS volumes. AWS Backup stores certain configuration parameters of the EC2 instance, including instance type, security groups, Amazon VPC, monitoring configuration, and tags. The backup data is stored as an Amazon EBS volume-backed AMI (Amazon Machine Image).</p></blockquote><p><img data-src="https://raw.githubusercontent.com/davidlu1001/davidlu1001.github.io/hexo/uPic/04_aws_backup_ami.png" alt=""></p><p><img data-src="https://raw.githubusercontent.com/davidlu1001/davidlu1001.github.io/hexo/uPic/05_aws_backup_ebs.png" alt=""></p><p>According to the previous AWS Backup execution logic, for all supported types (including newly supported EC2 on January 13), as long as the tag is matched, the backup will be performed.</p><p>So, for EC2 AMI / EBS snapshot, how Tags are added by default?</p><p>Checked the <code>Terraform</code> code:</p><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">resource <span class="string">&quot;aws_autoscaling_group&quot;</span> <span class="string">&quot;main&quot;</span> &#123;</span><br><span class="line">...</span><br><span class="line">  <span class="keyword">tag</span> <span class="title">&#123;</span></span><br><span class="line"><span class="title">    key</span>                 = <span class="string">&quot;Name&quot;</span></span><br><span class="line">    value               = <span class="string">&quot;Value&quot;</span></span><br><span class="line">    propagate_at_launch = <span class="literal">true</span></span><br><span class="line">  &#125;</span><br><span class="line">...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>Since <code>propagate_at_launch = true</code> is used, the tags of ASG will be copied when EC2 launches.</p><p>In addition, after EC2 is started, the initialization script will be run through Puppet to copy the ASG / EC2 tags to EBS Volumes. Code example as follows:</p><figure class="highlight dsconfig"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Retrieve the tags attached to the instance if no auto-scaling group exists.</span></span><br><span class="line"><span class="comment"># Add all tags retrieved from the auto-scaling group to all EBS volumes attached to the current EC2 instance.</span></span><br><span class="line"></span><br><span class="line"><span class="string">if</span> [ <span class="string">&quot;$asg&quot;</span> != <span class="string">&quot;&quot;</span> ]; <span class="string">then</span></span><br><span class="line"><span class="string"></span>    <span class="string">aws</span> <span class="string">ec2</span> <span class="built_in">create-tags</span> <span class="built_in">--region</span> <span class="string">&quot;$&#123;region_id&#125;&quot;</span> <span class="built_in">--resources</span> $&#123;<span class="string">volumes</span>&#125; <span class="built_in">--tags</span> <span class="string">&quot;$&#123;tags&#125;&quot;</span></span><br><span class="line"><span class="string">else</span></span><br><span class="line"><span class="string"></span>    <span class="string">tags</span>=$(<span class="string">aws</span> <span class="string">ec2</span> <span class="string">describe-tags</span> <span class="built_in">--region</span> <span class="string">&quot;$&#123;region_id&#125;&quot;</span> <span class="built_in">--filters</span> <span class="string">&quot;Name=resource-id,Values=&quot;</span>$&#123;<span class="string">instance_id</span>&#125;<span class="string">&quot;&quot;</span> <span class="built_in">--query</span> <span class="string">&#x27;Tags[*].&#123;Key:Key,Value:Value&#125;&#x27;</span>)</span><br><span class="line">    <span class="string">aws</span> <span class="string">ec2</span> <span class="built_in">create-tags</span> <span class="built_in">--region</span> <span class="string">&quot;$&#123;region_id&#125;&quot;</span> <span class="built_in">--resources</span> $&#123;<span class="string">volumes</span>&#125; <span class="built_in">--tags</span> <span class="string">&quot;$&#123;tags&#125;&quot;</span></span><br><span class="line"><span class="string">fi</span></span><br></pre></td></tr></table></figure><p>So the current Tags replication chain:</p><blockquote><p>ASG-&gt; EC2-&gt; EBS Volume</p></blockquote><p>As for the solution, just to differentiate <code>Tags</code> between EC2 and EBS, and delete the tags that will be matched by AWS Backup (here is <code>MakeSnapshot*</code>), to achieve the purpose of “Backing up EBS only, not EC2 instance”:</p><figure class="highlight dsconfig"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">if</span> [ <span class="string">&quot;$asg&quot;</span> != <span class="string">&quot;&quot;</span> ]; <span class="string">then</span></span><br><span class="line"><span class="string"></span>    <span class="string">aws</span> <span class="string">ec2</span> <span class="built_in">create-tags</span> <span class="built_in">--region</span> <span class="string">&quot;$&#123;region_id&#125;&quot;</span> <span class="built_in">--resources</span> $&#123;<span class="string">volumes</span>&#125; <span class="built_in">--tags</span> <span class="string">&quot;$&#123;tags&#125;&quot;</span></span><br><span class="line">    <span class="string">aws</span> <span class="string">ec2</span> <span class="built_in">delete-tags</span> <span class="built_in">--resources</span> <span class="string">&quot;$&#123;instance_id&#125;&quot;</span> <span class="built_in">--tags</span> <span class="string">Key</span>=<span class="string">MakeSnapshot</span> <span class="string">Key</span>=<span class="string">MakeSnapshotShortTerm</span> <span class="string">Key</span>=<span class="string">MakeSnapshotLongTerm</span></span><br><span class="line"><span class="string">else</span></span><br><span class="line"><span class="string"></span>    <span class="string">tags</span>=$(<span class="string">aws</span> <span class="string">ec2</span> <span class="string">describe-tags</span> <span class="built_in">--region</span> <span class="string">&quot;$&#123;region_id&#125;&quot;</span> <span class="built_in">--filters</span> <span class="string">&quot;Name=resource-id,Values=&quot;</span>$&#123;<span class="string">instance_id</span>&#125;<span class="string">&quot;&quot;</span> <span class="built_in">--query</span> <span class="string">&#x27;Tags[*].&#123;Key:Key,Value:Value&#125;&#x27;</span>)</span><br><span class="line">    <span class="string">aws</span> <span class="string">ec2</span> <span class="built_in">create-tags</span> <span class="built_in">--region</span> <span class="string">&quot;$&#123;region_id&#125;&quot;</span> <span class="built_in">--resources</span> $&#123;<span class="string">volumes</span>&#125; <span class="built_in">--tags</span> <span class="string">&quot;$&#123;tags&#125;&quot;</span></span><br><span class="line">    <span class="string">aws</span> <span class="string">ec2</span> <span class="built_in">delete-tags</span> <span class="built_in">--resources</span> <span class="string">&quot;$&#123;instance_id&#125;&quot;</span> <span class="built_in">--tags</span> <span class="string">Key</span>=<span class="string">MakeSnapshot</span> <span class="string">Key</span>=<span class="string">MakeSnapshotShortTerm</span> <span class="string">Key</span>=<span class="string">MakeSnapshotLongTerm</span></span><br><span class="line"><span class="string">fi</span></span><br></pre></td></tr></table></figure><p>The effect after cleaning:</p><p><img data-src="https://raw.githubusercontent.com/davidlu1001/davidlu1001.github.io/hexo/uPic/06_cost_ebs_after.png" alt=""></p><p>After this battle, I completely felt the fear of being dominated by AWS Bills :-(</p><p>You can’t plant heels in the same place, so just add monitoring for news from AWS :-)</p><p><img data-src="https://raw.githubusercontent.com/davidlu1001/davidlu1001.github.io/hexo/uPic/07_monitor%20plugin.png" alt=""></p><h1 id="About-DB-storage"><a href="#About-DB-storage" class="headerlink" title="About DB storage"></a>About DB storage</h1><p><strong>RDS backup storage excess free allocation</strong></p><blockquote><p>Potential increase to your Amazon Aurora bill [AWS Account: XXXXXXXXX]</p><p>Starting March 1, 2020, we will start charging for backup storage in excess of the free allocation. There will not be be any back-dated or retroactive charges for use of Aurora backups before March 1, 2020.</p><p>You can review your current backup retention policy and snapshots on the RDS Management Console. You can lower your monthly bill by reducing your backup retention window or by deleting unnecessary snapshots.</p></blockquote><p>Usually, in our SRE team who is on duty will also pay attention to daily BAU such as consultation / email etc., but on that day, the unlucky guy was too busy to ignore the AWS email when received it, and therefore didn’t create a JIRA ticket, but (the interesting thing is) everyone in the team missed it as well, which is pretty strange.</p><p>As a result, we didn’t realize until reviewing the AWS Cost, and the price is not cheap actually:</p><blockquote><p>$ 0.095 per additional GB-month of backup storage exceeding free allocation</p></blockquote><p>Currently the company’s default setting of <code>backup_retention_period</code> for RDS is<code>30</code> days, and this time, of course, we need to reduce it.</p><p>The effect is as follows, better than nothing:</p><p>Can see a <code>70%</code> drop for <code>USW2-Aurora:BackupUsage</code> after reducing retention period</p><p><img data-src="https://raw.githubusercontent.com/davidlu1001/davidlu1001.github.io/hexo/uPic/08_cost_rds.png" alt=""></p><h2 id="About-Data-Transfer"><a href="#About-Data-Transfer" class="headerlink" title="About Data Transfer"></a>About Data Transfer</h2><p>One of the more interesting things during this period is that, I found there is something called <strong><em>EC2-others</em></strong> in the AWS Cost Report. Here is the official statement: </p><blockquote><p>The <a href="https://aws.amazon.com/blogs/aws-cost-management/tips-and-tricks-for-exploring-your-data-in-aws-cost-explorer-part-2/"><strong>EC2-Other</strong></a> category includes multiple service-related usage types, tracking costs associated:</p><ul><li>Amazon EBS volumes and snapshots</li><li>Elastic IP addresses</li><li>NAT gateways</li><li>Data transfer</li></ul></blockquote><p>Through ithe Billing Report, I found that the following is the culprit:</p><p><img data-src="https://raw.githubusercontent.com/davidlu1001/davidlu1001.github.io/hexo/uPic/09_cost_datatransfer.png" alt=""></p><h3 id="How-to-locate"><a href="#How-to-locate" class="headerlink" title="How to locate"></a>How to locate</h3><p>Using <code>Cost Explorer</code>, we can locate the specific item with the following settings:</p><p><img data-src="https://raw.githubusercontent.com/davidlu1001/davidlu1001.github.io/hexo/uPic/10_ce_locate_dt.png" alt=""></p><p>Finally, the major part of Data Transfer is located, which comes from a (business) Load Balancer, followed by the Network Load Balancer (NLB) that for Logging.</p><h3 id="How-to-reduce-data-transmission-costs"><a href="#How-to-reduce-data-transmission-costs" class="headerlink" title="How to reduce data transmission costs"></a>How to reduce data transmission costs</h3><p>A picture is worth a thousand words:</p><p><img data-src="https://raw.githubusercontent.com/davidlu1001/davidlu1001.github.io/hexo/uPic/11_dt_cost.png" alt=""></p><p>There’re some general strategies, such as:</p><ul><li><p>Limit data transfer to the same AZ as much as possible, or between AZs or the same Region.</p></li><li><p>Use Private IP whenever possible.</p></li></ul><p>For example:</p><ul><li><p>EC2 that needs to communicate in a development or test environment should be in the same Availability Zone (AZ) to avoid data transfer costs.</p></li><li><p>EC2 can use CloudFront (CDN) if it needs to transfer data such as pictures / videos to Public.</p></li><li><p>For resources in different Regions or multiple Accounts, can use <span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL3ZwYy9sYXRlc3QvcGVlcmluZy93aGF0LWlzLXZwYy1wZWVyaW5nLmh0bWw=">VPC Peering<i class="fa fa-external-link-alt"></i></span> or <span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL3ZwYy9sYXRlc3QvdXNlcmd1aWRlL3ZwYy1zaGFyaW5nLmh0bWw=">VPC Sharing<i class="fa fa-external-link-alt"></i></span> to further reduce transfer costs.</p></li></ul><p>In reality, a multi-pronged strategy has been adopted.</p><p>After locating certain LB problems, the method of “directly reducing the transmitted data” was adopted, e.g.: </p><ul><li><p>Turn off <code>log_subrequest</code> config in <code>OpenResty(Nginx)</code></p><blockquote><p>disables logging of subrequests into <span class="exturl" data-url="aHR0cDovL25naW54Lm9yZy9lbi9kb2NzL2h0dHAvbmd4X2h0dHBfbG9nX21vZHVsZS5odG1sI2FjY2Vzc19sb2c=">access_log<i class="fa fa-external-link-alt"></i></span></p></blockquote></li><li><p>Add filter strategy in <code>Logstash</code> to discard some useless data</p></li><li><p>In <code>Rsyslog</code>, enable the compression mode for <span class="exturl" data-url="aHR0cHM6Ly9yc3lzbG9nLnJlYWR0aGVkb2NzLmlvL2VuL2xhdGVzdC9jb25maWd1cmF0aW9uL21vZHVsZXMvb21md2QuaHRtbA==">omfwd module<i class="fa fa-external-link-alt"></i></span>. Generally, a medium compression level should be fine, for example, <code>ZipLevel</code> can be set to <code>3-5</code>, and <code>compression.mode</code> can be set to <code>single</code>, which means each message will be evaluated and only the larger message will be compressed. And here in order to save costs, a more aggressive strategy is adopted, which will introduce delay but it’s acceptable.</p>  <figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">ZipLevel</span> = <span class="string">&quot;9&quot;</span></span><br><span class="line"><span class="attr">compression.mode</span> = <span class="string">&quot;stream: always&quot;</span></span><br><span class="line"><span class="attr">compression.stream.flushOnTXEnd</span> = <span class="string">&quot;off&quot;</span></span><br></pre></td></tr></table></figure></li><li><p>In addition, can also consider adjusting the <code>index.codec</code> configuration of <code>ElasticSearch</code>, using <code>best_compression</code> to replace the default compression algorithm <code>LZ4</code> to achieve a higher compression ratio, in order to reduce data transfer across AZ (such as <code>shard allocation</code>)</p></li></ul><p>Well, for the time being, it is predicted that <code>Cost Optimization</code> will be a protracted battle, especially in the current environment of global economic impact.</p><p>Therefore, we really need to pay more attention to cost optimization, not only from the beginning of architecture / solution design, development and daily maintenance, but also need to create a lean cost-centric culture.</p><h1 id="References"><a href="#References" class="headerlink" title="References"></a>References</h1><ul><li><p><span class="exturl" data-url="aHR0cHM6Ly9hd3MuYW1hem9uLmNvbS9hd3MtY29zdC1tYW5hZ2VtZW50Lw==">AWS Cost Management<i class="fa fa-external-link-alt"></i></span></p></li><li><p><span class="exturl" data-url="aHR0cHM6Ly93YS5hd3MuYW1hem9uLmNvbS93YXQucGlsbGFyLmNvc3RPcHRpbWl6YXRpb24uZW4uaHRtbA==">Cost Optimization-AWS Well-Architected Framework<i class="fa fa-external-link-alt"></i></span></p></li><li><p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL2F3cy1iYWNrdXAvbGF0ZXN0L2Rldmd1aWRlL3dvcmtpbmctd2l0aC1vdGhlci1zZXJ2aWNlcy5odG1s">AWS Backup: working-with-other-services<i class="fa fa-external-link-alt"></i></span></p></li><li><p><span class="exturl" data-url="aHR0cHM6Ly9hd3MuYW1hem9uLmNvbS9lbGFzdGljbG9hZGJhbGFuY2luZy9wcmljaW5nLw==">AWS Load Balancing pricing<i class="fa fa-external-link-alt"></i></span></p></li><li><p><a href="https://davidlu1001.github.io/2020/04/28/AWS-Savings-Plans/">AWS Savings Plans - Overview</a></p></li></ul>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;&lt;strong&gt;&lt;em&gt;Slide uploaded on 01/Jul/2020&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;iframe src=&quot;https://docs.google.com/presentation/d/e/2PACX-1vSopB1AL7XcOuGbqZxoTlNxnRE4ZKOcHOeIkvTAeIHQ5gvCQs69M8NV49u4dGgOR8fB34ryf2QuAHF_/embed?start=false&amp;loop=false&amp;delayms=3000&quot; frameborder=&quot;0&quot; width=&quot;960&quot; height=&quot;569&quot; allowfullscreen=&quot;true&quot; mozallowfullscreen=&quot;true&quot; webkitallowfullscreen=&quot;true&quot;&gt;&lt;/iframe&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Updated on 01/May/2020&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;There are other two areas where it is possible to reduce the AWS cost:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Considering migrate from Classic Load Balancer to Application Load Balancer (technical debt):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;ALB&lt;/code&gt; is about &lt;code&gt;10%&lt;/code&gt; cheaper than &lt;code&gt;ELB&lt;/code&gt; (&lt;a href=&quot;https://aws.amazon.com/elasticloadbalancing/pricing/&quot;&gt;AWS Load Balancing pricing&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Convenient to perform Canary Depoyments in CI / CD&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using AWS Savings Plans: please refer to my article about &lt;a href=&quot;https://davidlu1001.github.io/2020/04/28/AWS-Savings-Plans/&quot;&gt;AWS Savings Plans Overview&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;
&lt;p&gt;Rather than write a big, manual-style cost optimization guide, I’d like to share a few pits I’ve encountered during the process. &lt;/p&gt;
&lt;h1 id=&quot;Tools&quot;&gt;&lt;a href=&quot;#Tools&quot; class=&quot;headerlink&quot; title=&quot;Tools&quot;&gt;&lt;/a&gt;Tools&lt;/h1&gt;&lt;p&gt;Common tools for AWS cost optimization are as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;AWS Cost Explorer&lt;/li&gt;
&lt;li&gt;Cost Reports in S3&lt;/li&gt;
&lt;li&gt;AWS Trusted Advisor - Cost Optimization&lt;/li&gt;
&lt;/ol&gt;
&lt;h1 id=&quot;Strategies&quot;&gt;&lt;a href=&quot;#Strategies&quot; class=&quot;headerlink&quot; title=&quot;Strategies&quot;&gt;&lt;/a&gt;Strategies&lt;/h1&gt;&lt;p&gt;Based on &lt;code&gt;AWS Cost Optimization Best Practice&lt;/code&gt;, the main measures are probably the following aspects:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Right Sizing&lt;/strong&gt;: Use a more appropriate (convenient) Instance Type / Family (for EC2 / RDS) &lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Price models&lt;/strong&gt;: leverage Reserved Instances (RI) and Spot Instances (SI)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Delete / Stop unused resources&lt;/strong&gt;: e.g. EBS Volume / Snapshot, EC2 / RDS / EIP / ELB, etc.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Storage Tier / Backup Policy&lt;/strong&gt;: Move cold data to cheaper storage tiers like Glacier; Review EBS Snapshot / RDS backup policy&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Right Tagging&lt;/strong&gt;: Enforce allocation tagging, while improving Tag coverage and accuracy&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scheduling On / Off times&lt;/strong&gt;: Review existing Auto Scaling policies; Stop instances used in Dev and Prod when not in use and start them again when needed&lt;/li&gt;
&lt;/ol&gt;
    
    </summary>
    
    
      <category term="AWS" scheme="http://davidlu1001.github.io/categories/AWS/"/>
    
      <category term="SRE" scheme="http://davidlu1001.github.io/categories/SRE/"/>
    
      <category term="Cost" scheme="http://davidlu1001.github.io/categories/Cost/"/>
    
    
      <category term="AWS" scheme="http://davidlu1001.github.io/tags/AWS/"/>
    
      <category term="ElasticSearch" scheme="http://davidlu1001.github.io/tags/ElasticSearch/"/>
    
      <category term="SRE" scheme="http://davidlu1001.github.io/tags/SRE/"/>
    
      <category term="Cost Optimization" scheme="http://davidlu1001.github.io/tags/Cost-Optimization/"/>
    
      <category term="Backup" scheme="http://davidlu1001.github.io/tags/Backup/"/>
    
      <category term="Terraform" scheme="http://davidlu1001.github.io/tags/Terraform/"/>
    
  </entry>
  
  <entry>
    <title>ElasticSearch Runbook</title>
    <link href="http://davidlu1001.github.io/2020/04/16/ElasticSearch-Runbook/"/>
    <id>http://davidlu1001.github.io/2020/04/16/ElasticSearch-Runbook/</id>
    <published>2020-04-15T22:07:57.000Z</published>
    <updated>2020-04-15T22:07:57.000Z</updated>
    
    <content type="html"><![CDATA[<p>ElasticSearch can be a beast to manage. Knowing the most used endpoints during outages or simple maintenance by heart can be as challenging as it is time consuming. Because of this, this How-To article will layout some handy commands. In theory you could run them from any given node (data, client or master), however I’d recommend running them from a master node.</p><p>ssh into any master node (pro-tip: master instances are the ones within the master autoscaling group)</p><p>e.g.</p><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">ssh</span> <span class="selector-tag">elastic-master-</span>&lt;<span class="selector-tag">instance-id</span>&gt;.&lt;<span class="selector-tag">aws-region</span>&gt;<span class="selector-class">.x</span><span class="selector-class">.y</span><span class="selector-class">.com</span></span><br></pre></td></tr></table></figure><h2 id="Handy-aliases"><a href="#Handy-aliases" class="headerlink" title="Handy aliases"></a>Handy <code>aliases</code></h2><figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alias</span> tail-es=<span class="string">&#x27;tail -500f /var/log/elasticsearch/&lt;ES_NAME&gt;/&lt;ES_Name&gt;.log&#x27;</span></span><br><span class="line"><span class="keyword">alias</span> cat-nodes=<span class="string">&#x27;curl -s -XGET http://localhost:9200/_cat/nodes?v&#x27;</span></span><br><span class="line"><span class="keyword">alias</span> cat-shards=<span class="string">&#x27;curl -s -XGET http://localhost:9200/_cat/shards?h=index,shard,prirep,state,unassigned.reason&#x27;</span></span><br><span class="line"><span class="keyword">alias</span> cat-allocation=<span class="string">&#x27;curl -s -XGET http://localhost:9200/_cat/allocation?v&#x27;</span></span><br><span class="line"><span class="keyword">alias</span> cat-indices=<span class="string">&#x27;curl -s -XGET http://localhost:9200/_cat/indices?v&#x27;</span></span><br><span class="line"><span class="keyword">alias</span> <span class="keyword">get</span>-templates=<span class="string">&#x27;curl -s -XGET http://localhost:9200/_template?pretty&#x27;</span></span><br><span class="line"><span class="keyword">alias</span> cat-settings=<span class="string">&#x27;curl -s -XGET http://localhost:9200/_cluster/settings/?pretty&#x27;</span></span><br><span class="line"><span class="keyword">alias</span> cat-settings-<span class="keyword">all</span>=<span class="string">&#x27;curl -s -XGET &#x27;</span>\<span class="string">&#x27;&#x27;</span>http://localhost:<span class="number">9200</span>/_cluster/settings?include_defaults=<span class="keyword">true</span>&amp;pretty<span class="string">&#x27;\&#x27;&#x27; | jq .&#x27;</span></span><br><span class="line"><span class="keyword">alias</span> <span class="keyword">cluster</span>-status=<span class="string">&#x27;curl -s -XGET http://localhost:9200/_cluster/health?pretty&#x27;</span></span><br><span class="line"><span class="keyword">alias</span> <span class="keyword">check</span>-status=<span class="string">&#x27;while true; do sleep 5; cluster-status | grep status; done&#x27;</span></span><br><span class="line"><span class="keyword">alias</span> <span class="keyword">disable</span>-allocation=<span class="string">&#x27;curl -XPUT localhost:9200/_cluster/settings -d &#x27;</span>\<span class="string">&#x27;&#x27;</span>&#123;&quot;transient&quot; : \&#123;&quot;cluster.routing.allocation.enable&quot; : &quot;none&quot;&#125;&#125;<span class="string">&#x27;\&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="keyword">alias</span> <span class="keyword">enable</span>-allocation=<span class="string">&#x27;curl -XPUT localhost:9200/_cluster/settings -d &#x27;</span>\<span class="string">&#x27;&#x27;</span>&#123;&quot;transient&quot; : \&#123;&quot;cluster.routing.allocation.enable&quot; : &quot;all&quot;&#125;&#125;<span class="string">&#x27;\&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="keyword">alias</span> cat-shards-unassigned=<span class="string">&#x27;curl -s -XGET http://localhost:9200/_cat/shards | grep UNASSIGNED | awk &#x27;</span>\<span class="string">&#x27;&#x27;</span>&#123;print <span class="meta">$1</span>&#125;<span class="string">&#x27;\&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="keyword">alias</span> del-shards-unassigned=<span class="string">&#x27;curl -s -XGET http://localhost:9200/_cat/shards | grep UNASSIGNED | awk &#x27;</span>\<span class="string">&#x27;&#x27;</span>&#123;print <span class="meta">$1</span>&#125;<span class="string">&#x27;\&#x27;&#x27; | xargs -i curl -s -XDELETE &quot;http://localhost:9200/&#123;&#125;&quot;&#x27;</span></span><br><span class="line"><span class="keyword">alias</span> cat-<span class="keyword">snapshot</span>=<span class="string">&#x27;curl -s -XGET localhost:9200/_snapshot/&lt;S3_BUCKET_FOR_SNAPSHOT_REPOSITORY&gt;/_all?pretty | jq .snapshots[-1]&#x27;</span></span><br></pre></td></tr></table></figure><a id="more"></a><h2 id="Cheatsheet"><a href="#Cheatsheet" class="headerlink" title="Cheatsheet"></a>Cheatsheet</h2><p><span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL3NlbWF0ZXh0L2NoZWF0c2hlZXRzL2Jsb2IvbWFzdGVyL2VsYXN0aWNzZWFyY2gtZGV2b3BzLWNoZWF0c2hlZXQubWQ=">https://github.com/sematext/cheatsheets/blob/master/elasticsearch-devops-cheatsheet.md<i class="fa fa-external-link-alt"></i></span></p><h2 id="Node-types"><a href="#Node-types" class="headerlink" title="Node types"></a>Node types</h2><p>Elasticsearch nodes can take one ore more roles. Here we are using the following node types:</p><ul><li>Master</li></ul><p>The master node is responsible for lightweight cluster-wide actions such as creating or deleting an index, tracking which nodes are part of the cluster, and deciding which shards to allocate to which nodes. It is important for cluster health to have a stable master node. Any master-eligible node (all nodes by default) may be elected to become the master node by the master election process.</p><ul><li>Data </li></ul><p>Data nodes hold the shards that contain the documents you have indexed. Data nodes handle data related operations like CRUD, search, and aggregations. These operations are I/O-, memory-, and CPU-intensive. It is important to monitor these resources and to add more data nodes if they are overloaded. </p><p>Can be subdivided into warm and hot nodes if wanting to <span class="exturl" data-url="aHR0cHM6Ly93d3cuZWxhc3RpYy5jby9ibG9nL2ltcGxlbWVudGluZy1ob3Qtd2FybS1jb2xkLWluLWVsYXN0aWNzZWFyY2gtd2l0aC1pbmRleC1saWZlY3ljbGUtbWFuYWdlbWVudA==">Implement a Hot-Warm-Cold Architecture for ES<i class="fa fa-external-link-alt"></i></span></p><ul><li>Client</li></ul><p>Can only route requests, handle the search reduce phase, and distribute bulk indexing. Essentially, coordinating (aka client) only nodes behave as smart load balancers. </p><h2 id="Cluster-health"><a href="#Cluster-health" class="headerlink" title="Cluster health"></a>Cluster health</h2><p>To check the overall cluster health (green, yellow or red) and keep polling for status just type cluster-status.</p><figure class="highlight smali"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">elastic-master:~$ check-status</span><br><span class="line">  <span class="string">&quot;status&quot;</span><span class="keyword"> :</span> <span class="string">&quot;green&quot;</span>,</span><br><span class="line">  <span class="string">&quot;status&quot;</span><span class="keyword"> :</span> <span class="string">&quot;green&quot;</span>,</span><br><span class="line">  <span class="string">&quot;status&quot;</span><span class="keyword"> :</span> <span class="string">&quot;green&quot;</span>,</span><br></pre></td></tr></table></figure><h2 id="Tailing-logs"><a href="#Tailing-logs" class="headerlink" title="Tailing logs"></a>Tailing logs</h2><p>Elasticsearch logs live in <code>/var/log/elasticsearch/&lt;cluster-name&gt;/&lt;cluster-name&gt;.log</code>. See the alias <code>tail-es</code> above.</p><h2 id="Listing-allocations"><a href="#Listing-allocations" class="headerlink" title="Listing allocations"></a>Listing allocations</h2><p>To list the allocations per host, just type <code>cat-allocation</code>.<br>This will show things like:</p><ul><li>shards per host</li><li>used disk</li><li>available disk</li><li>indice size</li><li>total disk size</li><li>disk usage percent<ul><li>This is an important metric. as a rule of thumb, once disk usage reaches <code>85%</code> things starts to go wild. ES will not allocate new shards to nodes once they have more than 85% disk used.</li></ul></li></ul><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">elastic</span>-master:~$ cat-allocation</span><br><span class="line"><span class="attribute">shards</span> disk.indices disk.used disk.avail disk.total disk.percent host          ip            node</span><br><span class="line">   <span class="attribute">927</span>      <span class="number">434</span>.<span class="number">5</span>gb   <span class="number">472</span>.<span class="number">8</span>gb    <span class="number">265</span>.<span class="number">2</span>gb    <span class="number">738</span>.<span class="number">1</span>gb           <span class="number">64</span> <span class="number">172.21.5.57</span>   <span class="number">172.21.5.57</span>   elastic-data-<span class="number">096596</span>c<span class="number">27</span>d<span class="number">5</span>ea<span class="number">41</span>b<span class="number">8</span>-production</span><br><span class="line">   <span class="attribute">928</span>      <span class="number">440</span>.<span class="number">8</span>gb   <span class="number">479</span>.<span class="number">1</span>gb    <span class="number">258</span>.<span class="number">9</span>gb    <span class="number">738</span>.<span class="number">1</span>gb           <span class="number">64</span> <span class="number">172.21.22.28</span>  <span class="number">172.21.22.28</span>  elastic-data-<span class="number">015</span>b<span class="number">67</span>ccc<span class="number">6632775</span>b-production</span><br><span class="line">   <span class="attribute">927</span>      <span class="number">496</span>.<span class="number">4</span>gb   <span class="number">534</span>.<span class="number">7</span>gb    <span class="number">203</span>.<span class="number">3</span>gb    <span class="number">738</span>.<span class="number">1</span>gb           <span class="number">72</span> <span class="number">172.21.21.209</span> <span class="number">172.21.21.209</span> elastic-data-<span class="number">02</span>b<span class="number">5</span>fb<span class="number">2</span>c<span class="number">632</span>eacc<span class="number">9</span>f-production</span><br><span class="line">   <span class="attribute">927</span>      <span class="number">463</span>.<span class="number">8</span>gb     <span class="number">502</span>gb      <span class="number">236</span>gb    <span class="number">738</span>.<span class="number">1</span>gb           <span class="number">68</span> <span class="number">172.21.5.138</span>  <span class="number">172.21.5.138</span>  elastic-data-<span class="number">07</span>aa<span class="number">318</span>c<span class="number">82</span>d<span class="number">04</span>e<span class="number">61</span>c-production</span><br><span class="line">   <span class="attribute">927</span>      <span class="number">436</span>.<span class="number">2</span>gb     <span class="number">475</span>gb      <span class="number">263</span>gb    <span class="number">738</span>.<span class="number">1</span>gb           <span class="number">64</span> <span class="number">172.21.39.147</span> <span class="number">172.21.39.147</span> elastic-data-<span class="number">0</span>a<span class="number">7381067</span>cc<span class="number">43</span>e<span class="number">698</span>-production</span><br><span class="line">   <span class="attribute">928</span>      <span class="number">457</span>.<span class="number">5</span>gb   <span class="number">495</span>.<span class="number">8</span>gb    <span class="number">242</span>.<span class="number">2</span>gb    <span class="number">738</span>.<span class="number">1</span>gb           <span class="number">67</span> <span class="number">172.21.5.32</span>   <span class="number">172.21.5.32</span>   elastic-data-<span class="number">05</span>a<span class="number">9</span>cd<span class="number">4</span>cea<span class="number">5328486</span>-production</span><br><span class="line">   <span class="attribute">927</span>      <span class="number">419</span>.<span class="number">1</span>gb   <span class="number">457</span>.<span class="number">3</span>gb    <span class="number">280</span>.<span class="number">7</span>gb    <span class="number">738</span>.<span class="number">1</span>gb           <span class="number">61</span> <span class="number">172.21.21.160</span> <span class="number">172.21.21.160</span> elastic-data-<span class="number">0</span>abf<span class="number">96</span>b<span class="number">0106</span>bcba<span class="number">98</span>-production</span><br><span class="line">   <span class="attribute">928</span>      <span class="number">447</span>.<span class="number">2</span>gb     <span class="number">485</span>gb      <span class="number">253</span>gb    <span class="number">738</span>.<span class="number">1</span>gb           <span class="number">65</span> <span class="number">172.21.38.167</span> <span class="number">172.21.38.167</span> elastic-data-<span class="number">0</span>b<span class="number">00</span>da<span class="number">1</span>daa<span class="number">6</span>c<span class="number">3</span>e<span class="number">055</span>-production</span><br><span class="line">   <span class="attribute">927</span>      <span class="number">472</span>.<span class="number">8</span>gb   <span class="number">510</span>.<span class="number">6</span>gb    <span class="number">227</span>.<span class="number">4</span>gb    <span class="number">738</span>.<span class="number">1</span>gb           <span class="number">69</span> <span class="number">172.21.39.249</span> <span class="number">172.21.39.249</span> elastic-data-<span class="number">09512</span>dff<span class="number">9</span>c<span class="number">057</span>e<span class="number">62</span>b-production</span><br></pre></td></tr></table></figure><h2 id="Deleting-corrupted-lost-shards"><a href="#Deleting-corrupted-lost-shards" class="headerlink" title="Deleting corrupted / lost shards"></a>Deleting corrupted / lost shards</h2><p>Sometimes shards can get corrupted or entirely lost. This will put the cluster in a <code>RED/YELLOW</code> state. If no other node have the unassigned shard and index, then the only option will be to delete them. To do so, run the alias <code>del-shards-unassigned</code> mentioned above or the command below:</p><figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl -s -XGET http:<span class="regexp">//</span>localhost:<span class="number">9200</span><span class="regexp">/_cat/</span>shards | grep UNASSIGNED | awk &#123;<span class="string">&#x27;print $1&#x27;</span>&#125; | xargs -i curl -s -XDELETE <span class="string">&quot;http://localhost:9200/&#123;&#125;&quot;</span></span><br></pre></td></tr></table></figure><h2 id="Listing-nodes"><a href="#Listing-nodes" class="headerlink" title="Listing nodes"></a>Listing nodes</h2><p>To list the nodes participating in a cluster, just type <code>cat-nodes</code>. The node with a star (<code>*</code>) is the actual master within that cluster.</p><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">elastic</span>-master:~$ cat-nodes</span><br><span class="line"><span class="attribute">host</span>          ip            heap.percent ram.percent load node.role master name</span><br><span class="line"><span class="attribute">172</span>.<span class="number">21</span>.<span class="number">38</span>.<span class="number">167</span> <span class="number">172.21.38.167</span>           <span class="number">67</span>          <span class="number">99</span> <span class="number">8</span>.<span class="number">84</span> d         -      elastic-data-<span class="number">0</span>b<span class="number">00</span>da<span class="number">1</span>daa<span class="number">6</span>c<span class="number">3</span>e<span class="number">055</span>-production</span><br><span class="line"><span class="attribute">172</span>.<span class="number">21</span>.<span class="number">5</span>.<span class="number">57</span>   <span class="number">172.21.5.57</span>             <span class="number">22</span>          <span class="number">99</span> <span class="number">4</span>.<span class="number">27</span> d         -      elastic-data-<span class="number">096596</span>c<span class="number">27</span>d<span class="number">5</span>ea<span class="number">41</span>b<span class="number">8</span>-production</span><br><span class="line"><span class="attribute">172</span>.<span class="number">21</span>.<span class="number">39</span>.<span class="number">60</span>  <span class="number">172.21.39.60</span>            <span class="number">48</span>          <span class="number">91</span> <span class="number">0</span>.<span class="number">00</span> -         m      elastic-master-<span class="number">033</span>de<span class="number">9</span>aa<span class="number">1</span>c<span class="number">6</span>f<span class="number">03</span>b<span class="number">8</span>a-production</span><br><span class="line"><span class="attribute">172</span>.<span class="number">21</span>.<span class="number">22</span>.<span class="number">28</span>  <span class="number">172.21.22.28</span>            <span class="number">74</span>          <span class="number">99</span> <span class="number">6</span>.<span class="number">41</span> d         -      elastic-data-<span class="number">015</span>b<span class="number">67</span>ccc<span class="number">6632775</span>b-production</span><br><span class="line"><span class="attribute">172</span>.<span class="number">21</span>.<span class="number">39</span>.<span class="number">147</span> <span class="number">172.21.39.147</span>           <span class="number">21</span>          <span class="number">99</span> <span class="number">5</span>.<span class="number">07</span> d         -      elastic-data-<span class="number">0</span>a<span class="number">7381067</span>cc<span class="number">43</span>e<span class="number">698</span>-production</span><br><span class="line"><span class="attribute">172</span>.<span class="number">21</span>.<span class="number">36</span>.<span class="number">186</span> <span class="number">172.21.36.186</span>           <span class="number">25</span>          <span class="number">98</span> <span class="number">0</span>.<span class="number">06</span> -         -      elastic-client-<span class="number">0</span>bdfaa<span class="number">8</span>f<span class="number">29</span>b<span class="number">51</span>ee<span class="number">5</span>c-production</span><br><span class="line"><span class="attribute">172</span>.<span class="number">21</span>.<span class="number">6</span>.<span class="number">250</span>  <span class="number">172.21.6.250</span>            <span class="number">27</span>          <span class="number">87</span> <span class="number">0</span>.<span class="number">02</span> -         -      elastic-client-<span class="number">0579</span>bf<span class="number">60</span>b<span class="number">8746</span>b<span class="number">365</span>-production</span><br><span class="line"><span class="attribute">172</span>.<span class="number">21</span>.<span class="number">20</span>.<span class="number">96</span>  <span class="number">172.21.20.96</span>             <span class="number">9</span>          <span class="number">84</span> <span class="number">0</span>.<span class="number">01</span> -         -      elastic-client-<span class="number">0</span>a<span class="number">25</span>e<span class="number">84</span>d<span class="number">941</span>f<span class="number">3446</span>f-production</span><br><span class="line"><span class="attribute">172</span>.<span class="number">21</span>.<span class="number">5</span>.<span class="number">138</span>  <span class="number">172.21.5.138</span>            <span class="number">62</span>          <span class="number">99</span> <span class="number">6</span>.<span class="number">83</span> d         -      elastic-data-<span class="number">07</span>aa<span class="number">318</span>c<span class="number">82</span>d<span class="number">04</span>e<span class="number">61</span>c-production</span><br><span class="line"><span class="attribute">172</span>.<span class="number">21</span>.<span class="number">7</span>.<span class="number">188</span>  <span class="number">172.21.7.188</span>            <span class="number">20</span>          <span class="number">93</span> <span class="number">0</span>.<span class="number">00</span> -         *      elastic-master-<span class="number">049</span>a<span class="number">20</span>b<span class="number">4645</span>a<span class="number">557</span>eb-production</span><br><span class="line"><span class="attribute">172</span>.<span class="number">21</span>.<span class="number">21</span>.<span class="number">160</span> <span class="number">172.21.21.160</span>           <span class="number">17</span>          <span class="number">99</span> <span class="number">7</span>.<span class="number">49</span> d         -      elastic-data-<span class="number">0</span>abf<span class="number">96</span>b<span class="number">0106</span>bcba<span class="number">98</span>-production</span><br><span class="line"><span class="attribute">172</span>.<span class="number">21</span>.<span class="number">5</span>.<span class="number">32</span>   <span class="number">172.21.5.32</span>             <span class="number">66</span>          <span class="number">99</span> <span class="number">6</span>.<span class="number">33</span> d         -      elastic-data-<span class="number">05</span>a<span class="number">9</span>cd<span class="number">4</span>cea<span class="number">5328486</span>-production</span><br><span class="line"><span class="attribute">172</span>.<span class="number">21</span>.<span class="number">22</span>.<span class="number">253</span> <span class="number">172.21.22.253</span>           <span class="number">38</span>          <span class="number">85</span> <span class="number">0</span>.<span class="number">00</span> -         m      elastic-master-<span class="number">0644394056</span>fa<span class="number">488</span>a<span class="number">8</span>-production</span><br><span class="line"><span class="attribute">172</span>.<span class="number">21</span>.<span class="number">21</span>.<span class="number">209</span> <span class="number">172.21.21.209</span>           <span class="number">19</span>          <span class="number">99</span> <span class="number">5</span>.<span class="number">47</span> d         -      elastic-data-<span class="number">02</span>b<span class="number">5</span>fb<span class="number">2</span>c<span class="number">632</span>eacc<span class="number">9</span>f-production</span><br><span class="line"><span class="attribute">172</span>.<span class="number">21</span>.<span class="number">39</span>.<span class="number">249</span> <span class="number">172.21.39.249</span>           <span class="number">61</span>          <span class="number">99</span> <span class="number">4</span>.<span class="number">11</span> d         -      elastic-data-<span class="number">09512</span>dff<span class="number">9</span>c<span class="number">057</span>e<span class="number">62</span>b-production</span><br></pre></td></tr></table></figure><h2 id="Stucked-nodes"><a href="#Stucked-nodes" class="headerlink" title="Stucked nodes"></a>Stucked nodes</h2><p>We have come across an issue where one or more nodes get stucked, preventing the master from reallocating shards properly. This has happened multiple times. To find out which nodes are stucked you can either search for the exception <code>ReceiveTimeoutTransportException</code> from logs or search in Kibana if applicable.</p><h2 id="Decomissioning-Node"><a href="#Decomissioning-Node" class="headerlink" title="Decomissioning Node"></a>Decomissioning Node</h2><p>Node decomission is something you do when you “empty” a node, a.k.a, force shard reallocation. To do that just use one of the commands bellow, depending whether you want to exclude a node by <code>ip</code> or by <code>name</code>. Using <code>name</code> is the preferred way.</p><p>Both attributes, ip or name, can be retrieved from the <code>cat-nodes</code> alias mentioned above. You usually want to do this for one host at a time.</p><figure class="highlight roboconf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># exclude by IP</span></span><br><span class="line">curl -XPUT localhost:9200/_cluster/settings -d &#x27;&#123;</span><br><span class="line">  &quot;<span class="attribute">transient&quot;</span> :&#123;</span><br><span class="line">      &quot;cluster<span class="variable">.routing</span><span class="variable">.allocation</span><span class="variable">.exclude</span><span class="variable">._ip</span>&quot; : &quot;172.19.22.9&quot;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;&#x27;</span><br><span class="line"></span><br><span class="line"># exclude by NAME</span><br><span class="line">curl -XPUT localhost:9200/_cluster/settings -d &#x27;&#123;</span><br><span class="line">  &quot;transient&quot; :&#123;</span><br><span class="line">      &quot;cluster<span class="variable">.routing</span><span class="variable">.allocation</span><span class="variable">.exclude</span><span class="variable">._name</span>&quot; : &quot;elastic-data-05a4f739b4b5fc4ab&quot;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;&#x27;</span><br></pre></td></tr></table></figure><p>More info could be found: <span class="exturl" data-url="aHR0cHM6Ly93d3cuZWxhc3RpYy5jby9ndWlkZS9lbi9lbGFzdGljc2VhcmNoL3JlZmVyZW5jZS9jdXJyZW50L2FsbG9jYXRpb24tZmlsdGVyaW5nLmh0bWw=">ElasticSearch - cluster-level shard allocation-filtering<i class="fa fa-external-link-alt"></i></span></p><h2 id="Rolling-restart"><a href="#Rolling-restart" class="headerlink" title="Rolling restart"></a>Rolling restart</h2><ol><li>Disable shard allocation</li></ol><figure class="highlight ada"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">curl -XPUT localhost:<span class="number">9200</span>/_cluster/settings -d &#x27;&#123;</span><br><span class="line"><span class="string">&quot;transient&quot;</span> : &#123;</span><br><span class="line"><span class="string">&quot;cluster.routing.allocation.enable&quot;</span> : &quot;<span class="type">none</span><span class="string">&quot;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">&#125;&#x27;</span></span><br></pre></td></tr></table></figure><ol start="2"><li><p>Stop node (check the alias command on the host to stop a node)</p></li><li><p>Perform maintenance / upgrade</p></li><li><p>Restart the node, and confirm that it joined the cluster.</p></li><li><p>Re-enable shard allocation as follows:</p></li></ol><figure class="highlight ada"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">curl -XPUT localhost:<span class="number">9200</span>/_cluster/settings -d &#x27;&#123;</span><br><span class="line"><span class="string">&quot;transient&quot;</span> : &#123;</span><br><span class="line"><span class="string">&quot;cluster.routing.allocation.enable&quot;</span> : &quot;<span class="type">all</span><span class="string">&quot;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">&#125;&#x27;</span></span><br></pre></td></tr></table></figure><p>The above steps can also be done from <code>ansible playbook</code> as follows:</p><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Perform</span> <span class="string">a</span> <span class="string">rolling</span> <span class="string">restart</span> <span class="string">of</span> <span class="string">an</span> <span class="string">Elasticsearch</span> <span class="string">cluster</span></span><br><span class="line">  <span class="attr">gather_facts:</span> <span class="literal">no</span></span><br><span class="line">  <span class="attr">hosts:</span> <span class="string">elastic-client-es7:elastic-master-es7:elastic-data-warm-es7:elastic-data-hot-es7</span></span><br><span class="line">  <span class="attr">serial:</span> <span class="number">1</span></span><br><span class="line">  <span class="attr">become:</span> <span class="literal">yes</span></span><br><span class="line"></span><br><span class="line">  <span class="attr">vars_prompt:</span></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">&quot;service&quot;</span></span><br><span class="line">      <span class="attr">prompt:</span> <span class="string">&quot;Enter the cluster name (eg. A|B|C)&quot;</span></span><br><span class="line">      <span class="attr">private:</span> <span class="literal">no</span></span><br><span class="line">      <span class="attr">default:</span> <span class="string">&quot;unknown&quot;</span></span><br><span class="line"></span><br><span class="line">  <span class="attr">vars:</span></span><br><span class="line">    <span class="attr">es_disable_allocation:</span> <span class="string">&#x27;&#123;&quot;transient&quot;:&#123;&quot;cluster.routing.allocation.enable&quot;: &quot;none&quot;&#125;&#125;&#x27;</span></span><br><span class="line">    <span class="attr">es_enable_allocation:</span> <span class="string">&#x27;&#123;&quot;transient&quot;:&#123;&quot;cluster.routing.allocation.enable&quot;: &quot;all&quot;&#125;&#125;&#x27;</span></span><br><span class="line">    <span class="attr">es_http_port:</span> <span class="number">9200</span></span><br><span class="line">    <span class="attr">es_transport_port:</span> <span class="number">9300</span></span><br><span class="line"></span><br><span class="line">  <span class="attr">tasks:</span></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Ensure</span> <span class="string">Elasticsearch</span> <span class="string">is</span> <span class="string">running</span> <span class="string">on</span> <span class="string">node</span></span><br><span class="line">      <span class="attr">service:</span> <span class="string">name=elasticsearch-&#123;&#123;</span> <span class="string">service</span> <span class="string">&#125;&#125;</span> <span class="string">enabled=yes</span> <span class="string">state=started</span></span><br><span class="line">      <span class="attr">register:</span> <span class="string">response</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Wait</span> <span class="string">for</span> <span class="string">node</span> <span class="string">to</span> <span class="string">come</span> <span class="string">back</span> <span class="string">up</span> <span class="string">if</span> <span class="string">it</span> <span class="string">was</span> <span class="string">stopped</span></span><br><span class="line">      <span class="attr">wait_for:</span> <span class="string">port=&#123;&#123;</span> <span class="string">es_transport_port</span> <span class="string">&#125;&#125;</span> <span class="string">delay=45</span></span><br><span class="line">      <span class="attr">when:</span> <span class="string">response.changed</span> <span class="string">==</span> <span class="literal">true</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># the ansible the uri action needs httplib2</span></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">ensure</span> <span class="string">python-httplib2</span> <span class="string">is</span> <span class="string">installed</span></span><br><span class="line">      <span class="attr">apt:</span> <span class="string">name=python-httplib2</span> <span class="string">state=present</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Check</span> <span class="string">current</span> <span class="string">version</span></span><br><span class="line">      <span class="attr">uri:</span> <span class="string">url=http://localhost:&#123;&#123;</span> <span class="string">es_http_port</span> <span class="string">&#125;&#125;</span> <span class="string">method=GET</span></span><br><span class="line">      <span class="attr">register:</span> <span class="string">version_found</span></span><br><span class="line">      <span class="attr">retries:</span> <span class="number">10</span></span><br><span class="line">      <span class="attr">delay:</span> <span class="number">10</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Display</span> <span class="string">current</span> <span class="string">Elasticsearch</span> <span class="string">version</span></span><br><span class="line">      <span class="attr">debug:</span> <span class="string">var=version_found.json.version.number</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># enable first - in case the shards were disabled before for some reason.</span></span><br><span class="line">    <span class="comment"># it&#x27;s important because the next task will wait for the cluster to become green.</span></span><br><span class="line">    <span class="comment"># If the shard allocation is disabled the cluster will stay yellow and the tasks will hang until it times out.</span></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Enable</span> <span class="string">shard</span> <span class="string">allocation</span> <span class="string">for</span> <span class="string">the</span> &#123;&#123; <span class="string">service</span> &#125;&#125; <span class="string">cluster</span></span><br><span class="line">      <span class="attr">uri:</span></span><br><span class="line">        <span class="attr">url:</span> <span class="string">http://localhost:&#123;&#123;</span> <span class="string">es_http_port</span> <span class="string">&#125;&#125;/_cluster/settings</span></span><br><span class="line">        <span class="attr">method:</span> <span class="string">PUT</span></span><br><span class="line">        <span class="attr">body_format:</span> <span class="string">json</span></span><br><span class="line">        <span class="attr">body:</span> <span class="string">&quot;<span class="template-variable">&#123;&#123; es_enable_allocation &#125;&#125;</span>&quot;</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Wait</span> <span class="string">for</span> <span class="string">cluster</span> <span class="string">health</span> <span class="string">to</span> <span class="string">return</span> <span class="string">to</span> <span class="string">green</span></span><br><span class="line">      <span class="attr">uri:</span></span><br><span class="line">        <span class="attr">url:</span> <span class="string">http://localhost:&#123;&#123;</span> <span class="string">es_http_port</span> <span class="string">&#125;&#125;/_cluster/health</span></span><br><span class="line">        <span class="attr">method:</span> <span class="string">GET</span></span><br><span class="line">      <span class="attr">register:</span> <span class="string">response</span></span><br><span class="line">      <span class="attr">until:</span> <span class="string">&quot;response.json.status == &#x27;green&#x27;&quot;</span></span><br><span class="line">      <span class="attr">retries:</span> <span class="number">500</span></span><br><span class="line">      <span class="attr">delay:</span> <span class="number">15</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Disable</span> <span class="string">shard</span> <span class="string">allocation</span> <span class="string">for</span> <span class="string">the</span> &#123;&#123; <span class="string">service</span> &#125;&#125; <span class="string">cluster</span></span><br><span class="line">      <span class="attr">uri:</span></span><br><span class="line">        <span class="attr">url:</span> <span class="string">http://localhost:&#123;&#123;</span> <span class="string">es_http_port</span> <span class="string">&#125;&#125;/_cluster/settings</span></span><br><span class="line">        <span class="attr">method:</span> <span class="string">PUT</span></span><br><span class="line">        <span class="attr">body_format:</span> <span class="string">json</span></span><br><span class="line">        <span class="attr">body:</span> <span class="string">&quot;<span class="template-variable">&#123;&#123; es_disable_allocation &#125;&#125;</span>&quot;</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Restart</span> <span class="string">elasticsearch-&#123;&#123;</span> <span class="string">service</span> <span class="string">&#125;&#125;</span> <span class="string">on</span> <span class="string">node</span></span><br><span class="line">      <span class="attr">service:</span> <span class="string">name=elasticsearch-&#123;&#123;</span> <span class="string">service</span> <span class="string">&#125;&#125;</span> <span class="string">enabled=yes</span> <span class="string">state=restarted</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Wait</span> <span class="string">for</span> <span class="string">node</span> <span class="string">to</span> <span class="string">come</span> <span class="string">back</span> <span class="string">up</span></span><br><span class="line">      <span class="attr">wait_for:</span> <span class="string">port=&#123;&#123;</span> <span class="string">es_transport_port</span> <span class="string">&#125;&#125;</span> <span class="string">delay=35</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Wait</span> <span class="string">for</span> <span class="string">http</span> <span class="string">to</span> <span class="string">come</span> <span class="string">back</span> <span class="string">up</span></span><br><span class="line">      <span class="attr">wait_for:</span> <span class="string">port=&#123;&#123;</span> <span class="string">es_http_port</span> <span class="string">&#125;&#125;</span> <span class="string">delay=5</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Wait</span> <span class="string">for</span> <span class="string">cluster</span> <span class="string">health</span> <span class="string">to</span> <span class="string">return</span> <span class="string">to</span> <span class="string">yellow</span> <span class="string">or</span> <span class="string">green</span></span><br><span class="line">      <span class="attr">uri:</span> <span class="string">url=http://localhost:&#123;&#123;</span> <span class="string">es_http_port</span> <span class="string">&#125;&#125;/_cluster/health</span> <span class="string">method=GET</span></span><br><span class="line">      <span class="attr">register:</span> <span class="string">response</span></span><br><span class="line">      <span class="attr">until:</span> <span class="string">&quot;response.json.status == &#x27;yellow&#x27; or response.json.status == &#x27;green&#x27;&quot;</span></span><br><span class="line">      <span class="attr">retries:</span> <span class="number">500</span></span><br><span class="line">      <span class="attr">delay:</span> <span class="number">15</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Re-enable</span> <span class="string">shard</span> <span class="string">allocation</span> <span class="string">for</span> <span class="string">the</span> &#123;&#123; <span class="string">service</span> &#125;&#125; <span class="string">cluster</span></span><br><span class="line">      <span class="attr">uri:</span></span><br><span class="line">        <span class="attr">url:</span> <span class="string">http://localhost:&#123;&#123;</span> <span class="string">es_http_port</span> <span class="string">&#125;&#125;/_cluster/settings</span></span><br><span class="line">        <span class="attr">method:</span> <span class="string">PUT</span></span><br><span class="line">        <span class="attr">body_format:</span> <span class="string">json</span></span><br><span class="line">        <span class="attr">body:</span> <span class="string">&quot;<span class="template-variable">&#123;&#123; es_enable_allocation &#125;&#125;</span>&quot;</span></span><br><span class="line">      <span class="attr">register:</span> <span class="string">response</span></span><br><span class="line">      <span class="attr">until:</span> <span class="string">&quot;response.json.acknowledged == true&quot;</span></span><br><span class="line">      <span class="attr">retries:</span> <span class="number">10</span></span><br><span class="line">      <span class="attr">delay:</span> <span class="number">15</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Wait</span> <span class="string">for</span> <span class="string">the</span> <span class="string">node</span> <span class="string">to</span> <span class="string">recover</span></span><br><span class="line">      <span class="attr">uri:</span></span><br><span class="line">        <span class="attr">url:</span> <span class="string">http://localhost:&#123;&#123;</span> <span class="string">es_http_port</span> <span class="string">&#125;&#125;/_cat/health</span></span><br><span class="line">        <span class="attr">method:</span> <span class="string">GET</span></span><br><span class="line">        <span class="attr">return_content:</span> <span class="literal">yes</span></span><br><span class="line">      <span class="attr">register:</span> <span class="string">response</span></span><br><span class="line">      <span class="attr">until:</span> <span class="string">&quot;&#x27;green&#x27; in response.content&quot;</span></span><br><span class="line">      <span class="attr">retries:</span> <span class="number">500</span></span><br><span class="line">      <span class="attr">delay:</span> <span class="number">30</span></span><br></pre></td></tr></table></figure><p>Then we can run the ansible playbook to perform the rolling restart for Elasticsearch</p><figure class="highlight livescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">ansible-playbook <span class="string">\</span></span><br><span class="line">-i inventory/aws/production/a-us-west-<span class="number">2</span> <span class="string">\</span></span><br><span class="line">--ask-become-pass <span class="string">\</span></span><br><span class="line">playbooks/restart_elasticsearch_es7.yml</span><br></pre></td></tr></table></figure>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;ElasticSearch can be a beast to manage. Knowing the most used endpoints during outages or simple maintenance by heart can be as challenging as it is time consuming. Because of this, this How-To article will layout some handy commands. In theory you could run them from any given node (data, client or master), however I’d recommend running them from a master node.&lt;/p&gt;
&lt;p&gt;ssh into any master node (pro-tip: master instances are the ones within the master autoscaling group)&lt;/p&gt;
&lt;p&gt;e.g.&lt;/p&gt;
&lt;figure class=&quot;highlight css&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;1&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;selector-tag&quot;&gt;ssh&lt;/span&gt; &lt;span class=&quot;selector-tag&quot;&gt;elastic-master-&lt;/span&gt;&amp;lt;&lt;span class=&quot;selector-tag&quot;&gt;instance-id&lt;/span&gt;&amp;gt;.&amp;lt;&lt;span class=&quot;selector-tag&quot;&gt;aws-region&lt;/span&gt;&amp;gt;&lt;span class=&quot;selector-class&quot;&gt;.x&lt;/span&gt;&lt;span class=&quot;selector-class&quot;&gt;.y&lt;/span&gt;&lt;span class=&quot;selector-class&quot;&gt;.com&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;

&lt;h2 id=&quot;Handy-aliases&quot;&gt;&lt;a href=&quot;#Handy-aliases&quot; class=&quot;headerlink&quot; title=&quot;Handy aliases&quot;&gt;&lt;/a&gt;Handy &lt;code&gt;aliases&lt;/code&gt;&lt;/h2&gt;&lt;figure class=&quot;highlight pgsql&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;1&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;2&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;3&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;4&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;5&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;6&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;7&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;8&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;9&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;10&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;11&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;12&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;13&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;14&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;15&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;alias&lt;/span&gt; tail-es=&lt;span class=&quot;string&quot;&gt;&amp;#x27;tail -500f /var/log/elasticsearch/&amp;lt;ES_NAME&amp;gt;/&amp;lt;ES_Name&amp;gt;.log&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;alias&lt;/span&gt; cat-nodes=&lt;span class=&quot;string&quot;&gt;&amp;#x27;curl -s -XGET http://localhost:9200/_cat/nodes?v&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;alias&lt;/span&gt; cat-shards=&lt;span class=&quot;string&quot;&gt;&amp;#x27;curl -s -XGET http://localhost:9200/_cat/shards?h=index,shard,prirep,state,unassigned.reason&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;alias&lt;/span&gt; cat-allocation=&lt;span class=&quot;string&quot;&gt;&amp;#x27;curl -s -XGET http://localhost:9200/_cat/allocation?v&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;alias&lt;/span&gt; cat-indices=&lt;span class=&quot;string&quot;&gt;&amp;#x27;curl -s -XGET http://localhost:9200/_cat/indices?v&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;alias&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;get&lt;/span&gt;-templates=&lt;span class=&quot;string&quot;&gt;&amp;#x27;curl -s -XGET http://localhost:9200/_template?pretty&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;alias&lt;/span&gt; cat-settings=&lt;span class=&quot;string&quot;&gt;&amp;#x27;curl -s -XGET http://localhost:9200/_cluster/settings/?pretty&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;alias&lt;/span&gt; cat-settings-&lt;span class=&quot;keyword&quot;&gt;all&lt;/span&gt;=&lt;span class=&quot;string&quot;&gt;&amp;#x27;curl -s -XGET &amp;#x27;&lt;/span&gt;\&lt;span class=&quot;string&quot;&gt;&amp;#x27;&amp;#x27;&lt;/span&gt;http://localhost:&lt;span class=&quot;number&quot;&gt;9200&lt;/span&gt;/_cluster/settings?include_defaults=&lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;&amp;amp;pretty&lt;span class=&quot;string&quot;&gt;&amp;#x27;\&amp;#x27;&amp;#x27; | jq .&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;alias&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;cluster&lt;/span&gt;-status=&lt;span class=&quot;string&quot;&gt;&amp;#x27;curl -s -XGET http://localhost:9200/_cluster/health?pretty&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;alias&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;check&lt;/span&gt;-status=&lt;span class=&quot;string&quot;&gt;&amp;#x27;while true; do sleep 5; cluster-status | grep status; done&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;alias&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;disable&lt;/span&gt;-allocation=&lt;span class=&quot;string&quot;&gt;&amp;#x27;curl -XPUT localhost:9200/_cluster/settings -d &amp;#x27;&lt;/span&gt;\&lt;span class=&quot;string&quot;&gt;&amp;#x27;&amp;#x27;&lt;/span&gt;&amp;#123;&amp;quot;transient&amp;quot; : \&amp;#123;&amp;quot;cluster.routing.allocation.enable&amp;quot; : &amp;quot;none&amp;quot;&amp;#125;&amp;#125;&lt;span class=&quot;string&quot;&gt;&amp;#x27;\&amp;#x27;&amp;#x27;&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;alias&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;enable&lt;/span&gt;-allocation=&lt;span class=&quot;string&quot;&gt;&amp;#x27;curl -XPUT localhost:9200/_cluster/settings -d &amp;#x27;&lt;/span&gt;\&lt;span class=&quot;string&quot;&gt;&amp;#x27;&amp;#x27;&lt;/span&gt;&amp;#123;&amp;quot;transient&amp;quot; : \&amp;#123;&amp;quot;cluster.routing.allocation.enable&amp;quot; : &amp;quot;all&amp;quot;&amp;#125;&amp;#125;&lt;span class=&quot;string&quot;&gt;&amp;#x27;\&amp;#x27;&amp;#x27;&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;alias&lt;/span&gt; cat-shards-unassigned=&lt;span class=&quot;string&quot;&gt;&amp;#x27;curl -s -XGET http://localhost:9200/_cat/shards | grep UNASSIGNED | awk &amp;#x27;&lt;/span&gt;\&lt;span class=&quot;string&quot;&gt;&amp;#x27;&amp;#x27;&lt;/span&gt;&amp;#123;print &lt;span class=&quot;meta&quot;&gt;$1&lt;/span&gt;&amp;#125;&lt;span class=&quot;string&quot;&gt;&amp;#x27;\&amp;#x27;&amp;#x27;&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;alias&lt;/span&gt; del-shards-unassigned=&lt;span class=&quot;string&quot;&gt;&amp;#x27;curl -s -XGET http://localhost:9200/_cat/shards | grep UNASSIGNED | awk &amp;#x27;&lt;/span&gt;\&lt;span class=&quot;string&quot;&gt;&amp;#x27;&amp;#x27;&lt;/span&gt;&amp;#123;print &lt;span class=&quot;meta&quot;&gt;$1&lt;/span&gt;&amp;#125;&lt;span class=&quot;string&quot;&gt;&amp;#x27;\&amp;#x27;&amp;#x27; | xargs -i curl -s -XDELETE &amp;quot;http://localhost:9200/&amp;#123;&amp;#125;&amp;quot;&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;alias&lt;/span&gt; cat-&lt;span class=&quot;keyword&quot;&gt;snapshot&lt;/span&gt;=&lt;span class=&quot;string&quot;&gt;&amp;#x27;curl -s -XGET localhost:9200/_snapshot/&amp;lt;S3_BUCKET_FOR_SNAPSHOT_REPOSITORY&amp;gt;/_all?pretty | jq .snapshots[-1]&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;
    
    </summary>
    
    
      <category term="SRE" scheme="http://davidlu1001.github.io/categories/SRE/"/>
    
      <category term="ElasticSearch" scheme="http://davidlu1001.github.io/categories/ElasticSearch/"/>
    
    
      <category term="ElasticSearch" scheme="http://davidlu1001.github.io/tags/ElasticSearch/"/>
    
      <category term="SRE" scheme="http://davidlu1001.github.io/tags/SRE/"/>
    
  </entry>
  
  <entry>
    <title>Continuous Delivery Performance Testing</title>
    <link href="http://davidlu1001.github.io/2020/04/13/Continuous-Delivery-Performance-Testing/"/>
    <id>http://davidlu1001.github.io/2020/04/13/Continuous-Delivery-Performance-Testing/</id>
    <published>2020-04-13T10:08:58.000Z</published>
    <updated>2020-04-13T10:08:58.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Introduction"><a href="#Introduction" class="headerlink" title="Introduction"></a>Introduction</h1><p>This blog describes performance testing results of the continuous delivery (CD) workflow. Testing was performed to answer two questions:</p><ol><li>Which Docker storage engine is optimal for the continuous delivery workflow?</li><li>Which EC2 instance types perform best in terms of price and performance?</li></ol><h1 id="Test-Conditions"><a href="#Test-Conditions" class="headerlink" title="Test Conditions"></a>Test Conditions</h1><p>All testing was performed with the following conditions:</p><ul><li>Ubuntu <code>15.10</code></li><li>Docker <code>1.10.3</code></li><li>Docker Compose <code>1.6.2</code></li><li>All required Docker images cached in a local registry mirror to minimise network variations</li><li>All required Python wheels cached in a local Devpi mirror to minimise network variations</li><li>Empty docker data volume (/var/lib/docker) as starting state for each test</li></ul><p>Testing was performed for the internal web application. The test stage that was executed includes the following tasks:</p><ul><li>Pull base image</li><li>Build development image - this includes installing OS packages, building Python wheels and installing Python wheels</li><li>Run unit and integration tests</li></ul><a id="more"></a><h1 id="Docker-Storage-Engine-Comparison"><a href="#Docker-Storage-Engine-Comparison" class="headerlink" title="Docker Storage Engine Comparison"></a>Docker Storage Engine Comparison</h1><p>The Docker storage engine comparison was performed on a single EC2 instance with the following configuration:</p><ul><li>c4.large instance</li><li>2 x 16GB SSD disks attached as instance stores configure in a software RAID0 configuration unless otherwise noted. These disks were used for Docker data storage (i.e. mounted at /var/lib/docker)</li><li>ext4 file system used unless otherwise noted</li><li>30GB EBS volume attached as main OS volume</li></ul><h2 id="Test-Scenarios"><a href="#Test-Scenarios" class="headerlink" title="Test Scenarios"></a>Test Scenarios</h2><p>The following table describes each scenario that was tested.</p><p>It should be noted that some storage engines are more complex to setup than others. A low complexity rating means minimal configuration is required to enable the storage engine, whereas a high complexity rating means significant configuration is required to enable the storage engine (e.g. requires separate block storage)</p><table><thead><tr><th>Scenario</th><th align="center">Description</th><th align="center">Complexity</th><th align="right">More Information</th></tr></thead><tbody><tr><td>ZFS</td><td align="center">ZFS storage engine using software RAID0 (zfs file system)</td><td align="center">High</td><td align="right"><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vZW5naW5lL3VzZXJndWlkZS9zdG9yYWdlZHJpdmVyL3pmcy1kcml2ZXIv">https://docs.docker.com/engine/userguide/storagedriver/zfs-driver/<i class="fa fa-external-link-alt"></i></span><br><span class="exturl" data-url="aHR0cHM6Ly93aWtpLnVidW50dS5jb20vS2VybmVsL1JlZmVyZW5jZS9aRlM=">https://wiki.ubuntu.com/Kernel/Reference/ZFS<i class="fa fa-external-link-alt"></i></span></td></tr><tr><td>ZFS (ZRAID0)</td><td align="center">ZFS storage engine using native ZFS disk striping (zfs file system)</td><td align="center">High</td><td align="right"><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vZW5naW5lL3VzZXJndWlkZS9zdG9yYWdlZHJpdmVyL3pmcy1kcml2ZXIv">https://docs.docker.com/engine/userguide/storagedriver/zfs-driver/<i class="fa fa-external-link-alt"></i></span><br><span class="exturl" data-url="aHR0cHM6Ly93aWtpLnVidW50dS5jb20vS2VybmVsL1JlZmVyZW5jZS9aRlM=">https://wiki.ubuntu.com/Kernel/Reference/ZFS<i class="fa fa-external-link-alt"></i></span></td></tr><tr><td>Device Mapper (direct-lvm)</td><td align="center">Device Mapper storage engine using recommended direct-lvm mode</td><td align="center">High</td><td align="right"><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vZW5naW5lL3VzZXJndWlkZS9zdG9yYWdlZHJpdmVyL2RldmljZS1tYXBwZXItZHJpdmVyLyNjb25maWd1cmUtZGlyZWN0LWx2bS1tb2RlLWZvci1wcm9k">https://docs.docker.com/engine/userguide/storagedriver/device-mapper-driver/#configure-direct-lvm-mode-for-prod<i class="fa fa-external-link-alt"></i></span></td></tr><tr><td>Device Mapper (loop)</td><td align="center">Device Mapper storage engine using default loop mode.<br>This is the default storage engine on Ubuntu 15.10 and many Linux distributions.</td><td align="center">Low</td><td align="right"><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vZW5naW5lL3VzZXJndWlkZS9zdG9yYWdlZHJpdmVyL2RldmljZS1tYXBwZXItZHJpdmVyLyNkb2NrZXItYW5kLXRoZS1kZXZpY2UtbWFwcGVyLXN0b3I=">https://docs.docker.com/engine/userguide/storagedriver/device-mapper-driver/#docker-and-the-device-mapper-stor<i class="fa fa-external-link-alt"></i></span></td></tr><tr><td>BTRFS</td><td align="center">BTRFS storage engine (btrfs file system)</td><td align="center">Higg</td><td align="right"><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vZW5naW5lL3VzZXJndWlkZS9zdG9yYWdlZHJpdmVyL2J0cmZzLWRyaXZlci8=">https://docs.docker.com/engine/userguide/storagedriver/btrfs-driver/<i class="fa fa-external-link-alt"></i></span></td></tr><tr><td>Overlay</td><td align="center">Overlay storage engine</td><td align="center">Low</td><td align="right"><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vZW5naW5lL3VzZXJndWlkZS9zdG9yYWdlZHJpdmVyL292ZXJsYXlmcy1kcml2ZXIv">https://docs.docker.com/engine/userguide/storagedriver/overlayfs-driver/<i class="fa fa-external-link-alt"></i></span></td></tr></tbody></table><p>P.S.</p><blockquote><p>The original Docker storage engine, AUFS, was not considered given it is not supported in mainline Linux kernel.</p></blockquote><h2 id="Test-Results"><a href="#Test-Results" class="headerlink" title="Test Results"></a>Test Results</h2><p>The following chart illustrates the results:</p><p><img data-src="https://upload-images.jianshu.io/upload_images/3113154-3a0cbe9cba880e99.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt=""></p><h2 id="Issues"><a href="#Issues" class="headerlink" title="Issues"></a>Issues</h2><p>Initial testing was performed using the xfs file system, however this caused a failure during Docker image building when using the Overlay storage driver. This triggered a decision to use ext4 instead as the standard file system.</p><h1 id="EC2-Instance-Type-Comparison"><a href="#EC2-Instance-Type-Comparison" class="headerlink" title="EC2 Instance Type Comparison"></a>EC2 Instance Type Comparison</h1><p>Testing was performed on various EC2 instance types to determine which EC2 instance types perform the best, both in terms of raw performance and price / performance.</p><p>The price / performance comparison was calculated by computing the base cost of how much each run (in terms of time taken) cost on each instance type. Additional storage costs were not included in the calculation.</p><h2 id="Test-Scenarios-1"><a href="#Test-Scenarios-1" class="headerlink" title="Test Scenarios"></a>Test Scenarios</h2><p>The following table describes each scenario that was tested.</p><table><thead><tr><th>Scenario</th><th align="center">Instance Cost Per Hour (USD)</th><th align="right">Description</th></tr></thead><tbody><tr><td>m3.medium (EBS)</td><td align="center">0.093</td><td align="right">M3 Medium instance with single 30GB EBS Volume</td></tr><tr><td>r3.large (SSD)</td><td align="center">0.20 + additional storage</td><td align="right">R3 Large instance with single 30GB EBS Volume and 30GB SSD instance store volume.<br>The SSD instance store was used for the Docker data volume (/var/lib/docker)</td></tr><tr><td>t2.medium (EBS)</td><td align="center">0.08</td><td align="right">T2 Medium instance with single 30GB EBS Volume</td></tr><tr><td>c3.large (SSD)</td><td align="center">0.132 + additional storage</td><td align="right">C3 Large instance with single 30GB EBS Volume and 2x16GB SSD instance store volumes configured in software RAID0 configuration.<br>The SSD RAID0 volume was used for the Docker data volume (/var/lib/docker)</td></tr><tr><td>c4.large (EBS)</td><td align="center">0.137</td><td align="right">C4 Large instance with single 30GB EBS Volume</td></tr><tr><td>c4.large (Separate EBS)</td><td align="center">0.137 + additional storage</td><td align="right">C4 Large instance with separate 30GB EBS Volume for Docker data volume (/var/lib/docker)</td></tr><tr><td>c4.large (RAID0 EBS</td><td align="center">0.137 + additional storage</td><td align="right">C4 Large instance with 2 x separate 30GB EBS Volume configured in software RAID0 for Docker data volume (/var/lib/docker)</td></tr></tbody></table><h2 id="Test-Results-Performance"><a href="#Test-Results-Performance" class="headerlink" title="Test Results (Performance)"></a>Test Results (Performance)</h2><p>The following chart illustrates the results in terms of performance:</p><p><img data-src="https://upload-images.jianshu.io/upload_images/3113154-2595c65d154f95da.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt=""></p><h2 id="Test-Results-Price-Performance"><a href="#Test-Results-Price-Performance" class="headerlink" title="Test Results (Price / Performance)"></a>Test Results (Price / Performance)</h2><p>The following chart illustrates the results in terms of price / performance:</p><p><img data-src="https://upload-images.jianshu.io/upload_images/3113154-d0ab2c63ce95e49c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt=""></p><h1 id="Conclusions"><a href="#Conclusions" class="headerlink" title="Conclusions"></a>Conclusions</h1><h2 id="Storage-Engine"><a href="#Storage-Engine" class="headerlink" title="Storage Engine"></a>Storage Engine</h2><p>The <code>overlay storage engine</code> appears to provide the best performance for the tested continuous delivery workflow.</p><p>Given the overlay storage engine has low complexity and is easy to configure out of the box, this is the recommended storage engine for continuous delivery workflows.</p><p>An interesting observation was the performance of DeviceMapper direct-lvm mode (this is the storage engine configuration for AWS Linux ECS-optimised instances) vs loop mode. In principle, direct-lvm mode should offer better performance but this was not observed.</p><h2 id="EC2-Instance-Type-Price-Performance"><a href="#EC2-Instance-Type-Price-Performance" class="headerlink" title="EC2 Instance Type Price / Performance"></a>EC2 Instance Type Price / Performance</h2><p>The testing clearly discounted m3.medium and r3.large instance types as not suitable for the Continuous Delivery workflow. The m3.medium instance took twice as long to execute, whilst the r3.large performance was comparable to the t2.medium performance at 2.5x the price.</p><p>Interestingly, the use of SSD instance stores did not result in any performance improvements and the best performance result was achieved on a c4.large with EBS storage.</p><p>The use of dedicated and/or multiple EBS volumes configured in a RAID0 configuration also did not result in any measurable increase in performance.</p><p>The t2.medium instance type offers the best value for money of the instance types tested, however offers burst CPU performance that can be exhausted, and the impact of running continuous delivery workflows in terms of CPU credits must be further understood.</p><p>The <code>c4.large</code> instance type offers the best performance of the instance types and was second most cost effective. This instance type is tailored towards CPU intensive workloads, offering better peak performance than the t2.medium (8 ECUs vs 6.5 ECUs) and does not incur the risk of exhausting CPU credits. The c4.large also is EBS optimised with 500Mbps dedicated EBS bandwidth and support enhanced networking (t2.medium does not support either of these enhancements).</p><h2 id="End-to-End-Workflow-Testing"><a href="#End-to-End-Workflow-Testing" class="headerlink" title="End-to-End Workflow Testing"></a>End-to-End Workflow Testing</h2><p>Based upon <code>t2.medium</code> and <code>c4.large</code> being identified as the preferred instance types, additional testing of the workflow was conducted for these instance types. All testing was performed using the overlay storage driver.</p><p>The end-to-end workflow and results are described in the table below. Note the make tag and make publish steps were excluded as these steps are dependent on network performance to Docker Hub.</p><table><thead><tr><th>Workflow Step</th><th align="center">c4.large (seconds)</th><th align="center">t2.medium (seconds)</th><th align="right">Description</th></tr></thead><tbody><tr><td>make test</td><td align="center">299</td><td align="center">367</td><td align="right"><ul><li> Pull base image</li><li> Build development image - this includes installed OS packages, building Python wheels and installing Python wheels </li><li> Run unit and integration tests</td></tr><tr><td>make database aws</td><td align="center">38</td><td align="center">36</td><td align="right">Mount database volume from EBS snapshot</td></tr><tr><td>make toast</td><td align="center">153</td><td align="center">150</td><td align="right">Warms database volume</td></tr><tr><td>make release</td><td align="center">333</td><td align="center">363</td><td align="right"><ul><li>Pull release environment and acceptance testing images (mysql, redis, nginx, selenium, robot) </li><li> Build release image </li><li> Start release environment </li><li> Run acceptance tests</td></tr><tr><td>Total Time</td><td align="center">13m43s</td><td align="center">15m16s</td><td align="right"></td></tr><tr><td>Total Run Cost (cents)</td><td align="center">3.13</td><td align="center">2.04</td><td align="right"></td></tr></tbody></table>]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;Introduction&quot;&gt;&lt;a href=&quot;#Introduction&quot; class=&quot;headerlink&quot; title=&quot;Introduction&quot;&gt;&lt;/a&gt;Introduction&lt;/h1&gt;&lt;p&gt;This blog describes performance testing results of the continuous delivery (CD) workflow. Testing was performed to answer two questions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Which Docker storage engine is optimal for the continuous delivery workflow?&lt;/li&gt;
&lt;li&gt;Which EC2 instance types perform best in terms of price and performance?&lt;/li&gt;
&lt;/ol&gt;
&lt;h1 id=&quot;Test-Conditions&quot;&gt;&lt;a href=&quot;#Test-Conditions&quot; class=&quot;headerlink&quot; title=&quot;Test Conditions&quot;&gt;&lt;/a&gt;Test Conditions&lt;/h1&gt;&lt;p&gt;All testing was performed with the following conditions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Ubuntu &lt;code&gt;15.10&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Docker &lt;code&gt;1.10.3&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Docker Compose &lt;code&gt;1.6.2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;All required Docker images cached in a local registry mirror to minimise network variations&lt;/li&gt;
&lt;li&gt;All required Python wheels cached in a local Devpi mirror to minimise network variations&lt;/li&gt;
&lt;li&gt;Empty docker data volume (/var/lib/docker) as starting state for each test&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Testing was performed for the internal web application. The test stage that was executed includes the following tasks:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Pull base image&lt;/li&gt;
&lt;li&gt;Build development image - this includes installing OS packages, building Python wheels and installing Python wheels&lt;/li&gt;
&lt;li&gt;Run unit and integration tests&lt;/li&gt;
&lt;/ul&gt;
    
    </summary>
    
    
      <category term="DevOps" scheme="http://davidlu1001.github.io/categories/DevOps/"/>
    
      <category term="Performance" scheme="http://davidlu1001.github.io/categories/Performance/"/>
    
      <category term="Testing" scheme="http://davidlu1001.github.io/categories/Testing/"/>
    
    
      <category term="DevOps" scheme="http://davidlu1001.github.io/tags/DevOps/"/>
    
      <category term="Performance Testing" scheme="http://davidlu1001.github.io/tags/Performance-Testing/"/>
    
      <category term="CI / CD" scheme="http://davidlu1001.github.io/tags/CI-CD/"/>
    
  </entry>
  
  <entry>
    <title>Elasticsearch important settings</title>
    <link href="http://davidlu1001.github.io/2020/04/08/Elasticsearch-important-settings/"/>
    <id>http://davidlu1001.github.io/2020/04/08/Elasticsearch-important-settings/</id>
    <published>2020-04-08T11:36:06.000Z</published>
    <updated>2020-04-08T11:36:06.000Z</updated>
    
    <content type="html"><![CDATA[<p>Document important settings we shouldn’t be missing in elasticsearch clusters.</p><h1 id="Overview"><a href="#Overview" class="headerlink" title="Overview"></a>Overview</h1><h2 id="Cluster-settings-for-ES"><a href="#Cluster-settings-for-ES" class="headerlink" title="Cluster settings for ES"></a>Cluster settings for ES</h2><p>Could use <code>Kibana</code> Dev Tool to update cluster settings</p><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;persistent&quot;</span> : &#123;</span><br><span class="line">    <span class="attr">&quot;cluster&quot;</span> : &#123;</span><br><span class="line">      <span class="attr">&quot;routing&quot;</span> : &#123;</span><br><span class="line">        <span class="attr">&quot;allocation&quot;</span> : &#123;</span><br><span class="line">          <span class="attr">&quot;cluster_concurrent_rebalance&quot;</span> : <span class="string">&quot;12&quot;</span>,</span><br><span class="line">          <span class="attr">&quot;node_concurrent_recoveries&quot;</span> : <span class="string">&quot;6&quot;</span>,</span><br><span class="line">          <span class="attr">&quot;disk&quot;</span> : &#123;</span><br><span class="line">            <span class="attr">&quot;watermark&quot;</span> : &#123;</span><br><span class="line">              <span class="attr">&quot;high&quot;</span> : <span class="string">&quot;85%&quot;</span></span><br><span class="line">            &#125;</span><br><span class="line">          &#125;,</span><br><span class="line">          <span class="attr">&quot;enable&quot;</span> : <span class="string">&quot;all&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">&quot;indices&quot;</span> : &#123;</span><br><span class="line">      <span class="attr">&quot;recovery&quot;</span> : &#123;</span><br><span class="line">        <span class="attr">&quot;max_bytes_per_sec&quot;</span> : <span class="string">&quot;640mb&quot;</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">&quot;transient&quot;</span> : &#123;</span><br><span class="line">    <span class="attr">&quot;cluster&quot;</span> : &#123;</span><br><span class="line">      <span class="attr">&quot;routing&quot;</span> : &#123;</span><br><span class="line">        <span class="attr">&quot;allocation&quot;</span> : &#123;</span><br><span class="line">          <span class="attr">&quot;cluster_concurrent_rebalance&quot;</span> : <span class="string">&quot;12&quot;</span>,</span><br><span class="line">          <span class="attr">&quot;include&quot;</span> : &#123;</span><br><span class="line">            <span class="attr">&quot;_name&quot;</span> : <span class="string">&quot;elastic-data-*&quot;</span>,</span><br><span class="line">            <span class="attr">&quot;_ip&quot;</span> : <span class="string">&quot;&quot;</span></span><br><span class="line">          &#125;,</span><br><span class="line">          <span class="attr">&quot;node_concurrent_recoveries&quot;</span> : <span class="string">&quot;6&quot;</span>,</span><br><span class="line">          <span class="attr">&quot;balance&quot;</span> : &#123;</span><br><span class="line">            <span class="attr">&quot;threshold&quot;</span> : <span class="string">&quot;1.0f&quot;</span></span><br><span class="line">          &#125;,</span><br><span class="line">          <span class="attr">&quot;enable&quot;</span> : <span class="string">&quot;all&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">&quot;indices&quot;</span> : &#123;</span><br><span class="line">      <span class="attr">&quot;recovery&quot;</span> : &#123;</span><br><span class="line">        <span class="attr">&quot;max_bytes_per_sec&quot;</span> : <span class="string">&quot;640mb&quot;</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><a id="more"></a><h2 id="indices-recovery-max-bytes-per-sec"><a href="#indices-recovery-max-bytes-per-sec" class="headerlink" title="indices.recovery.max_bytes_per_sec"></a>indices.recovery.max_bytes_per_sec</h2><p>Defaults to <code>40mb</code>. Increasing to <code>320mb</code> increases throughput</p><figure class="highlight ada"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">curl -X PUT <span class="string">&quot;localhost:9200/_cluster/settings?pretty&quot;</span> -H <span class="symbol">&#x27;Content</span>-<span class="keyword">Type</span>: application/json&#x27; -d&#x27;</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;persistent&quot;</span> : &#123;</span><br><span class="line">        <span class="string">&quot;indices.recovery.max_bytes_per_sec&quot;</span> : &quot;320<span class="type">mb</span><span class="string">&quot;</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">&#125;&#x27;</span></span><br></pre></td></tr></table></figure><h1 id="cluster-routing-allocation-cluster-concurrent-rebalance"><a href="#cluster-routing-allocation-cluster-concurrent-rebalance" class="headerlink" title="cluster.routing.allocation.cluster_concurrent_rebalance"></a>cluster.routing.allocation.cluster_concurrent_rebalance</h1><p>By default ES will only move <code>2</code> shards at any given time.</p><p>To increase recovery speeds and shard rebalancing we should bump this up.</p><figure class="highlight ada"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">curl -XPUT localhost:<span class="number">9200</span>/_cluster/settings?pretty -H <span class="symbol">&#x27;Content</span>-<span class="keyword">Type</span>: application/json&#x27; -d &#x27;&#123;</span><br><span class="line">  <span class="string">&quot;persistent&quot;</span> : &#123;</span><br><span class="line">    <span class="string">&quot;cluster.routing.allocation.cluster_concurrent_rebalance&quot;</span>: <span class="number">12</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;&#x27;</span><br></pre></td></tr></table></figure>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;Document important settings we shouldn’t be missing in elasticsearch clusters.&lt;/p&gt;
&lt;h1 id=&quot;Overview&quot;&gt;&lt;a href=&quot;#Overview&quot; class=&quot;headerlink&quot; title=&quot;Overview&quot;&gt;&lt;/a&gt;Overview&lt;/h1&gt;&lt;h2 id=&quot;Cluster-settings-for-ES&quot;&gt;&lt;a href=&quot;#Cluster-settings-for-ES&quot; class=&quot;headerlink&quot; title=&quot;Cluster settings for ES&quot;&gt;&lt;/a&gt;Cluster settings for ES&lt;/h2&gt;&lt;p&gt;Could use &lt;code&gt;Kibana&lt;/code&gt; Dev Tool to update cluster settings&lt;/p&gt;
&lt;figure class=&quot;highlight json&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;1&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;2&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;3&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;4&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;5&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;6&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;7&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;8&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;9&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;10&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;11&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;12&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;13&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;14&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;15&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;16&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;17&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;18&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;19&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;20&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;21&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;22&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;23&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;24&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;25&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;26&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;27&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;28&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;29&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;30&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;31&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;32&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;33&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;34&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;35&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;36&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;37&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;38&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;39&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;40&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;41&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;42&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;43&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;44&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;45&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;46&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;attr&quot;&gt;&amp;quot;persistent&amp;quot;&lt;/span&gt; : &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;attr&quot;&gt;&amp;quot;cluster&amp;quot;&lt;/span&gt; : &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;      &lt;span class=&quot;attr&quot;&gt;&amp;quot;routing&amp;quot;&lt;/span&gt; : &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;attr&quot;&gt;&amp;quot;allocation&amp;quot;&lt;/span&gt; : &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;          &lt;span class=&quot;attr&quot;&gt;&amp;quot;cluster_concurrent_rebalance&amp;quot;&lt;/span&gt; : &lt;span class=&quot;string&quot;&gt;&amp;quot;12&amp;quot;&lt;/span&gt;,&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;          &lt;span class=&quot;attr&quot;&gt;&amp;quot;node_concurrent_recoveries&amp;quot;&lt;/span&gt; : &lt;span class=&quot;string&quot;&gt;&amp;quot;6&amp;quot;&lt;/span&gt;,&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;          &lt;span class=&quot;attr&quot;&gt;&amp;quot;disk&amp;quot;&lt;/span&gt; : &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;            &lt;span class=&quot;attr&quot;&gt;&amp;quot;watermark&amp;quot;&lt;/span&gt; : &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;              &lt;span class=&quot;attr&quot;&gt;&amp;quot;high&amp;quot;&lt;/span&gt; : &lt;span class=&quot;string&quot;&gt;&amp;quot;85%&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;            &amp;#125;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;          &amp;#125;,&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;          &lt;span class=&quot;attr&quot;&gt;&amp;quot;enable&amp;quot;&lt;/span&gt; : &lt;span class=&quot;string&quot;&gt;&amp;quot;all&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;        &amp;#125;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;      &amp;#125;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;    &amp;#125;,&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;attr&quot;&gt;&amp;quot;indices&amp;quot;&lt;/span&gt; : &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;      &lt;span class=&quot;attr&quot;&gt;&amp;quot;recovery&amp;quot;&lt;/span&gt; : &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;attr&quot;&gt;&amp;quot;max_bytes_per_sec&amp;quot;&lt;/span&gt; : &lt;span class=&quot;string&quot;&gt;&amp;quot;640mb&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;      &amp;#125;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;    &amp;#125;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;  &amp;#125;,&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;  &lt;span class=&quot;attr&quot;&gt;&amp;quot;transient&amp;quot;&lt;/span&gt; : &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;attr&quot;&gt;&amp;quot;cluster&amp;quot;&lt;/span&gt; : &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;      &lt;span class=&quot;attr&quot;&gt;&amp;quot;routing&amp;quot;&lt;/span&gt; : &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;attr&quot;&gt;&amp;quot;allocation&amp;quot;&lt;/span&gt; : &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;          &lt;span class=&quot;attr&quot;&gt;&amp;quot;cluster_concurrent_rebalance&amp;quot;&lt;/span&gt; : &lt;span class=&quot;string&quot;&gt;&amp;quot;12&amp;quot;&lt;/span&gt;,&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;          &lt;span class=&quot;attr&quot;&gt;&amp;quot;include&amp;quot;&lt;/span&gt; : &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;            &lt;span class=&quot;attr&quot;&gt;&amp;quot;_name&amp;quot;&lt;/span&gt; : &lt;span class=&quot;string&quot;&gt;&amp;quot;elastic-data-*&amp;quot;&lt;/span&gt;,&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;            &lt;span class=&quot;attr&quot;&gt;&amp;quot;_ip&amp;quot;&lt;/span&gt; : &lt;span class=&quot;string&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;          &amp;#125;,&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;          &lt;span class=&quot;attr&quot;&gt;&amp;quot;node_concurrent_recoveries&amp;quot;&lt;/span&gt; : &lt;span class=&quot;string&quot;&gt;&amp;quot;6&amp;quot;&lt;/span&gt;,&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;          &lt;span class=&quot;attr&quot;&gt;&amp;quot;balance&amp;quot;&lt;/span&gt; : &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;            &lt;span class=&quot;attr&quot;&gt;&amp;quot;threshold&amp;quot;&lt;/span&gt; : &lt;span class=&quot;string&quot;&gt;&amp;quot;1.0f&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;          &amp;#125;,&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;          &lt;span class=&quot;attr&quot;&gt;&amp;quot;enable&amp;quot;&lt;/span&gt; : &lt;span class=&quot;string&quot;&gt;&amp;quot;all&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;        &amp;#125;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;      &amp;#125;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;    &amp;#125;,&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;attr&quot;&gt;&amp;quot;indices&amp;quot;&lt;/span&gt; : &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;      &lt;span class=&quot;attr&quot;&gt;&amp;quot;recovery&amp;quot;&lt;/span&gt; : &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;attr&quot;&gt;&amp;quot;max_bytes_per_sec&amp;quot;&lt;/span&gt; : &lt;span class=&quot;string&quot;&gt;&amp;quot;640mb&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;      &amp;#125;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;    &amp;#125;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;  &amp;#125;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;
    
    </summary>
    
    
      <category term="SRE" scheme="http://davidlu1001.github.io/categories/SRE/"/>
    
      <category term="ElasticSearch" scheme="http://davidlu1001.github.io/categories/ElasticSearch/"/>
    
    
      <category term="ElasticSearch" scheme="http://davidlu1001.github.io/tags/ElasticSearch/"/>
    
      <category term="SRE" scheme="http://davidlu1001.github.io/tags/SRE/"/>
    
  </entry>
  
  <entry>
    <title>AWS RDS - Setting up IAM DB Authentication</title>
    <link href="http://davidlu1001.github.io/2020/03/13/AWS-RDS-Setting-up-IAM-DB-Authentication/"/>
    <id>http://davidlu1001.github.io/2020/03/13/AWS-RDS-Setting-up-IAM-DB-Authentication/</id>
    <published>2020-03-13T10:20:12.000Z</published>
    <updated>2020-03-13T10:20:12.000Z</updated>
    
    <content type="html"><![CDATA[<p>Firstly we need to create a RDS database account (user) within the database and associate it to 1-N IAM authentication. Below are just policies samples that the module will create behind the scenes. An example of this can be found here.</p><p>sample policy granting access to a database instance</p><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">   <span class="attr">&quot;Version&quot;</span>: <span class="string">&quot;2012-10-17&quot;</span>,</span><br><span class="line">   <span class="attr">&quot;Statement&quot;</span>: [</span><br><span class="line">      &#123;</span><br><span class="line">         <span class="attr">&quot;Effect&quot;</span>: <span class="string">&quot;Allow&quot;</span>,</span><br><span class="line">         <span class="attr">&quot;Action&quot;</span>: [</span><br><span class="line">             <span class="string">&quot;rds-db:connect&quot;</span></span><br><span class="line">         ],</span><br><span class="line">         <span class="attr">&quot;Resource&quot;</span>: [</span><br><span class="line">             <span class="string">&quot;arn:aws:rds-db:us-west-2:123456789012:dbuser:db-12ABC34DEFG5HIJ6KLMNOP78QR/david_lu&quot;</span></span><br><span class="line">         ]</span><br><span class="line">      &#125;</span><br><span class="line">   ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>sample policy granting access to a cluster</p><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">   <span class="attr">&quot;Version&quot;</span>: <span class="string">&quot;2012-10-17&quot;</span>,</span><br><span class="line">   <span class="attr">&quot;Statement&quot;</span>: [</span><br><span class="line">      &#123;</span><br><span class="line">         <span class="attr">&quot;Effect&quot;</span>: <span class="string">&quot;Allow&quot;</span>,</span><br><span class="line">         <span class="attr">&quot;Action&quot;</span>: [</span><br><span class="line">             <span class="string">&quot;rds-db:connect&quot;</span></span><br><span class="line">         ],</span><br><span class="line">         <span class="attr">&quot;Resource&quot;</span>: [</span><br><span class="line">             <span class="string">&quot;arn:aws:rds-db:us-west-2:123456789012:dbuser:cluster-CO4FHMOYDKJ7CVBEJS2UWDQX7I/david_lu&quot;</span></span><br><span class="line">         ]</span><br><span class="line">      &#125;</span><br><span class="line">   ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><a id="more"></a><p>Resource takes the form <code>arn:aws:rds-db:region:account-id:dbuser:dbi-resource-id/database-user-name</code></p><p>database-user-name is the name of the MySQL database account to associate with IAM authentication. In the example policy, the database account is david_lu.</p><p>With IAM database authentication, we don’t need to assign database passwords to the MySQL user accounts we create. Instead, authentication is handled by AWSAuthenticationPlugin—an AWS-provided plugin that works seamlessly with IAM to authenticate our IAM users.</p><h2 id="For-MySQL"><a href="#For-MySQL" class="headerlink" title="For MySQL"></a>For MySQL</h2><p>To create a database account for MySQL, connect to the DB instance or DB cluster and issue the CREATE USER statement, as shown in the following example. You can additionally narrow the grants down a bit.</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">USER</span> <span class="string">&#x27;iam_sre&#x27;</span> <span class="keyword">IDENTIFIED</span> <span class="keyword">WITH</span> AWSAuthenticationPlugin <span class="keyword">as</span> <span class="string">&#x27;RDS&#x27;</span>;</span><br><span class="line"><span class="keyword">GRANT</span> <span class="keyword">SELECT</span> <span class="keyword">ON</span> sre.* <span class="keyword">TO</span> <span class="string">&#x27;iam_sre&#x27;</span>@<span class="string">&#x27;%&#x27;</span>;</span><br><span class="line"><span class="keyword">FLUSH</span> <span class="keyword">PRIVILEGES</span>;</span><br></pre></td></tr></table></figure><p>The IDENTIFIED WITH clause allows MySQL to use the AWSAuthenticationPlugin to authenticate the database account (david_lu). The AS ‘RDS’ clause maps the jane_doe database account to the corresponding IAM user or role.</p><h2 id="For-Postgres"><a href="#For-Postgres" class="headerlink" title="For Postgres"></a>For Postgres</h2><p>To create a database account for Postgres, connect to the DB instance or DB cluster and issue the CREATE USER / Role statement, as shown in the following example. You can additionally narrow the grants down a bit.</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">ROLE</span> iam_sre <span class="keyword">WITH</span> LOGIN;</span><br><span class="line"><span class="keyword">GRANT</span> rds_iam <span class="keyword">TO</span> iam_sre;</span><br></pre></td></tr></table></figure><p>To connect the new created DB instance from CLI:</p><figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="builtin-name">export</span> <span class="attribute">RDSHOST</span>=<span class="string">&quot;sre-testing.xxxxxxx.ap-southeast-2.rds.amazonaws.com&quot;</span></span><br><span class="line"> </span><br><span class="line"><span class="builtin-name">export</span> <span class="attribute">PGPASSWORD</span>=<span class="string">&quot;<span class="variable">$(aws rds generate-db-auth-token --hostname $RDSHOST --port 5432 --region ap-southeast-2 --username iam_sre)</span>&quot;</span></span><br><span class="line"> </span><br><span class="line">psql <span class="string">&quot;host=<span class="variable">$RDSHOST</span> port=5432 dbname=sre user=iam_sre&quot;</span></span><br></pre></td></tr></table></figure><p>With IAM database authentication, we use an authentication token when we connect to a DB instance or DB cluster. An authentication token is a string of characters that we use instead of a password. Once we generate an authentication token, it’s valid for <code>15</code> minutes before it expires. If we try to connect using an expired token, the connection request is denied.</p><p>Every authentication token must be accompanied by a valid signature, using AWS signature version 4</p><p>We can connect from the command line to an RDS DB instance or Aurora DB cluster with the AWS CLI and mysql command line tool as described following.</p><h1 id="References"><a href="#References" class="headerlink" title="References"></a>References</h1><p><span class="exturl" data-url="aHR0cDovL2RvY3MuYXdzLmFtYXpvbi5jb20vQW1hem9uUkRTL2xhdGVzdC9Vc2VyR3VpZGUvVXNpbmdXaXRoUkRTLklBTURCQXV0aC5FbmFibGluZy5odG1s">http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.Enabling.html<i class="fa fa-external-link-alt"></i></span><br><span class="exturl" data-url="aHR0cDovL2RvY3MuYXdzLmFtYXpvbi5jb20vQW1hem9uUkRTL2xhdGVzdC9Vc2VyR3VpZGUvVXNpbmdXaXRoUkRTLklBTURCQXV0aC5JQU1Qb2xpY3kuaHRtbA==">http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.IAMPolicy.html<i class="fa fa-external-link-alt"></i></span><br><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL0FtYXpvblJEUy9sYXRlc3QvVXNlckd1aWRlL1VzaW5nV2l0aFJEUy5JQU1EQkF1dGguREJBY2NvdW50cy5odG1s">https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.DBAccounts.html<i class="fa fa-external-link-alt"></i></span><br><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL0FtYXpvblJEUy9sYXRlc3QvVXNlckd1aWRlL1VzaW5nV2l0aFJEUy5JQU1EQkF1dGguQ29ubmVjdGluZy5odG1s">https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.Connecting.html<i class="fa fa-external-link-alt"></i></span></p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;Firstly we need to create a RDS database account (user) within the database and associate it to 1-N IAM authentication. Below are just policies samples that the module will create behind the scenes. An example of this can be found here.&lt;/p&gt;
&lt;p&gt;sample policy granting access to a database instance&lt;/p&gt;
&lt;figure class=&quot;highlight json&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;1&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;2&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;3&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;4&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;5&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;6&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;7&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;8&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;9&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;10&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;11&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;12&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;13&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;14&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;   &lt;span class=&quot;attr&quot;&gt;&amp;quot;Version&amp;quot;&lt;/span&gt;: &lt;span class=&quot;string&quot;&gt;&amp;quot;2012-10-17&amp;quot;&lt;/span&gt;,&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;   &lt;span class=&quot;attr&quot;&gt;&amp;quot;Statement&amp;quot;&lt;/span&gt;: [&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;      &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;         &lt;span class=&quot;attr&quot;&gt;&amp;quot;Effect&amp;quot;&lt;/span&gt;: &lt;span class=&quot;string&quot;&gt;&amp;quot;Allow&amp;quot;&lt;/span&gt;,&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;         &lt;span class=&quot;attr&quot;&gt;&amp;quot;Action&amp;quot;&lt;/span&gt;: [&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;             &lt;span class=&quot;string&quot;&gt;&amp;quot;rds-db:connect&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;         ],&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;         &lt;span class=&quot;attr&quot;&gt;&amp;quot;Resource&amp;quot;&lt;/span&gt;: [&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;             &lt;span class=&quot;string&quot;&gt;&amp;quot;arn:aws:rds-db:us-west-2:123456789012:dbuser:db-12ABC34DEFG5HIJ6KLMNOP78QR/david_lu&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;         ]&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;      &amp;#125;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;   ]&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;

&lt;p&gt;sample policy granting access to a cluster&lt;/p&gt;
&lt;figure class=&quot;highlight json&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;1&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;2&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;3&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;4&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;5&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;6&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;7&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;8&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;9&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;10&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;11&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;12&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;13&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;14&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;   &lt;span class=&quot;attr&quot;&gt;&amp;quot;Version&amp;quot;&lt;/span&gt;: &lt;span class=&quot;string&quot;&gt;&amp;quot;2012-10-17&amp;quot;&lt;/span&gt;,&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;   &lt;span class=&quot;attr&quot;&gt;&amp;quot;Statement&amp;quot;&lt;/span&gt;: [&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;      &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;         &lt;span class=&quot;attr&quot;&gt;&amp;quot;Effect&amp;quot;&lt;/span&gt;: &lt;span class=&quot;string&quot;&gt;&amp;quot;Allow&amp;quot;&lt;/span&gt;,&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;         &lt;span class=&quot;attr&quot;&gt;&amp;quot;Action&amp;quot;&lt;/span&gt;: [&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;             &lt;span class=&quot;string&quot;&gt;&amp;quot;rds-db:connect&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;         ],&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;         &lt;span class=&quot;attr&quot;&gt;&amp;quot;Resource&amp;quot;&lt;/span&gt;: [&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;             &lt;span class=&quot;string&quot;&gt;&amp;quot;arn:aws:rds-db:us-west-2:123456789012:dbuser:cluster-CO4FHMOYDKJ7CVBEJS2UWDQX7I/david_lu&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;         ]&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;      &amp;#125;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;   ]&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;
    
    </summary>
    
    
      <category term="AWS" scheme="http://davidlu1001.github.io/categories/AWS/"/>
    
      <category term="SRE" scheme="http://davidlu1001.github.io/categories/SRE/"/>
    
      <category term="DB" scheme="http://davidlu1001.github.io/categories/DB/"/>
    
    
      <category term="AWS" scheme="http://davidlu1001.github.io/tags/AWS/"/>
    
      <category term="SRE" scheme="http://davidlu1001.github.io/tags/SRE/"/>
    
      <category term="DB" scheme="http://davidlu1001.github.io/tags/DB/"/>
    
      <category term="RDS" scheme="http://davidlu1001.github.io/tags/RDS/"/>
    
  </entry>
  
  <entry>
    <title>ElasticSearch Benchmark with Rally</title>
    <link href="http://davidlu1001.github.io/2020/01/23/ElasticSearch-Benchmark-with-Rally/"/>
    <id>http://davidlu1001.github.io/2020/01/23/ElasticSearch-Benchmark-with-Rally/</id>
    <published>2020-01-23T09:34:41.000Z</published>
    <updated>2020-01-23T09:34:41.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Overview"><a href="#Overview" class="headerlink" title="Overview"></a>Overview</h1><p>We need to perform the ES benchmark for the following purposes:</p><ul><li><p>Experiment with some configurations of ES for tuning</p></li><li><p>Compare the performance differences between different versions of ES</p></li><li><p>Capacity planning for the ES cluster, e.g.</p><ol><li><p>how many nodes does your online ES cluster need?</p></li><li><p>What is the configuration of each node? </p></li><li><p>What is the write performance limit of this cluster? </p></li><li><p>What about read performance?</p></li></ol></li></ul><h1 id="Tool"><a href="#Tool" class="headerlink" title="Tool"></a>Tool</h1><p>Several tools are available for performance testing, e.g. <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2RhbmllbGZpcmVtYW4vZXNwZXJm">esperf<i class="fa fa-external-link-alt"></i></span>, <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2xvZ3ppby9lbGFzdGljc2VhcmNoLXN0cmVzcy10ZXN0">elasticsearch-stress-test<i class="fa fa-external-link-alt"></i></span> or JMeter. </p><p>Here we’ll use Elastic official tool <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2VsYXN0aWMvcmFsbHk=">esrally<i class="fa fa-external-link-alt"></i></span>, which is a macrobenchmarking framework for Elasticsearch.</p><a id="more"></a><h1 id="Usage"><a href="#Usage" class="headerlink" title="Usage"></a>Usage</h1><p>Perform testing</p><figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Locally - use &quot;geonames&quot; data, test against existing ES, overwrite ES config for benchmarks, and export result as CSV format</span></span><br><span class="line"></span><br><span class="line">$ esrally <span class="attribute">--track</span>=geonames <span class="attribute">--target-hosts</span>=localhost:9204 <span class="attribute">--pipeline</span>=benchmark-only <span class="attribute">--challenge</span>=append-no-conflicts <span class="attribute">--report-format</span>=csv <span class="attribute">--report-file</span>=~/es_benchmarks/result.csv <span class="attribute">--user-tag</span>=<span class="string">&quot;shards:6&quot;</span> <span class="attribute">--track-params</span>=<span class="string">&quot;cluster_health:&#x27;yellow&#x27;,number_of_shards:6,bulk_size:5000&quot;</span></span><br></pre></td></tr></table></figure><p>List testing</p><figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$ esrally list races</span><br><span class="line"></span><br><span class="line">Race ID                               Race Timestamp    Track     Track Parameters                                            Challenge            Car      <span class="built_in"> User </span>Tags    Track Revision    Team Revision</span><br><span class="line">------------------------------------  ----------------  --------  ----------------------------------------------------------  -------------------  --------  -----------  ----------------  ---------------</span><br><span class="line">31163590-ddbb-40e8-a2a8-e7c2d5fe59da  20200120T195012Z  geonames  <span class="attribute">bulk_size</span>=5000, <span class="attribute">cluster_health</span>=yellow, <span class="attribute">number_of_shards</span>=16  append-no-conflicts  external               ec69176</span><br><span class="line">2da942c3-6b1b-4056-93ba-b617819b4e49  20200120T021551Z  geonames  <span class="attribute">cluster_health</span>=yellow, <span class="attribute">number_of_shards</span>=16                  append-no-conflicts  external               ec69176</span><br><span class="line">b7d492eb-37f3-4734-8ab5-e9f23f87341b  20200120T003713Z  geonames  <span class="attribute">number_of_shards</span>=6                                          append-no-conflicts  external               ec69176</span><br></pre></td></tr></table></figure><p>Compare results</p><figure class="highlight gherkin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">$ esrally compare --baseline=2da942c3-6b1b-4056-93ba-b617819b4e49 --contender=31163590-ddbb-40e8-a2a8-e7c2d5fe59da</span><br><span class="line"></span><br><span class="line">|<span class="string">                                              Metric </span>|<span class="string">         Task </span>|<span class="string">    Baseline </span>|<span class="string">  Contender </span>|<span class="string">     Diff </span>|<span class="string">   Unit </span>|</span><br><span class="line">|<span class="string"> --------------------------------------------------: </span>|<span class="string"> -----------: </span>|<span class="string"> ----------: </span>|<span class="string"> ---------: </span>|<span class="string"> -------: </span>|<span class="string"> -----: </span>|</span><br><span class="line">|<span class="string">          Cumulative indexing time of primary shards </span>|<span class="string">              </span>|<span class="string">     39.8328 </span>|<span class="string">    42.3733 </span>|<span class="string">  2.54053 </span>|<span class="string">    min </span>|</span><br><span class="line">|<span class="string"> Cumulative indexing throttle time of primary shards </span>|<span class="string">              </span>|<span class="string">           0 </span>|<span class="string">          0 </span>|<span class="string">        0 </span>|<span class="string">    min </span>|</span><br><span class="line">|<span class="string">             Cumulative merge time of primary shards </span>|<span class="string">              </span>|<span class="string">     9.66415 </span>|<span class="string">    10.4054 </span>|<span class="string">  0.74123 </span>|<span class="string">    min </span>|</span><br><span class="line">|<span class="string">            Cumulative merge count of primary shards </span>|<span class="string">              </span>|<span class="string">          56 </span>|<span class="string">        134 </span>|<span class="string">       78 </span>|<span class="string">        </span>|</span><br><span class="line">|<span class="string">    Cumulative merge throttle time of primary shards </span>|<span class="string">              </span>|<span class="string">   0.0255833 </span>|<span class="string">    0.01175 </span>|<span class="string"> -0.01383 </span>|<span class="string">    min </span>|</span><br><span class="line">|<span class="string">           Cumulative refresh time of primary shards </span>|<span class="string">              </span>|<span class="string">     9.38755 </span>|<span class="string">    10.4285 </span>|<span class="string">  1.04098 </span>|<span class="string">    min </span>|</span><br><span class="line">|<span class="string">          Cumulative refresh count of primary shards </span>|<span class="string">              </span>|<span class="string">         905 </span>|<span class="string">       1672 </span>|<span class="string">      767 </span>|<span class="string">        </span>|</span><br><span class="line">|<span class="string">             Cumulative flush time of primary shards </span>|<span class="string">              </span>|<span class="string">     0.01605 </span>|<span class="string"> 0.00281667 </span>|<span class="string"> -0.01323 </span>|<span class="string">    min </span>|</span><br><span class="line">|<span class="string">            Cumulative flush count of primary shards </span>|<span class="string">              </span>|<span class="string">          21 </span>|<span class="string">         22 </span>|<span class="string">        1 </span>|<span class="string">        </span>|</span><br><span class="line">|<span class="string">                                  Total Young Gen GC </span>|<span class="string">              </span>|<span class="string">      67.529 </span>|<span class="string">     71.123 </span>|<span class="string">    3.594 </span>|<span class="string">      s </span>|</span><br><span class="line">|<span class="string">                                    Total Old Gen GC </span>|<span class="string">              </span>|<span class="string">       6.193 </span>|<span class="string">      7.579 </span>|<span class="string">    1.386 </span>|<span class="string">      s </span>|</span><br><span class="line">|<span class="string">                                          Store size </span>|<span class="string">              </span>|<span class="string">     3.34169 </span>|<span class="string">    3.33626 </span>|<span class="string"> -0.00543 </span>|<span class="string">     GB </span>|</span><br><span class="line">|<span class="string">                                       Translog size </span>|<span class="string">              </span>|<span class="string"> 9.73232e-07 </span>|<span class="string">  0.0140035 </span>|<span class="string">    0.014 </span>|<span class="string">     GB </span>|</span><br><span class="line">|<span class="string">                              Heap used for segments </span>|<span class="string">              </span>|<span class="string">     4.85796 </span>|<span class="string">    4.75913 </span>|<span class="string"> -0.09883 </span>|<span class="string">     MB </span>|</span><br><span class="line">|<span class="string">                            Heap used for doc values </span>|<span class="string">              </span>|<span class="string">    0.164585 </span>|<span class="string">   0.368065 </span>|<span class="string">  0.20348 </span>|<span class="string">     MB </span>|</span><br><span class="line">|<span class="string">                                 Heap used for terms </span>|<span class="string">              </span>|<span class="string">     3.38647 </span>|<span class="string">    3.11659 </span>|<span class="string"> -0.26988 </span>|<span class="string">     MB </span>|</span><br><span class="line">|<span class="string">                                 Heap used for norms </span>|<span class="string">              </span>|<span class="string">    0.213257 </span>|<span class="string">    0.17749 </span>|<span class="string"> -0.03577 </span>|<span class="string">     MB </span>|</span><br><span class="line">|<span class="string">                                Heap used for points </span>|<span class="string">              </span>|<span class="string">    0.291598 </span>|<span class="string">   0.292653 </span>|<span class="string">  0.00105 </span>|<span class="string">     MB </span>|</span><br><span class="line">|<span class="string">                         Heap used for stored fields </span>|<span class="string">              </span>|<span class="string">    0.802048 </span>|<span class="string">   0.804329 </span>|<span class="string">  0.00228 </span>|<span class="string">     MB </span>|</span><br><span class="line">|<span class="string">                                       Segment count </span>|<span class="string">              </span>|<span class="string">         279 </span>|<span class="string">        256 </span>|<span class="string">      -23 </span>|<span class="string">        </span>|</span><br><span class="line">|<span class="string">                                      Min Throughput </span>|<span class="string"> index-append </span>|<span class="string">     21224.1 </span>|<span class="string">    20200.1 </span>|<span class="string"> -1023.95 </span>|<span class="string"> docs/s </span>|</span><br><span class="line">|<span class="string">                                   Median Throughput </span>|<span class="string"> index-append </span>|<span class="string">     22499.3 </span>|<span class="string">    21105.1 </span>|<span class="string"> -1394.22 </span>|<span class="string"> docs/s </span>|</span><br><span class="line">|<span class="string">                                      Max Throughput </span>|<span class="string"> index-append </span>|<span class="string">     22846.2 </span>|<span class="string">    22496.8 </span>|<span class="string"> -349.333 </span>|<span class="string"> docs/s </span>|</span><br><span class="line">|<span class="string">                             50th percentile latency </span>|<span class="string"> index-append </span>|<span class="string">     1625.08 </span>|<span class="string">    1669.35 </span>|<span class="string">  44.2758 </span>|<span class="string">     ms </span>|</span><br><span class="line">|<span class="string">                             90th percentile latency </span>|<span class="string"> index-append </span>|<span class="string">     2536.77 </span>|<span class="string">    2698.41 </span>|<span class="string">  161.644 </span>|<span class="string">     ms </span>|</span><br><span class="line">|<span class="string">                             99th percentile latency </span>|<span class="string"> index-append </span>|<span class="string">     5182.57 </span>|<span class="string">    6829.08 </span>|<span class="string">  1646.51 </span>|<span class="string">     ms </span>|</span><br><span class="line">|<span class="string">                           99.9th percentile latency </span>|<span class="string"> index-append </span>|<span class="string">     20297.2 </span>|<span class="string">    9961.53 </span>|<span class="string"> -10335.7 </span>|<span class="string">     ms </span>|</span><br><span class="line">|<span class="string">                            100th percentile latency </span>|<span class="string"> index-append </span>|<span class="string">       21013 </span>|<span class="string">    10312.9 </span>|<span class="string"> -10700.1 </span>|<span class="string">     ms </span>|</span><br><span class="line">|<span class="string">                                          error rate </span>|<span class="string"> index-append </span>|<span class="string">           0 </span>|<span class="string">          0 </span>|<span class="string">        0 </span>|<span class="string">      % </span>|</span><br></pre></td></tr></table></figure><p>We can also specify another ES to store the result, and compare / visualize in Kibana.</p><p>More usage info could be found <span class="exturl" data-url="aHR0cHM6Ly9lc3JhbGx5LnJlYWR0aGVkb2NzLmlvL2VuL3N0YWJsZS9pbmRleC5odG1s">here<i class="fa fa-external-link-alt"></i></span>.</p><h1 id="Monitoring"><a href="#Monitoring" class="headerlink" title="Monitoring"></a>Monitoring</h1><h2 id="Key-Metrics"><a href="#Key-Metrics" class="headerlink" title="Key Metrics"></a>Key Metrics</h2><ul><li><p>Throughput: The throughput of each operation, such as index, search, etc.</p></li><li><p>Latency: response time for each operation</p></li><li><p>Heap used for X</p></li></ul><p>More info for metrics: <span class="exturl" data-url="aHR0cHM6Ly9lc3JhbGx5LnJlYWR0aGVkb2NzLmlvL2VuL2xhdGVzdC9zdW1tYXJ5X3JlcG9ydC5odG1s">summary report<i class="fa fa-external-link-alt"></i></span></p><h2 id="General-Metrics-from-Kibana"><a href="#General-Metrics-from-Kibana" class="headerlink" title="General Metrics from Kibana"></a>General Metrics from Kibana</h2><p><img data-src="/images/kibana_metrics.png" alt="Metrics from Kibana 7"></p><h1 id="Plan"><a href="#Plan" class="headerlink" title="Plan"></a>Plan</h1><ul><li><p>Performance testing for ES7 with different settings - Locally (with Docker)</p></li><li><p>Performance testing for ES7 together with logstash filter (sending pre-configured percentage of traffic) - on AWS</p></li></ul><p>Results will be stored in CSV format, or can be setup and compare in Kibana for better visualization.</p><h1 id="Testing"><a href="#Testing" class="headerlink" title="Testing"></a>Testing</h1><p>The testing benchmark configurations are coming from <span class="exturl" data-url="aHR0cHM6Ly93d3cuZWxhc3RpYy5jby9ndWlkZS9lbi9lbGFzdGljc2VhcmNoL3JlZmVyZW5jZS9jdXJyZW50L3R1bmUtZm9yLWluZGV4aW5nLXNwZWVkLmh0bWw=">Tune for indexing speed<i class="fa fa-external-link-alt"></i></span> and <span class="exturl" data-url="aHR0cHM6Ly93d3cuZWxhc3RpYy5jby9ndWlkZS9lbi9lbGFzdGljc2VhcmNoL3JlZmVyZW5jZS9jdXJyZW50L3N5c3RlbS1jb25maWcuaHRtbA==">Important System Configrations<i class="fa fa-external-link-alt"></i></span></p><h1 id="References"><a href="#References" class="headerlink" title="References"></a>References</h1><p><span class="exturl" data-url="aHR0cHM6Ly9lc3JhbGx5LnJlYWR0aGVkb2NzLmlvL2VuL3N0YWJsZS9pbmRleC5odG1s">Rally official doc<i class="fa fa-external-link-alt"></i></span></p><p><span class="exturl" data-url="aHR0cHM6Ly90ZWNoLmViYXlpbmMuY29tL2VuZ2luZWVyaW5nL2VsYXN0aWNzZWFyY2gtcGVyZm9ybWFuY2UtdHVuaW5nLXByYWN0aWNlLWF0LWViYXkv">Performance Tuning Practice at Ebay<i class="fa fa-external-link-alt"></i></span></p><p><span class="exturl" data-url="aHR0cHM6Ly93d3cuZWxhc3RpYy5jby9ndWlkZS9lbi9lbGFzdGljc2VhcmNoL3JlZmVyZW5jZS9jdXJyZW50L3R1bmUtZm9yLWluZGV4aW5nLXNwZWVkLmh0bWw=">Tune for indexing speed<i class="fa fa-external-link-alt"></i></span></p><p><span class="exturl" data-url="aHR0cHM6Ly93d3cuZWxhc3RpYy5jby9ndWlkZS9lbi9lbGFzdGljc2VhcmNoL3JlZmVyZW5jZS9jdXJyZW50L3N5c3RlbS1jb25maWcuaHRtbA==">Important System Configrations<i class="fa fa-external-link-alt"></i></span></p><p><span class="exturl" data-url="aHR0cDovL2Jsb2cuc2NvdHRsb2dpYy5jb20vMjAxNi8xMS8yMi91c2luZy1yYWxseS10by1iZW5jaG1hcmstZWxhc3RpY3NlYXJjaC5odG1s">Using Rally to benchmark Elasticsearch queries<i class="fa fa-external-link-alt"></i></span></p>]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;Overview&quot;&gt;&lt;a href=&quot;#Overview&quot; class=&quot;headerlink&quot; title=&quot;Overview&quot;&gt;&lt;/a&gt;Overview&lt;/h1&gt;&lt;p&gt;We need to perform the ES benchmark for the following purposes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Experiment with some configurations of ES for tuning&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Compare the performance differences between different versions of ES&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Capacity planning for the ES cluster, e.g.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;how many nodes does your online ES cluster need?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is the configuration of each node? &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is the write performance limit of this cluster? &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What about read performance?&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&quot;Tool&quot;&gt;&lt;a href=&quot;#Tool&quot; class=&quot;headerlink&quot; title=&quot;Tool&quot;&gt;&lt;/a&gt;Tool&lt;/h1&gt;&lt;p&gt;Several tools are available for performance testing, e.g. &lt;a href=&quot;https://github.com/danielfireman/esperf&quot;&gt;esperf&lt;/a&gt;, &lt;a href=&quot;https://github.com/logzio/elasticsearch-stress-test&quot;&gt;elasticsearch-stress-test&lt;/a&gt; or JMeter. &lt;/p&gt;
&lt;p&gt;Here we’ll use Elastic official tool &lt;a href=&quot;https://github.com/elastic/rally&quot;&gt;esrally&lt;/a&gt;, which is a macrobenchmarking framework for Elasticsearch.&lt;/p&gt;
    
    </summary>
    
    
      <category term="SRE" scheme="http://davidlu1001.github.io/categories/SRE/"/>
    
      <category term="ElasticSearch" scheme="http://davidlu1001.github.io/categories/ElasticSearch/"/>
    
    
      <category term="ElasticSearch" scheme="http://davidlu1001.github.io/tags/ElasticSearch/"/>
    
      <category term="SRE" scheme="http://davidlu1001.github.io/tags/SRE/"/>
    
      <category term="Rally" scheme="http://davidlu1001.github.io/tags/Rally/"/>
    
  </entry>
  
  <entry>
    <title>Zookeeper Upgrade Checklist</title>
    <link href="http://davidlu1001.github.io/2019/11/22/Zookeeper-Upgrade-Checklist/"/>
    <id>http://davidlu1001.github.io/2019/11/22/Zookeeper-Upgrade-Checklist/</id>
    <published>2019-11-22T07:35:46.000Z</published>
    <updated>2019-11-22T07:35:46.000Z</updated>
    
    <content type="html"><![CDATA[<p>Just making some notes for the upgrade steps for Zookeeper as a memo.</p><h1 id="Prerequisites"><a href="#Prerequisites" class="headerlink" title="Prerequisites"></a>Prerequisites</h1><ul><li>Terraform work complete to create Zookeeper Auto Scaling Group (ASG)</li></ul><h2 id="Autoscaling"><a href="#Autoscaling" class="headerlink" title="Autoscaling"></a>Autoscaling</h2><p>Full autoscaling Zookeeper (actually adding new nodes to the Zookeeper ensemble automatically) is a bit tricky currently as a full rolling restart of the Zookeeper cluster would have to be orchestrated without ever losing quorum. Zookeeper 3.5 introduced <span class="exturl" data-url="aHR0cHM6Ly96b29rZWVwZXIuYXBhY2hlLm9yZy9kb2MvcjMuNS41L3pvb2tlZXBlclJlY29uZmlnLmh0bWw=">dynamic reconfiguration<i class="fa fa-external-link-alt"></i></span> which would most likely make this significantly easier. There are some other options for <span class="exturl" data-url="aHR0cHM6Ly93d3cuY3JlZGVyYS5jb20vYmxvZy90ZWNobm9sb2d5LXNvbHV0aW9ucy9ob3ctdG8tYXV0b21hdGUtem9va2VlcGVyLWluLWF3cy8=">autoscaling Zookeeper 3.4<i class="fa fa-external-link-alt"></i></span> but we’d need to weigh up whether that additional complexity is really worth it.</p><h2 id="Puppet"><a href="#Puppet" class="headerlink" title="Puppet"></a>Puppet</h2><p>Using the puppet module: <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2RlcmljL3B1cHBldC16b29rZWVwZXI=">deric/puppet-zookeeper<i class="fa fa-external-link-alt"></i></span></p><a id="more"></a><h3 id="Updating-Hiera-Puppet"><a href="#Updating-Hiera-Puppet" class="headerlink" title="Updating Hiera / Puppet"></a>Updating Hiera / Puppet</h3><p>When a new node starts within the ASG Puppet will run but Zookeeper will not be started automatically. Hiera will need to be updated and Puppet run again for Zookeeper to start.</p><p>“zookeeper::servers” - Needs to be set to a hash of the complete list of servers in the Zookeeper ensemble. This is typically set at the “dc” level in the hierarchy, e.g.</p><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">puppet</span>/hieradata-masterless/dc</span><br><span class="line"></span><br><span class="line"><span class="attribute">cat</span> test-us-west-<span class="number">2</span>.yaml</span><br><span class="line"><span class="attribute">zookeeper</span>::servers:</span><br><span class="line">  <span class="attribute">100</span>: zookeeper-<span class="number">0</span>cdf<span class="number">08</span>e<span class="number">0</span>d<span class="number">09</span>dd<span class="number">3530</span>.oregon.test.xyz.org</span><br><span class="line">  <span class="attribute">101</span>: zookeeper-<span class="number">01</span>ff<span class="number">482</span>cf<span class="number">77636240</span>.oregon.test.xyz.org</span><br><span class="line">  <span class="attribute">102</span>: zookeeper-<span class="number">0749</span>d<span class="number">84</span>c<span class="number">7</span>dfd<span class="number">4</span>d<span class="number">798</span>.oregon.test.xyz.org</span><br></pre></td></tr></table></figure><p>“zookeeper::id” - Needs to be set to the unique ID of the node within the Zookeeper cluster. This is set at the node level (as it’s always specific to a particular node) and needs to match with one (and only one) entry in “zookeeper::servers”. The corresponding node configuration for the above “zookeeper::servers” configuration would be</p><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">puppet</span>/hieradata-masterless/nodes</span><br><span class="line"></span><br><span class="line"><span class="attribute">cat</span> zookeeper-<span class="number">0</span>cdf<span class="number">08</span>e<span class="number">0</span>d<span class="number">09</span>dd<span class="number">3530</span>.yaml</span><br><span class="line"><span class="attribute">zookeeper</span>::id: <span class="number">100</span></span><br><span class="line"> </span><br><span class="line"><span class="attribute">cat</span> zookeeper-<span class="number">01</span>ff<span class="number">482</span>cf<span class="number">77636240</span>.yaml</span><br><span class="line"><span class="attribute">zookeeper</span>::id: <span class="number">101</span></span><br><span class="line"> </span><br><span class="line"><span class="attribute">cat</span> zookeeper-<span class="number">0749</span>d<span class="number">84</span>c<span class="number">7</span>dfd<span class="number">4</span>d<span class="number">798</span>.yaml</span><br><span class="line"><span class="attribute">zookeeper</span>::id: <span class="number">102</span></span><br></pre></td></tr></table></figure><h1 id="Migration-steps"><a href="#Migration-steps" class="headerlink" title="Migration steps"></a>Migration steps</h1><ul><li>Adding additional nodes to expand the existing ensemble. Must be repeated once for each new node<ul><li>Start Zookeeper on one of the replacement nodes. The node should join the ensemble as a follower.</li><li>Execute a Puppet run on each of the existing nodes one at a time. Restart the nodes in order with the lowest ZKID first but restart the leader last.</li><li>Ensure the nodes are all in sync</li></ul></li><li>Once the ensemble has been expanded and is in sync the original Trusty nodes can be removed. Starting with an ensemble of six nodes:<ul><li>Stop Zookeeper on one of the original nodes</li><li>Update the list of servers on the remaining nodes to remove the server that has been stopped.</li><li>Rolling restart of the cluster with the nodes in order from lowest to highest ZKID. The leader should be restarted last. At this point the ensemble should consist of five nodes.</li><li>Stop Zookeeper on the final two original Trusty nodes. The three replacement nodes left on line can still form a quorum and should continue to work as expected.</li><li>Update the list of servers on the remaining nodes to remove the servers that have been stopped.</li><li>Rolling restart of the cluster with the nodes in order from lowest to highest ZKID. The leader should be restarted last. At this point the ensemble should consist of three nodes.</li></ul></li></ul><h1 id="Tidy-up"><a href="#Tidy-up" class="headerlink" title="Tidy up"></a>Tidy up</h1><p>once we’re certain that we’re never going to revert to the original nodes</p><ul><li>Ensure replacement nodes are being monitored by Datadog</li><li>Ensure replacement nodes are sending logs to the Elastic lifesupport cluster</li><li>Ensure bash aliases are updated</li><li>Terraform work to decommission original nodes</li></ul>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;Just making some notes for the upgrade steps for Zookeeper as a memo.&lt;/p&gt;
&lt;h1 id=&quot;Prerequisites&quot;&gt;&lt;a href=&quot;#Prerequisites&quot; class=&quot;headerlink&quot; title=&quot;Prerequisites&quot;&gt;&lt;/a&gt;Prerequisites&lt;/h1&gt;&lt;ul&gt;
&lt;li&gt;Terraform work complete to create Zookeeper Auto Scaling Group (ASG)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;Autoscaling&quot;&gt;&lt;a href=&quot;#Autoscaling&quot; class=&quot;headerlink&quot; title=&quot;Autoscaling&quot;&gt;&lt;/a&gt;Autoscaling&lt;/h2&gt;&lt;p&gt;Full autoscaling Zookeeper (actually adding new nodes to the Zookeeper ensemble automatically) is a bit tricky currently as a full rolling restart of the Zookeeper cluster would have to be orchestrated without ever losing quorum. Zookeeper 3.5 introduced &lt;a href=&quot;https://zookeeper.apache.org/doc/r3.5.5/zookeeperReconfig.html&quot;&gt;dynamic reconfiguration&lt;/a&gt; which would most likely make this significantly easier. There are some other options for &lt;a href=&quot;https://www.credera.com/blog/technology-solutions/how-to-automate-zookeeper-in-aws/&quot;&gt;autoscaling Zookeeper 3.4&lt;/a&gt; but we’d need to weigh up whether that additional complexity is really worth it.&lt;/p&gt;
&lt;h2 id=&quot;Puppet&quot;&gt;&lt;a href=&quot;#Puppet&quot; class=&quot;headerlink&quot; title=&quot;Puppet&quot;&gt;&lt;/a&gt;Puppet&lt;/h2&gt;&lt;p&gt;Using the puppet module: &lt;a href=&quot;https://github.com/deric/puppet-zookeeper&quot;&gt;deric/puppet-zookeeper&lt;/a&gt;&lt;/p&gt;
    
    </summary>
    
    
      <category term="SRE" scheme="http://davidlu1001.github.io/categories/SRE/"/>
    
      <category term="ZooKeeper" scheme="http://davidlu1001.github.io/categories/ZooKeeper/"/>
    
    
      <category term="SRE" scheme="http://davidlu1001.github.io/tags/SRE/"/>
    
      <category term="Zookeeper" scheme="http://davidlu1001.github.io/tags/Zookeeper/"/>
    
  </entry>
  
  <entry>
    <title>ElasticSearch migrated from EC2 to AWS managed ES</title>
    <link href="http://davidlu1001.github.io/2019/11/12/ElasticSearch-Migration-to-AWS/"/>
    <id>http://davidlu1001.github.io/2019/11/12/ElasticSearch-Migration-to-AWS/</id>
    <published>2019-11-12T08:07:58.000Z</published>
    <updated>2019-11-12T08:07:58.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Background"><a href="#Background" class="headerlink" title="Background"></a>Background</h1><p>Let’s say there’s a legacy platform called XXX, and it still poses an operational cost for SRE to manage the monitoring / alerting / upgrade etc.</p><p>At present, XXX’s ElasticSearch architecture consists of 3 hosts (m4.xlarge) acting as master, client and data nodes, for XXX’s searching functionality.</p><p>All of them are using Ubuntu Trusty (14.04) image, which is no longer supported and need to be migrated to Xenial (16.04). Its usage seems pretty low and cost wise so we would like to migrate the XXX ES to AWS managed ElasticSearch Service to reduce operational overhead.</p><p>This doc will cover the project plan / checklist for migration.</p><h1 id="Solution-overview"><a href="#Solution-overview" class="headerlink" title="Solution overview"></a>Solution overview</h1><p>Elasticsearch (ES) indexes can be migrated with following steps:</p><ul><li><p>Create baseline indexes</p><ul><li>Create a snapshot repository and associate it to an AWS S3 Bucket.</li><li>Create the first snapshot of the indexes to be migrated, which is a full snapshot on EC2 - The snapshot will be automatically stored in the AWS S3 bucket created in the first step.</li><li>Restore this full snapshot to the AWS ES.</li></ul></li><li><p>Periodic incremental snapshots</p><ul><li>Repeat serval incremental snapshot and restore.</li></ul></li><li><p>Final snapshot and service switchover</p><ul><li>Stop services which can modify index data.</li><li>Create a final incremental snapshot of the EC2.</li><li>Perform service switchover to the AWS ES.</li></ul></li></ul><a id="more"></a><h1 id="Checklist"><a href="#Checklist" class="headerlink" title="Checklist"></a>Checklist</h1><p>Here’s a list of what will we do in detail:</p><h2 id="Assess-and-analyse-current-data"><a href="#Assess-and-analyse-current-data" class="headerlink" title="Assess and analyse current data"></a>Assess and analyse current data</h2><p>The ES data is around 30G, setting <code>number_of_replicas</code> to 1, then according to the <span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL2VsYXN0aWNzZWFyY2gtc2VydmljZS9sYXRlc3QvZGV2ZWxvcGVyZ3VpZGUvc2l6aW5nLWRvbWFpbnMuaHRtbA==">simplified version of calculation<i class="fa fa-external-link-alt"></i></span>:</p><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">Source</span> Data * (<span class="number">1</span> + Number of Replicas) * <span class="number">1</span>.<span class="number">45</span> = Minimum Storage Requirement</span><br></pre></td></tr></table></figure><p>So the minimum storage for AWS managed ES is about 90G.</p><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">curl</span> $es/_cat/indices?v</span><br><span class="line"><span class="attribute">health</span> status index              pri rep docs.count docs.deleted store.size pri.store.size</span><br><span class="line"><span class="attribute">green</span>  open   .marvel-<span class="number">2019</span>.<span class="number">10</span>.<span class="number">12</span>   <span class="number">1</span>   <span class="number">1</span>     <span class="number">115081</span>            <span class="number">0</span>      <span class="number">413</span>mb        <span class="number">206</span>.<span class="number">3</span>mb</span><br><span class="line"><span class="attribute">green</span>  open   site-<span class="number">1</span>.<span class="number">0</span>           <span class="number">200</span>   <span class="number">1</span>     <span class="number">937559</span>          <span class="number">884</span>      <span class="number">1</span>.<span class="number">6</span>gb        <span class="number">848</span>.<span class="number">7</span>mb</span><br><span class="line"><span class="attribute">green</span>  open   site_v<span class="number">1</span>            <span class="number">200</span>   <span class="number">1</span>     <span class="number">324638</span>           <span class="number">33</span>    <span class="number">335</span>.<span class="number">8</span>mb        <span class="number">167</span>.<span class="number">9</span>mb</span><br><span class="line"><span class="attribute">green</span>  open   .marvel-<span class="number">2019</span>.<span class="number">10</span>.<span class="number">14</span>   <span class="number">1</span>   <span class="number">1</span>     <span class="number">115009</span>            <span class="number">0</span>    <span class="number">424</span>.<span class="number">5</span>mb        <span class="number">212</span>.<span class="number">2</span>mb</span><br><span class="line"><span class="attribute">green</span>  open   .marvel-<span class="number">2019</span>.<span class="number">10</span>.<span class="number">15</span>   <span class="number">1</span>   <span class="number">1</span>     <span class="number">114962</span>            <span class="number">0</span>    <span class="number">424</span>.<span class="number">7</span>mb          <span class="number">212</span>mb</span><br><span class="line"><span class="attribute">green</span>  open   .marvel-<span class="number">2019</span>.<span class="number">10</span>.<span class="number">16</span>   <span class="number">1</span>   <span class="number">1</span>      <span class="number">14290</span>            <span class="number">0</span>     <span class="number">56</span>.<span class="number">3</span>mb         <span class="number">28</span>.<span class="number">3</span>mb</span><br><span class="line"><span class="attribute">green</span>  open   .marvel-<span class="number">2019</span>.<span class="number">10</span>.<span class="number">13</span>   <span class="number">1</span>   <span class="number">1</span>     <span class="number">115104</span>            <span class="number">0</span>    <span class="number">410</span>.<span class="number">5</span>mb          <span class="number">205</span>mb</span><br><span class="line"><span class="attribute">green</span>  open   search             <span class="number">200</span>   <span class="number">1</span>   <span class="number">20829799</span>      <span class="number">6167280</span>       <span class="number">24</span>gb           <span class="number">12</span>gb</span><br><span class="line"><span class="attribute">green</span>  open   .marvel-kibana       <span class="number">1</span>   <span class="number">1</span>          <span class="number">1</span>            <span class="number">0</span>      <span class="number">6</span>.<span class="number">4</span>kb          <span class="number">3</span>.<span class="number">2</span>kb</span><br></pre></td></tr></table></figure><h2 id="Create-an-S3-bucket-for-current-Elasticsearch-data-on-EC2"><a href="#Create-an-S3-bucket-for-current-Elasticsearch-data-on-EC2" class="headerlink" title="Create an S3 bucket for current Elasticsearch data (on EC2)"></a>Create an S3 bucket for current Elasticsearch data (on EC2)</h2><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">e</span>.g. s<span class="number">3</span>://es-backup (us-west-<span class="number">2</span>)</span><br></pre></td></tr></table></figure><h2 id="Make-ES-Cluster-snapshot-and-move-it-to-S3"><a href="#Make-ES-Cluster-snapshot-and-move-it-to-S3" class="headerlink" title="Make ES Cluster snapshot and move it to S3"></a>Make ES Cluster snapshot and move it to S3</h2><ul><li><p>check snapshot repository setttings</p><figure class="highlight ada"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">curl localhost:<span class="number">9200</span>/_snapshot?pretty</span><br><span class="line"> </span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;XXX-snapshot-s3-repo&quot;</span> : &#123;</span><br><span class="line">    <span class="string">&quot;type&quot;</span> : &quot;<span class="type">s3</span><span class="string">&quot;,</span></span><br><span class="line"><span class="string">    &quot;</span>settings<span class="string">&quot; : &#123;</span></span><br><span class="line"><span class="string">      &quot;</span>bucket<span class="string">&quot; : &quot;</span>es-backup<span class="string">&quot;,</span></span><br><span class="line"><span class="string">      &quot;</span>region<span class="string">&quot; : &quot;</span>us-west-<span class="number">2</span><span class="string">&quot;</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">  &#125;</span></span><br><span class="line"><span class="string">&#125;</span></span><br></pre></td></tr></table></figure></li><li><p>set snapshot repo to S3</p><figure class="highlight scilab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">curl -XPUT localhost:<span class="number">9200</span>/_snapshot/XXX-snapshot-s3-repo?verify=false -d <span class="string">&#x27;&#123;</span></span><br><span class="line"><span class="string">  &quot;</span><span class="built_in">type</span><span class="string">&quot;: &quot;</span>s3<span class="string">&quot;,</span></span><br><span class="line"><span class="string">  &quot;</span>settings<span class="string">&quot;: &#123;</span></span><br><span class="line"><span class="string">    &quot;</span>bucket<span class="string">&quot;: &quot;</span>es-backup<span class="string">&quot;,</span></span><br><span class="line"><span class="string">    &quot;</span>region<span class="string">&quot;: &quot;</span>us-west<span class="number">-2</span><span class="string">&quot;</span></span><br><span class="line"><span class="string">  &#125;</span></span><br><span class="line"><span class="string">&#125;&#x27;</span></span><br></pre></td></tr></table></figure></li><li><p>check indices</p><figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl localhost:<span class="number">9200</span><span class="regexp">/_cat/i</span>ndices?pretty</span><br></pre></td></tr></table></figure></li><li><p>make snapshots</p><figure class="highlight applescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># specified indices</span></span><br><span class="line">curl -XPUT <span class="string">&quot;localhost:9200/_snapshot/XXX-snapshot-s3-repo/marvel-20191101?wait_for_completion=true&quot;</span> -d &#x27;&#123;</span><br><span class="line">  <span class="string">&quot;indices&quot;</span>: <span class="string">&quot;.marvel-2019.11.01&quot;</span>,</span><br><span class="line">  <span class="string">&quot;ignore_unavailable&quot;</span>: <span class="string">&quot;true&quot;</span>,</span><br><span class="line">  <span class="string">&quot;include_global_state&quot;</span>: <span class="literal">false</span></span><br><span class="line">&#125;&#x27;</span><br><span class="line"> </span><br><span class="line">&#123;<span class="string">&quot;snapshot&quot;</span>:&#123;<span class="string">&quot;snapshot&quot;</span>:<span class="string">&quot;marvel-20191024&quot;</span>,<span class="string">&quot;indices&quot;</span>:[<span class="string">&quot;.marvel-2019.10.24&quot;</span>],<span class="string">&quot;state&quot;</span>:<span class="string">&quot;SUCCESS&quot;</span>,<span class="string">&quot;start_time&quot;</span>:<span class="string">&quot;2019-10-25T00:20:47.625Z&quot;</span>,<span class="string">&quot;start_time_in_millis&quot;</span>:<span class="number">1571962847625</span>,<span class="string">&quot;end_time&quot;</span>:<span class="string">&quot;2019-10-25T00:21:06.058Z&quot;</span>,<span class="string">&quot;end_time_in_millis&quot;</span>:<span class="number">1571962866058</span>,<span class="string">&quot;duration_in_millis&quot;</span>:<span class="number">18433</span>,<span class="string">&quot;failures&quot;</span>:[],<span class="string">&quot;shards&quot;</span>:&#123;<span class="string">&quot;total&quot;</span>:<span class="number">1</span>,<span class="string">&quot;failed&quot;</span>:<span class="number">0</span>,<span class="string">&quot;successful&quot;</span>:<span class="number">1</span>&#125;&#125;&#125;</span><br></pre></td></tr></table></figure></li><li><p>check snapshots</p><figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># check snapshots</span></span><br><span class="line"> </span><br><span class="line">curl localhost:<span class="number">9200</span><span class="regexp">/_snapshot/</span>XXX-snapshot-s3-repo/_all?pretty</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;snapshots&quot;</span> : [ &#123;</span><br><span class="line">    <span class="string">&quot;snapshot&quot;</span> : <span class="string">&quot;marvel-20191016_20191018&quot;</span>,</span><br><span class="line">    <span class="string">&quot;indices&quot;</span> : [ <span class="string">&quot;site_v1&quot;</span> ],</span><br><span class="line">    <span class="string">&quot;state&quot;</span> : <span class="string">&quot;SUCCESS&quot;</span>,</span><br><span class="line">    <span class="string">&quot;start_time&quot;</span> : <span class="string">&quot;2019-10-18T01:56:21.344Z&quot;</span>,</span><br><span class="line">    <span class="string">&quot;start_time_in_millis&quot;</span> : <span class="number">1571363781344</span>,</span><br><span class="line">    <span class="string">&quot;end_time&quot;</span> : <span class="string">&quot;2019-10-18T01:57:56.012Z&quot;</span>,</span><br><span class="line">    <span class="string">&quot;end_time_in_millis&quot;</span> : <span class="number">1571363876012</span>,</span><br><span class="line">    <span class="string">&quot;duration_in_millis&quot;</span> : <span class="number">94668</span>,</span><br><span class="line">    <span class="string">&quot;failures&quot;</span> : [ ],</span><br><span class="line">    <span class="string">&quot;shards&quot;</span> : &#123;</span><br><span class="line">      <span class="string">&quot;total&quot;</span> : <span class="number">200</span>,</span><br><span class="line">      <span class="string">&quot;failed&quot;</span> : <span class="number">0</span>,</span><br><span class="line">      <span class="string">&quot;successful&quot;</span> : <span class="number">200</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125; ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li></ul><h2 id="Create-and-configure-AWS-Elasticsearch-service-version-1-5"><a href="#Create-and-configure-AWS-Elasticsearch-service-version-1-5" class="headerlink" title="Create and configure AWS Elasticsearch service (version 1.5)"></a>Create and configure AWS Elasticsearch service (version 1.5)</h2><h2 id="Migrate-the-data-Restore-cluster-from-S3-to-AWS-ES"><a href="#Migrate-the-data-Restore-cluster-from-S3-to-AWS-ES" class="headerlink" title="Migrate the data (Restore cluster from S3 to AWS ES)"></a>Migrate the data (Restore cluster from S3 to AWS ES)</h2><ul><li><p>check current iam instance profile role on XXX ES hosts</p><figure class="highlight dsconfig"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">aws</span> <span class="string">sts</span> <span class="built_in">get-caller-identity</span></span><br><span class="line"><span class="built_in">&#123;</span></span><br><span class="line"><span class="built_in"></span>    <span class="string">&quot;Account&quot;</span>: <span class="string">&quot;XXXXXXXXXXX&quot;</span>,</span><br><span class="line">    <span class="string">&quot;UserId&quot;</span>: <span class="string">&quot;AROAISLXH37RO7WDQU7H2:i-f9f6cf20&quot;</span>,</span><br><span class="line">    <span class="string">&quot;Arn&quot;</span>: <span class="string">&quot;arn:aws:sts::XXXXXXXXXXX:assumed-role/elasticsearch-cloud-aws/i-f9f6cf20&quot;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li><li><p>set snapshot repository for AWS managed ES</p></li></ul><p>Must sign your snapshot requests, if your access policies specify IAM users or roles.</p><p>p.s. If using curl will get the following error message:</p><blockquote><p>{“Message”:”User: anonymous is not authorized to perform: iam:PassRole on resource: arn:aws:iam::XXXXXXXXXXX:role/test-role”}</p></blockquote><p>Example code:</p><figure class="highlight nix"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">python register_es_repo.py</span><br><span class="line"> </span><br><span class="line"><span class="built_in">import</span> boto3</span><br><span class="line"><span class="built_in">import</span> requests</span><br><span class="line"><span class="built_in">import</span> json</span><br><span class="line">from requests_aws4auth <span class="built_in">import</span> AWS4Auth</span><br><span class="line"> </span><br><span class="line"><span class="attr">host</span> = &#x27;https://vpc-es-XXX-prod-ji6wgswtt3btfue5pwqwgvudry.us-west-<span class="number">2</span>.es.amazonaws.com/&#x27;</span><br><span class="line"><span class="attr">region</span> = &#x27;us-west-<span class="number">2</span>&#x27;</span><br><span class="line"><span class="attr">service</span> = &#x27;es&#x27;</span><br><span class="line"><span class="attr">credentials</span> = boto3.Session().get_credentials()</span><br><span class="line"><span class="attr">awsauth</span> = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, <span class="attr">session_token=credentials.token)</span></span><br><span class="line"> </span><br><span class="line"><span class="comment"># Register repository</span></span><br><span class="line"><span class="attr">path</span> = &#x27;_snapshot/XXX-snapshot-s3-repo&#x27; <span class="comment"># the Elasticsearch API endpoint</span></span><br><span class="line"><span class="attr">url</span> = host + path</span><br><span class="line"> </span><br><span class="line"><span class="attr">payload</span> = &#123;</span><br><span class="line">  <span class="string">&quot;type&quot;</span>: <span class="string">&quot;s3&quot;</span>,</span><br><span class="line">  <span class="string">&quot;settings&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;bucket&quot;</span>: <span class="string">&quot;es-backup&quot;</span>,</span><br><span class="line">    <span class="string">&quot;region&quot;</span>: <span class="string">&quot;us-west-2&quot;</span>,</span><br><span class="line">    <span class="string">&quot;role_arn&quot;</span>: <span class="string">&quot;arn:aws:iam::XXXXXXXXXX:role/elasticsearch-cloud-aws&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="attr">headers</span> = &#123;<span class="string">&quot;Content-Type&quot;</span>: <span class="string">&quot;application/json&quot;</span>&#125;</span><br><span class="line"> </span><br><span class="line"><span class="attr">r</span> = requests.put(url, <span class="attr">auth=awsauth,</span> <span class="attr">json=payload,</span> <span class="attr">headers=headers)</span></span><br><span class="line"> </span><br><span class="line">print(r.status_code)</span><br><span class="line">print(r.text)</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="comment"># Output</span></span><br><span class="line"><span class="number">200</span></span><br><span class="line">&#123;<span class="string">&quot;acknowledged&quot;</span>:<span class="literal">true</span>&#125;</span><br></pre></td></tr></table></figure><ul><li><p>check snapshot repository settings</p><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">curl</span> <span class="literal">-XGET</span> <span class="variable">$es</span>/_snapshot?pretty</span><br><span class="line"> </span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;XXX-snapshot-s3-repo&quot;</span> : &#123;</span><br><span class="line">    <span class="string">&quot;type&quot;</span> : <span class="string">&quot;s3&quot;</span>,</span><br><span class="line">    <span class="string">&quot;settings&quot;</span> : &#123;</span><br><span class="line">      <span class="string">&quot;bucket&quot;</span> : <span class="string">&quot;es-backup&quot;</span>,</span><br><span class="line">      <span class="string">&quot;role_arn&quot;</span> : <span class="string">&quot;arn:aws:iam::XXXXXXXXXXX:role/aws-elasticsearch-backup&quot;</span>,</span><br><span class="line">      <span class="string">&quot;region&quot;</span> : <span class="string">&quot;us-west-2&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li><li><p>update search index settings to speed up restore process</p></li></ul><figure class="highlight arduino"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"># https:<span class="comment">//aws.amazon.com/premiumsupport/knowledge-center/elasticsearch-indexing-performance/</span></span></span><br><span class="line"># For huge index, reduce from <span class="number">30</span><span class="built_in">min</span> to <span class="number">10</span><span class="built_in">min</span> together with incremental snapshot:</span><br><span class="line"> </span><br><span class="line"><span class="meta"># before migration</span></span><br><span class="line">curl -XPUT <span class="string">&quot;$es/search/_settings&quot;</span> -d<span class="number">&#x27;</span> &#123;</span><br><span class="line">        <span class="string">&quot;number_of_replicas&quot;</span> : <span class="number">0</span>,</span><br><span class="line">        <span class="string">&quot;refresh_interval&quot;</span> : <span class="string">&quot;-1&quot;</span></span><br><span class="line">&#125;&#x27;</span><br><span class="line"> </span><br><span class="line"><span class="meta"># set it back after migration</span></span><br><span class="line">curl -XPUT <span class="string">&quot;$es/search/_settings&quot;</span> -d<span class="number">&#x27;</span> &#123;</span><br><span class="line">        <span class="string">&quot;number_of_replicas&quot;</span> : <span class="number">1</span>,</span><br><span class="line">        <span class="string">&quot;refresh_interval&quot;</span> : <span class="string">&quot;1s&quot;</span></span><br><span class="line">&#125;&#x27;</span><br></pre></td></tr></table></figure><ul><li><p>restore snapshots from S3 into AWS managed ES</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># e.g restore from specific snapshot</span></span><br><span class="line">curl -XPOST <span class="string">&quot;<span class="variable">$es</span>/_snapshot/XXX-snapshot-s3-repo/marvel-20191024/_restore&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># e.g. restore just one index, &quot;.marvel-2019.10.24&quot;, from &quot;marvel-20191024&quot; snapshot in the &quot;XXX-snapshot-s3-repo&quot; snapshot repository:</span></span><br><span class="line">curl -XPOST <span class="string">&quot;<span class="variable">$es</span>/_snapshot/XXX-snapshot-s3-repo/marvel-20191024/_restore&quot;</span> -d <span class="string">&#x27;&#123;&quot;indices&quot;: &quot;.marvel-2019.10.24&quot;&#125;&#x27;</span> -H <span class="string">&#x27;Content-Type: application/json&#x27;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment"># e.g restore all indices except for the .kibana index</span></span><br><span class="line">curl -XPOST <span class="string">&quot;<span class="variable">$es</span>/_snapshot/XXX-snapshot-s3-repo/marvel-20191024/_restore&quot;</span> -d <span class="string">&#x27;&#123;&quot;indices&quot;: &quot;*,-.kibana&quot;&#125;&#x27;</span> -H <span class="string">&#x27;Content-Type: application/json&#x27;</span></span><br></pre></td></tr></table></figure></li><li><p>Run <code>es_migrate.sh</code> script to snapshot / restore</p></li></ul><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">time</span> bash -x es_migrate.sh</span><br><span class="line"> </span><br><span class="line"><span class="attribute">real</span>    <span class="number">10</span>m<span class="number">7</span>.<span class="number">667</span>s</span><br><span class="line"><span class="attribute">user</span>    <span class="number">0</span>m<span class="number">0</span>.<span class="number">129</span>s</span><br><span class="line"><span class="attribute">sys</span> <span class="number">0</span>m<span class="number">0</span>.<span class="number">142</span>s</span><br></pre></td></tr></table></figure><h2 id="Switch-from-self-hosted-to-AWS-managed-ES"><a href="#Switch-from-self-hosted-to-AWS-managed-ES" class="headerlink" title="Switch from self-hosted to AWS managed ES"></a>Switch from self-hosted to AWS managed ES</h2><p>get ECS task parameter settings and set ES endpoint to AWS managed ES for application</p><h2 id="Testing"><a href="#Testing" class="headerlink" title="Testing"></a>Testing</h2><ul><li>General function testing</li><li>Rollback to old ELB in CNAME if errors</li><li>Data Integration Check</li></ul><p>Will generate <code>doc_count</code> diff result for old / new ES (take index <code>.marvel-*</code> for example):</p><figure class="highlight tap"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#index  old_doc_count   new_doc_count   diff_rate</span></span><br><span class="line"> </span><br><span class="line">.marvel-2019.11.08 <span class="number"> 114929 </span>    <span class="number"> 114929 </span>     0.0000%</span><br><span class="line">.marvel-2019.11.09 <span class="number"> 114930 </span>    <span class="number"> 114930 </span>     0.0000%</span><br><span class="line">.marvel-2019.11.10 <span class="number"> 114904 </span>    <span class="number"> 114904 </span>     0.0000%</span><br><span class="line">.marvel-2019.11.11 <span class="number"> 104327 </span>    <span class="number"> 103532 </span>     -0.7620%</span><br><span class="line">search             <span class="number"> 21025214 </span>  <span class="number"> 21025162 </span>   -0.0002%</span><br><span class="line">site-1.0           <span class="number"> 939656 </span>    <span class="number"> 939657 </span>     0.0001%</span><br><span class="line">site_v1            <span class="number"> 324638 </span>    <span class="number"> 324638 </span>     0.0000%</span><br><span class="line"> </span><br><span class="line">real    10m7.303s</span><br><span class="line">user    0m0.156s</span><br><span class="line">sys 0m0.121s</span><br></pre></td></tr></table></figure><h2 id="Tidy-up-resources-in-Puppet-Terraform"><a href="#Tidy-up-resources-in-Puppet-Terraform" class="headerlink" title="Tidy up resources in Puppet / Terraform"></a>Tidy up resources in Puppet / Terraform</h2><p>The last step is that clean up old resources in Puppet or Terraform (e.g. EC2 / SG / Route53 etc.)</p><h1 id="Reference"><a href="#Reference" class="headerlink" title="Reference"></a>Reference</h1><p><span class="exturl" data-url="aHR0cHM6Ly9hd3MuYW1hem9uLmNvbS9wcmVtaXVtc3VwcG9ydC9rbm93bGVkZ2UtY2VudGVyL2VsYXN0aWNzZWFyY2gtaW5kZXhpbmctcGVyZm9ybWFuY2Uv">knowledge-center/elasticsearch-indexing-performance<i class="fa fa-external-link-alt"></i></span></p><p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL2VsYXN0aWNzZWFyY2gtc2VydmljZS9sYXRlc3QvZGV2ZWxvcGVyZ3VpZGUvc2l6aW5nLWRvbWFpbnMuaHRtbA==">AWS - sizing domains<i class="fa fa-external-link-alt"></i></span></p><p><span class="exturl" data-url="aHR0cHM6Ly9tZWRpdW0uY29tL2tyYWtlbnN5c3RlbXMtYmxvZy9taWdyYXRpbmcteW91ci1zZWxmLWhvc3RlZC1lbGFzdGljc2VhcmNoLXRvLWF3cy1lbGFzdGljc2VhcmNoLXNlcnZpY2UtYS1kZXRhaWxlZC1ndWlkZS1mZjIyZWZlZGU1YTM=">Migrating your self-hosted ElasticSearch to AWS ElasticSearch Service<i class="fa fa-external-link-alt"></i></span></p>]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;Background&quot;&gt;&lt;a href=&quot;#Background&quot; class=&quot;headerlink&quot; title=&quot;Background&quot;&gt;&lt;/a&gt;Background&lt;/h1&gt;&lt;p&gt;Let’s say there’s a legacy platform called XXX, and it still poses an operational cost for SRE to manage the monitoring / alerting / upgrade etc.&lt;/p&gt;
&lt;p&gt;At present, XXX’s ElasticSearch architecture consists of 3 hosts (m4.xlarge) acting as master, client and data nodes, for XXX’s searching functionality.&lt;/p&gt;
&lt;p&gt;All of them are using Ubuntu Trusty (14.04) image, which is no longer supported and need to be migrated to Xenial (16.04). Its usage seems pretty low and cost wise so we would like to migrate the XXX ES to AWS managed ElasticSearch Service to reduce operational overhead.&lt;/p&gt;
&lt;p&gt;This doc will cover the project plan / checklist for migration.&lt;/p&gt;
&lt;h1 id=&quot;Solution-overview&quot;&gt;&lt;a href=&quot;#Solution-overview&quot; class=&quot;headerlink&quot; title=&quot;Solution overview&quot;&gt;&lt;/a&gt;Solution overview&lt;/h1&gt;&lt;p&gt;Elasticsearch (ES) indexes can be migrated with following steps:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Create baseline indexes&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Create a snapshot repository and associate it to an AWS S3 Bucket.&lt;/li&gt;
&lt;li&gt;Create the first snapshot of the indexes to be migrated, which is a full snapshot on EC2 - The snapshot will be automatically stored in the AWS S3 bucket created in the first step.&lt;/li&gt;
&lt;li&gt;Restore this full snapshot to the AWS ES.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Periodic incremental snapshots&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Repeat serval incremental snapshot and restore.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Final snapshot and service switchover&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Stop services which can modify index data.&lt;/li&gt;
&lt;li&gt;Create a final incremental snapshot of the EC2.&lt;/li&gt;
&lt;li&gt;Perform service switchover to the AWS ES.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
    
    </summary>
    
    
      <category term="AWS" scheme="http://davidlu1001.github.io/categories/AWS/"/>
    
      <category term="SRE" scheme="http://davidlu1001.github.io/categories/SRE/"/>
    
      <category term="ElasticSearch" scheme="http://davidlu1001.github.io/categories/ElasticSearch/"/>
    
    
      <category term="AWS" scheme="http://davidlu1001.github.io/tags/AWS/"/>
    
      <category term="ElasticSearch" scheme="http://davidlu1001.github.io/tags/ElasticSearch/"/>
    
      <category term="SRE" scheme="http://davidlu1001.github.io/tags/SRE/"/>
    
  </entry>
  
  <entry>
    <title>How to use Terraform include / exclude with Makefile</title>
    <link href="http://davidlu1001.github.io/2019/08/13/How-to-use-Terraform-include-exclude/"/>
    <id>http://davidlu1001.github.io/2019/08/13/How-to-use-Terraform-include-exclude/</id>
    <published>2019-08-13T09:59:05.000Z</published>
    <updated>2019-08-13T09:59:05.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Background"><a href="#Background" class="headerlink" title="Background"></a>Background</h1><p>When using make plan command, if there’re too many resources need to be applied, together with other unrelated resources that we don’t want to touch, then we need to use Terraform <span class="exturl" data-url="aHR0cHM6Ly93d3cudGVycmFmb3JtLmlvL2RvY3MvY29tbWFuZHMvcGxhbi5odG1sI3Jlc291cmNlLXRhcmdldGluZw==">resource targeting<i class="fa fa-external-link-alt"></i></span>:</p><figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">PLAN_OPTIONS</span>=<span class="string">&quot;-target=&quot;</span>A&quot; <span class="attribute">-target</span>=<span class="string">&quot;B&quot;</span> <span class="built_in">..</span>. <span class="attribute">-target</span>=<span class="string">&quot;N&quot;</span><span class="string">&quot; make plan</span></span><br></pre></td></tr></table></figure><p>But Terraform <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2hhc2hpY29ycC90ZXJyYWZvcm0vaXNzdWVzLzIyNTM=">doesn’t support –exclude feature<i class="fa fa-external-link-alt"></i></span> for the target at the moment (and we don’t want to copy &amp; paste over 50 times for the targets), that’s why need to find a way to implement <code>exclude / include</code> features within Makefile.</p><a id="more"></a><h1 id="How-to"><a href="#How-to" class="headerlink" title="How-to"></a>How-to</h1><ul><li>make plan</li></ul><p>Run <code>make plan</code> to show pending changes, also generate <code>current.plan</code> that we use later to filter targets.</p><ul><li>filter targets</li></ul><p>Then pick the resources you want by using <code>[INCLUDE | EXCLUDE]</code> which support POSIX Extended Regular Expressions (ERE)</p><p>E.g.</p><figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># use include to specify only the targets you want to apply</span></span><br><span class="line">$ make plan_include <span class="attribute">INCLUDE</span>=<span class="string">&#x27;mark3a&#x27;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment"># output</span></span><br><span class="line">terraform plan -out current.plan    <span class="attribute">-target</span>=<span class="string">&quot;aws_alb_target_group_attachment.routers-mark3a&quot;</span> <span class="attribute">-target</span>=<span class="string">&quot;module.routers-mark3a.aws_instance.main&quot;</span> <span class="attribute">-target</span>=<span class="string">&quot;module.routers-mark3a.aws_route53_record.main_instance&quot;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment"># exclude the targets</span></span><br><span class="line">$ make plan_exclude <span class="attribute">EXCLUDE</span>=<span class="string">&#x27;aws_security_group|mark3b&#x27;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment"># output</span></span><br><span class="line">terraform plan -out current.plan    <span class="attribute">-target</span>=<span class="string">&quot;aws_alb_target_group_attachment.routers-mark3a&quot;</span> <span class="attribute">-target</span>=<span class="string">&quot;aws_alb_target_group_attachment.routers-mark3c&quot;</span> <span class="attribute">-target</span>=<span class="string">&quot;module.asg_databus_production.aws_autoscaling_group.main&quot;</span> <span class="attribute">-target</span>=<span class="string">&quot;module.asg_router_production.aws_autoscaling_group.main&quot;</span> <span class="attribute">-target</span>=<span class="string">&quot;module.asg_router_production.aws_launch_configuration.main&quot;</span> <span class="attribute">-target</span>=<span class="string">&quot;module.routers-mark3a.aws_instance.main&quot;</span> <span class="attribute">-target</span>=<span class="string">&quot;module.routers-mark3a.aws_route53_record.main_instance&quot;</span> <span class="attribute">-target</span>=<span class="string">&quot;module.routers-mark3c.aws_instance.main&quot;</span> <span class="attribute">-target</span>=<span class="string">&quot;module.routers-mark3c.aws_route53_record.main_instance&quot;</span></span><br></pre></td></tr></table></figure><ul><li>make apply</li></ul><p>Finally can use <code>make apply</code> to apply the changes based on new <code>current.plan</code></p><h1 id="Explain"><a href="#Explain" class="headerlink" title="Explain"></a>Explain</h1><p>Show the output generated by <code>make plan</code>, then process the output (e.g. clear ANSI color / match resource names after empty line etc.) to generate the final format (e.g. -target=”A” -target=”B” -target=”C” …)</p><p>in Makefile:</p><figure class="highlight reasonml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"># For Terraform <span class="number">0.11</span></span><br><span class="line">define PLAN_OPTIONS_EXCLUDE</span><br><span class="line"><span class="constructor">$(<span class="params">shell</span> <span class="params">terraform</span> <span class="params">show</span> <span class="params">current</span>.<span class="params">plan</span> | <span class="params">perl</span> -<span class="params">pe</span> &#x27;<span class="params">s</span><span class="operator">/</span><span class="operator">/</span>\<span class="params">n</span><span class="operator">/</span> <span class="params">if</span> $$. <span class="operator">==</span> 1&#x27; | <span class="params">perl</span> -<span class="params">pe</span> &#x27;<span class="params">s</span><span class="operator">/</span>\<span class="params">x1b</span>\[[0-9;]<span class="operator">*</span>[<span class="params">mG</span>]<span class="operator">/</span><span class="operator">/</span><span class="params">g</span>&#x27; | <span class="params">perl</span> -<span class="params">ne</span> &#x27;<span class="params">if</span> ($$<span class="params">p</span>)</span> &#123; print unless /^$$/; $$p = <span class="number">0</span> &#125; $$p++ <span class="keyword">if</span> /^$$/&#x27;<span class="pattern-match">| awk &#x27;&#123;print <span class="constructor">$$2</span>&#125;&#x27; | grep -<span class="constructor">E</span> -v &#x27;<span class="constructor">$(1)</span>&#x27; | sed -e &#x27;s<span class="operator">/</span>^<span class="operator">/</span>-target=&quot;<span class="operator">/</span>g&#x27; -e &#x27;s<span class="operator">/</span><span class="constructor">$$</span><span class="operator">/</span>&quot;<span class="operator">/</span>g&#x27; | awk <span class="constructor">BEGIN</span>&#123;<span class="constructor">RS</span>=<span class="constructor">EOF</span>&#125;&#x27;&#123;gsub(<span class="operator">/</span>\n<span class="operator">/</span>,&quot; &quot;);print&#125;&#x27;)</span></span><br><span class="line"><span class="pattern-match">endef</span></span><br><span class="line"><span class="pattern-match"></span></span><br><span class="line"><span class="pattern-match">define <span class="constructor">PLAN_OPTIONS_INCLUDE</span></span></span><br><span class="line"><span class="pattern-match"><span class="constructor">$(<span class="params">shell</span> <span class="params">terraform</span> <span class="params">show</span> <span class="params">current</span>.<span class="params">plan</span> | <span class="params">perl</span> -<span class="params">pe</span> &#x27;<span class="params">s</span><span class="operator">/</span><span class="operator">/</span>\<span class="params">n</span><span class="operator">/</span> <span class="params">if</span> $$. <span class="operator">==</span> 1&#x27; | <span class="params">perl</span> -<span class="params">pe</span> &#x27;<span class="params">s</span><span class="operator">/</span>\<span class="params">x1b</span>\[[0-9;]<span class="operator">*</span>[<span class="params">mG</span>]<span class="operator">/</span><span class="operator">/</span><span class="params">g</span>&#x27; | <span class="params">perl</span> -<span class="params">ne</span> &#x27;<span class="params">if</span> ($$<span class="params">p</span>)</span> &#123; print unless <span class="operator">/</span>^<span class="constructor">$$</span><span class="operator">/</span>; <span class="constructor">$$p</span> = 0 &#125; <span class="constructor">$$p</span><span class="operator">++</span> <span class="keyword">if</span> <span class="operator">/</span>^<span class="constructor">$$</span><span class="operator">/</span>&#x27;| awk &#x27;&#123;print <span class="constructor">$$2</span>&#125;&#x27; | grep -<span class="constructor">E</span> &#x27;<span class="constructor">$(1)</span>&#x27; | sed -e &#x27;s<span class="operator">/</span>^<span class="operator">/</span>-target=&quot;<span class="operator">/</span>g&#x27; -e &#x27;s<span class="operator">/</span><span class="constructor">$$</span><span class="operator">/</span>&quot;<span class="operator">/</span>g&#x27; | awk <span class="constructor">BEGIN</span>&#123;<span class="constructor">RS</span>=<span class="constructor">EOF</span>&#125;&#x27;&#123;gsub(<span class="operator">/</span>\n<span class="operator">/</span>,&quot; &quot;);print&#125;&#x27;)</span></span><br><span class="line"><span class="pattern-match">endef</span></span><br><span class="line"><span class="pattern-match"></span></span><br><span class="line"><span class="pattern-match"># <span class="constructor">For</span> <span class="constructor">Terraform</span> 0.12 (using -no-color <span class="keyword">to</span> avoid dealing <span class="keyword">with</span> terminal color)</span></span><br><span class="line"><span class="pattern-match">define <span class="constructor">PLAN_OPTIONS_EXCLUDE</span></span></span><br><span class="line"><span class="pattern-match"><span class="constructor">$(<span class="params">shell</span> <span class="params">terraform</span> <span class="params">show</span> -<span class="params">no</span>-<span class="params">color</span> <span class="params">current</span>.<span class="params">plan</span> | <span class="params">perl</span> -<span class="params">nle</span> &#x27;<span class="params">if</span> (<span class="operator">/</span>\<span class="params">s</span># (.<span class="operator">*</span>?)</span>\s<span class="operator">/</span>) &#123;print <span class="constructor">$$1</span>&#125;&#x27; | grep -<span class="constructor">E</span> -v &#x27;<span class="constructor">$(1)</span>&#x27; | sed -e &#x27;s<span class="operator">/</span>^<span class="operator">/</span>-target=&quot;<span class="operator">/</span>g&#x27; -e &#x27;s<span class="operator">/</span><span class="constructor">$$</span><span class="operator">/</span>&quot;<span class="operator">/</span>g&#x27; | xargs)</span></span><br><span class="line"><span class="pattern-match">endef</span></span><br><span class="line"><span class="pattern-match"></span></span><br><span class="line"><span class="pattern-match">define <span class="constructor">PLAN_OPTIONS_INCLUDE</span></span></span><br><span class="line"><span class="pattern-match"><span class="constructor">$(<span class="params">shell</span> <span class="params">terraform</span> <span class="params">show</span> -<span class="params">no</span>-<span class="params">color</span> <span class="params">current</span>.<span class="params">plan</span> | <span class="params">perl</span> -<span class="params">nle</span> &#x27;<span class="params">if</span> (<span class="operator">/</span>\<span class="params">s</span># (.<span class="operator">*</span>?)</span>\s<span class="operator">/</span>) &#123;print <span class="constructor">$$1</span>&#125;&#x27; | grep -<span class="constructor">E</span> &#x27;<span class="constructor">$(1)</span>&#x27; | sed -e &#x27;s<span class="operator">/</span>^<span class="operator">/</span>-target=&quot;<span class="operator">/</span>g&#x27; -e &#x27;s<span class="operator">/</span><span class="constructor">$$</span><span class="operator">/</span>&quot;<span class="operator">/</span>g&#x27; | xargs)</span></span><br><span class="line"><span class="pattern-match">endef</span></span><br></pre></td></tr></table></figure><p>and then the macro <code>PLAN_OPTIONS_EXCLUDE</code> / <code>PLAN_OPTIONS_INCLUDE</code> can be triggered by <code>plan_exclude</code> / <code>plan_include</code> in Makefile:</p><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">plan_exclude:</span></span><br><span class="line">  terraform plan -out current.plan <span class="variable">$(<span class="built_in">strip</span> $(<span class="built_in">call</span> PLAN_OPTIONS_EXCLUDE,<span class="variable">$(EXCLUDE)</span>)</span>)</span><br><span class="line"></span><br><span class="line"><span class="section">plan_include:</span></span><br><span class="line">  terraform plan -out current.plan <span class="variable">$(<span class="built_in">strip</span> $(<span class="built_in">call</span> PLAN_OPTIONS_INCLUDE,<span class="variable">$(INCLUDE)</span>)</span>)</span><br></pre></td></tr></table></figure><h1 id="Gist"><a href="#Gist" class="headerlink" title="Gist"></a>Gist</h1><p>Final gist could be find <span class="exturl" data-url="aHR0cHM6Ly9naXN0LmdpdGh1Yi5jb20vZGF2aWRsdTEwMDEvZTgzMjAzODI5OWZmZjk5ZDRhNGIyYzZhNzVkNzFiNzg=">here<i class="fa fa-external-link-alt"></i></span></p><h1 id="Limitations"><a href="#Limitations" class="headerlink" title="Limitations"></a>Limitations</h1><p>Unfortunately, when the target list becomes very long (e.g. 49) You start to see <code>Too many command line arguments. Configuration path expected</code>, so this workaround is not enough…</p><p>Anyway Inverse targeting is becoming more and more important / necessary in huge terraform plans with lots of modules managed by different teams, so hope there’s an official solution for that in the new future.</p>]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;Background&quot;&gt;&lt;a href=&quot;#Background&quot; class=&quot;headerlink&quot; title=&quot;Background&quot;&gt;&lt;/a&gt;Background&lt;/h1&gt;&lt;p&gt;When using make plan command, if there’re too many resources need to be applied, together with other unrelated resources that we don’t want to touch, then we need to use Terraform &lt;a href=&quot;https://www.terraform.io/docs/commands/plan.html#resource-targeting&quot;&gt;resource targeting&lt;/a&gt;:&lt;/p&gt;
&lt;figure class=&quot;highlight routeros&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;1&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;attribute&quot;&gt;PLAN_OPTIONS&lt;/span&gt;=&lt;span class=&quot;string&quot;&gt;&amp;quot;-target=&amp;quot;&lt;/span&gt;A&amp;quot; &lt;span class=&quot;attribute&quot;&gt;-target&lt;/span&gt;=&lt;span class=&quot;string&quot;&gt;&amp;quot;B&amp;quot;&lt;/span&gt; &lt;span class=&quot;built_in&quot;&gt;..&lt;/span&gt;. &lt;span class=&quot;attribute&quot;&gt;-target&lt;/span&gt;=&lt;span class=&quot;string&quot;&gt;&amp;quot;N&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;&amp;quot; make plan&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;

&lt;p&gt;But Terraform &lt;a href=&quot;https://github.com/hashicorp/terraform/issues/2253&quot;&gt;doesn’t support –exclude feature&lt;/a&gt; for the target at the moment (and we don’t want to copy &amp;amp; paste over 50 times for the targets), that’s why need to find a way to implement &lt;code&gt;exclude / include&lt;/code&gt; features within Makefile.&lt;/p&gt;
    
    </summary>
    
    
      <category term="DevOps" scheme="http://davidlu1001.github.io/categories/DevOps/"/>
    
      <category term="SRE" scheme="http://davidlu1001.github.io/categories/SRE/"/>
    
      <category term="Terraform" scheme="http://davidlu1001.github.io/categories/Terraform/"/>
    
    
      <category term="DevOps" scheme="http://davidlu1001.github.io/tags/DevOps/"/>
    
      <category term="SRE" scheme="http://davidlu1001.github.io/tags/SRE/"/>
    
      <category term="Terraform" scheme="http://davidlu1001.github.io/tags/Terraform/"/>
    
      <category term="Makefile" scheme="http://davidlu1001.github.io/tags/Makefile/"/>
    
  </entry>
  
  <entry>
    <title>Move Terraform resources between remote states</title>
    <link href="http://davidlu1001.github.io/2019/04/28/Move-Terraform-resources-between-remote-states/"/>
    <id>http://davidlu1001.github.io/2019/04/28/Move-Terraform-resources-between-remote-states/</id>
    <published>2019-04-28T11:06:24.000Z</published>
    <updated>2019-04-28T11:06:24.000Z</updated>
    
    <content type="html"><![CDATA[<p>This document outlines the steps to move existing terraform resources to different remote states.</p><p>Reference: <span class="exturl" data-url="aHR0cHM6Ly9tZWRpdW0uY29tL0BseW5ubGluODI3L21vdmluZy10ZXJyYWZvcm0tcmVzb3VyY2VzLXN0YXRlcy1mcm9tLW9uZS1yZW1vdGUtc3RhdGUtdG8tYW5vdGhlci1jNzZmOGI3NmE5OTY=">https://medium.com/@lynnlin827/moving-terraform-resources-states-from-one-remote-state-to-another-c76f8b76a996<i class="fa fa-external-link-alt"></i></span></p><h3 id="Steps"><a href="#Steps" class="headerlink" title="Steps"></a>Steps</h3><h4 id="1-Move-Terraform-files-to-the-new-folder"><a href="#1-Move-Terraform-files-to-the-new-folder" class="headerlink" title="1. Move Terraform files to the new folder"></a>1. Move Terraform files to the new folder</h4><h4 id="2-Run-make-plan-to-get-a-list-of-resources-that-would-be-deleted"><a href="#2-Run-make-plan-to-get-a-list-of-resources-that-would-be-deleted" class="headerlink" title="2. Run make plan to get a list of resources that would be deleted"></a>2. Run make plan to get a list of resources that <em>would</em> be deleted</h4><p>sample to generate desired commands used for next step</p><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">terraform</span> plan | grep <span class="string">&#x27;\s-\s&#x27;</span> | sed -e <span class="string">&#x27;s/\s-\s//g&#x27;</span> | awk <span class="string">&#x27;&#123;print &quot;terraform state mv -state-out=monitoring/.terraform/terraform.tfstate &quot;<span class="variable">$1</span>,<span class="variable">$1</span>&#125;&#x27;</span></span><br></pre></td></tr></table></figure><a id="more"></a><h4 id="3-Ran-commands-bellow-to-move"><a href="#3-Ran-commands-bellow-to-move" class="headerlink" title="3. Ran commands bellow to move"></a>3. Ran commands bellow to move</h4><figure class="highlight pf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">cd infrastructure</span><br><span class="line"> </span><br><span class="line">terraform <span class="keyword">state</span> mv -state-out=monitoring/.terraform/terraform.tfstate datadog_monitor.monitor-<span class="number">01</span>-composite datadog_monitor.monitor-<span class="number">01</span>-composite</span><br><span class="line">...</span><br></pre></td></tr></table></figure><h4 id="4-Push-remote-state-in-new-folder"><a href="#4-Push-remote-state-in-new-folder" class="headerlink" title="4. Push remote state in new folder"></a>4. Push remote state in new folder</h4><figure class="highlight pf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">cd monitoring</span><br><span class="line"></span><br><span class="line">terraform <span class="keyword">state</span> push .terraform/terraform.tfstate</span><br></pre></td></tr></table></figure><p>As in the company we use <code>Makefile</code> to chain several steps together, so remember to push the remote state before run <code>make plan</code> (otherwise the tfstate will be deleted):</p><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">clean:</span><br><span class="line"><span class="built_in">rm</span> <span class="literal">-rf</span> .terraform</span><br><span class="line"><span class="built_in">rm</span> <span class="operator">-f</span> current.plan</span><br><span class="line"><span class="built_in">rm</span> <span class="operator">-f</span> *.tf.json</span><br><span class="line"></span><br><span class="line">plan: clean check_remote_modules init get_modules</span><br><span class="line">terraform plan <span class="literal">-out</span> current.plan <span class="variable">$</span>&#123;PLAN_OPTIONS&#125;</span><br></pre></td></tr></table></figure><h4 id="5-Check-by-make-plan"><a href="#5-Check-by-make-plan" class="headerlink" title="5. Check by make plan"></a>5. Check by make plan</h4><p>Example of a <code>terraform plan</code> from <code>infrastructure/monitoring</code> folder to confirm that nothing has changed</p><figure class="highlight pf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">If you ever <span class="built_in">set</span> or change modules or backend configuration <span class="keyword">for</span> Terraform,</span><br><span class="line">rerun this command <span class="keyword">to</span> reinitialize your working directory. If you forget, other</span><br><span class="line">commands will detect it and remind you <span class="keyword">to</span> do so if necessary.</span><br><span class="line">rm -f current.plan</span><br><span class="line">rm -f *.tf.json</span><br><span class="line">Nothing <span class="keyword">to</span> do</span><br><span class="line">terraform get</span><br><span class="line">terraform plan -out current.plan</span><br><span class="line">Refreshing Terraform <span class="keyword">state</span> in-memory prior <span class="keyword">to</span> plan...</span><br><span class="line">The refreshed <span class="keyword">state</span> will be used <span class="keyword">to</span> calculate this plan, but will not be</span><br><span class="line">persisted <span class="keyword">to</span> local or remote <span class="keyword">state</span> storage.</span><br><span class="line"> </span><br><span class="line">datadog_monitor.monitor-<span class="number">01</span>-composite: Refreshing <span class="keyword">state</span>... (ID: <span class="number">8413395</span>)</span><br><span class="line">...</span><br><span class="line"></span><br><span class="line">------------------------------------------------------------------------</span><br><span class="line"> </span><br><span class="line">No changes. Infrastructure is up-to-date.</span><br><span class="line"> </span><br><span class="line">This means that Terraform did not detect <span class="literal">any</span> differences between your</span><br><span class="line">configuration and real physical resources that exist. As a result, <span class="keyword">no</span></span><br><span class="line">actions need <span class="keyword">to</span> be performed.</span><br></pre></td></tr></table></figure><p><code>terraform plan</code> from main folder</p><figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">aws_alb_listener.<span class="attr">main:</span> Refreshing state... (<span class="attr">ID:</span> <span class="attr">arn:</span><span class="attr">aws:</span><span class="attr">elasticloadbalancing:</span>us-west<span class="number">-2</span>:...tion<span class="regexp">/22124b66b571c93f/</span><span class="number">1553846</span>d70106854)</span><br><span class="line">aws_alb_listener.<span class="attr">main:</span> Refreshing state... (<span class="attr">ID:</span> <span class="attr">arn:</span><span class="attr">aws:</span><span class="attr">elasticloadbalancing:</span>us-west<span class="number">-2</span>:...tion<span class="regexp">/ce700936e3b458e3/</span><span class="number">4</span>f8982a6592d6312)</span><br><span class="line">aws_alb_listener.<span class="attr">main:</span> Refreshing state... (<span class="attr">ID:</span> <span class="attr">arn:</span><span class="attr">aws:</span><span class="attr">elasticloadbalancing:</span>us-west<span class="number">-2</span>:...tion<span class="regexp">/cb803380f953aafc/</span><span class="number">3</span>c695ba20ad9d237)</span><br><span class="line"> </span><br><span class="line">------------------------------------------------------------------------</span><br><span class="line"> </span><br><span class="line">No changes. Infrastructure is up-to-date.</span><br><span class="line"> </span><br><span class="line">This means that Terraform did not detect any differences between your</span><br><span class="line">configuration and real physical resources that exist. As a result, no</span><br><span class="line">actions need to be performed.</span><br></pre></td></tr></table></figure><h4 id="6-Recovery-in-case-ruin-the-remote-state"><a href="#6-Recovery-in-case-ruin-the-remote-state" class="headerlink" title="6. Recovery in case ruin the remote state"></a>6. Recovery in case ruin the remote state</h4><p>For Step 4, if ran <code>make plan</code> before pushing the remote state in new folder, the dedicated remote state file will be deleted, and the make plan output will show that the new resources would be created.</p><p>So for recovery, there’re two places that can give some hints.</p><h5 id="Local-terraform-backup"><a href="#Local-terraform-backup" class="headerlink" title="Local terraform backup"></a>Local terraform backup</h5><p>Terraform usually creates a backup file under <code>.terraform/</code> (after run <code>terraform state mv</code> command in Step 3)</p><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">-rw-r--r--</span> 1 <span class="selector-tag">davidlu</span> 329622 <span class="selector-tag">May</span>  5 10<span class="selector-pseudo">:46</span> <span class="selector-tag">terraform</span><span class="selector-class">.tfstate</span>.1588632389<span class="selector-class">.backup</span></span><br><span class="line"><span class="selector-tag">-rw-r--r--</span> 1 <span class="selector-tag">davidlu</span> 328503 <span class="selector-tag">May</span>  5 10<span class="selector-pseudo">:46</span> <span class="selector-tag">terraform</span><span class="selector-class">.tfstate</span>.1588632399<span class="selector-class">.backup</span></span><br><span class="line"><span class="selector-tag">-rw-r--r--</span> 1 <span class="selector-tag">davidlu</span> 327160 <span class="selector-tag">May</span>  5 10<span class="selector-pseudo">:46</span> <span class="selector-tag">terraform</span><span class="selector-class">.tfstate</span>.1588632409<span class="selector-class">.backup</span></span><br><span class="line"><span class="selector-tag">-rw-r--r--</span> 1 <span class="selector-tag">davidlu</span> 326185 <span class="selector-tag">May</span>  5 10<span class="selector-pseudo">:47</span> <span class="selector-tag">terraform</span><span class="selector-class">.tfstate</span>.1588632420<span class="selector-class">.backup</span></span><br><span class="line"><span class="selector-tag">-rw-r--r--</span> 1 <span class="selector-tag">davidlu</span> 322535 <span class="selector-tag">May</span>  5 10<span class="selector-pseudo">:47</span> <span class="selector-tag">terraform</span><span class="selector-class">.tfstate</span>.1588632429<span class="selector-class">.backup</span></span><br><span class="line"><span class="selector-tag">-rw-r--r--</span> 1 <span class="selector-tag">davidlu</span> 319527 <span class="selector-tag">May</span>  5 10<span class="selector-pseudo">:47</span> <span class="selector-tag">terraform</span><span class="selector-class">.tfstate</span>.1588632438<span class="selector-class">.backup</span></span><br><span class="line"><span class="selector-tag">-rw-r--r--</span> 1 <span class="selector-tag">davidlu</span> 316520 <span class="selector-tag">May</span>  5 10<span class="selector-pseudo">:47</span> <span class="selector-tag">terraform</span><span class="selector-class">.tfstate</span>.1588632449<span class="selector-class">.backup</span></span><br></pre></td></tr></table></figure><h5 id="S3-bucket-recommend"><a href="#S3-bucket-recommend" class="headerlink" title="S3 bucket (recommend)"></a>S3 bucket (recommend)</h5><p>We can still restore an older remote state version from S3</p><p><strong><em>Steps</em></strong>:</p><ol><li><p>Download original older remote state file from S3 (e.g. <code>production-terraform/datastores/production</code>)</p></li><li><p>Extract the related resources (e.g. blaze)</p><ul><li>Search the keyword and copy &amp; paste</li><li>The above process can be tedious and error-prone: introduce <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL3RvbW5vbW5vbS9ncm9u">gron<i class="fa fa-external-link-alt"></i></span> here - would be helpful for <strong>double-check</strong> (can make JSON greppable and turn filtered data back into JSON)</li></ul></li></ol><figure class="highlight coq"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">e.g.</span><br><span class="line"></span><br><span class="line">gron original_older_remote_state.json | <span class="type">grep</span> blaze | <span class="type">gron</span> -u</span><br></pre></td></tr></table></figure><ol start="3"><li>Tweak config to match</li></ol><ul><li>Base the previous steps, we’ve got the remote state file in new folder, something like this:</li></ul><figure class="highlight clojure"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;version&quot;</span>: <span class="number">3</span>,</span><br><span class="line">    <span class="string">&quot;terraform_version&quot;</span>: <span class="string">&quot;0.11.14&quot;</span>,</span><br><span class="line">    <span class="string">&quot;serial&quot;</span>: <span class="number">3</span>,</span><br><span class="line">    <span class="string">&quot;lineage&quot;</span>: <span class="string">&quot;e092833e-c86c-edb2-1c15-c5e04f1923d7&quot;</span>,</span><br><span class="line">    <span class="string">&quot;modules&quot;</span>: [</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">&quot;path&quot;</span>: [</span><br><span class="line">                <span class="string">&quot;root&quot;</span></span><br><span class="line">            ],</span><br><span class="line">            <span class="string">&quot;outputs&quot;</span>: &#123;&#125;,</span><br><span class="line">            <span class="string">&quot;resources&quot;</span>: &#123;</span><br><span class="line"></span><br><span class="line">                [...RESOURCES...]</span><br><span class="line"></span><br><span class="line">            &#125;,</span><br><span class="line">            <span class="string">&quot;depends_on&quot;</span>: []</span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><ul><li>Download remote state file for new folder (e.g. <code>production-terraform/datastores/services/blaze</code>)</li></ul><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="attr">&quot;version&quot;</span>: <span class="number">3</span>,</span><br><span class="line">    <span class="attr">&quot;serial&quot;</span>: <span class="number">2</span>,</span><br><span class="line">    <span class="attr">&quot;lineage&quot;</span>: <span class="string">&quot;e092833e-c86c-edb2-1c15-c5e04f1923d7&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;backend&quot;</span>: &#123;</span><br><span class="line">        <span class="attr">&quot;type&quot;</span>: <span class="string">&quot;s3&quot;</span>,</span><br><span class="line">        <span class="attr">&quot;config&quot;</span>: &#123;</span><br><span class="line">            <span class="attr">&quot;bucket&quot;</span>: <span class="string">&quot;production-terraform&quot;</span>,</span><br><span class="line">            <span class="attr">&quot;key&quot;</span>: <span class="string">&quot;datastores/services/blaze&quot;</span>,</span><br><span class="line">            <span class="attr">&quot;region&quot;</span>: <span class="string">&quot;us-west-2&quot;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="attr">&quot;hash&quot;</span>: <span class="number">3065110275131057704</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">&quot;modules&quot;</span>: [</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">&quot;path&quot;</span>: [</span><br><span class="line">                <span class="string">&quot;root&quot;</span></span><br><span class="line">            ],</span><br><span class="line">            <span class="attr">&quot;outputs&quot;</span>: &#123;&#125;,</span><br><span class="line">            <span class="attr">&quot;resources&quot;</span>: &#123;&#125;,</span><br><span class="line">            <span class="attr">&quot;depends_on&quot;</span>: []</span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><ul><li>Make sure has the same lineage, and higher version of serial (compared with destination in S3)</li></ul><blockquote><p><strong><em>Differing lineage</em></strong>: The “lineage” is a unique ID assigned to a state when it is created. If a lineage is different, then it means the states were created at different times and its very likely you’re modifying a different state. Terraform will not allow this.</p><p><strong><em>Higher serial</em></strong>: Every state has a monotonically increasing “serial” number. If the destination state has a higher serial, Terraform will not allow you to write it since it means that changes have occurred since the state you’re attempting to write.</p><p>Ref: <span class="exturl" data-url="aHR0cHM6Ly93d3cudGVycmFmb3JtLmlvL2RvY3MvYmFja2VuZHMvc3RhdGUuaHRtbA==">https://www.terraform.io/docs/backends/state.html<i class="fa fa-external-link-alt"></i></span></p></blockquote><ul><li>Upload new tfstate file to S3</li></ul><figure class="highlight armasm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="symbol">e.g.</span></span><br><span class="line"></span><br><span class="line"><span class="symbol">aws</span> <span class="built_in">s3</span> <span class="meta">cp</span> blaze <span class="built_in">s3</span>:<span class="comment">//production-terraform/datastores/services/blaze</span></span><br></pre></td></tr></table></figure><ul><li>Run <code>terraform plan</code> in new folder (no changes expected)</li></ul>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;This document outlines the steps to move existing terraform resources to different remote states.&lt;/p&gt;
&lt;p&gt;Reference: &lt;a href=&quot;https://medium.com/@lynnlin827/moving-terraform-resources-states-from-one-remote-state-to-another-c76f8b76a996&quot;&gt;https://medium.com/@lynnlin827/moving-terraform-resources-states-from-one-remote-state-to-another-c76f8b76a996&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&quot;Steps&quot;&gt;&lt;a href=&quot;#Steps&quot; class=&quot;headerlink&quot; title=&quot;Steps&quot;&gt;&lt;/a&gt;Steps&lt;/h3&gt;&lt;h4 id=&quot;1-Move-Terraform-files-to-the-new-folder&quot;&gt;&lt;a href=&quot;#1-Move-Terraform-files-to-the-new-folder&quot; class=&quot;headerlink&quot; title=&quot;1. Move Terraform files to the new folder&quot;&gt;&lt;/a&gt;1. Move Terraform files to the new folder&lt;/h4&gt;&lt;h4 id=&quot;2-Run-make-plan-to-get-a-list-of-resources-that-would-be-deleted&quot;&gt;&lt;a href=&quot;#2-Run-make-plan-to-get-a-list-of-resources-that-would-be-deleted&quot; class=&quot;headerlink&quot; title=&quot;2. Run make plan to get a list of resources that would be deleted&quot;&gt;&lt;/a&gt;2. Run make plan to get a list of resources that &lt;em&gt;would&lt;/em&gt; be deleted&lt;/h4&gt;&lt;p&gt;sample to generate desired commands used for next step&lt;/p&gt;
&lt;figure class=&quot;highlight nginx&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;1&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;attribute&quot;&gt;terraform&lt;/span&gt; plan | grep &lt;span class=&quot;string&quot;&gt;&amp;#x27;\s-\s&amp;#x27;&lt;/span&gt; | sed -e &lt;span class=&quot;string&quot;&gt;&amp;#x27;s/\s-\s//g&amp;#x27;&lt;/span&gt; | awk &lt;span class=&quot;string&quot;&gt;&amp;#x27;&amp;#123;print &amp;quot;terraform state mv -state-out=monitoring/.terraform/terraform.tfstate &amp;quot;&lt;span class=&quot;variable&quot;&gt;$1&lt;/span&gt;,&lt;span class=&quot;variable&quot;&gt;$1&lt;/span&gt;&amp;#125;&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;
    
    </summary>
    
    
      <category term="SRE" scheme="http://davidlu1001.github.io/categories/SRE/"/>
    
      <category term="Terraform" scheme="http://davidlu1001.github.io/categories/Terraform/"/>
    
    
      <category term="SRE" scheme="http://davidlu1001.github.io/tags/SRE/"/>
    
      <category term="Terraform" scheme="http://davidlu1001.github.io/tags/Terraform/"/>
    
  </entry>
  
  <entry>
    <title>AWS Glue - Knowledge Base</title>
    <link href="http://davidlu1001.github.io/2018/08/20/AWS-Glue-Knowledge-Base/"/>
    <id>http://davidlu1001.github.io/2018/08/20/AWS-Glue-Knowledge-Base/</id>
    <published>2018-08-20T11:41:58.000Z</published>
    <updated>2018-08-20T11:41:58.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Glue-Official-FAQ"><a href="#Glue-Official-FAQ" class="headerlink" title="Glue - Official FAQ"></a>Glue - Official FAQ</h1><p>The official doc for troubleshooting could be found <span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL2dsdWUvbGF0ZXN0L2RnL3Ryb3VibGVzaG9vdGluZy1nbHVlLmh0bWw=">here<i class="fa fa-external-link-alt"></i></span></p><h1 id="Troubleshooting-Notes-amp-Tips"><a href="#Troubleshooting-Notes-amp-Tips" class="headerlink" title="Troubleshooting - Notes &amp; Tips"></a>Troubleshooting - Notes &amp; Tips</h1><p>Notes and tips for Glue when implementing the ETL process:</p><h2 id="Naming-rules-conventions-for-AWS-services"><a href="#Naming-rules-conventions-for-AWS-services" class="headerlink" title="Naming rules / conventions for AWS services"></a>Naming rules / conventions for AWS services</h2><ul><li>S3 bucket name can either NOT uppercase nor NOT contain “_” </li><li>Dynamo DB table / Name can NOT contain “-“</li><li>ES / Name: can NOT contain “-“</li></ul><h2 id="ACM-SSL-Cert"><a href="#ACM-SSL-Cert" class="headerlink" title="ACM SSL Cert"></a>ACM SSL Cert</h2><p>Using <code>us-east-1</code> region for AWS CloudFront (certificate)</p><a id="more"></a><h2 id="ElasticSearch-Schema"><a href="#ElasticSearch-Schema" class="headerlink" title="ElasticSearch Schema"></a>ElasticSearch Schema</h2><p>Updated the ES mappings in all environments so that field A searches are now case insensitive and will work with spaces in A names</p><p>e.g. Fixed the issue for field A so searches are now case insensitive and will work with spaces in A names</p><figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">query</span>=<span class="string">&#x27;&#123;&quot;query&quot;:&#123;&quot;terms&quot;:&#123;&quot;name&quot;:[&quot;gift vouchers&quot;]&#125;&#125;&#125;&#x27;</span></span><br><span class="line">python sign_request.py -X <span class="builtin-name">GET</span> <span class="variable">$es</span>/A/_search -d <span class="string">&quot;<span class="variable">$query</span>&quot;</span></span><br><span class="line">| jq -r <span class="string">&#x27;.hits.hits&#x27;</span></span><br><span class="line">[</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="string">&quot;_index&quot;</span>: <span class="string">&quot;A&quot;</span>,</span><br><span class="line">    <span class="string">&quot;_type&quot;</span>: <span class="string">&quot;A&quot;</span>,</span><br><span class="line">    <span class="string">&quot;_id&quot;</span>: <span class="string">&quot;AWSXXXXXXXXXXXXXXXX&quot;</span>,</span><br><span class="line">    <span class="string">&quot;_score&quot;</span>: Y.Z,</span><br><span class="line">    <span class="string">&quot;_source&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;name&quot;</span>: <span class="string">&quot;Gift Vouchers&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">]</span><br></pre></td></tr></table></figure><p>e.g. Fixed the issue for field B with spaces not returning quotes:</p><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;settings&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;analysis&quot;</span>: &#123;</span><br><span class="line">      <span class="attr">&quot;normalizer&quot;</span>: &#123;</span><br><span class="line">        <span class="attr">&quot;lowercase_normalizer&quot;</span>: &#123;</span><br><span class="line">          <span class="attr">&quot;type&quot;</span>: <span class="string">&quot;custom&quot;</span>,</span><br><span class="line">          <span class="attr">&quot;char_filter&quot;</span>: [],</span><br><span class="line">          <span class="attr">&quot;filter&quot;</span>: [</span><br><span class="line">            <span class="string">&quot;lowercase&quot;</span>,</span><br><span class="line">            <span class="string">&quot;asciifolding&quot;</span></span><br><span class="line">          ]</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">&quot;mappings&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;A&quot;</span>: &#123;</span><br><span class="line">      <span class="attr">&quot;properties&quot;</span>: &#123;</span><br><span class="line">        <span class="attr">&quot;name&quot;</span>: &#123;</span><br><span class="line">          <span class="attr">&quot;type&quot;</span>: <span class="string">&quot;text&quot;</span>,</span><br><span class="line">          <span class="attr">&quot;fields&quot;</span>: &#123;</span><br><span class="line">            <span class="attr">&quot;keyword&quot;</span>: &#123;</span><br><span class="line">              <span class="attr">&quot;type&quot;</span>: <span class="string">&quot;keyword&quot;</span>,</span><br><span class="line">              <span class="attr">&quot;ignore_above&quot;</span>: <span class="number">256</span>,</span><br><span class="line">              <span class="attr">&quot;normalizer&quot;</span>: <span class="string">&quot;lowercase_normalizer&quot;</span></span><br><span class="line">            &#125;</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h2 id="Publish-Glue-scripts-to-S3"><a href="#Publish-Glue-scripts-to-S3" class="headerlink" title="Publish Glue scripts to S3"></a>Publish Glue scripts to S3</h2><p>AWS S3 wildcards for copying scripts to bucket:</p><figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">aws s3 sync . s3:<span class="regexp">//</span>$(S3_BUCKET)<span class="regexp">/$(FUNCTION_NAME)_Glue/</span> --recursive --exclude <span class="string">&quot;*&quot;</span> --include <span class="string">&quot;glue_*.py&quot;</span></span><br></pre></td></tr></table></figure><h2 id="Glue-Crawler-DependsOn"><a href="#Glue-Crawler-DependsOn" class="headerlink" title="Glue - Crawler - DependsOn"></a>Glue - Crawler - <em>DependsOn</em></h2><p>If not using <code>DependsOn: Connection</code> in Glue Crawler, it won’t create Resource <code>Connection</code> before <code>Crawler</code>.</p><p>And the error message is as follows:</p><figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">===&gt; JDBC Connection not <span class="attr">registered:</span> (<span class="attr">Service:</span> AWSGlue; Status <span class="attr">Code:</span></span><br><span class="line"><span class="number">400</span>; Error <span class="attr">Code:</span> InvalidInputException; Request <span class="attr">ID:</span></span><br><span class="line"><span class="number">5</span>cd88624<span class="number">-78</span>f3<span class="number">-11e8</span>-bf94<span class="number">-23377</span>d56a892)</span><br></pre></td></tr></table></figure><p>Code in <code>AWS CloudFormation</code> template:</p><figure class="highlight dts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"># Create a crawler to crawl the flights data</span></span><br><span class="line"><span class="symbol">  CrawlerFlights:</span></span><br><span class="line"><span class="symbol">    Type:</span> AWS::Glue::Crawler</span><br><span class="line"><span class="symbol">    DependsOn:</span> GlueConnectionRDS</span><br></pre></td></tr></table></figure><h2 id="ES-Timeout-issue-using-NAT-gateway"><a href="#ES-Timeout-issue-using-NAT-gateway" class="headerlink" title="ES Timeout issue - using NAT gateway"></a>ES Timeout issue - using NAT gateway</h2><h3 id="Background"><a href="#Background" class="headerlink" title="Background"></a>Background</h3><p>ES use Internet Endpoint (not VPC Endpoint)<br>Can’t use <code>VPC endpoint</code> for search services - it must be accessible from <code>AWS AppSync</code><br>So when creating Glue connection, it would connect to DB</p><h3 id="AWS-Official-Doc"><a href="#AWS-Official-Doc" class="headerlink" title="AWS Official Doc"></a>AWS Official Doc</h3><blockquote><p>All JDBC data stores that are accessed by the job must be available from the VPC subnet.</p><p>If your job needs to access both VPC resources and the public internet, the VPC needs to have a Network Address Translation (NAT) gateway inside the VPC.</p><p>The network interface is not assigned any public IP addresses. AWS Glue requires internet access (for example, to access AWS services that don’t have VPC endpoints). You can configure a network address translation (NAT) instance inside your VPC, or you can use the Amazon VPC NAT gateway.</p></blockquote><h3 id="Adding-Glue-Subnet"><a href="#Adding-Glue-Subnet" class="headerlink" title="Adding Glue Subnet"></a>Adding Glue Subnet</h3><p>So a specific Glue subnet with public access is needed.</p><h3 id="DB-Security-Group"><a href="#DB-Security-Group" class="headerlink" title="DB Security Group"></a>DB Security Group</h3><p>For the DB need to add <code>self ref SG</code>, otherwise it’ll get ERROR as follows:</p><figure class="highlight subunit"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">Error: </span>Inbound Rule in Security Group Required</span><br></pre></td></tr></table></figure><p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL2dsdWUvbGF0ZXN0L2RnL2dsdWUtdHJvdWJsZXNob290aW5nLWVycm9ycy5odG1sI2Vycm9yLWluYm91bmQtc2VsZi1yZWZlcmVuY2UtcnVsZQ==">AWS Doc<i class="fa fa-external-link-alt"></i></span>:</p><blockquote><p>At least one security group must open all ingress ports. To limit traffic, the source security group in your inbound rule can be restricted to the same security group.</p></blockquote><h3 id="Code-Example-Security-Group"><a href="#Code-Example-Security-Group" class="headerlink" title="Code Example (Security Group)"></a>Code Example (Security Group)</h3><figure class="highlight dts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="symbol">ADBSecurityGroup:</span></span><br><span class="line"><span class="symbol">    Type:</span> AWS::EC2::SecurityGroup</span><br><span class="line"><span class="symbol">    Properties:</span></span><br><span class="line"><span class="symbol">      GroupDescription:</span></span><br><span class="line"><span class="symbol">        Fn:</span>:Sub: $&#123;AWS::StackName&#125; A DB Security Group</span><br><span class="line"><span class="symbol">      VpcId:</span></span><br><span class="line"><span class="symbol">        Fn:</span>:ImportValue:</span><br><span class="line"><span class="symbol">          Fn:</span>:Sub: $&#123;Environment&#125;-vpc-id</span><br><span class="line"><span class="symbol">      SecurityGroupIngress:</span></span><br><span class="line">        - CidrIp:</span><br><span class="line"><span class="symbol">            Fn:</span>:ImportValue:</span><br><span class="line"><span class="symbol">              Fn:</span>:Sub: $&#123;Environment&#125;-database-subnets-cidr</span><br><span class="line"><span class="symbol">          FromPort:</span> <span class="number">0</span></span><br><span class="line"><span class="symbol">          ToPort:</span> <span class="number">65535</span></span><br><span class="line"><span class="symbol">          IpProtocol:</span> tcp</span><br><span class="line"><span class="symbol">      Tags:</span></span><br><span class="line">        - Key: Name</span><br><span class="line"><span class="symbol">          Value:</span></span><br><span class="line"><span class="symbol">            Fn:</span>:Sub:</span><br><span class="line">$&#123;AWS::StackName&#125;-glue-A-primary-security-group</span><br><span class="line"><span class="symbol">  ASecurityGroupIngress:</span></span><br><span class="line"><span class="symbol">    Type:</span> AWS::EC2::SecurityGroupIngress</span><br><span class="line"><span class="symbol">    DependsOn:</span> ADBSecurityGroup</span><br><span class="line"><span class="symbol">    Properties:</span></span><br><span class="line"><span class="symbol">      GroupId:</span></span><br><span class="line"><span class="symbol">        Fn:</span>:Sub: $&#123;ADBSecurityGroup.GroupId&#125;</span><br><span class="line"><span class="symbol">      IpProtocol:</span> tcp</span><br><span class="line"><span class="symbol">      FromPort:</span> <span class="number">0</span></span><br><span class="line"><span class="symbol">      ToPort:</span> <span class="number">65535</span></span><br><span class="line"><span class="symbol">      SourceSecurityGroupId:</span></span><br><span class="line"><span class="symbol">        Fn:</span>:Sub: $&#123;ADBSecurityGroup.GroupId&#125;</span><br></pre></td></tr></table></figure><h1 id="Code-Example-Glue-Cloudformation-Template"><a href="#Code-Example-Glue-Cloudformation-Template" class="headerlink" title="Code Example (Glue Cloudformation Template)"></a>Code Example (Glue Cloudformation Template)</h1><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Glue Job</span></span><br><span class="line">  <span class="attr">XGlueJob</span>:<span class="string"></span></span><br><span class="line">    <span class="attr">Type</span>: <span class="string">AWS::Glue::Job</span></span><br><span class="line">    <span class="attr">Properties</span>:<span class="string"></span></span><br><span class="line">      <span class="attr">Name</span>:<span class="string"></span></span><br><span class="line">        <span class="attr">Fn</span>:<span class="string">:Sub: $&#123;AWS::StackName&#125;-X-GlueJob</span></span><br><span class="line"><span class="comment">      #LogUri: &quot;wikiData&quot; </span></span><br><span class="line">      <span class="attr">Connections</span>:<span class="string"></span></span><br><span class="line"><span class="comment">        # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-glue-job-connectionslist.html</span></span><br><span class="line">        <span class="attr">Connections</span>:<span class="string"></span></span><br><span class="line">          <span class="meta">-</span> <span class="string">Ref: XGlueConnectionRDS</span></span><br><span class="line">      <span class="attr">Role</span>:<span class="string"></span></span><br><span class="line">        <span class="attr">Ref</span>: <span class="string">GlueJobTriggerRole</span></span><br><span class="line">      <span class="attr">Command</span>:<span class="string"></span></span><br><span class="line"><span class="comment">        # The name of the job command: this must be &quot;glueetl&quot;</span></span><br><span class="line"><span class="comment">        # https://docs.aws.amazon.com/glue/latest/dg/aws-glue-api-jobs-job.html#aws-glue-api-jobs-job-JobCommand</span></span><br><span class="line">        <span class="attr">Name</span>: <span class="string">glueetl</span></span><br><span class="line">        <span class="attr">ScriptLocation</span>:<span class="string"></span></span><br><span class="line">          <span class="attr">Fn</span>:<span class="string">:Sub:</span></span><br><span class="line"><span class="attr">s3</span>:<span class="string">//$&#123;S3Bucket&#125;/productServicesGlue/glue.py</span></span><br><span class="line">      <span class="attr">DefaultArguments</span>:<span class="string"></span></span><br><span class="line">        <span class="meta">&quot;--continuation-option&quot;</span>: <span class="string">&quot;continuation-enabled&quot;</span></span><br><span class="line"><span class="comment">        # use &quot;TempDir&quot; refer to:</span></span><br><span class="line"><span class="comment">        # https://docs.aws.amazon.com/glue/latest/dg/populate-with-cloudformation-templates.html#sample-cfn-template-job-jdbc</span></span><br><span class="line">        <span class="meta">&quot;--TempDir&quot;</span>:<span class="string"></span></span><br><span class="line"><span class="attr">s3</span>:<span class="string">//aws-glue-temporary-&#123;account_id&#125;-$&#123;region&#125;/admin</span></span><br><span class="line">        <span class="meta">&quot;--extra-py-files&quot;</span>: <span class="string"></span></span><br><span class="line">          <span class="attr">Fn</span>:<span class="string">:Sub:</span></span><br><span class="line"><span class="attr">s3</span>:<span class="string">//$&#123;S3Bucket&#125;/productServicesGlue/productServicesGlue.zip</span></span><br><span class="line">        <span class="meta">&quot;--elasticsearch_host&quot;</span>:<span class="string"></span></span><br><span class="line">          <span class="attr">Ref</span>: <span class="string">ElasticSearchHost</span></span><br><span class="line">        <span class="meta">&quot;--db_name&quot;</span>:<span class="string"></span></span><br><span class="line">          <span class="attr">Ref</span>: <span class="string">XGlueDatabaseName</span></span><br><span class="line">        <span class="meta">&quot;--table_name&quot;</span>:<span class="string"></span></span><br><span class="line">          <span class="attr">Fn</span>:<span class="string">:Join:</span></span><br><span class="line">          <span class="meta">-</span> <span class="string">&#x27;&#x27;</span></span><br><span class="line">          <span class="meta">-</span> <span class="string">- Ref: XTablePrefixName</span></span><br><span class="line">            <span class="meta">-</span> <span class="string">Ref: XGlueTableName</span></span><br><span class="line">      <span class="attr">MaxRetries</span>: <span class="string">0</span></span><br><span class="line">      <span class="attr">AllocatedCapacity</span>: <span class="string">2  </span></span><br><span class="line">      <span class="attr">ExecutionProperty</span>:   <span class="string"></span></span><br><span class="line">        <span class="attr">MaxConcurrentRuns</span>: <span class="string">1</span></span><br></pre></td></tr></table></figure><h1 id="Reference"><a href="#Reference" class="headerlink" title="Reference"></a>Reference</h1><ul><li><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL0FtYXpvblMzL2xhdGVzdC9kZXYvQnVja2V0UmVzdHJpY3Rpb25zLmh0bWw=">https://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html<i class="fa fa-external-link-alt"></i></span></li><li><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL2FtYXpvbmR5bmFtb2RiL2xhdGVzdC9kZXZlbG9wZXJndWlkZS9Ib3dJdFdvcmtzLk5hbWluZ1J1bGVzRGF0YVR5cGVzLmh0bWw=">https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html<i class="fa fa-external-link-alt"></i></span></li><li><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL2NsaS9sYXRlc3QvcmVmZXJlbmNlL3MzL2luZGV4Lmh0bWwjdXNlLW9mLWV4Y2x1ZGUtYW5kLWluY2x1ZGUtZmlsdGVycw==">https://docs.aws.amazon.com/cli/latest/reference/s3/index.html#use-of-exclude-and-include-filters<i class="fa fa-external-link-alt"></i></span></li><li><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmF3cy5hbWF6b24uY29tL2dsdWUvbGF0ZXN0L2RnL2dsdWUtdHJvdWJsZXNob290aW5nLWVycm9ycy5odG1sI2Vycm9yLWluYm91bmQtc2VsZi1yZWZlcmVuY2UtcnVsZQ==">https://docs.aws.amazon.com/glue/latest/dg/glue-troubleshooting-errors.html#error-inbound-self-reference-rule<i class="fa fa-external-link-alt"></i></span></li></ul>]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;Glue-Official-FAQ&quot;&gt;&lt;a href=&quot;#Glue-Official-FAQ&quot; class=&quot;headerlink&quot; title=&quot;Glue - Official FAQ&quot;&gt;&lt;/a&gt;Glue - Official FAQ&lt;/h1&gt;&lt;p&gt;The official doc for troubleshooting could be found &lt;a href=&quot;https://docs.aws.amazon.com/glue/latest/dg/troubleshooting-glue.html&quot;&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&quot;Troubleshooting-Notes-amp-Tips&quot;&gt;&lt;a href=&quot;#Troubleshooting-Notes-amp-Tips&quot; class=&quot;headerlink&quot; title=&quot;Troubleshooting - Notes &amp;amp; Tips&quot;&gt;&lt;/a&gt;Troubleshooting - Notes &amp;amp; Tips&lt;/h1&gt;&lt;p&gt;Notes and tips for Glue when implementing the ETL process:&lt;/p&gt;
&lt;h2 id=&quot;Naming-rules-conventions-for-AWS-services&quot;&gt;&lt;a href=&quot;#Naming-rules-conventions-for-AWS-services&quot; class=&quot;headerlink&quot; title=&quot;Naming rules / conventions for AWS services&quot;&gt;&lt;/a&gt;Naming rules / conventions for AWS services&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;S3 bucket name can either NOT uppercase nor NOT contain “_” &lt;/li&gt;
&lt;li&gt;Dynamo DB table / Name can NOT contain “-“&lt;/li&gt;
&lt;li&gt;ES / Name: can NOT contain “-“&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;ACM-SSL-Cert&quot;&gt;&lt;a href=&quot;#ACM-SSL-Cert&quot; class=&quot;headerlink&quot; title=&quot;ACM SSL Cert&quot;&gt;&lt;/a&gt;ACM SSL Cert&lt;/h2&gt;&lt;p&gt;Using &lt;code&gt;us-east-1&lt;/code&gt; region for AWS CloudFront (certificate)&lt;/p&gt;
    
    </summary>
    
    
      <category term="AWS" scheme="http://davidlu1001.github.io/categories/AWS/"/>
    
      <category term="SRE" scheme="http://davidlu1001.github.io/categories/SRE/"/>
    
      <category term="ElasticSearch" scheme="http://davidlu1001.github.io/categories/ElasticSearch/"/>
    
      <category term="Glue" scheme="http://davidlu1001.github.io/categories/Glue/"/>
    
    
      <category term="AWS" scheme="http://davidlu1001.github.io/tags/AWS/"/>
    
      <category term="Glue" scheme="http://davidlu1001.github.io/tags/Glue/"/>
    
      <category term="ElasticSearch" scheme="http://davidlu1001.github.io/tags/ElasticSearch/"/>
    
      <category term="AppSync" scheme="http://davidlu1001.github.io/tags/AppSync/"/>
    
  </entry>
  
  <entry>
    <title>Clean puppet agent certificates</title>
    <link href="http://davidlu1001.github.io/2018/03/08/Clean-puppet-agent-certificates/"/>
    <id>http://davidlu1001.github.io/2018/03/08/Clean-puppet-agent-certificates/</id>
    <published>2018-03-08T10:42:16.000Z</published>
    <updated>2018-03-08T10:42:16.000Z</updated>
    
    <content type="html"><![CDATA[<p>This document outlines the steps to clean or regenerate puppet agent certificates in a traditional master/client setup.</p><p>First thing is to ssh into the agent </p><p>Then, delete all <code>*.pem</code> files in <code>/var/lib/puppet/ssl</code> associated to your instance.<br>e.g they should be in the form of <HOSTNAME>.pem</p><figure class="highlight crystal"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">root@<span class="symbol">gateway:</span>/var/<span class="class"><span class="keyword">lib</span>/<span class="title">puppet</span>/<span class="title">ssl</span><span class="comment"># hostname -f</span></span></span><br><span class="line">gateway.ap-southeast-<span class="number">2</span>.aws.ci.xxxxxx.com</span><br><span class="line"></span><br><span class="line">root@<span class="symbol">gateway:</span>/var/<span class="class"><span class="keyword">lib</span>/<span class="title">puppet</span>/<span class="title">ssl</span><span class="comment"># find . -type f | grep &quot;gateway.ap-southeast-2.aws.ci.xxxxxx.com&quot;</span></span></span><br><span class="line">./public_keys/gateway.ap-southeast-<span class="number">2</span>.aws.ci.xxxxxx.com.pem</span><br><span class="line">./private_keys/gateway.ap-southeast-<span class="number">2</span>.aws.ci.xxxxxx.com.pem</span><br><span class="line">./certificate_requests/gateway.ap-southeast-<span class="number">2</span>.aws.ci.xxxxxx.com.pem</span><br><span class="line">./certs/gateway.ap-southeast-<span class="number">2</span>.aws.ci.xxxxxx.com.pem</span><br><span class="line"></span><br><span class="line"><span class="comment"># to delete them</span></span><br><span class="line">find . -<span class="keyword">type</span> f | grep <span class="string">&quot;gateway.ap-southeast-2.aws.ci.xxxxxx.com&quot;</span> | xargs rm -rf</span><br></pre></td></tr></table></figure><p>Next step is to ssh into the puppet master and do the same</p><a id="more"></a><figure class="highlight crystal"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">root@puppet-<span class="symbol">master:</span>/var/<span class="class"><span class="keyword">lib</span>/<span class="title">puppet</span>/<span class="title">ssl</span><span class="comment"># find . -type f | grep &quot;gateway&quot;</span></span></span><br><span class="line">./ca/signed/gateway.ap-southeast-<span class="number">2</span>.aws.ci.xxxxxx.com.pem</span><br></pre></td></tr></table></figure><p>Ideally there should be only one file hanging in there.</p><p>Then back to the agent and try running a –noop puppet run to force a new certificate request. After this, go back to the puppet master and check for any pending cert waiting for approval.</p><figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">root@puppet-master:/<span class="keyword">var</span>/<span class="keyword">log</span>/puppet# puppet cert <span class="keyword">list</span> | grep <span class="string">&quot;gateway&quot;</span></span><br><span class="line">  <span class="string">&quot;gateway.ap-southeast-2.aws.ci.xxxxxx.com&quot;</span>                    (SHA256) E8:D2:83:89:34:A4:AD:14:EA:83:73:8A:B9:E6:98:D9:6C:4F:47:C6:07:5D:D6:D0:9A:F1:32:C8:33:74:98:D0</span><br><span class="line">Cool! Now you just need to sign the certificate.</span><br><span class="line"></span><br><span class="line">root@puppet-master:/<span class="keyword">var</span>/<span class="keyword">log</span>/puppet# puppet cert sign gateway.<span class="keyword">ap</span>-southeast-2.aws.<span class="keyword">ci</span>.xxxxxx.com</span><br><span class="line">Notice: Signed certificate request <span class="keyword">for</span> gateway.<span class="keyword">ap</span>-southeast-2.aws.<span class="keyword">ci</span>.xxxxxx.com</span><br><span class="line">Notice: Removing <span class="keyword">file</span> Puppet::SSL::CertificateRequest gateway.<span class="keyword">ap</span>-southeast-2.aws.<span class="keyword">ci</span>.xxxxxx.com at &#x27;/<span class="keyword">var</span>/lib/puppet/ssl/<span class="keyword">ca</span>/requests/gateway.<span class="keyword">ap</span>-southeast-2.aws.<span class="keyword">ci</span>.xxxxxx.com.pem&#x27;</span><br></pre></td></tr></table></figure><p>Lastly, go back to the agent host and try running a couple of puppet runs.</p><figure class="highlight brainfuck"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">sudo</span> <span class="comment">puppet</span> <span class="comment">agent</span> --<span class="comment">enable</span> <span class="comment">&amp;&amp;</span> <span class="comment">sudo</span> <span class="comment">puppet</span> <span class="comment">agent</span> <span class="literal">-</span><span class="comment">tv</span> --<span class="comment">noop;</span> <span class="comment">sudo</span> <span class="comment">puppet</span> <span class="comment">agent</span> --<span class="comment">disable</span></span><br></pre></td></tr></table></figure>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;This document outlines the steps to clean or regenerate puppet agent certificates in a traditional master/client setup.&lt;/p&gt;
&lt;p&gt;First thing is to ssh into the agent &lt;/p&gt;
&lt;p&gt;Then, delete all &lt;code&gt;*.pem&lt;/code&gt; files in &lt;code&gt;/var/lib/puppet/ssl&lt;/code&gt; associated to your instance.&lt;br&gt;e.g they should be in the form of &lt;HOSTNAME&gt;.pem&lt;/p&gt;
&lt;figure class=&quot;highlight crystal&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;1&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;2&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;3&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;4&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;5&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;6&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;7&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;8&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;9&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;10&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;11&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;root@&lt;span class=&quot;symbol&quot;&gt;gateway:&lt;/span&gt;/var/&lt;span class=&quot;class&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;lib&lt;/span&gt;/&lt;span class=&quot;title&quot;&gt;puppet&lt;/span&gt;/&lt;span class=&quot;title&quot;&gt;ssl&lt;/span&gt;&lt;span class=&quot;comment&quot;&gt;# hostname -f&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;gateway.ap-southeast-&lt;span class=&quot;number&quot;&gt;2&lt;/span&gt;.aws.ci.xxxxxx.com&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;root@&lt;span class=&quot;symbol&quot;&gt;gateway:&lt;/span&gt;/var/&lt;span class=&quot;class&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;lib&lt;/span&gt;/&lt;span class=&quot;title&quot;&gt;puppet&lt;/span&gt;/&lt;span class=&quot;title&quot;&gt;ssl&lt;/span&gt;&lt;span class=&quot;comment&quot;&gt;# find . -type f | grep &amp;quot;gateway.ap-southeast-2.aws.ci.xxxxxx.com&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;./public_keys/gateway.ap-southeast-&lt;span class=&quot;number&quot;&gt;2&lt;/span&gt;.aws.ci.xxxxxx.com.pem&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;./private_keys/gateway.ap-southeast-&lt;span class=&quot;number&quot;&gt;2&lt;/span&gt;.aws.ci.xxxxxx.com.pem&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;./certificate_requests/gateway.ap-southeast-&lt;span class=&quot;number&quot;&gt;2&lt;/span&gt;.aws.ci.xxxxxx.com.pem&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;./certs/gateway.ap-southeast-&lt;span class=&quot;number&quot;&gt;2&lt;/span&gt;.aws.ci.xxxxxx.com.pem&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;comment&quot;&gt;# to delete them&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;find . -&lt;span class=&quot;keyword&quot;&gt;type&lt;/span&gt; f | grep &lt;span class=&quot;string&quot;&gt;&amp;quot;gateway.ap-southeast-2.aws.ci.xxxxxx.com&amp;quot;&lt;/span&gt; | xargs rm -rf&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;

&lt;p&gt;Next step is to ssh into the puppet master and do the same&lt;/p&gt;
    
    </summary>
    
    
      <category term="DevOps" scheme="http://davidlu1001.github.io/categories/DevOps/"/>
    
      <category term="Puppet" scheme="http://davidlu1001.github.io/categories/Puppet/"/>
    
    
      <category term="DevOps" scheme="http://davidlu1001.github.io/tags/DevOps/"/>
    
      <category term="Puppet" scheme="http://davidlu1001.github.io/tags/Puppet/"/>
    
  </entry>
  
  <entry>
    <title>Linux - find print0 &amp; xargs</title>
    <link href="http://davidlu1001.github.io/2016/02/20/Linux-find-print0-xargs/"/>
    <id>http://davidlu1001.github.io/2016/02/20/Linux-find-print0-xargs/</id>
    <published>2016-02-20T08:59:59.000Z</published>
    <updated>2016-02-20T08:59:59.000Z</updated>
    
    <content type="html"><![CDATA[<h2 id="Errors"><a href="#Errors" class="headerlink" title="Errors"></a>Errors</h2><figure class="highlight gams"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">root@elastic-data:/mnt/elasticsearch/nodes/<span class="number">0</span>/indices# ls -rthl</span><br><span class="line">...</span><br><span class="line">drwxr-xr-x   <span class="number">4</span> elasticsearch elasticsearch <span class="number">4.0</span>K Apr <span class="number">15</span> <span class="number">14</span>:<span class="number">55</span> _nGsAa--TU-Yt8zAsPWzBw</span><br><span class="line">drwxr-xr-x   <span class="number">5</span> elasticsearch elasticsearch <span class="number">4.0</span>K Apr <span class="number">15</span> <span class="number">14</span>:<span class="number">55</span> -hqCV4oLRG-ttGc8A93BVw</span><br><span class="line">drwxr-xr-x   <span class="number">5</span> elasticsearch elasticsearch <span class="number">4.0</span>K Apr <span class="number">15</span> <span class="number">14</span>:<span class="number">55</span> y4ORbH-YRL65u5JPRWyM3A</span><br><span class="line">...</span><br><span class="line"></span><br><span class="line">root@elastic-data:/mnt/elasticsearch/nodes/<span class="number">0</span>/indices# du -sh *</span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;6&#x27;</span></span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;w&#x27;</span></span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;U&#x27;</span></span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;g&#x27;</span></span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;E&#x27;</span></span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;Q&#x27;</span></span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;q&#x27;</span></span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;I&#x27;</span></span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;R&#x27;</span></span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;f&#x27;</span></span><br><span class="line">du: eQWyw: <span class="keyword">No</span> such <span class="keyword">file</span> <span class="keyword">or</span> directory</span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;F&#x27;</span></span><br><span class="line">du: xFiduSIGlLbpGulXyqA: <span class="keyword">No</span> such <span class="keyword">file</span> <span class="keyword">or</span> directory</span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;q&#x27;</span></span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;C&#x27;</span></span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;V&#x27;</span></span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;4&#x27;</span></span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;o&#x27;</span></span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;R&#x27;</span></span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;G&#x27;</span></span><br><span class="line">du: invalid <span class="keyword">option</span> -- <span class="string">&#x27;-&#x27;</span></span><br><span class="line">du: invalid -t argument <span class="string">&#x27;tGc8A93BVw&#x27;</span></span><br></pre></td></tr></table></figure><a id="more"></a><h2 id="find"><a href="#find" class="headerlink" title="find"></a>find</h2><figure class="highlight livecodeserver"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">-print</span><br><span class="line"></span><br><span class="line">True; print <span class="keyword">the</span> full <span class="built_in">file</span> name <span class="keyword">on</span> <span class="title">the</span> <span class="title">standard</span> <span class="title">output</span>, <span class="title">followed</span> <span class="title">by</span> <span class="title">a</span> <span class="title">newline</span>.   <span class="title">If</span></span><br><span class="line">you are piping <span class="keyword">the</span> output <span class="keyword">of</span> find <span class="keyword">into</span> another program <span class="keyword">and</span> there is  <span class="keyword">the</span>  faintest</span><br><span class="line">possibility  that  <span class="keyword">the</span>  <span class="built_in">files</span> which you are searching <span class="keyword">for</span> might contain <span class="keyword">a</span> newline,</span><br><span class="line"><span class="keyword">then</span> you should seriously consider <span class="keyword">using</span> <span class="keyword">the</span> -print0  option  instead  <span class="keyword">of</span>  -print.</span><br><span class="line">See  <span class="keyword">the</span> UNUSUAL FILENAMES section <span class="keyword">for</span> information about how unusual <span class="keyword">characters</span> <span class="keyword">in</span></span><br><span class="line">filenames are handled.</span><br></pre></td></tr></table></figure><figure class="highlight applescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">-print0</span><br><span class="line"></span><br><span class="line">True; print <span class="keyword">the</span> full <span class="built_in">file</span> <span class="built_in">name</span> <span class="keyword">on</span> <span class="keyword">the</span> standard output, followed <span class="keyword">by</span> a null charac‐</span><br><span class="line">ter  (instead  <span class="keyword">of</span> <span class="keyword">the</span> newline <span class="built_in">character</span> <span class="keyword">that</span> -print uses).  This allows <span class="built_in">file</span> names</span><br><span class="line"><span class="keyword">that</span> <span class="keyword">contain</span> newlines <span class="keyword">or</span> other types <span class="keyword">of</span> white <span class="literal">space</span> <span class="keyword">to</span> be correctly interpreted <span class="keyword">by</span></span><br><span class="line">programs  <span class="keyword">that</span>  process <span class="keyword">the</span> find output.  This option corresponds <span class="keyword">to</span> <span class="keyword">the</span> <span class="number">-0</span> option</span><br><span class="line"><span class="keyword">of</span> xargs.</span><br></pre></td></tr></table></figure><h2 id="xargs"><a href="#xargs" class="headerlink" title="xargs"></a>xargs</h2><p>Deal with the problem of Argument list too long</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">xargs - build and <span class="keyword">execute</span> command <span class="keyword">lines</span> <span class="keyword">from</span> standard <span class="keyword">input</span></span><br><span class="line"></span><br><span class="line"><span class="number">-0</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">Input</span>  items  <span class="keyword">are</span> <span class="keyword">terminated</span> <span class="keyword">by</span> a <span class="literal">null</span> <span class="built_in">character</span> instead <span class="keyword">of</span> <span class="keyword">by</span> <span class="keyword">whitespace</span>, <span class="keyword">and</span> the</span><br><span class="line">quotes <span class="keyword">and</span> backslash <span class="keyword">are</span> <span class="keyword">not</span> special (every <span class="built_in">character</span> <span class="keyword">is</span> taken  literally).   Dis‐</span><br><span class="line">ables  the  <span class="keyword">end</span>  <span class="keyword">of</span> <span class="keyword">file</span> <span class="keyword">string</span>, which <span class="keyword">is</span> treated <span class="keyword">like</span> <span class="keyword">any</span> other argument.  Useful</span><br><span class="line"><span class="keyword">when</span> <span class="keyword">input</span> items might contain white <span class="keyword">space</span>, quote marks, <span class="keyword">or</span> backslashes.  The  GNU</span><br><span class="line">find -print0 <span class="keyword">option</span> produces <span class="keyword">input</span> suitable <span class="keyword">for</span> this mode.</span><br></pre></td></tr></table></figure><h2 id="Examples"><a href="#Examples" class="headerlink" title="Examples"></a>Examples</h2><p>So for the above errors, using following command check size for the directores:</p><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">find</span> ./ -type d -print<span class="number">0</span> | xargs -<span class="number">0</span> du -s | sort -nk<span class="number">1</span></span><br></pre></td></tr></table></figure><p>Then we can get the result without errors:</p><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">22790848</span>./TTW<span class="number">3</span>kNrZQd-BETbHymRqjQ</span><br><span class="line"><span class="attribute">24080308</span>./ENSiFN<span class="number">9</span>NQe-bv<span class="number">3</span>YH<span class="number">71</span>xG<span class="number">9</span>g</span><br><span class="line"><span class="attribute">33621480</span>./J<span class="number">50</span>jz<span class="number">5</span>dFS_m<span class="number">6</span>DsiVRBAEMA</span><br><span class="line"><span class="attribute">35486820</span>./<span class="number">618</span>IZDqDTnee_WoAynubXg</span><br><span class="line"><span class="attribute">1202871344</span>./</span><br></pre></td></tr></table></figure><p>More examples:</p><figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">➜  ~ <span class="keyword">ls</span> -rthl *.<span class="keyword">test</span></span><br><span class="line">-rw-r--r-- 1 root root 0 Feb 20 18:21 file1.<span class="keyword">test</span></span><br><span class="line">-rw-r--r-- 1 root root 0 Feb 20 18:21 file2.<span class="keyword">test</span></span><br><span class="line">-rw-r--r-- 1 root root 0 Feb 20 18:21 <span class="keyword">file</span> 3.<span class="keyword">test</span></span><br><span class="line">-rw-r--r-- 1 root root 0 Feb 20 18:21 fi1e 4.<span class="keyword">test</span></span><br><span class="line"></span><br><span class="line">➜  ~ find . -name <span class="string">&quot;*.test&quot;</span> -<span class="keyword">print</span></span><br><span class="line">./fi1e 3.<span class="keyword">test</span></span><br><span class="line">./<span class="keyword">file</span> 4.<span class="keyword">test</span></span><br><span class="line">./file1.<span class="keyword">test</span></span><br><span class="line">./file2.<span class="keyword">test</span></span><br><span class="line"></span><br><span class="line">➜  ~ find . -name <span class="string">&quot;*.test&quot;</span> -print0</span><br><span class="line">./fi1e 3.<span class="keyword">test</span>./<span class="keyword">file</span> 4.<span class="keyword">test</span>./file1.<span class="keyword">test</span>./file2.<span class="keyword">test</span></span><br><span class="line"></span><br><span class="line">➜  ~ find . -name <span class="string">&quot;*.test&quot;</span> -<span class="keyword">print</span> | xargs <span class="keyword">rm</span></span><br><span class="line"><span class="keyword">rm</span>: cannot remove ‘./fi1e’: <span class="keyword">No</span> such <span class="keyword">file</span> or directory</span><br><span class="line"><span class="keyword">rm</span>: cannot remove ‘3.<span class="keyword">test</span>’: <span class="keyword">No</span> such <span class="keyword">file</span> or directory</span><br><span class="line"><span class="keyword">rm</span>: cannot remove ‘./<span class="keyword">file</span>’: <span class="keyword">No</span> such <span class="keyword">file</span> or directory</span><br><span class="line"><span class="keyword">rm</span>: cannot remove ‘4.<span class="keyword">test</span>’: <span class="keyword">No</span> such <span class="keyword">file</span> or directory</span><br><span class="line"></span><br><span class="line">➜  ~ find . -name <span class="string">&quot;*.test&quot;</span> -print0 | xargs -0 <span class="keyword">rm</span></span><br></pre></td></tr></table></figure>]]></content>
    
    <summary type="html">
    
      &lt;h2 id=&quot;Errors&quot;&gt;&lt;a href=&quot;#Errors&quot; class=&quot;headerlink&quot; title=&quot;Errors&quot;&gt;&lt;/a&gt;Errors&lt;/h2&gt;&lt;figure class=&quot;highlight gams&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;1&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;2&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;3&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;4&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;5&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;6&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;7&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;8&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;9&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;10&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;11&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;12&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;13&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;14&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;15&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;16&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;17&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;18&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;19&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;20&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;21&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;22&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;23&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;24&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;25&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;26&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;27&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;28&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;29&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;30&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;root@elastic-data:/mnt/elasticsearch/nodes/&lt;span class=&quot;number&quot;&gt;0&lt;/span&gt;/indices# ls -rthl&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;...&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;drwxr-xr-x   &lt;span class=&quot;number&quot;&gt;4&lt;/span&gt; elasticsearch elasticsearch &lt;span class=&quot;number&quot;&gt;4.0&lt;/span&gt;K Apr &lt;span class=&quot;number&quot;&gt;15&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;14&lt;/span&gt;:&lt;span class=&quot;number&quot;&gt;55&lt;/span&gt; _nGsAa--TU-Yt8zAsPWzBw&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;drwxr-xr-x   &lt;span class=&quot;number&quot;&gt;5&lt;/span&gt; elasticsearch elasticsearch &lt;span class=&quot;number&quot;&gt;4.0&lt;/span&gt;K Apr &lt;span class=&quot;number&quot;&gt;15&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;14&lt;/span&gt;:&lt;span class=&quot;number&quot;&gt;55&lt;/span&gt; -hqCV4oLRG-ttGc8A93BVw&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;drwxr-xr-x   &lt;span class=&quot;number&quot;&gt;5&lt;/span&gt; elasticsearch elasticsearch &lt;span class=&quot;number&quot;&gt;4.0&lt;/span&gt;K Apr &lt;span class=&quot;number&quot;&gt;15&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;14&lt;/span&gt;:&lt;span class=&quot;number&quot;&gt;55&lt;/span&gt; y4ORbH-YRL65u5JPRWyM3A&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;...&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;root@elastic-data:/mnt/elasticsearch/nodes/&lt;span class=&quot;number&quot;&gt;0&lt;/span&gt;/indices# du -sh *&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;6&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;w&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;U&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;g&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;E&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;Q&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;q&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;I&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;R&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;f&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: eQWyw: &lt;span class=&quot;keyword&quot;&gt;No&lt;/span&gt; such &lt;span class=&quot;keyword&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;or&lt;/span&gt; directory&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;F&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: xFiduSIGlLbpGulXyqA: &lt;span class=&quot;keyword&quot;&gt;No&lt;/span&gt; such &lt;span class=&quot;keyword&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;or&lt;/span&gt; directory&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;q&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;C&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;V&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;4&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;o&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;R&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;G&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid &lt;span class=&quot;keyword&quot;&gt;option&lt;/span&gt; -- &lt;span class=&quot;string&quot;&gt;&amp;#x27;-&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;du: invalid -t argument &lt;span class=&quot;string&quot;&gt;&amp;#x27;tGc8A93BVw&amp;#x27;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;
    
    </summary>
    
    
      <category term="Linux" scheme="http://davidlu1001.github.io/categories/Linux/"/>
    
    
      <category term="Linux" scheme="http://davidlu1001.github.io/tags/Linux/"/>
    
  </entry>
  
</feed>
