<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Artem Penner on Medium]]></title>
        <description><![CDATA[Stories by Artem Penner on Medium]]></description>
        <link>https://medium.com/@dhelios?source=rss-5cc2847807d5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*SEnk8J3Z41FgFA60.</url>
            <title>Stories by Artem Penner on Medium</title>
            <link>https://medium.com/@dhelios?source=rss-5cc2847807d5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 23:45:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dhelios/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Terraform by examples. Part 2.]]></title>
            <link>https://medium.com/@dhelios/terraform-by-examples-part-2-f50f35e5a8d4?source=rss-5cc2847807d5------2</link>
            <guid isPermaLink="false">https://medium.com/p/f50f35e5a8d4</guid>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Artem Penner]]></dc:creator>
            <pubDate>Tue, 28 Nov 2017 22:37:38 GMT</pubDate>
            <atom:updated>2017-11-28T22:37:38.820Z</atom:updated>
            <content:encoded><![CDATA[<p>Previously I published <a href="https://medium.com/@dhelios/terraform-by-examples-part-1-ef3e3be7b88b">my first note</a> about Terraform and described (actually it’s notes for me, to easy remember something) a simple IaC.</p><p>Now let’s try do the same, but using modules from <a href="http://registry.terraform.io/">Terraform registry</a> and storing <strong>tfstate </strong>file in S3.</p><h3>Configure S3 for State file.</h3><p>You need preconfigured s3 bucket with versioning and ecryption enabled. Specify bucket name, key (full path to file in bucket) and aws region.</p><pre><strong>terraform </strong>{<br>  <strong>backend &quot;s3&quot; </strong>{<br>    <strong>bucket </strong>= <strong>&quot;fstate-terraform-file&quot;<br>    key </strong>= <strong>&quot;demo1/terraform.tfstate&quot;<br>    region </strong>= <strong>&quot;us-west-2&quot;<br>  </strong>}<br>}</pre><p>describe which provider we will use</p><pre><strong>provider &quot;aws&quot; </strong>{<br>  <strong>region                  </strong>= <strong>&quot;${</strong><em>var</em><strong>.aws_region}&quot;<br>  shared_credentials_file </strong>= <strong>&quot;${</strong><em>var</em><strong>.aws_credentials}&quot;<br>  profile                 </strong>= <strong>&quot;${</strong><em>var</em><strong>.aws_profile}&quot;<br></strong>}</pre><p>After that run terraform init which download necerssery modules, providers and setting up remote state file (tfstate file). Let’s check it</p><pre>laptop-A018226:stage $ cat .terraform/terraform.tfstate<br>{<br>    &quot;version&quot;: 3,<br>    &quot;serial&quot;: 1,<br>    &quot;lineage&quot;: &quot;42cafcfc-d9d5-4868-b0ee-0b964d37b65d&quot;,<br>    &quot;backend&quot;: {<br>        &quot;type&quot;: &quot;s3&quot;,<br>        &quot;config&quot;: {<br>            &quot;bucket&quot;: &quot;fstate-terraform-file&quot;,<br>            &quot;key&quot;: &quot;demo1/terraform.tfstate&quot;,<br>            &quot;region&quot;: &quot;us-west-2&quot;<br>        },<br>        &quot;hash&quot;: 12832387552685118467<br>    },<br>    &quot;modules&quot;: [<br>        {<br>            &quot;path&quot;: [<br>                &quot;root&quot;<br>            ],<br>            &quot;outputs&quot;: {},<br>            &quot;resources&quot;: {},<br>            &quot;depends_on&quot;: []<br>        }<br>    ]<br>}</pre><p><strong>Note: </strong>We can’t use variable in terraform section, there for it’s processing early than other terraform hcl definitions.</p><h3>File structure</h3><ul><li>images.tf — ami related logic, finds latest centos image for deploy;</li><li>output.tf — self explained, for output dynamic variablesl;</li><li>templates.tf — template processing for scripts;</li><li>vars.tf — self explained, all variables;</li><li>main.tf — IaC definitions using modules from Terraform registry.</li></ul><p>I’m not spliting my configuration to dev/stage/prod branches, it’ll be next time.</p><p><strong>NOTE</strong>: <em>Best practices, use specific version of module</em> t<em>o prevent automatic upgrades to new major versions that may contain breaking<br>changes, it is recommended to add version = “…” constraints to the<br>corresponding provider blocks in configuration, with the constraint strings<br>suggested below</em>.</p><h3>VPC (<strong>terraform-aws-modules/vpc/aws)</strong></h3><p>Let’s create three public networks within specified availabilty zones.</p><p><strong>vars.tf</strong></p><pre><em># vpc variables<br></em><strong>variable &quot;vpc_name&quot; </strong>{<br>  <strong>default </strong>= <strong>&quot;zabbixVpc&quot;<br></strong>}<br><br><strong>variable &quot;vpc_cidr&quot; </strong>{<br>  <strong>default </strong>= <strong>&quot;192.168.128.0/20&quot;<br></strong>}<br><br><strong>variable &quot;vpc_azs&quot; </strong>{<br>  <strong>type </strong>= <strong>&quot;list&quot;<br><br>  default </strong>= [<br>    <strong>&quot;us-west-2a&quot;</strong>,<br>    <strong>&quot;us-west-2b&quot;</strong>,<br>    <strong>&quot;us-west-2c&quot;</strong>,<br>  ]<br>}<br><br><strong>variable &quot;public_subnets&quot; </strong>{<br>  <strong>type </strong>= <strong>&quot;list&quot;<br><br>  default </strong>= [<br>    <strong>&quot;192.168.128.0/24&quot;</strong>,<br>    <strong>&quot;192.168.129.0/24&quot;</strong>,<br>    <strong>&quot;192.168.130.0/24&quot;</strong>,<br>  ]<br>}<br><br><strong>variable &quot;database_subnets&quot; </strong>{<br>  <strong>type </strong>= <strong>&quot;list&quot;<br><br>  default </strong>= [<br>    <strong>&quot;192.168.131.0/24&quot;</strong>,<br>    <strong>&quot;192.168.132.0/24&quot;</strong>,<br>  ]<br>}<br><br><strong>variable &quot;vpc_nat_gateway&quot; </strong>{<br>  <strong>default </strong>= <strong>false<br></strong>}<br><br><strong>variable &quot;vpc_vpn_gateway&quot; </strong>{<br>  <strong>default </strong>= <strong>false<br></strong>}</pre><p><strong>main.tf</strong></p><pre><em>###<br>## create network<br>###<br></em><strong>module &quot;vpc&quot; </strong>{<br>  <strong>source </strong>= <strong>&quot;terraform-aws-modules/vpc/aws&quot;<br><br>  name </strong>= <strong>&quot;${</strong><em>var</em><strong>.vpc_name}&quot;<br>  cidr </strong>= <strong>&quot;${</strong><em>var</em><strong>.vpc_cidr}&quot;<br><br>  azs </strong>= <strong>&quot;${</strong><em>var</em><strong>.vpc_azs}&quot;<br><br>  public_subnets   </strong>= <strong>&quot;${</strong><em>var</em><strong>.public_subnets}&quot;<br>  database_subnets </strong>= <strong>&quot;${</strong><em>var</em><strong>.database_subnets}&quot;<br><br>  create_database_subnet_group </strong>= <strong>false<br><br>  enable_nat_gateway </strong>= <strong>false<br>  enable_vpn_gateway </strong>= <strong>false<br><br>  tags </strong>{<br>    <strong>Terraform   </strong>= <strong>&quot;true&quot;<br>    Environment </strong>= <strong>&quot;stage&quot;<br>  </strong>}<br>}</pre><p>you can find all input/output variables on module pages at registry. We’ll use output from this module to attach security groups, autoscaling groups, ec2 instances to this VPC.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YaldFba_uqDzKeh6Rx09VA.png" /><figcaption>Module description.</figcaption></figure><h3>Security groups (<strong>terraform-aws-modules/security-group/aws)</strong></h3><p>Now let’s define security groups for zabbix deployment. We’ll need two outputs from previous module are <strong>vpc_id</strong> and list of <strong>public subnets</strong>.</p><pre><strong>module &quot;zabbix_web_sg&quot; </strong>{<br>  <strong>source </strong>= <strong>&quot;terraform-aws-modules/security-group/aws&quot;<br><br>  name        </strong>= <strong>&quot;zabbix_web_sg&quot;<br>  description </strong>= <strong>&quot;security group for zabbix web fronend&quot;<br>  vpc_id      </strong>= <strong>&quot;${</strong><em>module</em><strong>.vpc.vpc_id}&quot;<br><br>  ingress_cidr_blocks </strong>= <strong>&quot;${</strong><em>module</em><strong>.vpc.public_subnets_cidr_blocks}&quot;<br>  ingress_rules       </strong>= [<strong>&quot;http-80-tcp&quot;</strong>, <strong>&quot;https-443-tcp&quot;</strong>]<br><br>  <strong>tags </strong>{<br>    <strong>Terraform   </strong>= <strong>&quot;true&quot;<br>    Environment </strong>= <strong>&quot;stage&quot;<br>    Service     </strong>= <strong>&quot;zabbix&quot;<br>    Subsystem   </strong>= <strong>&quot;zabbix_web&quot;<br>  </strong>}<br>}</pre><pre><strong>module &quot;zabbix_app_sg&quot; </strong>{<br>  <strong>source </strong>= <strong>&quot;terraform-aws-modules/security-group/aws&quot;<br><br>  name        </strong>= <strong>&quot;zabbix_app_sg&quot;<br>  description </strong>= <strong>&quot;security group for zabbix server&quot;<br>  vpc_id      </strong>= <strong>&quot;${</strong><em>module</em><strong>.vpc.vpc_id}&quot;</strong></pre><pre><strong>....<br>....</strong></pre><h3>Autoscaling groups (<strong>terraform-aws-modules/autoscaling/aws)</strong></h3><p>To archive hight availability, let’s deploy ASG for zabbix server in Active/Backup configuration and for Web Frontend in Active/Active.</p><pre><strong>module &quot;zabbix_app_asg&quot; </strong>{<br>  <strong>source </strong>= <strong>&quot;terraform-aws-modules/autoscaling/aws&quot;<br><br>  name </strong>= <strong>&quot;zabbix_app_asg&quot;<br><br>  </strong><em># Launch configuration<br>  </em><strong>lc_name                     </strong>= <strong>&quot;${</strong><em>var</em><strong>.zabbix_app_lc_name}&quot;<br>  image_id                    </strong>= <strong>&quot;${</strong><em>data</em><strong>.aws_ami.centos.id}&quot;<br>  instance_type               </strong>= <strong>&quot;${</strong><em>var</em><strong>.default_instance_type}&quot;<br>  associate_public_ip_address </strong>= <strong>true<br>  key_name                    </strong>= <strong>&quot;${</strong><em>var</em><strong>.default_key_name}&quot;<br><br>  security_groups </strong>= [<br>    <strong>&quot;${</strong><em>module</em><strong>.zabbix_app_sg.this_security_group_id}&quot;</strong>,<br>    <strong>&quot;${</strong><em>module</em><strong>.allow_egress_all.this_security_group_id}&quot;</strong>,<br>    <strong>&quot;${</strong><em>module</em><strong>.allow_ingress_from_bastion.this_security_group_id}&quot;</strong>,<br>  ]<br><br>  <strong>user_data </strong>= <strong>&quot;${</strong><em>data</em><strong>.template_file.zabbix_app_init.rendered}&quot;<br><br>  load_balancers </strong>= [<strong>&quot;${</strong><em>module</em><strong>.zabbix_app_elb.this_elb_id}&quot;</strong>]<br><br>  <em># Auto scaling group<br>  </em><strong>asg_name                  </strong>= <strong>&quot;${</strong><em>var</em><strong>.zabbix_app_asg_name}&quot;<br>  vpc_zone_identifier       </strong>= <strong>&quot;${</strong><em>module</em><strong>.vpc.public_subnets}&quot;<br>  health_check_type         </strong>= <strong>&quot;EC2&quot;<br>  min_size                  </strong>= 0<br>  <strong>max_size                  </strong>= 1<br>  <strong>desired_capacity          </strong>= 1<br>  <strong>wait_for_capacity_timeout </strong>= 0<br><br>  <strong>tags </strong>= [<br>    {<br>      <strong>key                 </strong>= <strong>&quot;Environment&quot;<br>      value               </strong>= <strong>&quot;stage&quot;<br>      propagate_at_launch </strong>= <strong>true<br>    </strong>},<br>    {<br>      <strong>key                 </strong>= <strong>&quot;Terraform&quot;<br>      value               </strong>= <strong>&quot;true&quot;<br>      propagate_at_launch </strong>= <strong>true<br>    </strong>},<br>    {<br>      <strong>key                 </strong>= <strong>&quot;Service&quot;<br>      value               </strong>= <strong>&quot;zabbix&quot;<br>      propagate_at_launch </strong>= <strong>true<br>    </strong>},<br>    {<br>      <strong>key                 </strong>= <strong>&quot;Subsystem&quot;<br>      value               </strong>= <strong>&quot;zabbix_app&quot;<br>      propagate_at_launch </strong>= <strong>true<br>    </strong>},<br>  ]<br>}</pre><p>So module has launch configuration section, in which we describe instance configuration and ASG section in which we described how to archive hight availability for service. I deploy application using <strong>user_data</strong> parameter and shell script templating.</p><pre><strong>data &quot;template_file&quot; &quot;zabbix_app_init&quot; </strong>{<br>  <strong>template </strong>= <strong>&quot;${file(&quot;${path.module}/scripts/install_zabbix_app.sh&quot;)}&quot;<br><br>  vars </strong>= {<br>    <strong>zabbix_db_private_addr </strong>= <strong>&quot;${</strong><em>module</em><strong>.zabbix_db.this_db_instance_address}&quot;<br>    zabbix_db_password </strong>= <strong>&quot;${</strong><em>var</em><strong>.zabbix_db_password}&quot;<br>    zabbix_db_name </strong>= <strong>&quot;zabbix&quot;<br>    zabbix_db_user </strong>= <strong>&quot;zabbix&quot;<br>  </strong>}<br>}</pre><p>Example of script</p><pre><strong>#!/bin/bash<br><br></strong><em># Install web server<br></em>yum install -y http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-1.el7.centos.noarch.rpm<br>yum install -y zabbix-web-mysql.noarch<br><br>setsebool -P httpd_can_network_connect_db=on<br>setsebool -P httpd_can_connect_zabbix=on<br><br>mkdir -p /etc/zabbix/web/<br>chown apache:apache /etc/zabbix/web<br><br>cat <em>&gt; /etc/zabbix/web/zabbix.conf.php &lt;&lt; </em><strong>&#39;_END&#39;<br></strong>&lt;?php<br>// Zabbix GUI configuration file.<br>global $$DB;<br>$$DB[&#39;TYPE&#39;]            = &#39;MYSQL&#39;;<br>$$DB[&#39;SERVER&#39;]       = &#39;${zabbix_db_private_addr}&#39;;<br>$$DB[&#39;PORT&#39;]            = &#39;${zabbix_db_port}&#39;;<br>$$DB[&#39;DATABASE&#39;]         = &#39;zabbix&#39;;<br>$$DB[&#39;USER&#39;]            = &#39;zabbix&#39;;<br>$$DB[&#39;PASSWORD&#39;]         = &#39;${zabbix_db_password}&#39;;<br>// Schema name. Used for IBM DB2 and PostgreSQL.<br>$$DB[&#39;SCHEMA&#39;]       = &#39;&#39;;<br>$$ZBX_SERVER            = &#39;${zabbix_app_server_addr}&#39;;<br>$$ZBX_SERVER_PORT     = &#39;10051&#39;;<br>$$ZBX_SERVER_NAME     = &#39;&#39;;<br>$$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;<br><strong>_END<br><br></strong>chmod +x /etc/zabbix/web/zabbix.conf.php<br>chown apache:apache /etc/zabbix/web/zabbix.conf.php<br>yum install -y epel-release<br>yum install -y crudini.noarch<br>crudini --set /etc/php.ini Date date.timezone Europe/Moscow<br>systemctl start httpd<br>systemctl enable httpd</pre><p>I use double $ sign as escape squence.</p><h3>EC2 instance (<strong>terraform-aws-modules/ec2-instance/aws</strong>)</h3><p>It’s similar to launch configuration</p><pre><strong>module &quot;bastion_ec2&quot; </strong>{<br>  <strong>source </strong>= <strong>&quot;terraform-aws-modules/ec2-instance/aws&quot;<br><br>  name  </strong>= <strong>&quot;${</strong><em>var</em><strong>.bastion_ec2_name}&quot;<br>  count </strong>= 1<br><br>  <strong>ami                         </strong>= <strong>&quot;${</strong><em>data</em><strong>.aws_ami.centos.id}&quot;<br>  instance_type               </strong>= <strong>&quot;${</strong><em>var</em><strong>.default_instance_type}&quot;<br>  key_name                    </strong>= <strong>&quot;${</strong><em>var</em><strong>.default_key_name}&quot;<br>  monitoring                  </strong>= <strong>false<br>  associate_public_ip_address </strong>= <strong>true<br><br>  subnet_id </strong>= <strong>&quot;${</strong><em>module</em><strong>.vpc.public_subnets[0]}&quot;<br><br>  vpc_security_group_ids </strong>= [<br>    <strong>&quot;${</strong><em>module</em><strong>.bastion_sg.this_security_group_id}&quot;</strong>,<br>    <strong>&quot;${</strong><em>module</em><strong>.allow_egress_all.this_security_group_id}&quot;</strong>,<br>    <strong>&quot;${</strong><em>module</em><strong>.allow_ingress_from_bastion.this_security_group_id}&quot;</strong>,<br>  ]<br><br>  <strong>tags </strong>{<br>    <strong>Terraform   </strong>= <strong>&quot;true&quot;<br>    Environment </strong>= <strong>&quot;stage&quot;<br>    Service     </strong>= <strong>&quot;bastion&quot;<br>    Subsystem   </strong>= <strong>&quot;sshd&quot;<br>  </strong>}<br>}</pre><h3>ELB (<strong>terraform-aws-modules/elb/aws)</strong></h3><p>Finaly we need to specify elb for web frontend and application backend</p><pre><strong>module &quot;zabbix_web_elb&quot; </strong>{<br>  <strong>source </strong>= <strong>&quot;terraform-aws-modules/elb/aws&quot;<br><br>  name </strong>= <strong>&quot;${</strong><em>var</em><strong>.zabbix_web_elb_name}&quot;<br><br>  subnets </strong>= <strong>&quot;${</strong><em>module</em><strong>.vpc.public_subnets}&quot;<br><br>  security_groups </strong>= [<br>    <strong>&quot;${</strong><em>module</em><strong>.zabbix_lb_sg.this_security_group_id}&quot;</strong>,<br>    <strong>&quot;${</strong><em>module</em><strong>.allow_egress_all.this_security_group_id}&quot;</strong>,<br>  ]<br><br>  <strong>internal </strong>= <strong>false<br><br>  listener </strong>= [<br>    {<br>      <strong>instance_port     </strong>= <strong>&quot;80&quot;<br>      instance_protocol </strong>= <strong>&quot;HTTP&quot;<br>      lb_port           </strong>= <strong>&quot;80&quot;<br>      lb_protocol       </strong>= <strong>&quot;HTTP&quot;<br>    </strong>},<br>  ]<br><br>  <strong>health_check </strong>= [<br>    {<br>      <strong>target              </strong>= <strong>&quot;HTTP:80/zabbix/&quot;<br>      interval            </strong>= <strong>&quot;${</strong><em>var</em><strong>.zabbix_web_elb_hc_interval}&quot;<br>      healthy_threshold   </strong>= <strong>&quot;${</strong><em>var</em><strong>.zabbix_web_elb_hc_healthy_threshold}&quot;<br>      unhealthy_threshold </strong>= <strong>&quot;${</strong><em>var</em><strong>.zabbix_web_elb_hc_unhealthy_threshold}&quot;<br>      timeout             </strong>= <strong>&quot;${</strong><em>var</em><strong>.zabbix_web_elb_hc_timeout}&quot;<br>    </strong>},<br>  ]<br><br>  <strong>tags </strong>{<br>    <strong>Terraform   </strong>= <strong>&quot;true&quot;<br>    Environment </strong>= <strong>&quot;stage&quot;<br>    Service     </strong>= <strong>&quot;zabbix&quot;<br>    Subsystem   </strong>= <strong>&quot;zabbix_web&quot;<br>  </strong>}<br>}</pre><p>application elb differs using TCP target instead of HTTP</p><pre><strong>listener </strong>= [<br>  {<br>    <strong>instance_port     </strong>= <strong>&quot;${</strong><em>var</em><strong>.zabbix_app_listen_port}&quot;<br>    instance_protocol </strong>= <strong>&quot;TCP&quot;<br>    lb_port           </strong>= <strong>&quot;${</strong><em>var</em><strong>.zabbix_app_listen_port}&quot;<br>    lb_protocol       </strong>= <strong>&quot;TCP&quot;<br>  </strong>},<br>]<br><br><strong>health_check </strong>= [<br>  {<br>    <strong>target              </strong>= <strong>&quot;${format(&quot;TCP:%s&quot;, </strong><em>var</em><strong>.zabbix_app_listen_port)}&quot;<br>    interval            </strong>= <strong>&quot;${</strong><em>var</em><strong>.zabbix_app_elb_hc_interval}&quot;<br>    healthy_threshold   </strong>= <strong>&quot;${</strong><em>var</em><strong>.zabbix_app_elb_hc_healthy_threshold}&quot;<br>    unhealthy_threshold </strong>= <strong>&quot;${</strong><em>var</em><strong>.zabbix_app_elb_hc_unhealthy_threshold}&quot;<br>    timeout             </strong>= <strong>&quot;${</strong><em>var</em><strong>.zabbix_app_elb_hc_timeout}&quot;<br>  </strong>},<br>]</pre><h3>Deploy</h3><p>Before appling configuration, get all needed modules terraform get</p><pre>laptop-A018226:stage $ terraform apply<br>data.aws_ami.centos: Refreshing state...</pre><pre>An execution plan has been generated and is shown below.<br>Resource actions are indicated with the following symbols:<br>  + create<br> &lt;= read (data resources)</pre><pre>Terraform will perform the following actions:</pre><pre>&lt;= data.template_file.zabbix_app_init<br>....<br>....<br>Plan: 51 to add, 0 to change, 0 to destroy.</pre><pre>Do you want to perform these actions?<br>  Terraform will perform the actions described above.<br>  Only &#39;yes&#39; will be accepted to approve.</pre><pre>Enter a value: yes</pre><pre>.....<br>.....</pre><pre>Apply complete! Resources: 51 added, 0 changed, 0 destroyed.</pre><pre>Outputs:</pre><pre>bastion_private_ip = [<br>    192.168.128.170<br>]<br>bastion_public_ip = [<br>    52.36.61.86<br>]<br>zabbix_app_elb_dns_name = internal-zabbix-app-elb-712473970.us-west-2.elb.amazonaws.com<br>zabbix_db_private_ip = zabbix-db.cbfnshrrnrtb.us-west-2.rds.amazonaws.com<br>zabbix_web_elb_dns_name = zabbix-web-elb-919716925.us-west-2.elb.amazonaws.com</pre><p>Let’s check if zabbix available</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KPIBlIAIbmXbcWw-xtrMew.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WBRmd1Mh8KiPUzURMZuyUw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f50f35e5a8d4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Terraform by examples. Part 1]]></title>
            <link>https://medium.com/@dhelios/terraform-by-examples-part-1-ef3e3be7b88b?source=rss-5cc2847807d5------2</link>
            <guid isPermaLink="false">https://medium.com/p/ef3e3be7b88b</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[terraform]]></category>
            <dc:creator><![CDATA[Artem Penner]]></dc:creator>
            <pubDate>Tue, 14 Nov 2017 19:02:18 GMT</pubDate>
            <atom:updated>2019-02-17T19:00:01.302Z</atom:updated>
            <content:encoded><![CDATA[<p>First of all I should say that I’m not familiar with Terraform and it’s just my notes about this tool. If you’ve never heard / used this tool it’s better to visit <a href="https://www.terraform.io/intro/getting-started/install.html">https://www.terraform.io/intro/getting-started/install.html</a></p><p>I decided to learn terraform “by examples”, so I tried to implement simple service (I chose zabbix) using only this tool. Deployment will consist of four parts, each in separate network with it’s own security rules.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*VD8oHWXZ5nJJCgsL5IG5Rw.png" /><figcaption>Network diagram</figcaption></figure><h3><strong>Setup environment.</strong></h3><p>Download terraform from <a href="https://www.terraform.io/downloads.html">https://www.terraform.io/downloads.html</a> or use your package manager.</p><pre>$ brew install terraform terraform-inventory</pre><p>Create aws user using AMI interface and download your credentials AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY.</p><h3>Init &amp;&amp; download provider plugin.</h3><p>Create directory where we’ll store our code base infrastructure definitions and create file <strong>main.tf</strong></p><blockquote>When invoking any command that loads the Terraform configuration, Terraform loads all configuration files within the directory specified in alphabetical order.</blockquote><blockquote>The files loaded must end in either .tf or .tf.json to specify the format that is in use. Otherwise, the files are ignored.</blockquote><pre>provider &quot;aws&quot; {<br>    region = &quot;${var.aws_region}&quot;<br>    shared_credentials_file = &quot;/Users/dhelios/.aws/credentials&quot;<br>    profile = &quot;TFtests&quot;<br>}</pre><p>file <strong>vars.tf</strong> will be used for variables definitions</p><pre># region / regions where we deploy our infrastructure<br>variable aws_region { default = &quot;us-west-2&quot; }<br>variable default_AZ { default = &quot;us-west-2a&quot; }<br>variable default_keyname { default = &quot;TF-tests-key.rsa&quot; }</pre><p>After that call <strong>terraform init </strong>to download provider specific plugins.</p><h3>Network definitions.</h3><p>In file networks.tf I describe all network patterns and add additional network related variables in vars.tf file.</p><p><strong>networks.tf:</strong></p><pre># Create separate virtual private cloud for project.<br>resource &quot;aws_vpc&quot; &quot;default&quot; {<br>    cidr_block = &quot;${var.cidr_block_main}&quot;<br>    enable_dns_hostnames = true</pre><pre>tags {<br>        Name = &quot;${var.vpc_name}&quot;<br>    }<br>}</pre><pre># BEGIN: Network Section</pre><pre># network to place database instances of our service<br>resource &quot;aws_subnet&quot; &quot;subnet_for_db&quot; {<br>    vpc_id  = &quot;${aws_vpc.default.id}&quot;<br>    cidr_block = &quot;${var.cidr_block_for_db}&quot;<br>    availability_zone = &quot;${var.default_AZ}&quot;<br>    map_public_ip_on_launch = true</pre><pre>tags {<br>        Name = &quot;Subnet_For_Databases&quot;<br>    }<br>}</pre><pre># Network to place zabbix server and zabbix web backends<br>resource &quot;aws_subnet&quot; &quot;subnet_for_app&quot; {<br>    vpc_id = &quot;${aws_vpc.default.id}&quot;<br>    cidr_block = &quot;${var.cidr_block_for_app}&quot;<br>    availability_zone = &quot;${var.default_AZ}&quot;<br>    map_public_ip_on_launch = true</pre><pre>tags {<br>        Name = &quot;Subnet_For_Applications&quot;<br>    }<br>}</pre><pre># Network for ELB<br>resource &quot;aws_subnet&quot; &quot;subnet_for_lb&quot; {<br>    vpc_id = &quot;${aws_vpc.default.id}&quot;<br>    cidr_block = &quot;${var.cidr_block_for_lb}&quot;<br>    availability_zone = &quot;${var.default_AZ}&quot;<br>    map_public_ip_on_launch = true</pre><pre>tags {<br>        Name = &quot;Subnet_For_LoadBalancers&quot;<br>    }<br>}</pre><pre># Network for bastion host<br>resource &quot;aws_subnet&quot; &quot;subnet_for_bhost&quot; {<br>    vpc_id = &quot;${aws_vpc.default.id}&quot;<br>    cidr_block = &quot;${var.cidr_block_for_bhost}&quot;<br>    availability_zone = &quot;${var.default_AZ}&quot;<br>    map_public_ip_on_launch = true</pre><pre>tags {<br>        Name = &quot;Subnet_For_BastionHost&quot;<br>    }<br>}</pre><pre># Enable internet access<br>resource &quot;aws_internet_gateway&quot; &quot;gw&quot; {<br>    vpc_id = &quot;${aws_vpc.default.id}&quot;</pre><pre>tags {<br>        Name = &quot;${var.vpc_name}_ig&quot;<br>    }<br>}</pre><pre># specify default router<br>resource &quot;aws_route_table&quot; &quot;r&quot; {<br>  vpc_id = &quot;${aws_vpc.default.id}&quot;</pre><pre>route {<br>    cidr_block = &quot;0.0.0.0/0&quot;<br>    gateway_id = &quot;${aws_internet_gateway.gw.id}&quot;<br>  }</pre><pre>tags {<br>    Name = &quot;aws_route_table&quot;<br>  }<br>}</pre><pre>##<br>## Add routes to networks<br>## (first three addresses in every <br>## network are reserved for aws services + one for subnet)</pre><pre>resource &quot;aws_route_table_association&quot; &quot;rt_app&quot; {<br>  subnet_id      = &quot;${aws_subnet.subnet_for_app.id}&quot;<br>  route_table_id = &quot;${aws_route_table.r.id}&quot;<br>}</pre><pre>resource &quot;aws_route_table_association&quot; &quot;rt_db&quot; {<br>  subnet_id      = &quot;${aws_subnet.subnet_for_db.id}&quot;<br>  route_table_id = &quot;${aws_route_table.r.id}&quot;<br>}</pre><pre>resource &quot;aws_route_table_association&quot; &quot;rt_lb&quot; {<br>  subnet_id      = &quot;${aws_subnet.subnet_for_lb.id}&quot;<br>  route_table_id = &quot;${aws_route_table.r.id}&quot;<br>}</pre><pre>resource &quot;aws_route_table_association&quot; &quot;rt_bhost&quot; {<br>    subnet_id = &quot;${aws_subnet.subnet_for_bhost.id}&quot;<br>    route_table_id = &quot;${aws_route_table.r.id}&quot;<br>}<br># END: Network Section</pre><p>All variables used in <strong>networks.tf</strong> must be defined in <strong>vars.tf</strong></p><pre>variable vpc_name { default = &quot;PocVpc&quot; }<br>variable cidr_block_main { default = &quot;192.168.188.0/24&quot; }<br># ---<br># Network for databases<br># HostMin:   192.168.188.5  (first four is reserved by amazon)<br># HostMax:   192.168.188.14<br>variable cidr_block_for_db { default = &quot;192.168.188.0/28&quot; }<br># ---<br># Network for app services<br># HostMin:   192.168.188.21 (first four is reserved by amazon)<br># HostMax:   192.168.188.30<br>variable cidr_block_for_app { default = &quot;192.168.188.16/28&quot; }<br># ---<br># Network for LB<br># HostMin:   192.168.188.37 (first four is reserved by amazon)<br># HostMax:   192.168.188.46<br>variable cidr_block_for_lb { default = &quot;192.168.188.32/28&quot; }<br># ---<br># Network for Bastion Host<br># HostMin:   192.168.188.53 (first four is reserved by amazon)<br># HostMax:   192.168.188.62<br>variable cidr_block_for_bhost { default = &quot;192.168.188.48/28&quot; }<br># ---<br># Constanstant CIDR blocks<br>variable cidr_block_internet { default = &quot;0.0.0.0/0&quot; }</pre><h3>Security groups</h3><p>All security policies will be described in <strong>security.tf. </strong>Each<strong> </strong>security group has a self explained descriptions.</p><pre># BEGIN: Sec Groups<br>resource &quot;aws_security_group&quot; &quot;sg_allow_internet&quot; {<br>    name = &quot;sg_allow_internet&quot;<br>    description = &quot;allow all outcomming traffic&quot;<br>    vpc_id = &quot;${aws_vpc.default.id}&quot;</pre><pre># allow internet access, but block all incoming traffic<br>    egress {<br>        from_port = 0<br>        to_port = 0<br>        protocol = &quot;-1&quot;<br>        cidr_blocks = [&quot;${var.cidr_block_internet}&quot;]<br>    }<br>}</pre><pre>resource &quot;aws_security_group&quot; &quot;sg_bastion&quot; {<br>    name = &quot;sg_bastion&quot;<br>    description = &quot;allow traffic on bastion host&quot;<br>    vpc_id = &quot;${aws_vpc.default.id}&quot;</pre><pre># allow sshd connection<br>    ingress {<br>        from_port = 22<br>        to_port = 22<br>        protocol = &quot;tcp&quot;<br>        cidr_blocks = [&quot;${var.cidr_block_internet}&quot;]<br>    }<br>}</pre><pre>resource &quot;aws_security_group&quot; &quot;sg_admin&quot; {<br>    name = &quot;sg_allow_bhost_traffic&quot;<br>    description = &quot;allow all traffic from bastion host&quot;<br>    vpc_id = &quot;${aws_vpc.default.id}&quot;</pre><pre>ingress {<br>        from_port = 0<br>        to_port = 0<br>        protocol = &quot;-1&quot;<br>        cidr_blocks = [&quot;${var.cidr_block_for_bhost}&quot;]<br>    }<br>}</pre><pre>resource &quot;aws_security_group&quot; &quot;sg_lb2app&quot; {<br>    name = &quot;sg_allow_lb_to_app&quot;<br>    description = &quot;allow traffic from load balancer and to load balancer&quot;<br>    vpc_id = &quot;${aws_vpc.default.id}&quot;</pre><pre>ingress {<br>        from_port = &quot;${var.web_http_port}&quot;<br>        to_port = &quot;${var.web_http_port}&quot;<br>        protocol = &quot;tcp&quot;<br>        cidr_blocks = [&quot;${var.cidr_block_for_lb}&quot;]<br>    }</pre><pre>ingress {<br>        from_port = &quot;${var.web_https_port}&quot;<br>        to_port = &quot;${var.web_https_port}&quot;<br>        protocol = &quot;tcp&quot;<br>        cidr_blocks = [&quot;${var.cidr_block_for_lb}&quot;]<br>    }<br>}</pre><pre>resource &quot;aws_security_group&quot; &quot;elb&quot; {<br>    name = &quot;sg_elb&quot;<br>    description = &quot;elb security group&quot;<br>    vpc_id = &quot;${aws_vpc.default.id}&quot;</pre><pre>ingress {<br>        from_port = &quot;${var.elb_listen_http_port}&quot;<br>        to_port = &quot;${var.elb_listen_http_port}&quot;<br>        protocol = &quot;tcp&quot;<br>        cidr_blocks = [&quot;${var.cidr_block_internet}&quot;]<br>    }</pre><pre>egress {<br>        from_port = 0<br>        to_port = 0<br>        protocol = &quot;-1&quot;<br>        cidr_blocks = [&quot;${var.cidr_block_internet}&quot;]<br>    }<br>}</pre><pre>resource &quot;aws_security_group&quot; &quot;sg_app2db&quot; {<br>    name = &quot;sg_allow_app_to_db&quot;<br>    description = &quot;allow traffic from app hosts to databases&quot;<br>    vpc_id = &quot;${aws_vpc.default.id}&quot;</pre><pre>ingress {<br>        from_port = &quot;${var.db_port}&quot;<br>        to_port = &quot;${var.db_port}&quot;<br>        protocol = &quot;tcp&quot;<br>        cidr_blocks = [&quot;${var.cidr_block_for_app}&quot;]<br>    }<br>}</pre><pre>resource &quot;aws_security_group&quot; &quot;sg_web2app&quot; {<br>    name = &quot;sg_allow_web_to_app&quot;<br>    description = &quot;allow traffic from app hosts to databases&quot;<br>    vpc_id = &quot;${aws_vpc.default.id}&quot;</pre><pre>ingress {<br>        from_port = &quot;${var.app_port}&quot;<br>        to_port = &quot;${var.app_port}&quot;<br>        protocol = &quot;tcp&quot;<br>        cidr_blocks = [&quot;${var.cidr_block_for_app}&quot;]<br>    }<br>}</pre><pre># END: Sec Groups</pre><h3>Discover AMIs</h3><p>Virtual Machine images in aws have unique identifies depends on region. To simplify deployments and do not track id changes let’s describe automatic image selection.</p><pre>data &quot;aws_ami&quot; &quot;ubuntu&quot; {<br>  most_recent = true<br>  filter {<br>    name   = &quot;name&quot;<br>    values = [&quot;ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*&quot;]<br>  }<br>  filter {<br>    name   = &quot;virtualization-type&quot;<br>    values = [&quot;hvm&quot;]<br>  }<br>  owners = [&quot;099720109477&quot;] # Canonical<br>}</pre><pre>data &quot;aws_ami&quot; &quot;centos&quot; {<br>  most_recent = true<br>  filter {<br>    name = &quot;name&quot;<br>    values = [&quot;CentOS Linux 7 x86_64*&quot;]<br>  }<br>  filter {<br>    name = &quot;virtualization-type&quot;<br>    values = [&quot;hvm&quot;]<br>  }<br>  filter {<br>    name = &quot;product-code&quot;<br>    values = [&quot;aw0evgkw8e5c1q413zgy5pjce&quot;]<br>  }<br>  owners = [&quot;679593333241&quot;] # aws<br>}</pre><pre>data &quot;aws_ami&quot; &quot;aws_linux&quot; {<br>  most_recent = true<br>  filter {<br>    name = &quot;name&quot;<br>    values = [&quot;amzn-ami-hvm-*&quot;]<br>  }<br>  filter {<br>    name = &quot;virtualization-type&quot;<br>    values = [&quot;hvm&quot;]<br>  }<br>  owners = [&quot;137112412989&quot;] # aws<br>}</pre><h3>Deploy instances</h3><p>Database and application (zabbix-server) instances will be EC2 t2.micro virtual machines and web-server I’ll implement throught autoscalling group.</p><blockquote>An <em>Auto Scaling group</em> contains a collection of EC2 instances that share similar characteristics and are treated as a logical grouping for the purposes of instance scaling and management</blockquote><blockquote>An Auto Scaling group starts by launching enough EC2 instances to meet its desired capacity. The Auto Scaling group maintains this number of instances by performing periodic health checks on the instances in the group</blockquote><pre># BEGIN: Instances<br>resource &quot;aws_instance&quot; &quot;bhost&quot; {<br>    ami = &quot;${data.aws_ami.centos.id}&quot;<br>    instance_type = &quot;${var.bhost_inst_type}&quot;<br>    subnet_id = &quot;${aws_subnet.subnet_for_bhost.id}&quot;<br>    key_name = &quot;${var.default_keyname}&quot;<br>    vpc_security_group_ids = [<br>        &quot;${aws_security_group.sg_allow_internet.id}&quot;, <br>        &quot;${aws_security_group.sg_bastion.id}&quot;<br>    ]</pre><pre>monitoring = false</pre><pre>count = &quot;${var.bhost_inst_count}&quot;</pre><pre>tags {<br>        Name = &quot;Bastion Host&quot;<br>    }<br>}</pre><pre>resource &quot;aws_instance&quot; &quot;app&quot; {<br>    ami = &quot;${data.aws_ami.centos.id}&quot;<br>    instance_type = &quot;${var.app_inst_type}&quot;<br>    subnet_id = &quot;${aws_subnet.subnet_for_app.id}&quot;<br>    key_name = &quot;${var.default_keyname}&quot;<br>    vpc_security_group_ids = [<br>        &quot;${aws_security_group.sg_allow_internet.id}&quot;, <br>        &quot;${aws_security_group.sg_bastion.id}&quot;,<br>        &quot;${aws_security_group.sg_web2app.id}&quot; <br>    ]</pre><pre>monitoring = false</pre><pre>count = &quot;${var.app_inst_count}&quot;</pre><pre>user_data = &lt;&lt;-EOF<br>                #!/bin/bash<br>                yum install -y <a href="http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-1.el7.centos.noarch.rpm">http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-1.el7.centos.noarch.rpm</a><br>                yum install -y zabbix-server-mysql.x86_64 policycoreutils.x86_64 policycoreutils-devel<br>                setenforce 0<br>                cat &gt; /etc/zabbix/zabbix_server.conf&lt;&lt;&#39;_END&#39;<br>                LogFile=/var/log/zabbix/zabbix_server.log<br>                LogFileSize=0<br>                PidFile=/var/run/zabbix/zabbix_server.pid<br>                SocketDir=/var/run/zabbix<br>                DBName=zabbix<br>                DBUser=zabbix<br>                DBHost=${aws_instance.db.private_ip}<br>                DBPassword=${var.db_password}<br>                SNMPTrapperFile=/var/log/snmptrap/snmptrap.log<br>                Timeout=4<br>                AlertScriptsPath=/usr/lib/zabbix/alertscripts<br>                ExternalScripts=/usr/lib/zabbix/externalscripts<br>                LogSlowQueries=3000<br>                _END<br>                cat &gt; /tmp/zabbix-policy.te &lt;&lt;&#39;_END&#39;<br>                module zabbix-policy 1.0;<br>                require {<br>                    type mysqld_port_t;<br>                    type zabbix_var_run_t;<br>                    type zabbix_t;<br>                    class sock_file create;<br>                    class tcp_socket name_connect;<br>                    class process setrlimit;<br>                    class unix_stream_socket connectto;<br>                }<br>                #============= zabbix_t ==============<br>                allow zabbix_t mysqld_port_t:tcp_socket name_connect;<br>                allow zabbix_t self:process setrlimit;<br>                <br>                allow zabbix_t self:unix_stream_socket connectto;<br>                allow zabbix_t zabbix_var_run_t:sock_file create;<br>                _END<br>                checkmodule -M -m -o /tmp/zabbix-policy.mod /tmp/zabbix-policy.te<br>                semodule_package -o /tmp/zabbix-policy.pp -m /tmp/zabbix-policy.mod<br>                semodule -i /tmp/zabbix-policy.pp<br>                systemctl start zabbix-server.service<br>                systemctl enable zabbix-server.service<br>                EOF</pre><pre>tags {<br>        Name = &quot;application&quot;<br>    }</pre><pre>}</pre><p>Terraform does not provide loop statement like this</p><pre>for (i := 0; i &lt; 10; i++) {<br>  resource &quot;aws_instance&quot; &quot;web&quot; {<br>    instance_type = &quot;t2.medium&quot;<br>    tags {<br>        Name = &quot;web&quot; + i<br>    }<br>  }<br>}</pre><p>But provides similar functionality through <strong>count</strong> attribute.</p><pre>resource &quot;aws_instance&quot; &quot;web&quot; {<br>    instance_type = &quot;t2.medium&quot;<br>    count = &quot;10&quot;<br>    tags {<br>        Name = &quot;web + ${count.index}&quot;<br>    }<br>  }</pre><p>Another usefull thing is a <strong>user_data</strong> attribute. You can specify script payload or cloud daemon instruction. Data can be inline template (example above) or file template (need to install dependencies template module).</p><h3>Autoscalling groups</h3><p>autoscalling group declaration is similar to <strong>aws_instance</strong>. We described desired configuration in <strong>aws_launch_configuration </strong>resource and added additional scalling options in <strong>aws_autoscaling_group</strong> resource. After that we defined load balancer which would be track the state of our service.</p><pre>data &quot;template_file&quot; &quot;app_payload&quot; {<br>    template = &quot;${file(&quot;user_data_app.tpl&quot;)}&quot;<br>    vars = {<br>        db_private_addr = &quot;${aws_instance.db.private_ip}&quot;<br>        db_password = &quot;${var.db_password}&quot;<br>        db_port = &quot;${var.db_port}&quot;<br>        zbx_private_addr = &quot;${aws_instance.app.private_ip}&quot;<br>    }<br>}</pre><pre>resource &quot;aws_launch_configuration&quot; &quot;web_lc&quot; {<br>    image_id = &quot;${data.aws_ami.centos.id}&quot;<br>    instance_type = &quot;${var.web_inst_type}&quot;<br>    security_groups  = [<br>            &quot;${aws_security_group.sg_admin.id}&quot;,<br>            &quot;${aws_security_group.sg_allow_internet.id}&quot;,<br>            &quot;${aws_security_group.sg_lb2app.id}&quot;<br>    ]</pre><pre>key_name = &quot;${var.default_keyname}&quot;</pre><pre>user_data = &quot;${data.template_file.app_payload.rendered}&quot;<br>    enable_monitoring = false</pre><pre>lifecycle {<br>        create_before_destroy = true<br>    }<br>}</pre><pre>resource &quot;aws_autoscaling_group&quot; &quot;web_asg&quot; {<br>    launch_configuration = &quot;${aws_launch_configuration.web_lc.id}&quot;<br>    min_size = &quot;${var.web_inst_min_count}&quot;<br>    max_size = &quot;${var.web_inst_max_count}&quot;</pre><pre>vpc_zone_identifier = [&quot;${aws_subnet.subnet_for_app.id}&quot;]<br>    tag {<br>        key = &quot;Name&quot;<br>        value = &quot;web-asg&quot;<br>        propagate_at_launch = true<br>    }</pre><pre>load_balancers = [&quot;${aws_elb.frontend_lb.name}&quot;]<br>    health_check_type = &quot;ELB&quot;</pre><pre>}</pre><pre>resource &quot;aws_elb&quot; &quot;frontend_lb&quot; {<br>    name = &quot;frontendlb&quot;<br>    security_groups = [ <br>        &quot;${aws_security_group.elb.id}&quot;]<br>    subnets = [&quot;${aws_subnet.subnet_for_lb.id}&quot;]</pre><pre>health_check {<br>        healthy_threshold = 2<br>        unhealthy_threshold = 2<br>        timeout = 3<br>        interval = 30<br>        target = &quot;HTTP:${var.web_http_port}/zabbix/&quot;<br>    }</pre><pre>listener {<br>        lb_port = &quot;${var.elb_listen_http_port}&quot;<br>        lb_protocol = &quot;http&quot;<br>        instance_port = &quot;${var.web_http_port}&quot;<br>        instance_protocol = &quot;http&quot;<br>    }</pre><pre>}</pre><pre># END: Instances</pre><blockquote><strong>NOTE: </strong>Inline templates must escape their interpolations (as seen by the double <em>$</em> above). Unescaped interpolations will be processed <em>before</em> the template.</blockquote><p>As you see I didn’t use inline template like described before. Instead of that I created shell script template and rendered it every time when I launched new instance.</p><p><strong>Updated vars.tf file:</strong></p><pre># instancies<br>variable bhost_inst_type  { default = &quot;t2.nano&quot; }<br>variable bhost_inst_count { default = 1 }</pre><pre>variable app_inst_type { default = &quot;t2.nano&quot; }<br>variable app_inst_count { default = 1 }<br>variable app_port { default = 10051 }</pre><pre>variable web_inst_type { default = &quot;t2.nano&quot; }<br>variable web_inst_min_count { default = 1 }<br>variable web_inst_max_count { default = 2 }<br>variable web_http_port { default = 80 }<br>variable web_https_port { default = 443 }</pre><pre>variable db_inst_type {default = &quot;t2.nano&quot; }<br>variable db_inst_count { default = 1 }<br>variable mysql_nofile { default = 10000 }<br>variable db_password { default = &quot;mypasswd1&quot; }<br>variable db_port { default = 3306 }</pre><pre>variable elb_listen_http_port { default = 80 }</pre><h3>Plan &amp;&amp; Apply</h3><p>Run terraform plan and see what’s resource it will create, also it shows static parameters which we specify in resources.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KAigtt1fR7uMPpI8DNTdqA.png" /></figure><p>Try to login and check if our payload in <strong>user_data </strong>executed successfully and setup our environment as needed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r2IkkruurCS8Pg_zwXw5Nw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4LAlnH8AETkhPf4Ej5elkg.png" /></figure><p>Complete Ex: <a href="https://github.com/d-helios/examples/tree/master/terraform/TF-zabbix.simple">https://github.com/d-helios/examples/tree/master/terraform/TF-zabbix.simple</a></p><p>In <a href="https://medium.com/@dhelios/terraform-by-examples-part-2-f50f35e5a8d4">next article</a> I’ll show you how to do the same things, but with less efforts. We will consider using s3 as a backend to storing your tfstate files and look at the modules available at <a href="https://registry.terraform.io/">https://registry.terraform.io/</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ef3e3be7b88b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Memory caches and slab objects]]></title>
            <link>https://medium.com/@dhelios/memory-caches-and-slab-objects-c1de113ce235?source=rss-5cc2847807d5------2</link>
            <guid isPermaLink="false">https://medium.com/p/c1de113ce235</guid>
            <category><![CDATA[page-cache]]></category>
            <category><![CDATA[slabs]]></category>
            <category><![CDATA[linux]]></category>
            <dc:creator><![CDATA[Artem Penner]]></dc:creator>
            <pubDate>Tue, 14 Nov 2017 07:39:28 GMT</pubDate>
            <atom:updated>2017-11-14T07:42:27.875Z</atom:updated>
            <content:encoded><![CDATA[<p>Common SLAB caches in the linux kernel 2.6 &amp;&amp; 3.XX.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61223886c638e1331d1db0b5a3a21d09/href">https://medium.com/media/61223886c638e1331d1db0b5a3a21d09/href</a></iframe><h3>How to observ slab cache objects?</h3><p>You can get raw statitics reading /proc/slabinfo or can use slabtop. Read the man, it’s short and comprehensive <a href="http://man7.org/linux/man-pages/man1/slabtop.1.html">http://man7.org/linux/man-pages/man1/slabtop.1.html</a></p><pre>root@ip-192-168-172-53:~# slabtop -o -s c<br> Active / Total Objects (% used)    : 435085 / 447064 (97.3%)<strong><br></strong> Active / Total Slabs (% used)      : 13244 / 13244 (100.0%)<br> Active / Total Caches (% used)     : 87 / 138 (63.0%)<br> Active / Total Size (% used)       : 118774.38K / 124296.67K (95.6%)<br> Minimum / Average / Maximum Object : 0.01K / 0.28K / 16.00K</pre><pre>OBJS ACTIVE  USE OBJ SIZE  SLABS OBJ/SLAB CACHE SIZE NAME<br> 29700  27715  93%    1.05K    990       30     <strong>31680K</strong> ext4_inode_cache<br> 28539  28201  98%    0.59K   1057       27     <strong>16912K</strong> inode_cache<br> 77049  74432  96%    0.19K   3669       21     <strong>14676K</strong> dentry<br> 56100  55629  99%    0.12K   1650       34      6600K kernfs_node_cache<br>  7800   7800 100%    0.81K    200       39      6400K fuse_inode<br>  9940   8818  88%    0.57K    355       28      5680K radix_tree_node<br> 49179  49179 100%    0.10K   1261       39      5044K buffer_head<br>   720    638  88%    5.81K    144        5      4608K task_struct<br>  3168   3140  99%    1.00K     99       32      3168K kmalloc-1024<br>  5664   5645  99%    0.50K    177       32      2832K kmalloc-512<br>  4224   3839  90%    0.66K    176       24      2816K proc_inode_cache<br> 11340  11090  97%    0.20K    567       20      2268K vm_area_struct<br>  1088   1069  98%    2.00K     68       16      2176K kmalloc-2048<br>   705    601  85%    2.06K     47       15      1504K sighand_cache<br>  5376   3682  68%    0.25K    168       32      1344K kmalloc-256<br>  1312   1208  92%    1.00K     41       32      1312K signal_cache<br>  1794   1641  91%    0.69K     78       23      1248K shmem_inode_cache<br> 19520  19520 100%    0.06K    305       64      1220K anon_vma_chain<br>   296    266  89%    4.00K     37        8      1184K kmalloc-4096<br>  5754   5754 100%    0.19K    274       21      1096K cred_jar<br> 31872  31286  98%    0.03K    249      128       996K kmalloc-32<br>   496    496 100%    2.00K     31       16       992K mm_struct<br>   112    112 100%    8.00K     28        4       896K kmalloc-8192<br>  9894   9894 100%    0.08K    194       51       776K anon_vma<br>  1058   1037  98%    0.69K     46       23       736K sock_inode_cache<br> 11328  10712  94%    0.06K    177       64       708K kmalloc-64<br>   748    748 100%    0.94K     22       34       704K xfs_inode<br>  6636   5508  83%    0.09K    158       42       632K kmalloc-96<br>  1470   1470 100%    0.38K     70       21       560K kmem_cache<br>  2856   2856 100%    0.19K    136       21       544K kmalloc-192<br> 11832  11832 100%    0.04K    116      102       464K ext4_extent_status<br>   621    621 100%    0.69K     27       23       432K files_cache<br>  2976   2849  95%    0.12K     93       32       372K kmalloc-128<br>  5950   5950 100%    0.05K     70       85       280K ftrace_event_field<br> 15872  15872 100%    0.02K     62      256       248K kmalloc-16<br>  3304   3304 100%    0.07K     59       56       236K Acpi-Operand<br>  1792   1792 100%    0.12K     56       32       224K pid<br>   288    288 100%    0.66K     12       24       192K ovl_inode<br>   483    483 100%    0.38K     23       21       184K mnt_cache<br>  1886   1886 100%    0.09K     41       46       164K trace_event_file<br>  1156   1156 100%    0.12K     34       34       136K jbd2_journal_head<br>  1536   1536 100%    0.06K     24       64        96K kmem_cache_node<br>    45     45 100%    2.06K      3       15        96K dmaengine-unmap-256<br>   102    102 100%    0.94K      3       34        96K RAW<br>   140    140 100%    0.56K      5       28        80K task_group<br>  1836   1836 100%    0.04K     18      102        72K Acpi-Namespace<br>   216    216 100%    0.32K      9       24        72K taskstats<br>    78     78 100%    0.81K      2       39        64K bdev_cache<br>    32     32 100%    2.00K      2       16        64K TCP</pre><p>Whats conclussian can we makes based on this output?<br><strong>General output:</strong></p><ul><li><strong>TOTAL SIZE — </strong>tototal slab objects memory consumption in kb</li></ul><p><strong>Object specific output:</strong></p><ul><li><strong>NAME</strong> — <em>there are SLABs for specific and generic use</em>.</li><li><strong>CACHE SIZE</strong> — <em>Total size (KB) of active SLAB cache entries</em>. <br><strong>Constant: </strong>Monotoneous workload w.r.t specific SLAB cache entry. <br><strong>Increasing:</strong> The workload is active in using the specific SLAB cache entry. For specific SLABs, it means a specific activity for some part of the kernel. <br><strong>Decreasing:</strong> The specific or generic kernel code (including modules) are releasing allocated SLAB cache entries. This means that the generic or specific SLAB cache demand is decreasing generally indicating decrease in the workload</li><li><strong>(OBJ- ACTIVE)*OBJ SIZE</strong> — <em>Total size (KB) of available SLAB cache entries</em>. <strong>Constant: </strong>Stable workload w.r.t. specific SLAB cache entry. <br><strong>Increasing: </strong>There is recent significant request on specific SLAB cache. It means the increase in use is quite recent so a specific workload might be causing this. This can also mean that there is a leak for the specific SLAB and it can be a defect in the kernel or a kernel module. <br><strong>Decreasing:</strong> Available entries for some other SLAB cache needed to enlarge and therefore this specific SLAB cache entries are shrank.</li><li><strong>USE %</strong> — <em>Percentage (%) of active cache entries to the total available for specific SLAB.<br></em><strong>Low:</strong> The specific SLAB cache has not been active for some time and there are a lot available SLABs. If there are a couple of low percentages constantly, a contention on SLAB cache is unlikely.<br><strong>High:</strong> For values near 100% it means that this SLAB cache cannot shrink and if columns ACTIVE and CACHE SIZE keep increasing this can be an agressive load or a leak on the specific SLAB.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c1de113ce235" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Oracle JDK in CentOS environment]]></title>
            <link>https://medium.com/@dhelios/oracle-jdk-in-centos-environment-1aaff3a02af4?source=rss-5cc2847807d5------2</link>
            <guid isPermaLink="false">https://medium.com/p/1aaff3a02af4</guid>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Artem Penner]]></dc:creator>
            <pubDate>Mon, 13 Nov 2017 10:24:19 GMT</pubDate>
            <atom:updated>2017-11-13T10:24:19.335Z</atom:updated>
            <content:encoded><![CDATA[<p>It’s sad but oracle does not provide public yum repository for oracle jdk. Here is simple script to download it.</p><pre>#!/bin/bash</pre><pre># jdk_version: jdk version e.g: 8 or 9<br># ext: rpm or tar.gz</pre><pre>jdk_version=${1:-8}<br>ext=${2:-rpm}</pre><pre>readonly url=&quot;<a href="http://www.oracle.com/">http://www.oracle.com</a>&quot;<br>readonly jdk_download_url1=&quot;$url/technetwork/java/javase/downloads/index.html&quot;<br>readonly jdk_download_url2=$(<br>    curl -s $jdk_download_url1 | \<br>    egrep -o &quot;\/technetwork\/java/\javase\/downloads\/jdk${jdk_version}-downloads-.+?\.html&quot; | \<br>    head -1 | \<br>    cut -d &#39;&quot;&#39; -f 1<br>)<br>[[ -z &quot;$jdk_download_url2&quot; ]] &amp;&amp; echo &quot;Could not get jdk download url - $jdk_download_url1&quot; &gt;&gt; /dev/stderr</pre><pre>readonly jdk_download_url3=&quot;${url}${jdk_download_url2}&quot;<br>readonly jdk_download_url4=$(<br>    curl -s $jdk_download_url3 | \<br>    egrep -o &quot;http\:\/\/download.oracle\.com\/otn-pub\/java\/jdk\/[8-9](u[0-9]+|\+).*\/jdk-${jdk_version}.*(-|_)linux-(x64|x64_bin).$ext&quot;<br>)</pre><pre>for dl_url in ${jdk_download_url4[@]}; do<br> curl -O \<br>         --insecure -L\<br>         --header &quot;Cookie: oraclelicense=accept-securebackup-cookie&quot; \<br>         -N $dl_url<br>done</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1aaff3a02af4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>