<?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 CHIRAG CHAUDHURI on Medium]]></title>
        <description><![CDATA[Stories by CHIRAG CHAUDHURI on Medium]]></description>
        <link>https://medium.com/@chiragchaudhuri02?source=rss-6979b4140746------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*YU5c7bunihutWhLxEyUNVQ.jpeg</url>
            <title>Stories by CHIRAG CHAUDHURI on Medium</title>
            <link>https://medium.com/@chiragchaudhuri02?source=rss-6979b4140746------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 04:29:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chiragchaudhuri02/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[BIG DATA PROBLEM]]></title>
            <link>https://medium.com/@chiragchaudhuri02/big-data-problem-d0bb9d4e97f7?source=rss-6979b4140746------2</link>
            <guid isPermaLink="false">https://medium.com/p/d0bb9d4e97f7</guid>
            <category><![CDATA[big-data]]></category>
            <category><![CDATA[big-data-volume]]></category>
            <dc:creator><![CDATA[CHIRAG CHAUDHURI]]></dc:creator>
            <pubDate>Tue, 15 Sep 2020 22:37:16 GMT</pubDate>
            <atom:updated>2020-09-16T12:41:28.229Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*80cuLKZzm2fMNRh_l9quXg.jpeg" /></figure><h3>What is Big Data?</h3><p><strong>Ever wondered how much data big tech giants like facebook and twitter generate?</strong></p><p>According to a recent report Facebook generates about 4 petabytes(1000000 gigabytes) of data per day — that’s a million gigabytes. The total amount of data adds up to about 120 petabytes in a month and more than an Exabyte(1000 petabytes) in an year.</p><p>This huge chunk of data can be collectively referred to as <strong>Big Data</strong>.</p><p><strong><em>“Big data is a term that describes the large volume of data — both structured and unstructured — that inundates a business on a day-to-day basis.”</em></strong></p><h4>Characteristics of Big Data</h4><p>Some of the characteristics of these data sets include the following:</p><ol><li><strong>Volume</strong></li></ol><p>The quantity of generated and stored data. The size of the data determines the value and potential insight, and whether it can be considered big data or not.</p><p>2.<strong> Variety</strong></p><p>The type and nature of the data. The earlier technologies like RDBMSs were capable to handle structured data efficiently and effectively. However, the change in type and nature from structured to semi-structured or unstructured(variety) challenged the existing tools and technologies. The Big Data technologies evolved with the prime intention to capture, store, and process the semi-structured and unstructured data generated with high speed(velocity), and huge in size (volume).</p><p>3. <strong>Velocity</strong></p><p>The speed at which the data is generated and processed to meet the demands and challenges that lie in the path of growth and development. Big data is often available in real-time. Compared to small data, big data are produced more continually. Two kinds of velocity related to big data are the frequency of generation and the frequency of handling, recording, and publishing.</p><p>4. <strong>Veracity</strong></p><p>It is the extended definition for big data, which refers to the data quality and the data value. The data quality of captured data can vary greatly, affecting the accurate analysis.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KQ9EfVN5GCuLgFnWj29VWg.jpeg" /></figure><h3>The Problem</h3><h4>Volume/Size Problem</h4><p>There is a huge explosion in the data available. Look back a few years, and compare it with today, there has been an exponential increase in the data that enterprises can access. They have data for everything, right from what a consumer likes, to how they react, to a particular scent, to the amazing restaurant that opened up in Italy last weekend.</p><p>This data exceeds the amount of data that can be stored and computed, as well as retrieved. The challenge is not so much the availability, but the management of this data. With statistics claiming that data would increase 6.6 times the distance between earth and moon by 2020, this is definitely a challenge.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*MIKHdcN8j2ZZjZChKsTLEQ.jpeg" /></figure><h4>Velocity/Speed Problem</h4><p>The term “data” is not limited to the “stagnant” data that is available at common disposal. A lot of data keeps updating every second, and organizations need to be aware of that too. For instance, if a retail company wants to analyze customer behavior, real-time data from their current purchases can help.</p><p>It is important for businesses to keep themselves updated with this data, along with the “stagnant” and always available data. This will help build better insights and enhance decision-making capabilities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*yI9oXWy9OWASSRYjYrN4kQ.jpeg" /></figure><h4>Variety Problem</h4><p>In addition to volume and velocity, variety is fast becoming a third <a href="https://www.techrepublic.com/blog/big-data-analytics/">big data</a> “V-factor.” The problem is especially prevalent in large enterprises, which have many systems of record and also an abundance of data under management that is structured, semi-structured and unstructured. These enterprises often have multiple purchasing, manufacturing, sales, finance, and other departmental functions in separate subsidiaries and branch facilities, and they end up with “siloed” systems because of the functional duplicity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8DGrxF0tg2SFzXiEECsANw.jpeg" /></figure><h4>Veracity Problem</h4><p>Data veracity, in general, is how accurate or truthful a data set may be. In the context of big data, however, it takes on a bit more meaning. More specifically, when it comes to the accuracy of big data, it’s not just the quality of the data itself but how trustworthy the data source, type, and processing of it is. Removing things like bias, abnormalities or inconsistencies, duplication, and volatility are just a few aspects that factor into improving the accuracy of big data.</p><p>The second side of data veracity entails ensuring the processing method of the actual data makes sense based on business needs and the output is pertinent to objectives. Obviously, this is especially important when incorporating primary market research with big data. Interpreting big data in the right way ensures results are relevant and actionable. Further, access to big data means you could spend months sorting through information without focus and a without a method of identifying what data points are relevant. As a result, data should be analyzed in a timely manner, as is difficult with big data, otherwise the insights would fail to be useful.</p><h3>Managing Big Data the Hadoop Way</h3><h4>What is HADOOP?</h4><p><strong>“Hadoop, an open-source software framework, uses HDFS (the Hadoop Distributed File System) and MapReduce to analyze big data on clusters of commodity hardware — that is, in a distributed computing environment.”</strong></p><p>The Hadoop Distributed File System (HDFS) was developed to allow companies to more easily manage huge volumes of data in a simple and pragmatic way. Hadoop allows big problems to be decomposed into smaller elements so that analysis can be done quickly and cost effectively. HDFS is a versatile, resilient, clustered approach to managing files in a big data environment.</p><p>HDFS is not the final destination for files. Rather it is a data “service” that offers a unique set of capabilities needed when data volumes and velocity are high.</p><p>MapReduce is a software framework that enables developers to write programs that can process massive amounts of unstructured data in parallel across a distributed group of processors. MapReduce was designed by Google as a way of efficiently executing a set of functions against a large amount of data in batch mode.</p><p>The “map” component distributes the programming problem or tasks across a large number of systems and handles the placement of the tasks in a way that balances the load and manages recovery from failures. After the distributed computation is completed, another function called “reduce” aggregates all the elements back together to provide a result.</p><h3>Companies that use Hadoop</h3><p>Various MNCs around the world use Hadoop to manage their Big Data. Let’s look at some of them:</p><h4>Amazon</h4><p><strong>Amazon uses Elastic MapReduce(EMR) and Elastic Cloud Compute(EC2)</strong></p><p>Amazon web services, the top E-commerce today, simplifies its big data processing and analytics using Elastic MapReduce web service. EMR provides a managed framework of Hadoop that employs easy, fast and cost-effective mechanism to distribute and compute vast amounts of data across Amazon EC2 instances. The major functions performed by Hadoop in Amazon web services include log analysis, data warehousing, web indexing, financial analysis, machine learning, scientific simulation and bioinformatics.</p><h4>Facebook</h4><p><strong>Facebook uses Hadoop and Hive</strong></p><p>According to some experts and company professionals, Hadoop functions on every product of Facebook and in a variety of ways. User actions including ‘like,’ ‘status update’ or ‘add comment’ are stored and saved in an excellent distributed and personalized database, MySQL. Similarly, Facebook messenger application runs on HBase, and all the messages sent and received on Facebook are gathered and hoarded in HBase.</p><p>In addition, all of the external advertisers’ and developers’ campaigns and applications running on this social media platform use Hive to generate their success reports. Facebook has built a higher level data warehousing infrastructure using features of Hive that help in querying the database using SQL language-HiveQL.</p><h4>Adobe</h4><p><strong>Adobe uses Apache HBase and Apache Hadoop</strong></p><p>Planning a deployment of around 80 nodes cluster, Adobe’s processes currently have 30 nodes running on HDFS, HBase and Hadoop in clusters in the range of 5–14 nodes for its production and development operations. Adobe is a well-known international enterprise whose products and services are used worldwide; one of them is its Digital Marketing business Unit. Hadoop ecosystem has been deployed on Adobe’s VMware vSphere for several Adobe users. This deployment has reduced time to insight data and costs by using existing servers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d0bb9d4e97f7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CREATE A LOADBALANCER WITH HAPROXY ON AWS USING ANSIBLE]]></title>
            <link>https://medium.com/@chiragchaudhuri02/create-a-loadbalancer-with-haproxy-on-aws-using-ansible-e223b625d7f4?source=rss-6979b4140746------2</link>
            <guid isPermaLink="false">https://medium.com/p/e223b625d7f4</guid>
            <category><![CDATA[apache-web-server]]></category>
            <category><![CDATA[ansible]]></category>
            <category><![CDATA[ec2]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[haproxy]]></category>
            <dc:creator><![CDATA[CHIRAG CHAUDHURI]]></dc:creator>
            <pubDate>Mon, 14 Sep 2020 19:26:52 GMT</pubDate>
            <atom:updated>2020-09-14T19:26:52.394Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*osJkfFSQN2Cqm14sPnQdHQ.png" /></figure><h3>HAproxy</h3><p><strong>HAProxy</strong> is free, open source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spreads requests across multiple servers.</p><h3>Ansible</h3><p><strong>Ansible</strong> is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code.</p><p>This story demonstrates how one can create a loadbalancer with HAproxy on AWS using Ansible.</p><h3>Pre-requisites:</h3><ol><li>Ansible</li><li>AWS Account</li></ol><h3>Process</h3><p>The first and foremost task is to create a playbook that will bind the processes of creating an instance, and deploying the load balancer web servers. I have used the concept of roles in Ansible for easier management of the whole process. I have also used vaults for the important credentials that I will be requiring for the task.</p><p><strong>Configuration file for ansible</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/1*xq39zDWphJmuWbhtNUjNVw.png" /></figure><p><strong>ansible-playbook</strong></p><pre>- hosts: localhost<br>  roles:<br>          - role: launch1</pre><pre>- hosts: tag_Name_LoadBalancer<br>  remote_user: ec2-user<br>  become: yes<br>  become_user: root<br>  roles:<br>          - role: lbserver</pre><pre>- hosts: tag_Name_Webserver<br>  remote_user: ec2-user<br>  become: yes<br>  become_user: root<br>  roles:<br>          - role: webserver</pre><p>Now lets discuss about different parts of the playbook.</p><h4>Creating the instances:</h4><ol><li>Each playbook requires one or more hosts where the task is going to be carried.</li></ol><pre>- hosts: localhost<br>  roles:<br>          - role: launch1</pre><p>This segment selects localhost as the host and uses the role launch1 to create the instances for the loadbalancer and web servers.</p><p>2. The structure of role launch1 is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/237/1*8tfYyKwKp_TW28iCN8hGzw.png" /></figure><p>3. The main.yml file in tasks directory consists of all the tasks that creates the EC2 instances on AWS.</p><p><strong>tasks/main.yml</strong></p><pre>---<br>#tasks file for ec2<br>          - name: Create ec2 key<br>            ec2_key:<br>                    name: &quot;key&quot;<br>                    region: &quot;ap-south-1&quot;<br>                    aws_access_key: &quot;{{ access_key }}&quot;<br>                    aws_secret_key: &quot;{{ secret_key }}&quot;<br>            register: ec2_key<br>          - name: Save the key in a file<br>            copy: <br>              content: &quot;{{ ec2_key.key.private_key }}&quot;  <br>              dest: &quot;/keys/key.pem&quot;  <br>              mode: 0400<br>            when: ec2_key.changed<br>          - name: LoadBalancers on aws<br>            ec2:<br>                    key_name: &quot;key&quot;<br>                    instance_type: &quot;t2.micro&quot;<br>                    image: &quot;ami-0ebc1ac48dfd14136&quot;<br>                    wait: yes<br>                    count: 1<br>                    vpc_subnet_id: &quot;subnet-3df9c355&quot;<br>                    instance_tags: {&quot;Name&quot;:&quot;LoadBalancer&quot;}<br>                    assign_public_ip: yes<br>                    region: &quot;ap-south-1&quot;<br>                    state: present<br>                    group_id: &quot;sg-035d6d20f3467a621&quot;<br>                    aws_access_key: &quot;{{ access_key }}&quot;<br>                    aws_secret_key: &quot;{{ secret_key }}&quot;<br>            register: ec21<br>          - name: Webservers on aws<br>            ec2:<br>                    key_name: &quot;key&quot;<br>                    instance_type: &quot;t2.micro&quot;<br>                    image: &quot;ami-0ebc1ac48dfd14136&quot;<br>                    wait: yes<br>                    count: 4<br>                    vpc_subnet_id: &quot;subnet-3df9c355&quot;<br>                    instance_tags: {&quot;Name&quot;:&quot;Webserver&quot;}<br>                    assign_public_ip: yes<br>                    region: &quot;ap-south-1&quot;<br>                    state: present<br>                    group_id: &quot;sg-035d6d20f3467a621&quot;<br>                    aws_access_key: &quot;{{ access_key }}&quot;<br>                    aws_secret_key: &quot;{{ secret_key }}&quot;<br>            register: ec22<br>          - name: Wait for Loadbalancer to launch<br>            wait_for:<br>                    host: &quot;{{ item.public_ip }}&quot;<br>                    port: 22<br>                    state: started<br>            with_items: &quot;{{ ec21.instances }}&quot;<br>          - name: Wait for Webserver to launch<br>            wait_for:<br>                    host: &quot;{{ item.public_ip }}&quot;<br>                    port: 22<br>                    state: started<br>            with_items: &quot;{{ ec22.instances }}&quot;<br>          - name: &quot;Refresh Inventory&quot;<br>            meta: refresh_inventory</pre><p>4. The first part is to create the key and save it to a file. This file will be used to login into the instance. It is an essential part since without this key we can’t deploy web server in the instance.</p><pre>- name: Create ec2 key<br>            ec2_key:<br>                    name: &quot;key&quot;<br>                    region: &quot;ap-south-1&quot;<br>                    aws_access_key: &quot;{{ access_key }}&quot;<br>                    aws_secret_key: &quot;{{ secret_key }}&quot;<br>            register: ec2_key<br>          - name: Save the key in a file<br>            copy: <br>              content: &quot;{{ ec2_key.key.private_key }}&quot;  <br>              dest: &quot;/keys/key.pem&quot;  <br>              mode: 0400<br>            when: ec2_key.changed</pre><p>5. The next part creates the instances according to our needs. The ec2 module does just that. It helps us in configuring the instances and then creates them.</p><pre>- name: LoadBalancers on aws<br>            ec2:<br>                    key_name: &quot;key&quot;<br>                    instance_type: &quot;t2.micro&quot;<br>                    image: &quot;ami-0ebc1ac48dfd14136&quot;<br>                    wait: yes<br>                    count: 1<br>                    vpc_subnet_id: &quot;subnet-3df9c355&quot;<br>                    instance_tags: {&quot;Name&quot;:&quot;LoadBalancer&quot;}<br>                    assign_public_ip: yes<br>                    region: &quot;ap-south-1&quot;<br>                    state: present<br>                    group_id: &quot;sg-035d6d20f3467a621&quot;<br>                    aws_access_key: &quot;{{ access_key }}&quot;<br>                    aws_secret_key: &quot;{{ secret_key }}&quot;<br>            register: ec21<br>          - name: Webservers on aws<br>            ec2:<br>                    key_name: &quot;key&quot;<br>                    instance_type: &quot;t2.micro&quot;<br>                    image: &quot;ami-0ebc1ac48dfd14136&quot;<br>                    wait: yes<br>                    count: 4<br>                    vpc_subnet_id: &quot;subnet-3df9c355&quot;<br>                    instance_tags: {&quot;Name&quot;:&quot;Webserver&quot;}<br>                    assign_public_ip: yes<br>                    region: &quot;ap-south-1&quot;<br>                    state: present<br>                    group_id: &quot;sg-035d6d20f3467a621&quot;<br>                    aws_access_key: &quot;{{ access_key }}&quot;<br>                    aws_secret_key: &quot;{{ secret_key }}&quot;<br>            register: ec22</pre><p>6. After the instance is created it needs some time launch and get ready. For this purpose I have used the wait_for module. What this does is it creates a buffer for the instances to launch and get ready.</p><pre>          - name: Wait for Loadbalancer to launch<br>            wait_for:<br>                    host: &quot;{{ item.public_ip }}&quot;<br>                    port: 22<br>                    state: started<br>            with_items: &quot;{{ ec21.instances }}&quot;<br>          - name: Wait for Webserver to launch<br>            wait_for:<br>                    host: &quot;{{ item.public_ip }}&quot;<br>                    port: 22<br>                    state: started<br>            with_items: &quot;{{ ec22.instances }}&quot;</pre><p>7. Now the inventory has to be refreshed.</p><pre>- name: &quot;Refresh Inventory&quot;<br>  meta: refresh_inventory</pre><p>8. In the ec2 module I have used variables access_key and secret_key. These variable are declared in the main.yml file under vars directory. For better security I have used vaults to encrypt it. A vault can be created using the following command.</p><pre>ansible-vault create foo.yml</pre><p>Next we move on to the part of deploying the load balancer.</p><h4>Deploying Load Balancer</h4><p>The host for the loadbalancer is one of the EC2 instances. To have the host directly added to our inventory, we need to apply the concept of ansible dynamic inventory. EC2 instances can be added to our inventory using the ec2.py(It can be downloaded from <a href="https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py">here</a>). To make this plugin work we have to export a few parameters throuth the shell.</p><pre>export <strong>AWS_ACCESS_KEY_ID=</strong>&#39;AK123&#39;<br>export <strong>AWS_SECRET_ACCESS_KEY=</strong>&#39;abc123&#39;<br>export <strong>AWS_REGION=</strong>&#39;region-id&#39;</pre><ol><li>Since the instance is tagged as “Ansible” in the ec2 module of the role “launch”, it can be referred to as “tag_Name_LoadBalancer”.</li></ol><pre>- hosts: tag_Name_LoadBalancer</pre><p>2. Different instances may have different logging in credentials but, here I have used the Amazon Linux 2 ami so, the username “ec2-user” has to be used. In the first part we have created a key for logging in to the instance. This has to be mentioned in the private_key_file section of the ansible configuration file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/1*xq39zDWphJmuWbhtNUjNVw.png" /></figure><p>3. By default the ec2-user does not have a lot of powers. To solve this problem the concept of privilege escalation is used.</p><pre>become: yes<br>become_user: root</pre><p>4. Next in line is the part of roles. I have created the role lbserver to configure deploy the loadbalancer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/282/1*OKANFCHc3G-2Q1ozS5HYmQ.png" /></figure><p>5. The HAproxy loadbalancer needs a configuration file. This is specified in the haproxy.cfg file under templates directory. It is used to create and copy the final configuration file for web server. The benefit is that we are able to use variables which help us in specifying parameters in the config file which are initialized during run time. The <strong>template</strong> files will usually have the . j2 extension, which denotes the Jinja2 templating engine used.</p><p>6. Now coming to the main.yml under tasks directory.</p><p><strong>tasks/main.yml</strong></p><pre>- name: Install Haproxy<br>  package:<br>          name: haproxy<br>          state: present</pre><pre>- name: Start Haproxy<br>  service:<br>          name: haproxy<br>          state: started</pre><pre>- name: Configure Haproxy<br>  template:<br>          src: haproxy.cfg<br>          dest: &quot;/etc/haproxy/haproxy.cfg&quot;<br>  notify: Restart the LoadBalancer</pre><p>This tasks file first installs the HAproxy loadbalancer and then starts it. The template module copies the configuration file to the designated folder and the notify keyword notifies a handler which has been configured to restart the loadbalancer with desired configuration.</p><p><strong>handlers/main.yml</strong></p><pre>---<br># handlers file for lbserver<br>- name: Restart the LoadBalancer<br>  service:<br>    name: &quot;haproxy&quot;<br>    state: restarted</pre><h4>Deploying the Web Servers</h4><p>Now that the LoadBalancer is properly configured the web servers can be created.</p><ol><li>The EC2 instances for the web servers have already been created. They can be acessed using the group name “tag_Name_Webserver”.</li></ol><pre>- hosts: tag_Name_Webserver</pre><p>2. Like LoadBalancer the role for web servers also needs privilege escalation to perform some specific tasks.</p><pre>become: yes<br>become_user: root</pre><p>4. Now the role web server can be created. This role will install httpd on the instances for web server and configure them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/277/1*HRAgNInRRvgQck_AbdlnIA.png" /></figure><p>5. The main.yml file under tasks directory specifies all the tasks that the role is going to perform.</p><pre>---<br># tasks file for webserver</pre><pre>- name: &quot;Install Webserver&quot;<br>  package:<br>          name: &quot;httpd&quot;<br>          state: present<br>  register: status</pre><pre>- name: Webpage<br>  template:<br>    src: &quot;index.html.j2&quot;<br>    dest: &quot;/var/www/html/index.html&quot;<br>    mode: &quot;0666&quot;</pre><pre>- name: &quot;Start Webserver&quot;<br>  service:<br>          name: httpd<br>          state: started</pre><p>This role installs httpd web server on the instances, copies the index.html file to the designated directory and starts the web server. The copying part is done using the template module since, the index.html is configured to display the IP addresses of each instance.</p><p><strong>templates/index.html.j2</strong></p><pre>&lt;html&gt;<br>&lt;body bgcolor=aqua&gt; <br>hello this is my project&lt;br&gt;<br>instance ip is {{ ansible_default_ipv4.address }}&lt;br&gt;<br>hostname - {{ ansible_hostname }} <br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>6. Since now everything is configured, the playbook can be executed using the command.</p><pre>ansible-playbook --vault-id aws@prompt webserver.yml</pre><p>I have used one so, I have mentioned it in the command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X9SEJSvpptWbCfs6QhhhJw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o4hfXeTYgOe9mAbbEYYZzw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tqOW2Ve1EdJMtbafKrYjmA.png" /></figure><p>The instances are deployed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DP_UTVhht2mfUVKFFsJZSA.png" /></figure><p>The websites are accessible at &lt;LoadBalancer public IP&gt;:8080.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6vWR-SpksluEY9oVfhm0lg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BhaEg6_9Zpb4XlQBBSUOpA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zz9xAywt7FBg2ahuhuY6pw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ba1Rgrahf1eaQTwo7m8h9A.png" /></figure><h4>I did this project under the mentorship of Mr. <a href="https://www.linkedin.com/in/vimaldaga/">Vimal Daga</a> during the (RH294) Automation With Ansible Training by <a href="https://www.linkedin.com/in/linuxworld/">LinuxWorld India</a>. Thank you <a href="https://www.linkedin.com/in/vimaldaga/">Vimal</a> Sir, <a href="https://www.linkedin.com/in/preeti-chandak/">Preeti</a> Ma’am and <a href="https://www.linkedin.com/in/linuxworld/">LinuxWorld India</a> for giving me this opportunity.</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e223b625d7f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DEPLOY WORDPRESS WITH RDS BACKEND ON AWS USING TERRAFORM]]></title>
            <link>https://medium.com/@chiragchaudhuri02/deploy-wordpress-with-rds-backend-on-aws-using-terraform-e19d9d8a76c5?source=rss-6979b4140746------2</link>
            <guid isPermaLink="false">https://medium.com/p/e19d9d8a76c5</guid>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[amazon-eks]]></category>
            <category><![CDATA[wordpress]]></category>
            <category><![CDATA[amazon-rds]]></category>
            <dc:creator><![CDATA[CHIRAG CHAUDHURI]]></dc:creator>
            <pubDate>Wed, 09 Sep 2020 11:43:23 GMT</pubDate>
            <atom:updated>2020-09-09T11:43:23.378Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*QasZai2g0_PzhGI640_0eg.png" /></figure><h3>Amazon Elastic Kubernetes Service</h3><p><strong>Amazon Elastic Kubernetes Service (Amazon EKS)</strong> is a fully managed Kubernetes service. Customers such as Intel, Snap, Intuit, GoDaddy, and Autodesk trust EKS to run their most sensitive and mission critical applications because of its security, reliability, and scalability.</p><h3><strong>Amazon Relational Database Service</strong></h3><p><strong>Amazon Relational Database Service</strong> (or <strong>Amazon RDS</strong>) is a distributed relational database service by Amazon Web Services (AWS). It is a web service running “in the cloud” designed to simplify the setup, operation, and scaling of a relational database for use in applications.</p><h3>Terraform</h3><p><strong>Terraform</strong> is an open-source infrastructure as code software tool created by HashiCorp. Users define and provision data center infrastructure using a declarative configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON.</p><h3>Wordpress</h3><p><strong>WordPress</strong> (<strong>WP</strong>, <strong>WordPress.org</strong>) is a free and open-source content management system (CMS) written in PHP and paired with a MySQL or MariaDB database.</p><h3>Pre-requisites</h3><ol><li>AWS Account</li><li>Terraform installed</li><li>AWS CLI configured</li></ol><h3>Process</h3><ol><li>First we need to specify the providers. For deploying the whole setup we need the aws and kubernetes providers.</li></ol><pre><em>provider</em> &quot;aws&quot; {</pre><pre>region                  = &quot;ap-south-1&quot;</pre><pre>shared_credentials_file = &quot;C:/Users/HP/.aws/credentials&quot;</pre><pre>profile                 = &quot;default&quot;</pre><pre>}</pre><pre><em>provider</em> &quot;kubernetes&quot; {</pre><pre>host                   = aws_eks_cluster.ekscluster.endpoint</pre><pre>cluster_ca_certificate = base64decode(aws_eks_cluster.ekscluster.certificate_authority[0].data)</pre><pre>token                  = data.aws_eks_cluster_auth.eksauth.token</pre><pre>load_config_file       = false</pre><pre>}</pre><p>Terraform uses the aws provider to login to aws using the credentials file. The kubernetes provider on the other hand allows terraform to authenticate into the eks cluster and create the deployments, services etc.</p><p>2. Next part is creating the security groups.</p><pre><em>resource</em> &quot;aws_security_group&quot; &quot;secure&quot; {</pre><pre>name        = &quot;secure&quot;</pre><pre>description = &quot;Allow TLS inbound traffic&quot;</pre><pre>vpc_id      = &quot;vpc-fa9a8792&quot;</pre><pre>ingress {</pre><pre>description = &quot;SSH&quot;</pre><pre>from_port   = 22</pre><pre>to_port     = 22</pre><pre>protocol    = &quot;tcp&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>ingress {</pre><pre>description = &quot;HTTP&quot;</pre><pre>from_port   = 80</pre><pre>to_port     = 80</pre><pre>protocol    = &quot;tcp&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>ingress {</pre><pre>description = &quot;Service&quot;</pre><pre>from_port   = 30001</pre><pre>to_port     = 30001</pre><pre>protocol    = &quot;tcp&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>ingress {</pre><pre>description = &quot;Database&quot;</pre><pre>from_port   = 3306</pre><pre>to_port     = 3306</pre><pre>protocol    = &quot;tcp&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>egress {</pre><pre>from_port   = 0</pre><pre>to_port     = 0</pre><pre>protocol    = &quot;-1&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>tags = {</pre><pre>Name = &quot;wordpress&quot;</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;aws_security_group&quot; &quot;rdssecure&quot; {</pre><pre>name        = &quot;rds-secure&quot;</pre><pre>description = &quot;Allow TLS inbound traffic&quot;</pre><pre>vpc_id      = &quot;vpc-fa9a8792&quot;</pre><pre>ingress {</pre><pre>description = &quot;Database&quot;</pre><pre>from_port   = 3306</pre><pre>to_port     = 3306</pre><pre>protocol    = &quot;tcp&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>egress {</pre><pre>from_port   = 0</pre><pre>to_port     = 0</pre><pre>protocol    = &quot;-1&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>tags = {</pre><pre>Name = &quot;rds&quot;</pre><pre>}</pre><pre>}</pre><p>Two security groups will be created. One for the eks cluster and the other is for the RDS db instance. The security groups allow network traffic on some specific ports and block traffic on other ports therefore keeping the deployments secure.</p><p>3. For the backend I used MySQL 5.7. The following code snippet provisions a database instance on AWS with MySQL 5.7.</p><pre><em>resource</em> &quot;aws_db_instance&quot; &quot;rdsinstance&quot; {</pre><pre>allocated_storage    = 20</pre><pre>max_allocated_storage= 40</pre><pre>storage_type         = &quot;gp2&quot;</pre><pre>engine               = &quot;mysql&quot;</pre><pre>engine_version       = &quot;5.7&quot;</pre><pre>instance_class       = &quot;db.t2.micro&quot;</pre><pre>name                 = &quot;mydb&quot;</pre><pre>username             = &quot;admin&quot;</pre><pre>password             = &quot;abcd1234&quot;</pre><pre>parameter_group_name = &quot;default.mysql5.7&quot;</pre><pre>skip_final_snapshot  = true</pre><pre>auto_minor_version_upgrade = true</pre><pre>vpc_security_group_ids = [aws_security_group.rdssecure.id]</pre><pre>publicly_accessible = true</pre><pre>port = 3306</pre><pre>}</pre><p>4. For creating the EKS cluster an IAM role and few IAM role policy attachments are needed. This code snippet will create all the required ones.</p><pre><em>resource</em> &quot;aws_iam_role&quot; &quot;IAM&quot; {</pre><pre>name = &quot;eks-cluster-example&quot;</pre><pre>assume_role_policy = &lt;&lt;POLICY</pre><pre>{</pre><pre>&quot;Version&quot;: &quot;2012-10-17&quot;,</pre><pre>&quot;Statement&quot;: [</pre><pre>{</pre><pre>&quot;Effect&quot;: &quot;Allow&quot;,</pre><pre>&quot;Principal&quot;: {</pre><pre>&quot;Service&quot;: &quot;eks.amazonaws.com&quot;,</pre><pre>&quot;Service&quot;: &quot;ec2.amazonaws.com&quot;</pre><pre>},</pre><pre>&quot;Action&quot;: &quot;sts:AssumeRole&quot;</pre><pre>}</pre><pre>]</pre><pre>}</pre><pre>POLICY</pre><pre>}</pre><pre><em>resource</em> &quot;aws_iam_role_policy_attachment&quot; &quot;Cluster-Policy&quot; {</pre><pre>policy_arn = &quot;arn:aws:iam::aws:policy/AmazonEKSClusterPolicy&quot;</pre><pre>role       = aws_iam_role.IAM.name</pre><pre>}</pre><pre><em>resource</em> &quot;aws_iam_role_policy_attachment&quot; &quot;WorkerNodePolicy&quot; {</pre><pre>policy_arn = &quot;arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy&quot;</pre><pre>role       = aws_iam_role.IAM.name</pre><pre>}</pre><pre><em>resource</em> &quot;aws_iam_role_policy_attachment&quot; &quot;EKS_CNI_Policy&quot; {</pre><pre>policy_arn = &quot;arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy&quot;</pre><pre>role       = aws_iam_role.IAM.name</pre><pre>}</pre><pre><em>resource</em> &quot;aws_iam_role_policy_attachment&quot; &quot;EC2ContainerRegistryReadOnly&quot; {</pre><pre>policy_arn = &quot;arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly&quot;</pre><pre>role       = aws_iam_role.IAM.name</pre><pre>}</pre><p>5. Next part is cluster creation. The following code with create an EKS cluster and configure the nodes.</p><pre><em>resource</em> &quot;aws_eks_cluster&quot; &quot;ekscluster&quot; {</pre><pre>name     = &quot;eks-cluster&quot;</pre><pre>role_arn = aws_iam_role.IAM.arn</pre><pre>vpc_config {</pre><pre>subnet_ids = [&quot;subnet-3df9c355&quot;,&quot;subnet-8b076cc7&quot;]</pre><pre>security_group_ids = [aws_security_group.secure.id]</pre><pre>}</pre><pre>depends_on = [</pre><pre>aws_iam_role_policy_attachment.Cluster-Policy,</pre><pre>]</pre><pre>tags = {</pre><pre>&quot;Name&quot; = &quot; EKS-CLUSTER&quot;</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;aws_eks_node_group&quot; &quot;eks-ng&quot; {</pre><pre>cluster_name    = aws_eks_cluster.ekscluster.name</pre><pre>node_group_name = &quot;task6&quot;</pre><pre>node_role_arn   = aws_iam_role.IAM.arn</pre><pre>subnet_ids      = [&quot;subnet-3df9c355&quot;,&quot;subnet-8b076cc7&quot;]</pre><pre>instance_types  = [&quot;t2.micro&quot;]</pre><pre>scaling_config {</pre><pre>desired_size = 2</pre><pre>min_size     = 1</pre><pre>max_size     = 3</pre><pre>}</pre><pre>depends_on = [</pre><pre>aws_iam_role_policy_attachment.WorkerNodePolicy,</pre><pre>aws_iam_role_policy_attachment.EKS_CNI_Policy,</pre><pre>aws_iam_role_policy_attachment.EC2ContainerRegistryReadOnly,</pre><pre>]</pre><pre>}</pre><p>6. After the eks cluster is created kubernetes provider needs to authenticate into the cluster. The data source “aws_eks_cluster_auth” provides the token attribute which can be used in doing so.</p><pre><em>data</em> &quot;aws_eks_cluster_auth&quot; &quot;eksauth&quot; {</pre><pre>name = aws_eks_cluster.ekscluster.name</pre><pre>}</pre><p>7. The following snippets will create the deployment, pvc and service for wordpress.</p><pre><em>resource</em> &quot;kubernetes_service&quot; &quot;kubeservice&quot; {</pre><pre>metadata {</pre><pre>name = &quot;wordpress&quot;</pre><pre>labels = {</pre><pre>&quot;app&quot; = &quot;wordpress&quot;</pre><pre>}</pre><pre>}</pre><pre>spec {</pre><pre>selector = {</pre><pre>&quot;app&quot; = &quot;wordpress&quot;</pre><pre>&quot;tier&quot;= &quot;frontend&quot;</pre><pre>}</pre><pre>port {</pre><pre>port        = 80</pre><pre>node_port = 30001</pre><pre>}</pre><pre>type = &quot;LoadBalancer&quot;</pre><pre>}</pre><pre>depends_on = [aws_eks_node_group.eks-ng]</pre><pre>timeouts {</pre><pre>create = &quot;15m&quot;</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;kubernetes_persistent_volume_claim&quot; &quot;kubepvc&quot; {</pre><pre>metadata {</pre><pre>name = &quot;wordpress-pv-claim&quot;</pre><pre>labels = {</pre><pre>&quot;app&quot; = &quot;wordpress&quot;</pre><pre>}</pre><pre>}</pre><pre>spec {</pre><pre>access_modes = [&quot;ReadWriteOnce&quot;]</pre><pre>resources {</pre><pre>requests = {</pre><pre>storage = &quot;1Gi&quot;</pre><pre>}</pre><pre>}</pre><pre>}</pre><pre>depends_on = [aws_eks_node_group.eks-ng]</pre><pre>timeouts {</pre><pre>create = &quot;15m&quot;</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;kubernetes_deployment&quot; &quot;kube&quot; {</pre><pre>metadata {</pre><pre>name = &quot;wordpress&quot;</pre><pre>labels = {</pre><pre>&quot;app&quot; = &quot;wordpress&quot;</pre><pre>}</pre><pre>}</pre><pre>spec {</pre><pre>replicas = 1</pre><pre>selector {</pre><pre>match_labels = {</pre><pre>&quot;app&quot; = &quot;wordpress&quot;</pre><pre>&quot;tier&quot;= &quot;frontend&quot;</pre><pre>}</pre><pre>}</pre><pre>strategy {</pre><pre>type = &quot;Recreate&quot;</pre><pre>}</pre><pre>template {</pre><pre>metadata {</pre><pre>labels = {</pre><pre>&quot;app&quot; = &quot;wordpress&quot;</pre><pre>&quot;tier&quot;= &quot;frontend&quot;</pre><pre>}</pre><pre>}</pre><pre>spec {</pre><pre>container {</pre><pre>image = &quot;wordpress&quot;</pre><pre>name  = &quot;wordpress&quot;</pre><pre>env {</pre><pre>name = &quot;WORDPRESS_DB_NAME&quot;</pre><pre>value= aws_db_instance.rdsinstance.name</pre><pre>}</pre><pre>env {</pre><pre>name = &quot;WORDPRESS_DB_HOST&quot;</pre><pre>value= aws_db_instance.rdsinstance.endpoint</pre><pre>}</pre><pre>env {</pre><pre>name = &quot;WORDPRESS_DB_USER&quot;</pre><pre>value= aws_db_instance.rdsinstance.username</pre><pre>}</pre><pre>env {</pre><pre>name = &quot;WORDPRESS_DB_PASSWORD&quot;</pre><pre>value= aws_db_instance.rdsinstance.password</pre><pre>}</pre><pre>port {</pre><pre>container_port = 80</pre><pre>name = &quot;wordpress&quot;</pre><pre>}</pre><pre>volume_mount {</pre><pre>name = &quot;wordpress-ps&quot;</pre><pre>mount_path = &quot;/var/www/html&quot;</pre><pre>}</pre><pre>}</pre><pre>volume {</pre><pre>name = &quot;wordpress-ps&quot;</pre><pre>persistent_volume_claim {</pre><pre>claim_name = &quot;wordpress-pv-claim&quot;</pre><pre>}</pre><pre>}</pre><pre>}</pre><pre>}</pre><pre>}</pre><pre>depends_on = [aws_eks_node_group.eks-ng]</pre><pre>timeouts {</pre><pre>create = &quot;30m&quot;</pre><pre>}</pre><pre>}</pre><p>8. To automate the whole process I have used a null resource which will start the browser on my device and open wordpress. I have also added a few outputs.</p><pre><em>output</em> &quot;endpoint&quot; {</pre><pre>value = aws_eks_cluster.ekscluster.endpoint</pre><pre>}</pre><pre><em>output</em> &quot;kubeconfig-certificate-authority-data&quot; {</pre><pre>value = aws_eks_cluster.ekscluster.certificate_authority[0].data</pre><pre>}</pre><pre><em>output</em> &quot;loadbalancer&quot; {</pre><pre>value = kubernetes_service.kubeservice.load_balancer_ingress[0].hostname</pre><pre>}</pre><pre><em>output</em> &quot;db_name&quot; {</pre><pre>value = aws_db_instance.rdsinstance.name</pre><pre>}</pre><pre><em>output</em> &quot;db_endpoint&quot; {</pre><pre>value = aws_db_instance.rdsinstance.endpoint</pre><pre>}</pre><pre><em>resource</em> &quot;null_resource&quot; &quot;start-chrome&quot;  {</pre><pre>provisioner &quot;local-exec&quot; {</pre><pre>command = &quot;start chrome ${kubernetes_service.kubeservice.load_balancer_ingress[0].hostname}&quot;</pre><pre>}</pre><pre>depends_on = [</pre><pre>kubernetes_service.kubeservice,</pre><pre>kubernetes_deployment.kube</pre><pre>]</pre><pre>}</pre><p>10. Now that the code is ready, the build can be started. Open the termininal, navigate to the directory containing the code and execute the following command.</p><pre>terraform init</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*3dglHq8ZN5pIkndRtv_czg.png" /></figure><p>Now run</p><pre>terraform apply --auto-approve</pre><p>to start the build. The build will take sometime to complete.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zkucQ9N8AouoqXBSZGqZ1w.png" /><figcaption>build successful</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ysi4GpollrUpxCGahJ-Wtw.png" /><figcaption>nodes</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E9jzRzX4b-yrt58KiHH8Iw.png" /><figcaption>DB instance</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B-YMXxovCVUH5_hPQ9soJw.png" /><figcaption>eks cluster</figcaption></figure><p>Wordpress is accessible now.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8hD95kPEzE8OesUSkhFQpg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YPDDyS3lkY0N1vxM79S0Nw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3BaqVGS7S1NoS19uwrwQ3Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rBZyKF36CfpauOhu9z0llg.png" /></figure><h4>Click <a href="https://github.com/chirag0202/cloud-task6-aws-eks-wordpress-rds-terraform">here</a> for Github Repo</h4><p><strong><em>I did this activity under the mentorship of Mr. </em></strong><a href="https://www.linkedin.com/in/vimaldaga/"><strong><em>Vimal Daga</em></strong></a><strong><em> for the “</em>Hybrid Multi Cloud<em>” training under </em></strong><a href="https://www.linkedin.com/in/linuxworld/"><strong><em>LinuxWorld India</em></strong></a><strong><em>. I would like to thank Mr. </em></strong><a href="https://www.linkedin.com/in/vimaldaga/"><strong><em>Vimal Daga</em></strong></a><strong><em> Sir, </em></strong><a href="https://www.linkedin.com/in/preeti-chandak/"><strong><em>Preeti Ma’am</em></strong></a><strong><em> and the whole </em></strong><a href="https://www.linkedin.com/in/linuxworld/"><strong><em>LinuxWorld India</em></strong></a><strong><em> team for giving me this opportunity.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e19d9d8a76c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DEPLOY WEB SERVER ON AWS EC2 INSTANCE USING ANSIBLE]]></title>
            <link>https://medium.com/@chiragchaudhuri02/deploy-web-server-on-aws-ec2-instance-using-ansible-bb0f5db74be7?source=rss-6979b4140746------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb0f5db74be7</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[red-hat-training]]></category>
            <category><![CDATA[ansible]]></category>
            <category><![CDATA[ec2]]></category>
            <category><![CDATA[apache-web-server]]></category>
            <dc:creator><![CDATA[CHIRAG CHAUDHURI]]></dc:creator>
            <pubDate>Mon, 24 Aug 2020 22:42:21 GMT</pubDate>
            <atom:updated>2020-09-14T14:01:54.704Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*BVzbt-Bdr5GsmIXt9vrvvw.png" /></figure><h3>Ansible</h3><p><strong>Automation</strong> is the ultimate need for <strong>DevOps</strong> practice and ‘<strong>Automate</strong> everything’ is the key principle of <strong>DevOps</strong>. One such tool for DevOps Automation Tool is Ansible. It is a very simple yet very significant open-source software in the world of IT Automation. It can be used for software provisioning, configuration management, and application-deployment tool thus enabling infrastructure as code.</p><h3>AWS EC2</h3><p>Amazon Elastic Compute Cloud (EC2) is a part of Amazon’s cloud-computing platform, Amazon Web Services (AWS), that allows users to rent virtual computers on which to run their own computer applications. EC2 encourages scalable deployment of applications by providing a web service through which a user can boot an Amazon Machine Image (AMI) to configure a virtual machine, which Amazon calls an “instance”, containing any software desired.</p><p>In this story I have discussed how to provision and AWS EC2 instance and deploy a web server on it. So let’s begin…</p><h3>Pre-requisites:</h3><ol><li>AWS account</li><li>Linux VM with ansible installed</li><li>boto and boto3 python packages</li></ol><h3>PROCESS:</h3><p>The first and foremost task is to create a playbook that will bind the processes of creating an instance and deploying a web server. I have used the concept of roles in ansible for easier management of the whole process. I have also used vaults for the important credentials that I will be requiring for the task.</p><p><strong>Configuration file for ansible</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/352/1*eBYr860yjPLY7BDOfSkyTQ.png" /></figure><p><strong>ansible-playbook</strong></p><pre>- hosts: localhost<br>  roles:<br>          - role: launch</pre><pre>- hosts: tag_Name_Ansible<br>  remote_user: ec2-user<br>  become: yes<br>  become_user: root</pre><pre>  gather_facts: yes<br>  pre_tasks:<br>          - name: &quot;install python&quot;<br>            raw: &quot;sudo yum install python3 -y&quot;<br>  roles:<br>          - role: webserver<br>            port: 8080</pre><p>Now lets discuss about different parts of the playbook.</p><h4>Creating an Instance:</h4><ol><li>Each playbook requires one or more hosts where the task is going to be carried.</li></ol><pre>- hosts: localhost<br>  roles:<br>          - role: launch</pre><p>This segment selects localhost as the host and uses the role launch to create the instance.</p><p>2. The structure of role launch is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/477/1*OgL9xZ8phb1Q1Alxwf9g0w.png" /></figure><p>3. The main.yml file in tasks directory consists of all the tasks that creates the EC2 instance on AWS.</p><p><strong>tasks/main.yml</strong></p><pre>- name: Create ec2 key<br>  ec2_key:<br>        name: &quot;key&quot;<br>        region: &quot;ap-south-1&quot;<br>        aws_access_key: &quot;{{ access_key }}&quot;<br>        aws_secret_key: &quot;{{ secret_key }}&quot;<br>  register: ec2_key<br>- name: Save the key in a file<br>  copy: <br>        content: &quot;{{ ec2_key.key.private_key }}&quot;  <br>        dest: &quot;/root/task2/key.pem&quot;  <br>        mode: 0400<br>  when: ec2_key.changed<br>- name: ec2 on aws<br>  ec2:<br>        key_name: &quot;key&quot;<br>        instance_type: &quot;t2.micro&quot;<br>        image: &quot;ami-0ebc1ac48dfd14136&quot;<br>        wait: yes<br>        count: 1<br>        vpc_subnet_id: &quot;subnet-3df9c355&quot;<br>        instance_tags: {&quot;Name&quot;:&quot;Ansible&quot;}<br>        assign_public_ip: yes<br>        region: &quot;ap-south-1&quot;<br>        state: present<br>        group_id: &quot;sg-058677c48fa2f2f5b&quot;<br>        aws_access_key: &quot;{{ access_key }}&quot;<br>        aws_secret_key: &quot;{{ secret_key }}&quot;<br>  register: ec2<br>- name: Wait for Instance to launch<br>  wait_for:<br>         host: &quot;{{ item.public_ip }}&quot;<br>         port: 22<br>         state: started<br>  with_items: &quot;{{ ec2.instances }}&quot;<br>- name: &quot;Refresh Inventory&quot;<br>  meta: refresh_inventory</pre><p>4. The first part is to create the key and save it to a file. This file will be used to login into the instance. It is an essential part since without this key we can’t deploy web server in the instance.</p><pre>- name: Create ec2 key<br>  ec2_key:<br>        name: &quot;key&quot;<br>        region: &quot;ap-south-1&quot;<br>        aws_access_key: &quot;{{ access_key }}&quot;<br>        aws_secret_key: &quot;{{ secret_key }}&quot;<br>  register: ec2_key<br>- name: Save the key in a file<br>  copy: <br>        content: &quot;{{ ec2_key.key.private_key }}&quot;  <br>        dest: &quot;/root/task2/key.pem&quot;  <br>        mode: 0400<br>  when: ec2_key.changed</pre><p>5. The next part creates the instance according to our needs. The ec2 module does just that. It helps us in configuring the instance and then creates it.</p><pre>- name: ec2 on aws<br>  ec2:<br>        key_name: &quot;key&quot;<br>        instance_type: &quot;t2.micro&quot;<br>        image: &quot;ami-0ebc1ac48dfd14136&quot;<br>        wait: yes<br>        count: 1<br>        vpc_subnet_id: &quot;subnet-3df9c355&quot;<br>        instance_tags: {&quot;Name&quot;:&quot;Ansible&quot;}<br>        assign_public_ip: yes<br>        region: &quot;ap-south-1&quot;<br>        state: present<br>        group_id: &quot;sg-058677c48fa2f2f5b&quot;<br>        aws_access_key: &quot;{{ access_key }}&quot;<br>        aws_secret_key: &quot;{{ secret_key }}&quot;<br>  register: ec2</pre><p>6. After the instance is created it needs some time launch and get ready. For this purpose I have used the wait_for module. What this does is it creates a buffer for the instance to launch and get ready.</p><pre>- name: Wait for Instance to launch<br>  wait_for:<br>         host: &quot;{{ item.public_ip }}&quot;<br>         port: 22<br>         state: started<br>  with_items: &quot;{{ ec2.instances }}&quot;</pre><p>7. Now the inventory has to be refreshed.</p><pre>- name: &quot;Refresh Inventory&quot;<br>  meta: refresh_inventory</pre><p>8. In the ec2 module I have used variables access_key and secret_key. These variable are declared in the main.yml file under vars directory. For better security I have used vaults to encrypt it. A vault can be created using the following command.</p><pre>ansible-vault create foo.yml</pre><p>Next we move on to the part of deploying the web server.</p><h4>Deploying Web Server</h4><ol><li>The host for this part is not localhost. Instead the EC2 instance is the host now. To have the host directly added to our inventory we need to apply the concept of ansible dynamic inventory. For this the EC2 instances added to our inventory using the ec2.py(It can be downloaded from <a href="https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py">here</a>) file. To make this plugin work we have to export a few parameters throuth the shell.</li></ol><pre>export <strong>AWS_ACCESS_KEY_ID=</strong>&#39;AK123&#39;<br>export <strong>AWS_SECRET_ACCESS_KEY=</strong>&#39;abc123&#39;<br>export <strong>AWS_REGION=</strong>&#39;region-id&#39;</pre><ol><li>Since the instance is tagged as “Ansible” in the ec2 module of the role “launch”, it can be referred to as “tag_Name_Ansible”.</li></ol><pre>- hosts: tag_Name_Ansible</pre><p>3. Different instances may have different logging in credentials but, here I have used the Amazon Linux 2 ami so, the username “ec2-user” has to be used. In the first part we have created a key for logging in to the instance. This has to be mentioned in the private_key_file section of the ansible configuration file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/352/1*eBYr860yjPLY7BDOfSkyTQ.png" /></figure><p>4. By default the ec2-user does not have a lot of powers. To solve this problem the concept of privilege escalation is used.</p><pre>become: yes<br>become_user: root</pre><p>5. The part below gathers a few facts about the host(EC2 instance in this case) and installs python3 since it is the latest version of python.</p><pre>  gather_facts: yes<br>  pre_tasks:<br>          - name: &quot;install python&quot;<br>            raw: &quot;sudo yum install python3 -y&quot;</pre><p>6. Next in line is the part of roles. I have created the role webserver to configure deploy the webserver. I have created a custom configuration for the webserver but this is not necessary. According to the configuration the webserver will listen on port number 8080.</p><pre>roles:<br>          - role: webserver<br>            port: 8080</pre><p>The role webserver has the following structure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/440/1*STK0Ixjdn2iNfajf7_hB_Q.png" /></figure><p>7. In this case the main.yml under vars folder contains the variable declarations for port and credentials. This file is also encrypted using vaults.</p><p>8. Since I am using a custom configuration for web server a configuration file is needed. This is specified in the localconf.conf.j2 file under templates directory.</p><p><strong>localconf.conf.j2</strong></p><pre>Listen {{port}}<br>&lt;VirtualHost {{ ansible_default_ipv4.address }}:{{port}}&gt;<br>DocumentRoot &quot;/var/www/chirag&quot;<br>&lt;Directory &quot;/var/www/chirag&quot;&gt;<br>  AllowOverride All<br>  Require all granted<br>&lt;/Directory&gt;<br>&lt;/VirtualHost&gt;</pre><p>This template is used to create and copy the final configuration file for web server. The benefit is that we are able to use variables which help us in specifying parameters in the config file which are initialized during run time. The ansible_default_ipv4.address variable is used to specify the IP address at which the webpage will be available. The <strong>template</strong> files will usually have the . j2 extension, which denotes the Jinja2 templating engine used.</p><p>9. Now coming to the main.yml under tasks directory.</p><p><strong>tasks/main.yml</strong></p><pre>- name: &quot;Install Webserver&quot;<br>  package:<br>          name: &quot;httpd&quot;<br>          state: present<br>  register: status</pre><pre>- name: &quot;Create Document Root&quot;<br>  file:<br>          path: &quot;/var/www/chirag&quot;<br>          state: directory<br>  tags: webgit</pre><pre>- name: &quot;Start Webserver&quot;<br>  service:<br>          name: httpd<br>          state: started</pre><pre>- name: &quot;Configure Webserver&quot;<br>  template:<br>          src: templates/localconf.conf.j2<br>          dest: /etc/httpd/conf.d/web.conf<br>          mode: &#39;0666&#39;<br>  when: status.rc == 0<br>  tags: webconf<br>  notify: service httpd</pre><pre>- name: &quot;Get webpage from Github&quot;<br>  get_url:<br>          url: &quot;<a href="https://raw.githubusercontent.com/chirag0202/project/master/index.html">https://raw.githubusercontent.com/chirag0202/project/master/index.html</a>&quot;<br>          dest: &quot;/var/www/chirag&quot;<br>          mode: &#39;0666&#39;<br>  tags: webgit</pre><p>10. The first task is to install the apache web server.</p><pre>- name: &quot;Install Webserver&quot;<br>  package:<br>          name: &quot;httpd&quot;<br>          state: present<br>  register: status</pre><p>11. Next in line is creating and specifying the document root directory.</p><pre>- name: &quot;Create Document Root&quot;<br>  file:<br>          path: &quot;/var/www/chirag&quot;<br>          state: directory<br>  tags: webgit</pre><p>12. Starting the web server.</p><pre>- name: &quot;Start Webserver&quot;<br>  service:<br>          name: httpd<br>          state: started</pre><p>13. Here comes the part of configuring the web server. The template for the configuration file is already created and now it can be used to create the final configuration file.</p><pre>- name: &quot;Configure Webserver&quot;<br>  template:<br>          src: templates/localconf.conf.j2<br>          dest: /etc/httpd/conf.d/web.conf<br>          mode: &#39;0666&#39;<br>  when: status.rc == 0<br>  tags: webconf<br>  notify: service httpd</pre><p>14. Now the web server has to be restarted. The notify clause in (13) triggers the following handler and restarts the web server. The handler is specified in the handlers/main.yml file.</p><p><strong>handlers/main.yml</strong></p><pre>- name: &quot;service httpd&quot;<br>  service:<br>          name: httpd<br>          state: restarted</pre><p>15. The final part is to download the web page from github.</p><pre>- name: &quot;Get webpage from Github&quot;<br>  get_url:<br>          url: &quot;<a href="https://raw.githubusercontent.com/chirag0202/project/master/index.html">https://raw.githubusercontent.com/chirag0202/project/master/index.html</a>&quot;<br>          dest: &quot;/var/www/chirag&quot;<br>          mode: &#39;0666&#39;<br>  tags: webgit</pre><p>16. Everything is configured and the build can be started. Navigate to the directory where the playbook is located and run the following command.</p><pre>ansible-playbook --vault-id aws@prompt --vault-id chirag@prompt webserver.yml</pre><p>Since I have used 2 vaults, I had to mention their vault IDs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rZ_zZTSbQqqvYMht--7xUw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bFwPQzLnReTLUUrL9n6YCQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lyryZ_o16Jv2OPW2EwPNxQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4WwWxg2AWkg0wMp0Vf-kkw.png" /></figure><p>The web page can be found at &lt;public-ip-of-instance&gt;:&lt;port-number&gt;.</p><h4>I did this project under the mentorship of Mr. <a href="https://www.linkedin.com/in/vimaldaga/">Vimal Daga</a> during the (RH294) Automation With Ansible Training by <a href="https://www.linkedin.com/in/linuxworld/">LinuxWorld India</a>. Thank you <a href="https://www.linkedin.com/in/vimaldaga/">Vimal</a> Sir, <a href="https://www.linkedin.com/in/preeti-chandak/">Preeti</a> Ma’am and <a href="https://www.linkedin.com/in/linuxworld/">LinuxWorld India</a> for giving me this opportunity.</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb0f5db74be7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DEPLOY WEBSERVER ON DOCKER USING ANSIBLE]]></title>
            <link>https://medium.com/@chiragchaudhuri02/deploy-webserver-on-docker-using-ansible-c48b8a050d8b?source=rss-6979b4140746------2</link>
            <guid isPermaLink="false">https://medium.com/p/c48b8a050d8b</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[ansible]]></category>
            <category><![CDATA[red-hat-training]]></category>
            <category><![CDATA[apache-web-server]]></category>
            <dc:creator><![CDATA[CHIRAG CHAUDHURI]]></dc:creator>
            <pubDate>Sun, 02 Aug 2020 22:27:38 GMT</pubDate>
            <atom:updated>2020-08-02T22:38:28.871Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/690/1*lLPAWfbRvr9dHd5Y2yeCEQ.jpeg" /></figure><h3>Ansible:</h3><p><strong>Ansible</strong> is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration</p><h3>Docker:</h3><p><strong>Docker</strong> is a set of platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels. All containers are run by a single operating system kernel and therefore use fewer resources than virtual machines.</p><p>Now you must have got a basic idea about what ansible and docker is. In this story I’ll demonstrate how to launch webserver on Docker container using ansible. This can be done in a few ways but I’ll demonstrate the following:</p><ol><li>Directly interact with Docker daemon and launch the webserver.</li><li>Use Kubernetes to launch the same on docker.</li></ol><p><strong>**This story does not cover installation of ansible on the controller node.</strong></p><h3>Pre-requisites:</h3><ol><li>Ansible installed and configured.</li><li>Kubernetes Cluster(I have used minikube for this story).</li></ol><h3>Process:</h3><h4>Direct interaction with the docker daemon</h4><ol><li>First we have to specify the hosts(The hosts should have been already added to the inventory of ansible configuration file) where the tasks have to be carried out.</li></ol><pre>- hosts: worker</pre><p>In my case the hosts have been grouped under worker in the inventory. I have configured only 1 node for this task but multiple ones can be configured to do the same.</p><p>2. I have used a bunch of variables as it gives it flexibility.</p><pre>vars:<br>  - pname: &quot;docker-ce-18.09.1-3.el7.x86_64&quot;<br>    default_container_name: webserver<br>    default_container_image: httpd:latest</pre><p><strong>Now I shall specify the tasks.</strong></p><p>3. We need to create a docker.repo for docker installation.</p><pre>- name: &quot;Docker&quot;<br>    yum_repository:<br>        name: docker<br>        description: docker YUM repo<br>        baseurl: <a href="https://download.docker.com/linux/centos/7/x86_64/stable/">https://download.docker.com/linux/centos/7/x86_64/stable/</a><br>        gpgcheck: no</pre><p>4. Next comes the package installation part that will install Docker on our node.</p><pre>- package:<br>      name: &quot;{{ pname }}&quot;<br>      state: present</pre><p>5. To interact with the docker daemon ansible needs the docker SDK for python.</p><pre>- name: Install Docker Module for Python<br>    pip:<br>      name: docker-py</pre><p>6. Now that Docker is installed and SDK is downloaded we can start the service.</p><pre>- service:<br>      name: &quot;docker&quot;<br>      state: started</pre><p>7. Now we have to pull the image from Docker Hub. I have used the httpd official image here.</p><pre>- name: Pull default Docker image<br>    docker_image:<br>      name: &quot;{{ default_container_image }}&quot;<br>      source: pull</pre><p>8. A webserver is absolutely useless if there is no webpage to show. So I am first copying my webpage from the controller node to the worker node and then I will mount it on the docker container as a volume.</p><pre>- copy:<br>      src: &quot;index.html&quot;<br>      dest: &quot;/root/html&quot;<br>    ignore_errors: yes</pre><p>9. Now that everything is done we can launch the container.</p><pre>- name: Create default containers<br>    docker_container: <br>      name: &quot;{{ default_container_name }}&quot;<br>      image: &quot;{{ default_container_image }}&quot;<br>      state: present<br>      exposed_ports:<br>       - 80/tcp<br>       - 80/udp<br>      published_ports:<br>       - 0.0.0.0:8086:80/tcp<br>       - 0.0.0.0:8086:80/udp<br>      volumes:<br>       - /root/html/:/usr/local/apache2/htdocs/</pre><p>Then on the terminal rin the following command</p><pre>ansible-playbook &lt;playbook-name&gt;</pre><p>This will launch the container and we will be able to see the webpage at http://&lt;ip for worker node&gt;:&lt;target port(8086 here)&gt;.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zlKSXfk_xJiZKaGXZlagww.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wEZsTIkmfTEQHE9GUUF4Sw.png" /><figcaption>Webpage</figcaption></figure><p>I have used a very simple html file as my webpage. You can do loads of other modifications as per your needs.</p><h4>Using Kubernetes</h4><p>Kubernetes is an open-source container-orchestration system for automating computer application deployment, scaling, and management. It was originally designed by Google and is now maintained by the Cloud Native Computing Foundation.</p><p>Kubernetes has it own script for writing manifest files. Just like ansible it also uses yaml language.</p><p>I have created a webos.yml manifest file that will create configure the webserver.</p><p><strong>webos.yml</strong></p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: webos<br>  namespace: deploy<br>  labels:<br>    app: webos<br>spec:<br>  ports:<br>    - port: 80<br>      nodePort: 30001<br>  selector:<br>    app: webos<br>  type: NodePort</pre><pre>---</pre><pre>apiVersion: v1<br>kind: PersistentVolumeClaim<br>metadata:<br>  name: webos-pv-claim<br>  namespace: deploy<br>  labels:<br>    app: webos<br>spec:<br>  accessModes:<br>  - ReadWriteOnce<br>  resources:<br>    requests:<br>      storage: 20Gi</pre><pre>---</pre><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: webos<br>  namespace: deploy<br>  labels:<br>    app: webos<br>spec:<br>  selector:<br>    matchLabels:<br>      app: webos<br>  template:<br>    metadata:<br>      namespace: deploy<br>      labels:<br>        app: webos<br>    spec:<br>      containers:<br>      - image: httpd:latest<br>        name: webos<br>        ports:<br>        - containerPort: 80<br>          name: webos<br>        volumeMounts:<br>        - name: webos-ps<br>          mountPath: /usr/local/apache2/htdocs<br>      volumes:<br>      - name: webos-ps<br>        persistentVolumeClaim:<br>          claimName: webos-pv-claim</pre><p>Now lets move on to the portion of ansible.</p><ol><li>Start the minikube VM.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fwGRNgfW9o-IbaBeYi7z1Q.png" /></figure><p>2. Select the hosts. I have used the same worker group in this case.</p><pre>- hosts: worker</pre><p>3. Create the tasks. To use kubernetes we have to configure kubectl on the worker node.</p><pre>- name: Kubectl<br>     shell: curl -LO &quot;<a href="https://storage.googleapis.com/kubernetes-release/release/$(curl">https://storage.googleapis.com/kubernetes-release/release/$(curl</a> -s <a href="https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl">https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl</a>&quot;<br>   - name: kubectl permission<br>     shell: chmod +x ./kubectl<br>   - name: kubectl bin<br>     shell: mv -f ./kubectl /usr/local/bin/kubectl<br>     ignore_errors: yes</pre><p>4. Kubectl needs a kubernetes config file. Using ansible copy the config file and all the certificates and keys to the worker node. Here all of them have been bundled under the kubecnf directory.</p><pre>   - copy:<br>      src: &quot;/root/kubecnf&quot;<br>      dest: &quot;/root&quot;<br>     ignore_errors: yes<br>   - name: kubeconfig<br>     shell: mv -u /root/kubecnf/.kube /root/<br>     ignore_errors: yes</pre><p>5. Copy the code for webpage to the worker node.</p><pre>   - copy:<br>      src: &quot;/root/task1/index.html&quot;<br>      dest: &quot;/root&quot;<br>     ignore_errors: yes</pre><p>6. Ansible needs the Kubernetes and Openshift module to interact with the kubernetes cluster. Install them using pip.</p><pre>- name: Install kubernetes Module for Python<br>     pip:<br>      name: kubernetes</pre><pre>- name: Install Openshift Module for Python<br>     pip:<br>      name: Openshift</pre><p>7. Create a custom Namespace under which the pods, services etc. will be lauched.</p><pre>- name: Create a k8s namespace<br>     k8s:<br>      name: deploy<br>      api_version: v1<br>      kind: Namespace<br>      state: present</pre><p>8. Create the deployment. The lookup plugin will take the kubernetes manifest file from your controller and create the deployment, pvc, service etc.</p><pre>- name: Create a Deployment by reading the definition from a local file<br>     k8s:<br>      state: present<br>      definition: &quot;{{ lookup(&#39;file&#39;, &#39;/root/task1/webos.yml&#39;) }}&quot;</pre><p>9. Now we can copy the code to the pod. Since the deployment takes some time to create, a buffer spanning few seconds will give the deployment enough time to create before copying the code to the pods.</p><pre>- name: buffer<br>     shell: sleep 20</pre><pre>- name: kubectl cp<br>     shell: kubectl cp /root/index.html deploy/&quot;$(kubectl get pod -n deploy|grep webos|awk &#39;{print $1}&#39;)&quot;:/usr/local/apache2/htdocs/</pre><p>10. Use the following command to launch the whole setup.</p><pre>ansible-playbook &lt;playbook-name&gt;</pre><p>Webserver will be launched at http://&lt;minikube ip&gt;:&lt;nodeport&gt; .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/949/1*eXk9oFJMeUisgJft3pC4LQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wEZsTIkmfTEQHE9GUUF4Sw.png" /></figure><h4>I did this under the mentorship of Mr. <a href="https://www.linkedin.com/in/vimaldaga/">Vimal Daga</a> during (RH294) Redhat Ansible Training by <a href="https://www.linkedin.com/in/linuxworld/">LinuxWorld India.</a> Thank You <a href="https://www.linkedin.com/in/vimaldaga/">Vimal</a> Sir, <a href="https://www.linkedin.com/in/preeti-chandak/">Preeti</a> Ma’am and the whole <a href="https://www.linkedin.com/in/linuxworld/">LinuxWorld India</a> Team For giving me this opportunity.</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c48b8a050d8b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DEPLOYING WORDPRESS AND MYSQL ON CUSTOM VPC WITH NAT USING TERRAFORM]]></title>
            <link>https://medium.com/@chiragchaudhuri02/depwordpress-and-mysql-on-custom-vpc-with-nat-using-terraform-e5195901845a?source=rss-6979b4140746------2</link>
            <guid isPermaLink="false">https://medium.com/p/e5195901845a</guid>
            <category><![CDATA[vpc]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[wordpress]]></category>
            <category><![CDATA[nat]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[CHIRAG CHAUDHURI]]></dc:creator>
            <pubDate>Fri, 24 Jul 2020 12:19:43 GMT</pubDate>
            <atom:updated>2020-07-24T12:38:01.083Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*ykg7aB6dTUoz_BPB4qns0g.jpeg" /></figure><p>If you check my previous story, you can see that it had a similar to this one. The only difference is that the instances in the private subnet(MySQL) do not have any internet connectivity. This story will show you how to setup the whole thing with internet connectivity on both private and public subnets.</p><p>Click <a href="https://medium.com/@chiragchaudhuri02/deploying-wordpress-and-mysql-on-aws-with-custom-vpc-using-terraform-677bdfaa9992?source=friends_link&amp;sk=14bb564613c381a98aae28e0af9e2070">here</a> to see my previous story.</p><p>Internet connectivity on private subnets can be achieved using NAT gateways.</p><h3>NAT Gateway</h3><p>A <strong>NAT gateway </strong>gives cloud resources without public IP addresses access to the internet without exposing those resources to incoming internet connections.</p><h3>Pre-requisites:-</h3><ol><li>AWS account</li><li>AWS CLI installed and configured</li><li>Terraform installed and added to path</li><li>VSCode with HashiCorp Terraform and Terraform Autocomplete plugins installed</li></ol><h3>Process:-</h3><ol><li>First we need to create the provider. Our provider is AWS therefore, we select AWS with other relevant information like the credentials.</li></ol><pre><em>provider</em> &quot;aws&quot; <br>{<br>region                  = &quot;ap-south-1&quot;<br>shared_credentials_file = &quot;C:/Users/KIIT/.aws/credentials&quot;<br>profile                 = &quot;chirag&quot;<br>}</pre><p>2. Next we shall create the key pairs which shall be used to remotely login to the instances.</p><pre><em>resource</em> &quot;tls_private_key&quot; &quot;keypair&quot; {</pre><pre>algorithm   = &quot;RSA&quot;</pre><pre>}</pre><pre><em>resource</em> &quot;local_file&quot; &quot;privatekey&quot; {</pre><pre>content     = tls_private_key.keypair.private_key_pem</pre><pre>filename = &quot;key1.pem&quot;</pre><pre>}</pre><pre><em>resource</em> &quot;aws_key_pair&quot; &quot;deployer&quot; {</pre><pre>key_name   = &quot;key1.pem&quot;</pre><pre>public_key = tls_private_key.keypair.public_key_openssh</pre><pre>}</pre><p>3. We shall now start with the VPC creation. The VPC will have many parts namely subnets, ip-routing tables for each subnet, internet gateway for the public subnet, nat gateway for the private subnet, etc.</p><pre><em>resource</em> &quot;aws_vpc&quot; &quot;main&quot; {</pre><pre>cidr_block       = &quot;192.168.0.0/16&quot;</pre><pre>instance_tenancy = &quot;default&quot;</pre><pre>tags = {</pre><pre>Name = &quot;myvpc&quot;</pre><pre>}</pre><pre>}</pre><p>4. A subnet is like a sub-network within an even bigger network(VPC). In this case we shall create 2 subnets under the VPC we just created. One will be a public subnet while the other one is going to be a private one. We shall create the Wordpress instance in the public instance as it requires internet connectivity. MySQL instance will be created in private subnet as we don’t want it to be exposed to the outside world.</p><p>5. We shall create the following Routes and Routing tables(A routing table is a data table stored in a router or a network host that lists the routes to particular network destinations, and in some cases, metrics associated with those routes.).</p><pre><em>resource</em> &quot;aws_route_table&quot; &quot;routetable1&quot; {</pre><pre>vpc_id    = aws_vpc.main.id</pre><pre>}</pre><pre><em>resource</em> &quot;aws_route&quot; &quot;route1&quot; {</pre><pre>route_table_id         = aws_route_table.routetable1.id</pre><pre>destination_cidr_block = &quot;0.0.0.0/0&quot;</pre><pre>gateway_id = aws_internet_gateway.intgw.id</pre><pre>}</pre><pre><em>resource</em> &quot;aws_route_table_association&quot; &quot;routetableassoc1&quot; {</pre><pre>subnet_id      = aws_subnet.subnet1.id</pre><pre>route_table_id = aws_route_table.routetable1.id</pre><pre>}</pre><pre><em>resource</em> &quot;aws_route_table&quot; &quot;routetable2&quot; {</pre><pre>vpc_id    = aws_vpc.main.id</pre><pre>}</pre><pre><em>resource</em> &quot;aws_route&quot; &quot;route2&quot; {</pre><pre>route_table_id         = aws_route_table.routetable2.id</pre><pre>destination_cidr_block = &quot;0.0.0.0/0&quot;</pre><pre>instance_id= aws_instance.wordpress.id</pre><pre>}</pre><pre><em>resource</em> &quot;aws_route_table_association&quot; &quot;routetableassoc2&quot; {</pre><pre>subnet_id      = aws_subnet.subnet2.id</pre><pre>route_table_id = aws_route_table.routetable2.id</pre><pre>}</pre><pre><em>resource</em> &quot;aws_subnet&quot; &quot;subnet1&quot; {</pre><pre>vpc_id            = aws_vpc.main.id</pre><pre>availability_zone = &quot;ap-south-1a&quot;</pre><pre>cidr_block        = &quot;192.168.1.0/24&quot;</pre><pre>map_public_ip_on_launch=&quot;true&quot;</pre><pre>tags              = map(&quot;Name&quot;,&quot;Subnet1&quot;)</pre><pre>}</pre><pre><em>resource</em> &quot;aws_subnet&quot; &quot;subnet2&quot; {</pre><pre>vpc_id            = aws_vpc.main.id</pre><pre>availability_zone = &quot;ap-south-1a&quot;</pre><pre>cidr_block        = &quot;192.168.2.0/24&quot;</pre><pre>map_public_ip_on_launch=&quot;false&quot;</pre><pre>tags              = map(&quot;Name&quot;,&quot;Subnet2&quot;)</pre><pre>}</pre><p>6. 1 internet gateway and 1 NAT gateway will be created to give internet connectivity to the public and private subnet respectively.</p><pre><em>resource</em> &quot;aws_internet_gateway&quot; &quot;intgw&quot; {</pre><pre>vpc_id = aws_vpc.main.id</pre><pre>tags = {</pre><pre>Name = &quot;main&quot;</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;aws_eip&quot; &quot;nat&quot; {</pre><pre>vpc = true</pre><pre>depends_on                = [aws_internet_gateway.intgw]</pre><pre>}</pre><pre><em>resource</em> &quot;aws_nat_gateway&quot; &quot;natgw&quot; {</pre><pre>allocation_id = aws_eip.nat.id</pre><pre>subnet_id     = aws_subnet.subnet2.id</pre><pre>depends_on = [aws_internet_gateway.intgw]</pre><pre>tags = {</pre><pre>Name = &quot;gw NAT&quot;</pre><pre>}</pre><pre>}</pre><p>7. Security Groups are very important as they filter out any undesirable traffic to the instances. For both the instances we shall create 1 security group each that will prevent any undesirable connection to and from the instances.</p><pre><em>resource</em> &quot;aws_security_group&quot; &quot;secure1&quot; {</pre><pre>name          = &quot;secure1&quot;</pre><pre>vpc_id        = aws_vpc.main.id</pre><pre>description   = &quot;Allow TLS inbound traffic&quot;</pre><pre>ingress {</pre><pre>description = &quot;SSH&quot;</pre><pre>from_port   = 22</pre><pre>to_port     = 22</pre><pre>protocol    = &quot;tcp&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>ingress {</pre><pre>description = &quot;HTTP&quot;</pre><pre>from_port   = 80</pre><pre>to_port     = 80</pre><pre>protocol    = &quot;tcp&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>ingress {</pre><pre>description = &quot;WORDPRESS&quot;</pre><pre>from_port   = 443</pre><pre>to_port     = 443</pre><pre>protocol    = &quot;tcp&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>egress {</pre><pre>from_port   = 0</pre><pre>to_port     = 0</pre><pre>protocol    = &quot;-1&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>tags = {</pre><pre>Name = &quot;subnet1sg&quot;</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;aws_security_group&quot; &quot;secure2&quot; {</pre><pre>name          = &quot;secure2&quot;</pre><pre>vpc_id        = aws_vpc.main.id</pre><pre>description   = &quot;Allow TLS inbound traffic&quot;</pre><pre>ingress {</pre><pre>description = &quot;SSH&quot;</pre><pre>from_port   = 22</pre><pre>to_port     = 22</pre><pre>protocol    = &quot;tcp&quot;</pre><pre>cidr_blocks = [aws_subnet.subnet1.cidr_block]</pre><pre>}</pre><pre>ingress {</pre><pre>description = &quot;MYSQL&quot;</pre><pre>from_port   = 3306</pre><pre>to_port     = 3306</pre><pre>protocol    = &quot;tcp&quot;</pre><pre>cidr_blocks = [aws_subnet.subnet1.cidr_block]</pre><pre>}</pre><pre>egress {</pre><pre>from_port   = 0</pre><pre>to_port     = 0</pre><pre>protocol    = &quot;-1&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>tags = {</pre><pre>Name = &quot;subnet2sg&quot;</pre><pre>}</pre><pre>}</pre><p>8. Now that all the components of the VPC are created we shall create the instances.</p><pre><em>resource</em> &quot;aws_instance&quot;  &quot;wordpress&quot; {</pre><pre>ami           = &quot;ami-7e257211&quot;</pre><pre>instance_type = &quot;t2.micro&quot;</pre><pre>subnet_id     = aws_subnet.subnet1.id</pre><pre>key_name  = aws_key_pair.deployer.key_name</pre><pre>security_groups =  [aws_security_group.secure1.id]</pre><pre>vpc_security_group_ids = [aws_security_group.secure1.id]</pre><pre>associate_public_ip_address = true</pre><pre>depends_on = [aws_internet_gateway.intgw]</pre><pre>tags = {</pre><pre>Name = &quot;Wordpress&quot;</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;aws_instance&quot;  &quot;mysql&quot; {</pre><pre>ami           = &quot;ami-0979674e4a8c6ea0c&quot;</pre><pre>instance_type = &quot;t2.micro&quot;</pre><pre>subnet_id     = aws_subnet.subnet2.id</pre><pre>key_name      = aws_key_pair.deployer.key_name</pre><pre>security_groups =  [aws_security_group.secure2.id]</pre><pre>vpc_security_group_ids = [aws_security_group.secure2.id]</pre><pre>associate_public_ip_address = false</pre><pre>tags = {</pre><pre>Name = &quot;Mysql&quot;</pre><pre>}</pre><pre>}</pre><p>9. We shall create a separate file output.tf that will display some of the important info like instance IDs, IPs etc. This file will also launch the website on chrome.</p><p><strong>output.tf</strong></p><pre><em>resource</em> &quot;null_resource&quot; &quot;publicip&quot;  {</pre><pre>provisioner &quot;local-exec&quot; {</pre><pre>command = &quot;echo  ${aws_instance.wordpress.public_ip} &gt; publicip.txt&quot;</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;null_resource&quot; &quot;privateip&quot;  {</pre><pre>provisioner &quot;local-exec&quot; {</pre><pre>command = &quot;echo  ${aws_instance.mysql.private_ip} &gt; privateip.txt&quot;</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;null_resource&quot; &quot;nulllocal1&quot;  {</pre><pre>provisioner &quot;local-exec&quot; {</pre><pre>command = &quot;start chrome  ${aws_instance.wordpress.public_ip}&quot;</pre><pre>}</pre><pre>depends_on = [</pre><pre>aws_instance.wordpress</pre><pre>]</pre><pre>}</pre><pre><em>output</em> &quot;wordpressid&quot;{</pre><pre>value=aws_instance.wordpress.id</pre><pre>}</pre><pre><em>output</em> &quot;mysqlid&quot;{</pre><pre>value=aws_instance.mysql.id</pre><pre>}</pre><pre><em>output</em> &quot;eipid&quot;{</pre><pre>value=aws_eip.nat.id</pre><pre>}</pre><pre><em>output</em> &quot;eippublicip&quot;{</pre><pre>value=  aws_eip.nat.public_ip</pre><pre>}</pre><p>10. Use the following code in terminal to launch the setup.</p><pre>terraform init<br>terraform apply -auto-approve</pre><p>Terraform init will install the required plugins and it is a compulsory command when executing terraform for 1st time in the folder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*muaxvN89NMvSnwVc1bN4Hg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zlXQGLYzkkn6RjK1zkBVfA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kYRK30kja6-j5Z2BTkFi1g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wRx3hBkWoa8bY8W2aqx01g.png" /></figure><h3>Voila! Your setup will be deployed.</h3><p>Click <a href="https://github.com/chirag0202/wordpress-mysql-terraform-vpc-nat-aws"><strong>here</strong></a> for the github repo.</p><h4>I did this task under the mentorship of Mr.<a href="https://www.linkedin.com/in/vimaldaga/"> Vimal Daga.</a> I would like to thank <a href="https://www.linkedin.com/in/vimaldaga/">Vimal</a> Sir, <a href="https://www.linkedin.com/in/preeti-chandak/">Preeti</a> Ma’am and <a href="https://www.linkedin.com/in/linuxworld/">LinuxWorld India </a>for giving me this ooportunity.</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e5195901845a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DEPLOYING WORDPRESS AND MYSQL ON AWS WITH CUSTOM VPC USING TERRAFORM]]></title>
            <link>https://medium.com/@chiragchaudhuri02/deploying-wordpress-and-mysql-on-aws-with-custom-vpc-using-terraform-677bdfaa9992?source=rss-6979b4140746------2</link>
            <guid isPermaLink="false">https://medium.com/p/677bdfaa9992</guid>
            <category><![CDATA[wordpress]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[vpc]]></category>
            <dc:creator><![CDATA[CHIRAG CHAUDHURI]]></dc:creator>
            <pubDate>Wed, 22 Jul 2020 07:03:46 GMT</pubDate>
            <atom:updated>2020-07-24T12:06:01.252Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*wc5Xxi-cyoU17h5vrtO4wQ.png" /></figure><h3>What is Amazon VPC?</h3><p>Amazon Virtual Private Cloud is a commercial cloud computing service that provides users a virtual private cloud, by “provisioning a logically isolated section of Amazon Web Services Cloud”.</p><h3>Wordpress</h3><p>WordPress is a free and open-source content management system written in PHP and paired with a MySQL or MariaDB database.</p><h3>Pre-requisites:-</h3><ol><li>AWS account</li><li>AWS CLI installed and configured</li><li>Terraform installed and added to path</li><li>VSCode with HashiCorp Terraform and Terraform Autocomplete plugins installed</li></ol><h3>Process:-</h3><ol><li>First we need to create the provider. Our provider is AWS therefore, we select AWS with other relevant information like the credentials.</li></ol><pre><em>provider</em> &quot;aws&quot; {</pre><pre>region                  = &quot;ap-south-1&quot;</pre><pre>shared_credentials_file = &quot;C:/Users/KIIT/.aws/credentials&quot;</pre><pre>profile                 = &quot;chirag&quot;</pre><pre>}</pre><p>2. Next we shall create the key pairs which shall be used to remotely login to the instances.</p><pre><em>resource</em> &quot;tls_private_key&quot; &quot;keypair&quot; {</pre><pre>algorithm   = &quot;RSA&quot;</pre><pre>}</pre><pre><em>resource</em> &quot;local_file&quot; &quot;privatekey&quot; {</pre><pre>content     = tls_private_key.keypair.private_key_pem</pre><pre>filename = &quot;key1.pem&quot;</pre><pre>}</pre><pre><em>resource</em> &quot;aws_key_pair&quot; &quot;deployer&quot; {</pre><pre>key_name   = &quot;key1.pem&quot;</pre><pre>public_key = tls_private_key.keypair.public_key_openssh</pre><pre>}</pre><p>3. We shall now start with the VPC creation. The VPC will have many parts namely subnets, ip-routing tables for each subnet, internet gateway for the public subnet etc.</p><pre><em>resource</em> &quot;aws_vpc&quot; &quot;main&quot; {</pre><pre>cidr_block       = &quot;192.168.0.0/16&quot;</pre><pre>instance_tenancy = &quot;default&quot;</pre><pre>tags = {</pre><pre>Name = &quot;myvpc&quot;</pre><pre>}</pre><pre>}</pre><p>4. A subnet is like a sub-network within an even bigger network(VPC). In this case we shall create 2 subnets under the VPC we just created. One will be a public subnet while the other one is going to be a private one. We shall create the Wordpress instance in the public instance as it requires internet connectivity. Since MySQL doesn’t need an outside connection, it will be launched in private subnet. It only needs to have connection with the wordpress instance.</p><p>5. We shall create the following Routes and Routing tables(A routing table is a data table stored in a router or a network host that lists the routes to particular network destinations, and in some cases, metrics associated with those routes.).</p><pre><em>resource</em> &quot;aws_route_table&quot; &quot;routetable1&quot; {</pre><pre>vpc_id    = aws_vpc.main.id</pre><pre>}</pre><pre><em>resource</em> &quot;aws_route&quot; &quot;route1&quot; {</pre><pre>route_table_id         = aws_route_table.routetable1.id</pre><pre>destination_cidr_block = &quot;0.0.0.0/0&quot;</pre><pre>gateway_id = aws_internet_gateway.intgw.id</pre><pre>}</pre><pre><em>resource</em> &quot;aws_route_table_association&quot; &quot;routetableassoc1&quot; {</pre><pre>subnet_id      = aws_subnet.subnet1.id</pre><pre>route_table_id = aws_route_table.routetable1.id</pre><pre>}</pre><pre><em>resource</em> &quot;aws_route_table&quot; &quot;routetable2&quot; {</pre><pre>vpc_id    = aws_vpc.main.id</pre><pre>}</pre><pre><em>resource</em> &quot;aws_route&quot; &quot;route2&quot; {</pre><pre>route_table_id         = aws_route_table.routetable2.id</pre><pre>destination_cidr_block = &quot;0.0.0.0/0&quot;</pre><pre>instance_id= aws_instance.wordpress.id</pre><pre>}</pre><pre><em>resource</em> &quot;aws_route_table_association&quot; &quot;routetableassoc2&quot; {</pre><pre>subnet_id      = aws_subnet.subnet2.id</pre><pre>route_table_id = aws_route_table.routetable2.id</pre><pre>}</pre><p>6. For the public subnet to have internet connectivity it needs an internet gateway to be attached with the routing table for the respective subnet.</p><pre><em>resource</em> &quot;aws_internet_gateway&quot; &quot;intgw&quot; {</pre><pre>vpc_id = aws_vpc.main.id</pre><pre>tags = {</pre><pre>Name = &quot;main&quot;</pre><pre>}</pre><pre>}</pre><p>7. Security Groups are very important as they filter out any undesirable traffic to the instances. For both the instances we shall create 1 security group each that will prevent any undesirable connection to and from the instances.</p><pre><em>resource</em> &quot;aws_security_group&quot; &quot;secure1&quot; {</pre><pre>name          = &quot;secure1&quot;</pre><pre>vpc_id        = aws_vpc.main.id</pre><pre>description   = &quot;Allow TLS inbound traffic&quot;</pre><pre>ingress {</pre><pre>description = &quot;SSH&quot;</pre><pre>from_port   = 22</pre><pre>to_port     = 22</pre><pre>protocol    = &quot;tcp&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>ingress {</pre><pre>description = &quot;HTTP&quot;</pre><pre>from_port   = 80</pre><pre>to_port     = 80</pre><pre>protocol    = &quot;tcp&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>ingress {</pre><pre>description = &quot;WORDPRESS&quot;</pre><pre>from_port   = 443</pre><pre>to_port     = 443</pre><pre>protocol    = &quot;tcp&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>egress {</pre><pre>from_port   = 0</pre><pre>to_port     = 0</pre><pre>protocol    = &quot;-1&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>tags = {</pre><pre>Name = &quot;subnet1sg&quot;</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;aws_security_group&quot; &quot;secure2&quot; {</pre><pre>name          = &quot;secure2&quot;</pre><pre>vpc_id        = aws_vpc.main.id</pre><pre>description   = &quot;Allow TLS inbound traffic&quot;</pre><pre>ingress {</pre><pre>description = &quot;SSH&quot;</pre><pre>from_port   = 22</pre><pre>to_port     = 22</pre><pre>protocol    = &quot;tcp&quot;</pre><pre>cidr_blocks = [aws_subnet.subnet1.cidr_block]</pre><pre>}</pre><pre>ingress {</pre><pre>description = &quot;MYSQL&quot;</pre><pre>from_port   = 3306</pre><pre>to_port     = 3306</pre><pre>protocol    = &quot;tcp&quot;</pre><pre>cidr_blocks = [aws_subnet.subnet1.cidr_block]</pre><pre>}</pre><pre>egress {</pre><pre>from_port   = 0</pre><pre>to_port     = 0</pre><pre>protocol    = &quot;-1&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>tags = {</pre><pre>Name = &quot;subnet2sg&quot;</pre><pre>}</pre><pre>}</pre><p>8. Now that all the components of the VPC are created we shall create the instances.</p><pre><em>resource</em> &quot;aws_instance&quot;  &quot;wordpress&quot; {</pre><pre>ami           = &quot;ami-7e257211&quot;</pre><pre>instance_type = &quot;t2.micro&quot;</pre><pre>subnet_id     = aws_subnet.subnet1.id</pre><pre>key_name  = aws_key_pair.deployer.key_name</pre><pre>security_groups =  [aws_security_group.secure1.id]</pre><pre>vpc_security_group_ids = [aws_security_group.secure1.id]</pre><pre>associate_public_ip_address = true</pre><pre>depends_on = [aws_internet_gateway.intgw]</pre><pre>tags = {</pre><pre>Name = &quot;Wordpress&quot;</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;aws_instance&quot;  &quot;mysql&quot; {</pre><pre>ami           = &quot;ami-0979674e4a8c6ea0c&quot;</pre><pre>instance_type = &quot;t2.micro&quot;</pre><pre>subnet_id     = aws_subnet.subnet2.id</pre><pre>key_name      = aws_key_pair.deployer.key_name</pre><pre>security_groups =  [aws_security_group.secure2.id]</pre><pre>vpc_security_group_ids = [aws_security_group.secure2.id]</pre><pre>associate_public_ip_address = false</pre><pre>tags = {</pre><pre>Name = &quot;Mysql&quot;</pre><pre>}</pre><pre>}</pre><p>9. We shall create a separate file output.tf that will display some of the important info like instance IDs, IPs etc. This file will also launch the website on chrome.</p><p><strong>output.tf</strong></p><pre><em>resource</em> &quot;null_resource&quot; &quot;publicip&quot;  {</pre><pre>provisioner &quot;local-exec&quot; {</pre><pre>command = &quot;echo  ${aws_instance.wordpress.public_ip} &gt; publicip.txt&quot;</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;null_resource&quot; &quot;privateip&quot;  {</pre><pre>provisioner &quot;local-exec&quot; {</pre><pre>command = &quot;echo  ${aws_instance.mysql.private_ip} &gt; privateip.txt&quot;</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;null_resource&quot; &quot;nulllocal1&quot;  {</pre><pre>provisioner &quot;local-exec&quot; {</pre><pre>command = &quot;start chrome  ${aws_instance.wordpress.public_ip}&quot;</pre><pre>}</pre><pre>depends_on = [</pre><pre>aws_instance.wordpress</pre><pre>]</pre><pre>}</pre><pre><em>output</em> &quot;wordpressid&quot;{</pre><pre>value=aws_instance.wordpress.id</pre><pre>}</pre><pre><em>output</em> &quot;mysqlid&quot;{</pre><pre>value=aws_instance.mysql.id</pre><pre>}</pre><p>10. Use the following code in terminal to launch the setup.</p><pre>terraform init<br>terraform apply -auto-approve</pre><p>Terraform init will install the required plugins and it is a compulsory command when executing terraform for 1st time in the folder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U2J4RO0v3_VL8sX0pLOrFg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zlXQGLYzkkn6RjK1zkBVfA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bT1wN59U1fKWRxsm1eD_bg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wRx3hBkWoa8bY8W2aqx01g.png" /></figure><h4>Voila! Your setup will be deployed.</h4><h4>I did this task under the mentorship of Mr.<a href="https://www.linkedin.com/in/vimaldaga/"> Vimal Daga.</a> I would like to thank <a href="https://www.linkedin.com/in/vimaldaga/">Vimal</a> Sir, <a href="https://www.linkedin.com/in/preeti-chandak/">Preeti</a> Ma’am and <a href="https://www.linkedin.com/in/linuxworld/">LinuxWorld India </a>for giving me this ooportunity.</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=677bdfaa9992" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[AUTOMATING WEBSERVER DEPLOYMENT ON AWS USING TERRAFORM]]></title>
            <link>https://medium.com/@chiragchaudhuri02/automating-webserver-deployment-on-aws-using-terraform-6a4cad5bd1e5?source=rss-6979b4140746------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a4cad5bd1e5</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[elastic-file-system]]></category>
            <category><![CDATA[cloudfront]]></category>
            <category><![CDATA[s3-bucket]]></category>
            <category><![CDATA[terraform]]></category>
            <dc:creator><![CDATA[CHIRAG CHAUDHURI]]></dc:creator>
            <pubDate>Wed, 22 Jul 2020 03:11:28 GMT</pubDate>
            <atom:updated>2020-07-22T03:11:28.063Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*WCXUqu-Lgeo6ZiUzlXnTLg.png" /></figure><p>Hello again everyone! Through this story, I’m going to explain step by step the process of automating deployment of webserver using terraform.</p><p>I am going to use few AWS services like EC2, EFS, S3 and Cloudfront. So, a bit of knowledge about these concepts will help in better understanding of the setup.</p><h3>Pre-requisites:-</h3><ol><li>AWS account</li><li>Terraform installed and added to the path</li><li>AWS CLI</li><li>VSCode with Hashicorp terraform and terraform autocomplete plugins installed</li></ol><h3>Process:-</h3><ol><li>Configure AWS CLI using the command</li></ol><pre>aws configure</pre><p>Then enter the credentials of your AWS account.</p><p>2. Now we can start creating the terraform files. Open VSCode and create a new folder. In the folder create a main.tf fie. We will use this file to setup the AWS provider and create the key pairs. The key pairs will help in remotely connecting to the EC2 instance using SSH.</p><p><strong>main.tf</strong></p><pre><em>provider</em> &quot;aws&quot; {</pre><pre>region                  = &quot;ap-south-1&quot;</pre><pre>shared_credentials_file = &quot;C:/Users/KIIT/.aws/credentials&quot;</pre><pre>profile                 = &quot;chirag&quot;</pre><pre>}</pre><pre><em>resource</em> &quot;tls_private_key&quot; &quot;keypair&quot; {</pre><pre>algorithm   = &quot;RSA&quot;</pre><pre>}</pre><pre><em>resource</em> &quot;local_file&quot; &quot;privatekey&quot; {</pre><pre>content     = tls_private_key.keypair.private_key_pem</pre><pre>filename = &quot;key1.pem&quot;</pre><pre>}</pre><pre><em>resource</em> &quot;aws_key_pair&quot; &quot;deployer&quot; {</pre><pre>key_name   = &quot;key1.pem&quot;</pre><pre>public_key = tls_private_key.keypair.public_key_openssh</pre><pre>}</pre><p>3. We will now create 2 security groups that will allow NFS, SSH and http access to the instance. The security groups have been named as secure and secure1. For the security groups we have created a separate security.yf file.</p><p><strong>security.tf</strong></p><pre><em>resource</em> &quot;aws_security_group&quot; &quot;secure&quot; {</pre><pre>name        = &quot;secure&quot;</pre><pre>description = &quot;Allow TLS inbound traffic&quot;</pre><pre>ingress {</pre><pre>description = &quot;SSH&quot;</pre><pre>from_port   = 22</pre><pre>to_port     = 22</pre><pre>protocol    = &quot;tcp&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>ingress {</pre><pre>description = &quot;HTTP&quot;</pre><pre>from_port   = 80</pre><pre>to_port     = 80</pre><pre>protocol    = &quot;tcp&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>egress {</pre><pre>from_port   = 0</pre><pre>to_port     = 0</pre><pre>protocol    = &quot;-1&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>tags = {</pre><pre>Name = &quot;httpos1&quot;</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;aws_security_group&quot; &quot;secure1&quot; {</pre><pre>name        = &quot;secure1&quot;</pre><pre>description = &quot;Allow TLS inbound traffic&quot;</pre><pre>ingress {</pre><pre>description = &quot;NFS&quot;</pre><pre>from_port   = 2049</pre><pre>to_port     = 2049</pre><pre>protocol    = &quot;tcp&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>egress {</pre><pre>from_port   = 0</pre><pre>to_port     = 0</pre><pre>protocol    = &quot;-1&quot;</pre><pre>cidr_blocks = [&quot;0.0.0.0/0&quot;]</pre><pre>}</pre><pre>tags = {</pre><pre>Name = &quot;NFS&quot;</pre><pre>}</pre><pre>}</pre><p>4. We will now create the EC2 instance and EFS. The EFS will mounted to the /var/www/html folder as the website code will be stored here only. Benefit of EFS is that it is available across the network and can be mounted on any instance in the same network.</p><p>5. The efs.tf file will create the EFS and attach it the subnet where the instance will be created. To mount the EFS we will have to remotely login to the EC2 instance. The instance.tf will create, configure the EC2 instance and mount the EFS to the respective folder</p><p><strong>efs.tf</strong></p><pre><em>resource</em> &quot;aws_efs_file_system&quot; &quot;EFS&quot; {</pre><pre>creation_token = &quot;my-product&quot;</pre><pre>tags = {</pre><pre>Name = &quot;MyEFS&quot;</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;aws_efs_mount_target&quot; &quot;alpha&quot; {</pre><pre>file_system_id = aws_efs_file_system.EFS.id</pre><pre>subnet_id      = aws_instance.instance.subnet_id</pre><pre>}</pre><p><strong>instance.tf</strong></p><pre><em>resource</em> &quot;aws_instance&quot;  &quot;instance&quot; {</pre><pre>ami           = &quot;ami-052c08d70def0ac62&quot;</pre><pre>instance_type = &quot;t2.micro&quot;</pre><pre>key_name  = aws_key_pair.deployer.key_name</pre><pre>security_groups =  [ &quot;secure&quot;,&quot;secure1&quot; ]</pre><pre>connection {</pre><pre>type     = &quot;ssh&quot;</pre><pre>user     = &quot;ec2-user&quot;</pre><pre>private_key = tls_private_key.keypair.private_key_pem</pre><pre>host     = aws_instance.instance.public_ip</pre><pre>}</pre><pre>provisioner &quot;remote-exec&quot; {</pre><pre>inline = [</pre><pre>&quot;sudo yum install httpd  -y&quot;,</pre><pre>&quot;sudo yum install amazon-efs-utils -y&quot;,</pre><pre>&quot;sudo yum install nfs-utils -y&quot;,</pre><pre>&quot;sudo yum install php -y&quot;,</pre><pre>&quot;sudo yum install git -y&quot;,</pre><pre>&quot;sudo systemctl start httpd&quot;,</pre><pre>&quot;sudo systemctl enable httpd&quot;,</pre><pre>]</pre><pre>}</pre><pre>tags = {</pre><pre>Name = &quot;httpos1&quot;</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;null_resource&quot; &quot;nullremote3&quot;  {</pre><pre>depends_on = [</pre><pre>aws_efs_mount_target.alpha,</pre><pre>null_resource.nulllocal5</pre><pre>]</pre><pre>connection {</pre><pre>type     = &quot;ssh&quot;</pre><pre>user     = &quot;ec2-user&quot;</pre><pre>private_key = tls_private_key.keypair.private_key_pem</pre><pre>host     = aws_instance.instance.public_ip</pre><pre>}</pre><pre>provisioner &quot;remote-exec&quot; {</pre><pre>inline = [</pre><pre>&quot;sleep 40&quot;,</pre><pre>&quot;sudo chmod ugo+rw /etc/fstab&quot;,</pre><pre>&quot;sudo echo &#39;${aws_efs_file_system.EFS.id}:/ /var/www/html efstls,_netdev&#39; &gt;&gt; /etc/fstab&quot;,</pre><pre>&quot;sudo mount -a -t efs,nfs4 defaults&quot;,</pre><pre>&quot;sudo rm -rf /var/www/html/*&quot;,</pre><pre>&quot;sudo git clone https://github.com/chirag0202/cloudtask2-html.git /var/www/html/&quot;</pre><pre>]</pre><pre>}</pre><pre>}</pre><p>6. Now since EFS and instance creation is complete we will create a S3 bucket and a cloudfront distribution that will redirect to the bucket. We have set the access-control for S3 bucket and its objects as “public-readable”. The cloudfront.tf file will create an S3 bucket, a cloudfront distribution, S3 policy and a data source for IAM policy. It will also upload the images for the website to the bucket.</p><p><strong>cloudfront.tf</strong></p><pre><em>resource</em> &quot;aws_s3_bucket&quot; &quot;bucket&quot; {</pre><pre>bucket = &quot;imgsource1&quot;</pre><pre>acl    = &quot;public-read&quot;</pre><pre>tags = {</pre><pre>Name = &quot;imgsource1&quot;</pre><pre>}</pre><pre>}</pre><pre><em>locals</em> {</pre><pre>s3_origin_id = &quot;S3-imgsource1&quot;</pre><pre>}</pre><pre><em>resource</em> &quot;aws_cloudfront_origin_access_identity&quot; &quot;origin_access_identity&quot; {</pre><pre>comment = &quot;Some comment&quot;</pre><pre>}</pre><pre><em>resource</em> &quot;aws_cloudfront_distribution&quot; &quot;s3_distribution&quot; {</pre><pre>origin {</pre><pre>domain_name = aws_s3_bucket.bucket.bucket_regional_domain_name</pre><pre>origin_id   = local.s3_origin_id</pre><pre>s3_origin_config {</pre><pre>origin_access_identity = aws_cloudfront_origin_access_identity.origin_access_identity.cloudfront_access_identity_path</pre><pre>}</pre><pre>}</pre><pre>enabled             = true</pre><pre>is_ipv6_enabled     = true</pre><pre>default_cache_behavior {</pre><pre>allowed_methods  = [&quot;GET&quot;, &quot;HEAD&quot;]</pre><pre>cached_methods   = [&quot;GET&quot;, &quot;HEAD&quot;]</pre><pre>target_origin_id = local.s3_origin_id</pre><pre>forwarded_values {</pre><pre>query_string = false</pre><pre>cookies {</pre><pre>forward = &quot;none&quot;</pre><pre>}</pre><pre>}</pre><pre>viewer_protocol_policy = &quot;allow-all&quot;</pre><pre>min_ttl                = 0</pre><pre>default_ttl            = 3600</pre><pre>max_ttl                = 86400</pre><pre>}</pre><pre>price_class = &quot;PriceClass_All&quot;</pre><pre>restrictions {</pre><pre>geo_restriction {</pre><pre>restriction_type = &quot;none&quot;</pre><pre>}</pre><pre>}</pre><pre>tags = {</pre><pre>Environment = &quot;production&quot;</pre><pre>}</pre><pre>viewer_certificate {</pre><pre>cloudfront_default_certificate = true</pre><pre>}</pre><pre>}</pre><pre><em>data</em> &quot;aws_iam_policy_document&quot; &quot;s3_policy&quot; {</pre><pre>statement {</pre><pre>actions   = [&quot;s3:GetObject&quot;]</pre><pre>resources = [&quot;${aws_s3_bucket.bucket.arn}/*&quot;]</pre><pre>principals {</pre><pre>type        = &quot;AWS&quot;</pre><pre>identifiers = [aws_cloudfront_origin_access_identity.origin_access_identity.iam_arn]</pre><pre>}</pre><pre>}</pre><pre>statement {</pre><pre>actions   = [&quot;s3:ListBucket&quot;]</pre><pre>resources = [aws_s3_bucket.bucket.arn]</pre><pre>principals {</pre><pre>type        = &quot;AWS&quot;</pre><pre>identifiers = [aws_cloudfront_origin_access_identity.origin_access_identity.iam_arn]</pre><pre>}</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;aws_s3_bucket_policy&quot; &quot;bucketpolicy&quot; {</pre><pre>bucket = aws_s3_bucket.bucket.id</pre><pre>policy = data.aws_iam_policy_document.s3_policy.json</pre><pre>}</pre><pre><em>resource</em> &quot;aws_s3_bucket_object&quot; &quot;object&quot; {</pre><pre>bucket = aws_s3_bucket.bucket.bucket</pre><pre>key    = &quot;file.jpg&quot;</pre><pre>source = &quot;image/file.jpg&quot;</pre><pre>acl    = &quot;public-read&quot;</pre><pre>etag = filemd5(&quot;image/file.jpg&quot;)</pre><pre>}</pre><p>7. We have now completed majority of the steps. To achieve complete automation we will have integrated the steps of creating webpage and pushing it to the GitHub repo with Terraform. The code also inserts the cloudfront distribution URL for the image into the source code for the webpage.</p><p>8. The terraform code in the file miscellaneous.tf does all of what is mentioned in point 7 and also deploys the website on chrome.</p><p><strong>miscellaneous.tf</strong></p><pre><em>resource</em> &quot;null_resource&quot; &quot;nulllocal4&quot;  {</pre><pre>provisioner &quot;local-exec&quot; {</pre><pre>command = &quot;echo  ${aws_instance.instance.public_ip} &gt; publicip.txt&quot;</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;null_resource&quot; &quot;nulllocal5&quot;  {</pre><pre>depends_on = [</pre><pre>aws_cloudfront_distribution.s3_distribution</pre><pre>]</pre><pre>provisioner &quot;local-exec&quot; {</pre><pre>working_dir=&quot;C:/Users/KIIT/Desktop/Hybrid Cloud/tera/Cloudtask2-html&quot;</pre><pre>command = &lt;&lt;EOT</pre><pre>del /f index.html</pre><pre>echo &quot;&lt;html&gt;&lt;h1&gt;Hybrid Multi Cloud&lt;/h1&gt;&lt;br&gt;&lt;img src=http://${aws_cloudfront_distribution.s3_distribution.domain_name}/file.jpg width=&quot;640&quot; height=&quot;443&quot;&gt;&lt;/html&gt;&quot; &gt; index.html</pre><pre>git add .</pre><pre>git commit -m change</pre><pre>git push</pre><pre>EOT</pre><pre>interpreter = [&quot;PowerShell&quot;,&quot;-Command&quot;]</pre><pre>}</pre><pre>}</pre><pre><em>resource</em> &quot;null_resource&quot; &quot;nulllocal3&quot;  {</pre><pre>depends_on = [</pre><pre>null_resource.nullremote3,</pre><pre>]</pre><pre>provisioner &quot;local-exec&quot; {</pre><pre>command = &quot;start chrome  ${aws_instance.instance.public_ip}&quot;</pre><pre>}</pre><pre>}</pre><pre><em>output</em> &quot;instanceid&quot;{</pre><pre>value=aws_instance.instance.id</pre><pre>}</pre><pre><em>output</em> &quot;efsid&quot;{</pre><pre>value=aws_efs_file_system.EFS.id</pre><pre>}</pre><pre><em>output</em> &quot;originaccessidentity&quot;{</pre><pre>value=aws_cloudfront_origin_access_identity.origin_access_identity.cloudfront_access_identity_path</pre><pre>}</pre><p>9. After the terraform code has been created we can start the build. Run the following commands in the terminals to start the build.</p><pre>terraform init</pre><p>This command will initialize terraform in the folder and install all the necessary plugins. This is a compulsory command when executing the code for the first time.</p><pre>terraform validate</pre><p>Use this to check if the code is syntactically correct.</p><pre>terraform apply -auto-approve</pre><p>Finally, use this command to start the build.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Th6QbzgYbVhgJHhGvfAfdQ.png" /></figure><p><a href="https://drive.google.com/file/d/1BvbUKCtzHFy2hEIsOY3-DEKOg5kOspDs/preview">Cloudtask 2.mp4</a></p><p>This video shows the building process till the site is deployed.</p><p>10. Voila! The site is deployed.</p><pre>terraform destroy -auto-approve</pre><p>Use this command to destroy the build.</p><p>Click <a href="https://github.com/chirag0202/cloudtask2-aws-terraform-efs-webserver"><strong>here</strong></a> for GitHub repo</p><h4><strong><em>I did this task under the mentorship of Mr. </em></strong><a href="https://www.linkedin.com/in/vimaldaga/"><strong><em>Vimal Daga</em></strong></a><strong><em>. All thanks to Mr. </em></strong><a href="https://www.linkedin.com/in/vimaldaga/"><strong><em>Vimal Daga</em></strong></a><strong><em> Sir, </em></strong><a href="https://www.linkedin.com/in/preeti-chandak/"><strong><em>Preeti</em></strong></a><strong><em> Ma’am and the whole </em></strong><a href="https://www.linkedin.com/in/linuxworld/"><strong><em>LinuxWorld India</em></strong></a><strong><em> team for giving me this opportunity.</em></strong></h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a4cad5bd1e5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DEPLOY WEBSERVER ON KUBERNETES USING JENKINSFILE APPROACH]]></title>
            <link>https://medium.com/@chiragchaudhuri02/deploy-webserver-on-kubernetes-using-jenkinsfile-approach-f4d5e520a94c?source=rss-6979b4140746------2</link>
            <guid isPermaLink="false">https://medium.com/p/f4d5e520a94c</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[jenkins]]></category>
            <category><![CDATA[jenkinsfile]]></category>
            <category><![CDATA[apache-web-server]]></category>
            <dc:creator><![CDATA[CHIRAG CHAUDHURI]]></dc:creator>
            <pubDate>Sun, 12 Jul 2020 22:58:38 GMT</pubDate>
            <atom:updated>2020-07-12T22:58:38.954Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kkPP-oHYBMkipYrAeOT0pw.jpeg" /></figure><h3>Jenkinsfile</h3><p>A Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline and is checked into source control.</p><p>This Story explains deployment of a website using apache webserver on Kubernetes. There are a lot of other ways to do the same but, here I have used Jenkinsfile approach.</p><h3>Demo</h3><h4>Pre-requisites:</h4><ol><li>Jenkins</li><li>Minikube</li><li>Job-DSL plugin for Jenkins</li><li>Build-Result Trigger plugin for Jenkins</li><li>Github plugin for Jenkins</li></ol><p>This story does not cover the installation of these softwares and plugins.</p><h4>Process:</h4><ol><li>First we need to create the DSL script for the job. The DSL script will create 4 jobs. DSL scripts use groovy syntax and the script has been saved to a .groovy file and uploaded to Github. Create a seed job to pull the script from github repo and use the job DSL plugin to deploy the setup. On deploying the seed job for 1st time you may face an error that say Script not approved. Go to<strong> Manage Jenkins</strong>&gt;<strong>In-Process Script Approval</strong> and approve the script. You have to do this each time you update the script. DSL script:-</li></ol><pre>job(&quot;Pull code from Github Repo&quot;){<br>              description(&quot;The job will pull the code from github repo and save it to a folder&quot;)</pre><pre>              scm{<br>                    github(&#39;chirag0202/project&#39;,&#39;master&#39;)<br>                  }</pre><pre>              triggers{<br>                        githubPush()<br>                        }<br>                steps{<br>                      shell(&quot;sudo cp -rvf index.html /task6&quot;)<br>                      }<br>         <br>}<br>job(&quot;Deploy code on web server&quot;){<br>              description(&quot;The job will deploy the code on a webserver&quot;)</pre><pre>                  scm{<br>                    github(&#39;chirag0202/webserver-kube&#39;,&#39;master&#39;)<br>                  }</pre><pre>                  triggers{<br>                        upstream(&quot;Pull code from Github Repo&quot;)<br>                        }</pre><pre>                  steps{<br>                      shell(readFileFromWorkspace(&quot;job2.sh&quot;))<br>                      }</pre><pre>                  steps{<br>                      shell(readFileFromWorkspace(&quot;job2-2.sh&quot;))<br>                      }<br>          <br>}<br>job(&quot;Test Webserver&quot;){<br>              description(&quot;The job will test the webserver&quot;)</pre><pre>              triggers{<br>                        upstream(&quot;Deploy code on web server&quot;)<br>                        }</pre><pre>              steps{<br>                      shell(readFileFromWorkspace(&quot;job3.sh&quot;))<br>                      }<br>          <br>}<br>job(&quot;Send Email&quot;){<br>              description(&quot;Send email to developer if job 3 fails&quot;)</pre><pre>              triggers{<br>                buildResult(&#39;* * * * *&#39;) {<br>                   combinedJobs()<br>                   triggerInfo(&#39;Test Webserver&#39;, BuildResult.UNSTABLE, BuildResult.FAILURE)<br>        }<br>      }<br>                steps{<br>                      shell(&quot;python3 /mail/mail.py&quot;)<br>                      }</pre><pre>}<br>buildPipelineView(&#39;Deploy Webserver Using Groovy&#39;) {<br>    title(&#39;Deploy Webserver Using Groovy&#39;)<br>    displayedBuilds(4)<br>    selectedJob(&#39;Pull code from Github Repo&#39;)<br>    alwaysAllowManualTrigger()<br>    showPipelineParameters()<br>}<br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I9NfkrM63KPxa7k5jA0aUQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qpzFlu58fY3qEdE9ufjPhA.png" /></figure><p>2. Job 1 will pull the code for Website from SCM(Github) when the developer has pushed the code to the repository and copy it to a separate directory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ECaJjredJDzYSqELOc82KA.png" /></figure><p>3. Job 2 will deploy the webserver on Kubernetes. The build steps for Jobs 2 and 3 are saved in 3 shell scripts.</p><p><strong>Kubernetes Manifest for Webserver</strong></p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: webtest<br>  labels:<br>    app: webtest<br>spec:<br>  ports:<br>  - port: 80<br>    nodePort: 30003<br>  selector:<br>    app: webtest<br>    tier: production<br>  type: NodePort<br>---<br>apiVersion: v1<br>kind: PersistentVolumeClaim<br>metadata:<br>  name: webtest-pv-claim<br>  labels: <br>    app: webtest<br>spec:<br>  accessModes:<br>    - ReadWriteOnce<br>  resources:<br>    requests:<br>      storage: 1Gi<br>---<br>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: webtest<br>  labels:<br>    app: webtest<br>spec:<br>  selector:<br>   matchLabels:<br>     app: webtest<br>     tier: production<br>  strategy:<br>    type: Recreate<br>  template:<br>    metadata:<br>      labels:<br>        app: webtest<br>        tier: production<br>    spec:<br>       containers:<br>       - image: vimal13/apache-webserver-php<br>         name: webtest<br>         ports:<br>         - containerPort: 80<br>           name: webtest<br>         volumeMounts:<br>         - name: webtest-pv<br>           mountPath: /var/www/html<br>       volumes:<br>       - name: webtest-pv<br>         persistentVolumeClaim:<br>           claimName: webtest-pv-claim</pre><p><strong>Kustomization.yaml</strong></p><pre>apiVersion: kustomize.config.k8s.io/v1beta1<br>kind: Kustomization<br>resources:<br>  - http.yaml</pre><p><strong>Groovy Script 1 for Job 2</strong></p><pre>if sudo /usr/local/bin/kubectl get all --kubeconfig /root/.kube/config |grep webtest<br>then<br>sudo /usr/local/bin/kubectl delete all --all --kubeconfig /root/.kube/config <br>sudo /usr/local/bin/kubectl delete pvc --all --kubeconfig /root/.kube/config <br>sudo /usr/local/bin/kubectl create -k . --kubeconfig /root/.kube/config <br>else<br>sudo /usr/local/bin/kubectl create -k . --kubeconfig /root/.kube/config <br>fi</pre><p><strong>Groovy Script 2 for Job 2</strong></p><pre>sleep 15<br>sudo /usr/local/bin/kubectl cp /task6/* $(sudo /usr/local/bin/kubectl get pod --kubeconfig /root/.kube/config|grep webtest| awk &#39;{print $1}&#39;):/var/www/html --kubeconfig /root/.kube/config <br>sudo /usr/local/bin/kubectl get all --kubeconfig /root/.kube/config</pre><p>The sleep 15 command acts as a buffer which allows the webserver to successfully deploy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hTgaBNjyE-DkfT46_Vt7NQ.png" /></figure><p>4. Job 3 tests if there is any error in the deployment. The website will be available at <strong>http://&lt;minikube ip&gt;:&lt;port no.&gt;</strong></p><p><strong>Groovy Script for Job 3</strong></p><pre>status=$(sudo curl -s -o /dev/null -w &quot;%{http_code}&quot; <a href="http://192.168.99.106:30003/index.html">http://192.168.99.106:30003/index.html</a>)<br>if status==200<br>then<br>exit 0<br>else <br>exit 1<br>fi</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*buMTDoxtVkNzgo2YzILgLg.png" /></figure><p>5. If webserver deployment fails or Job 3 build is unstable, Job 4 will send an email to the Developer. I have used a simple python script to send the email. .</p><p><strong>Python Script for Job 4</strong></p><pre>import smtplib<br>from email.mime.multipart import MIMEMultipart<br>from email.mime.text import MIMEText<br>mail_content = &#39;Webserver deployment failed.&#39;<br>sender_address = &lt;mail id of sender&gt;<br>sender_pass = &lt;password&gt;<br>receiver_address = &lt;mail id of receiver&gt;<br>#Setup the MIME<br>message = MIMEMultipart()<br>message[&#39;From&#39;] = sender_address<br>message[&#39;To&#39;] = receiver_address<br>message[&#39;Subject&#39;] = &#39;A test mail sent by Python. It has an attachment.&#39;   #The subject line<br>#The body and the attachments for the mail<br>message.attach(MIMEText(mail_content, &#39;plain&#39;))<br>#Create SMTP session for sending the mail<br>session = smtplib.SMTP(&#39;smtp.gmail.com&#39;, 587) #use gmail with port<br>session.starttls() #enable security<br>session.login(sender_address, sender_pass) #login with mail_id and password<br>text = message.as_string()<br>session.sendmail(sender_address, receiver_address, text)<br>session.quit()<br>print(&#39;Mail Sent&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0qSu1RdchvZySU6OZ1IRHQ.png" /></figure><p>The DSL script has set up BuildResultTrigger Plugin which will trigger job 4 if webserver build fails.</p><p>6. The DSL script also creates a Build Pipeline View that shows the build status of all the jobs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*abm-cKxCKd2UQbnl02pdlg.png" /></figure><p>7. If the build is successful the website is deployed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FMLNGUkXbVpIk5yP_t-2AA.png" /></figure><p>8. If the website build fails or the build is unstable the developer will receive this email.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*QcX3ydUgEngUcBy0lgOrlQ.jpeg" /></figure><h4><a href="https://github.com/chirag0202/webserver-kube">Github repo</a> for Kubernetes Manifest File.</h4><h4><a href="https://github.com/chirag0202/deploy-webserver-using-jenkinsfile">Github repo</a> for Jenkinsfile.</h4><h4><a href="https://github.com/chirag0202/project">Github repo</a> for Website.</h4><p><strong><em>This Story is my task 6 for the DevOps: Assembly Lines training by LinuxWorld India that I did under the mentorship of Mr. </em></strong><a href="https://www.linkedin.com/in/vimaldaga/"><strong><em>Vimal Daga</em></strong></a><strong><em> Sir. Thank you </em></strong><a href="https://www.linkedin.com/in/vimaldaga/"><strong><em>Vimal</em></strong></a><strong><em> Sir, </em></strong><a href="https://www.linkedin.com/in/preeti-chandak/"><strong><em>Preeti</em></strong></a><strong><em> Ma’am and the whole </em></strong><a href="https://www.linkedin.com/in/linuxworld/"><strong><em>LinuxWorld India</em></strong></a><strong><em> for giving me this opportunity.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f4d5e520a94c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DEPLOYING NEXTCLOUD ON AWS USING AMAZON EKS]]></title>
            <link>https://medium.com/@chiragchaudhuri02/deploying-nextcloud-on-aws-using-amazon-eks-f71516aa06aa?source=rss-6979b4140746------2</link>
            <guid isPermaLink="false">https://medium.com/p/f71516aa06aa</guid>
            <category><![CDATA[nextcloud]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[amazon-eks]]></category>
            <dc:creator><![CDATA[CHIRAG CHAUDHURI]]></dc:creator>
            <pubDate>Wed, 08 Jul 2020 00:30:49 GMT</pubDate>
            <atom:updated>2020-07-08T00:30:49.909Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*EW-5IJ3TJX1q8GnRNI_P-Q.jpeg" /></figure><h3>Amazon Elastic Kubernetes Service</h3><p><strong>Amazon Elastic Kubernetes Service (Amazon EKS)</strong> is a fully managed Kubernetes service. Customers such as Intel, Snap, Intuit, GoDaddy, and Autodesk trust EKS to run their most sensitive and mission critical applications because of its security, reliability, and scalability.</p><h3>Nextcloud</h3><p><strong>Nextcloud</strong> is a suite of client-server software for creating and using file hosting services. Nextcloud is free and open-source, which means that anyone is allowed to install and operate it on their own private server devices.</p><h3>Demo</h3><h4>Pre-requisites</h4><ol><li>AWS account</li><li>AWS CLI</li><li>eksctl</li><li>kubectl</li></ol><p>These softwares have to be downloaded from the internet and the path has to be added to the system variables.</p><h4>Process</h4><ol><li>First we need to create a Kubernetes cluster on AWS using EKS. There a three ways to do that but, we’ll be using the CLI option. You’ll need an IAM user with Administrator Access or the root user.</li><li>Open Command Prompt on Windows OS or any other equivalent program depending on which OS you are using.</li></ol><pre>aws configure</pre><p>Use this code and enter the user details along with the region id to login to AWS using CLI.</p><p>3. Now we can create the kubernetes cluster. Create a cluster.yml file that boasts the following code.</p><p><strong>cluster.yml</strong></p><pre>apiVersion: eksctl.io/v1alpha5<br>kind: ClusterConfig<br>metadata:<br> name: mycluster<br> region: ap-south-1</pre><pre>nodeGroups:<br> — name: ng1<br> desiredCapacity: 2<br> instanceType: t2.micro<br> ssh:<br> publicKeyName: keycloud<br> — name: ng-mixed<br> minSize: 1<br> maxSize: 3<br> instancesDistribution:<br> maxPrice: 0.017<br> instanceTypes: [“t2.micro”] # At least one instance type should be specified<br> onDemandBaseCapacity: 0<br> onDemandPercentageAboveBaseCapacity: 50<br> spotInstancePools: 2 <br> ssh:<br> publicKeyName: keycloud</pre><p>This cluster configuration file will create 2 nodegroups namely ng1 and ng-mixed. To start creating the cluster execute the cluster.yml file.</p><pre>eksctl create cluster -f cluster.yml</pre><p>The cluster building process will take some time to finish.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/1*ABPpkwcDs1a5-w94P05wIw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o_CxiEpygbdpXVEufPKaEA.png" /></figure><p>4. Once the cluster is created update the kubeconfig file.</p><pre>aws eks update-kubeconfig --name mycluster</pre><p>View your kubernetes config file using</p><pre>kubectl config view</pre><p>5. We shall now create the mariadb_deploy.yml file. The file consists of 3 parts- Service, PVC and Deployment. Deployment consists of the replicaset, container specifications and image details. The PVC will create a request for a persistent volume of size 1GiB. This persistent volume uses EBS( Elastic Block Storage) to store the data. The volume is mounted to the “/var/lib/mysql” folder since it stores all the data. The last part is Service. Service</p><p><strong>mariadb_deploy.yml</strong></p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: nextcloud-mariadb<br>  labels:<br>    app: nextcloud<br>spec:<br>  ports:<br>  - port: 3306<br>  selector:<br>    app: nextcloud<br>    tier: mariadb<br>  clusterIP: None<br>---<br>apiVersion: v1<br>kind: PersistentVolumeClaim<br>metadata:<br>  name: mariadb-pv-claim<br>  labels: <br>    app: nextcloud<br>spec:<br>  accessModes:<br>    - ReadWriteOnce<br>  resources:<br>    requests:<br>      storage: 1Gi<br>---<br>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: nextcloud-mariadb<br>  labels:<br>    app: nextcloud<br>spec:<br>  selector:<br>   matchLabels:<br>     app: nextcloud<br>     tier: mariadb<br>  strategy:<br>    type: Recreate<br>  template:<br>    metadata:<br>      labels:<br>        app: nextcloud<br>        tier: mariadb<br>    spec:<br>       containers:<br>       - image: mariadb:latest<br>         name: mariadb<br>         env:<br>         - name: MYSQL_ROOT_PASSWORD<br>           valueFrom:<br>             secretKeyRef:<br>              name: mariadb-pass<br>              key: password<br>         - name: MYSQL_USER<br>           value: chirag<br>         - name: MYSQL_PASSWORD<br>           valueFrom:<br>             secretKeyRef:<br>               name: mariadbuser-pass<br>               key: password1<br>         - name: MYSQL_DATABASE<br>           value: mydb<br>         ports:<br>         - containerPort: 3306<br>           name: mysql<br>         volumeMounts:<br>         - name: mariadb-ps<br>           mountPath: /var/lib/mysql<br>       volumes:<br>       - name: mariadb-ps<br>         persistentVolumeClaim:<br>           claimName: mariadb-pv-claim</pre><p>6. Since we have create the file for database, we shall now create the nextcloud_deploy.yml file. Almost the basic structure of the 3 parts remain same except for few tweaks. The Service acts as a loadbalancer and exposes the setup to the outside world.</p><p>nextcloud_deploy.yml</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: nextcloud<br>  labels:<br>    app: nextcloud<br>spec:<br>  ports:<br>  - port: 80<br>    nodePort: 30001<br>  selector:<br>    app: nextcloud<br>    tier: frontend<br>  type: LoadBalancer<br>---<br>apiVersion: v1<br>kind: PersistentVolumeClaim<br>metadata:<br>  name: nextcloud-pv-claim<br>  labels:<br>    app: nextcloud<br>spec:<br>  accessModes:<br>  - ReadWriteOnce<br>  resources:<br>    requests:<br>      storage: 1Gi<br>---<br>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: nextcloud<br>  labels:<br>    app: nextcloud<br>spec:<br>  selector:<br>    matchLabels:<br>      app: nextcloud<br>      tier: frontend<br>  strategy:<br>    type: Recreate<br>  template:<br>    metadata:<br>      labels:<br>        app: nextcloud<br>        tier: frontend<br>    spec:<br>      containers:<br>      - image: nextcloud:latest<br>        name: nextcloud<br>        env:<br>        - name: MYSQL_ROOT_PASSWORD<br>          valueFrom:<br>            secretKeyRef:<br>              name: mariadb-pass<br>              key: password<br>        - name: MYSQL_PASSWORD<br>          valueFrom:<br>            secretKeyRef:<br>              name: mariadbuser-pass<br>              key: password1<br>        - name: MYSQL_USER<br>          value: chirag<br>        - name: MySQL_DATABASE<br>          value: mydb<br>        ports:<br>        - containerPort: 80<br>          name: nextcloud<br>        volumeMounts:<br>        - name: nextcloud-ps<br>          mountPath: /var/www/html<br>      volumes:<br>      - name: nextcloud-ps<br>        persistentVolumeClaim:<br>          claimName: nextcloud-pv-claim</pre><p>7. Now both the mariadb_deploy.yml and nextcloud_deploy.yml has been created. We shall now create the kustomization.yml file as it lets us deploy the whole setup with just one command and a few other functionalities.</p><p><strong>kustomization.yml</strong></p><pre>apiVersion: kustomize.config.k8s.io/v1beta1<br>kind: Kustomization<br>secretGenerator:<br>- name: mariadb-pass<br>  literals:<br>  - password=redhat<br>- name: mariadbuser-pass<br>  literals:<br>  - password1=redhat<br>resources:<br>  - mariadb_deploy.yml<br>  - nextcloud_deploy.yml</pre><p>8. Deploy the whole setup using this command.</p><pre>kubectl create -k .</pre><p><strong>Voila! </strong>You have just launched Nextcloud on AWS using EKS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NMVobripGxETISFfdqCoeA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wIPylvNIrUG_pkkmqt-_Aw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y3UQOVyGR56xZ8OgSRzeKA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iyf2gvXazZXByPRKWNdapw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kK3BFgWqc2_P_NtyvOUy0g.png" /></figure><h4>Click <a href="https://github.com/chirag0202/nextcloud-eks">here</a> for the Github Repo.</h4><p><strong><em>I did this activity under the mentorship of Mr. </em></strong><a href="https://www.linkedin.com/in/vimaldaga/"><strong><em>Vimal Daga</em></strong></a><strong><em> for the “</em>Amazon EKS — Kubernetes on AWS<em>” training under </em></strong><a href="https://www.linkedin.com/in/linuxworld/"><strong><em>LinuxWorld India</em></strong></a><strong><em>. I would like to thank Mr. </em></strong><a href="https://www.linkedin.com/in/vimaldaga/"><strong><em>Vimal Daga</em></strong></a><strong><em> Sir, </em></strong><a href="https://www.linkedin.com/in/preeti-chandak/"><strong><em>Preeti</em></strong></a><strong><em> Maám and the whole </em></strong><a href="https://www.linkedin.com/in/linuxworld/"><strong><em>LinuxWorld India</em></strong></a><strong><em> team for giving me this opportunity.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f71516aa06aa" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>