<?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 Adeola Adefolaju on Medium]]></title>
        <description><![CDATA[Stories by Adeola Adefolaju on Medium]]></description>
        <link>https://medium.com/@aadefolaju?source=rss-b80f9ff65340------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*br04g698tJUdrhkNwo0Lpg.jpeg</url>
            <title>Stories by Adeola Adefolaju on Medium</title>
            <link>https://medium.com/@aadefolaju?source=rss-b80f9ff65340------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 14:04:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aadefolaju/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[Configure Amazon CloudFront with Origin Access Control, generate and review CloudFront Logs]]></title>
            <link>https://medium.com/@aadefolaju/configure-amazon-cloudfront-with-origin-access-control-generate-and-review-cloudfront-logs-53a586004c66?source=rss-b80f9ff65340------2</link>
            <guid isPermaLink="false">https://medium.com/p/53a586004c66</guid>
            <dc:creator><![CDATA[Adeola Adefolaju]]></dc:creator>
            <pubDate>Fri, 16 Aug 2024 15:10:58 GMT</pubDate>
            <atom:updated>2024-08-16T15:21:38.655Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/333/1*fx31CShj6b4Q35oeCvEIjA.png" /></figure><h4><strong>Introduction</strong></h4><p>This demo involves restricting access to static content (my resume) stored in an S3 bucket and force all requests to come through CloudFront instead of going directly to an S3 bucket. I also configured CloudFront to create logs that contain very detailed information about every user request that CloudFront receives which we can then view.</p><h4>Steps</h4><p>1. Create an S3 bucket — enable public access and upload an image to the bucket</p><p>2. Create a CloudFront distribution and test that everything is working</p><p>3. Remove Public Access — update the bucket policy to remove public access and allow only CloudFront to access the contents of the bucket and also re-enable the block public access settings</p><p>4. Create an Origin Access Control — and update the CloudFront origin to add the Origin Access Control and wait for it to finish deploying before testing.</p><p>5. Create a new s3 bucket to store CloudFront logs</p><p>6. Enable logging in CloudFront distribution settings</p><p>7. Review the logs.</p><p>8. Conclusion</p><h4>Prerequisites</h4><ol><li>An AWS account</li><li>Basic knowledge of AWS — Amazon S3, CloudFront, Edge Location</li></ol><h4>Terminologies</h4><ol><li><strong>S3 bucket</strong> — Amazon Simple Storage Service (S3) provides a simple web services interface that can be used to store and retrieve any amount of data, at any time, from anywhere on the web.</li><li><strong>CloudFront</strong> — Amazon CloudFront is a web service that speeds up distribution of your static and dynamic web content, such as .html, .css, .js, and image files, to your users.<br>CloudFront delivers your content through a worldwide network of data centers called edge locations. When a user requests content that you’re serving with CloudFront, the request is routed to the edge location that provides the lowest latency (time delay), so that content is delivered with the best possible performance.</li><li><strong>Edge location</strong> — A collection of servers which are in geographically dispersed locations. These edge locations are used by CloudFront to keep a cache or copy of your objects — your HTML, css, pdf, jpeg files, images and contents you are serving.</li><li><strong>CloudFront Origin</strong> — The origin of all the files the distribution will serve. This can be an S3 bucket, an EC2 instance, an Elastic Load Balancer or Route 53.</li><li><strong>CloudFront Distribution </strong>— The name given to the origin and configuration settings for the content you wish to distribute using CloudFront (CDN)</li></ol><p>Let’s begin…</p><h4>1. Create an S3 bucket.</h4><p>Remove the “Block Public Access” settings and acknowledge your settings. Scroll down to the bottom and “Create bucket”. Select the bucket name, permissions and update your bucket policy. Use the policy generator to create your bucket policy as shown below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*-m_O-wGTE1L64y3IyJoEtg.png" /></figure><p>Step 1: Type of Policy — s3</p><p>Step 2: Effect — Allow</p><p>Principal — *</p><p>Actions — from the drop down search for “GetObject” and select it</p><p>ARN — Copy and paste your bucket ARN followed by /* (all objects inside the bucket will be affected by this policy. Should look like this</p><p>arn:aws:s3:::my-static-webpage-cf-origin/*</p><p>Select Add Statement</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*qgJF_XzUOmFgwcL2uCXxjw.png" /></figure><p>Step 3: Select Generate Policy and copy the Generated Policy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/811/1*pzYRQxYTPN3eLA13Yugf_w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*MEAfp4GqQQDzHWOPAr8GiA.png" /></figure><p>Scroll down and Select Save changes.</p><p>Next upload an image to the bucket. I have uploaded my resume in a pdf format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*mXhFIJSLvrxZ0E3SrfJB6A.png" /></figure><h4>2. Create a CloudFront Distribution</h4><p>Search for CloudFront, Create Distribution, and select the origin which is the S3 bucket we just created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*QMIcJbJWzQ2boB-FrvLG_g.png" /></figure><p>Under Web Application Firewall (WAF) choose “Enable security protections” which sets your configuration to use AWS WAF to secure your web page from most common web threats and security vulnerabilities. Accept all the defaults, scroll down and select create distribution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*UVvGvTdfT2byuUbuw5tRpw.png" /></figure><p>Now it will take a few minutes to finish deploying. And once it has finished deploying we see this;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*xtbmJK3onbndV_3ED_QP3Q.png" /></figure><p>Once its deployed, copy the distribution domain name and paste in a new browser tab but before hitting enter add the name of your file followed by a forward slash like this “/filename” and hit enter. That is; <a href="https://d1zvctl9zxwtbc.cloudfront.net/Adeola-Adefolaju-CV.pdf">https://d1zvctl9zxwtbc.cloudfront.net/Adeola-Adefolaju-CV.pdf</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*CHkD4PP9UiWbP1xFOl_fKw.png" /></figure><h4>3. Remove Public Access from the S3 bucket</h4><p>Notice that we still have public access enabled on our bucket but we want to force all access to come through our CloudFront url only. So, head back to your S3 bucket and select “Permissions”, select edit on “Block Public access” and check the box to block access. Save changes and confirm your changes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*VED6vZ3Pw6dSH7es6HeOqg.png" /></figure><p>Then, update the bucket policy, scroll down to bucket policy and select edit. We need to remove the present bucket policy and generate a new one as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*gTLeDHmApJVBZwqeXi6_5Q.png" /></figure><p>Now, those two changes on our s3 bucket have removed public access from our S3 bucket.</p><h4>4. Create our Origin Access Control</h4><p>Go back to CloudFront, and select “Origin Access” from the left hand menu, Select “Create origin access”, give it a name and description, accept the default setting and select “Create”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*XvsyHmGoJ6BQGheQPY1gUg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*4WtfVNPH4W5FoxUewYDNUQ.png" /></figure><p>Next, we add the origin access control to our origin. Go to “Distributions” on the left, select your distribution, select “Origin”, select your origin using the radio button and select edit. Scroll down to “Origin Access” and select “Origin Access Control Setings” which is the recommended setting, which means that the bucket will restrict access to only CloudFront. And under “Origin Access Control”, select the origin access you just created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*xYE0jnhu_Rs-d4zfm36cPg.png" /></figure><p>Scroll down and select save changes. Then select the “General tab” and you can see the status telling us it is deploying and this will take a few minutes to finish.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*eCjtdBszyoD2ehIhzoLgow.png" /></figure><p>Next, to test if it’s working, select the distribution domain name, paste in a new browser tab followed by /filename like this: <a href="https://d1zvctl9zxwtbc.cloudfront.net/Adeola-Adefolaju-CV.pdf">https://d1zvctl9zxwtbc.cloudfront.net/Adeola-Adefolaju-CV.pdf</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*7vLn3VZ65U66seVjhMM_ug.png" /></figure><p>And we have been able to restrict access to the bucket to the CloudFront service only.</p><h4>5. Create a new s3 bucket to store CloudFront logs</h4><p>We’ll create a new S3 bucket for logging to store our CloudFront logs.</p><h4>6. Enable logging in CloudFront distribution settings</h4><p>Go to your CloudFront distribution and select it to edit it. Scroll down to “Standard logging”, select on and choose your newly created s3 bucket for logging.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*nFaDHbLfXZAM-IWr8j6zCg.png" /></figure><p>It gives you a warning that you need to enable ACLs to allow CloudFront to write the logs, so select enable ACLs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*r9wcpfPkkUocI7-RJ6R4BQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*g7Vw37w7R7uq5J1vnYAw3w.png" /></figure><p>Scroll down and select save changes and we can see that standard logging is now enabled in our settings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*B6rxhSiRV7zfwjOd4apG0g.png" /></figure><p>Lets now generate some traffic to our CloudFront distribution. To do that copy the distribution domain name, add a forward slash and the name of your file like we did previously and press enter. Then copy the this, open a few more tabs and paste just to generate some traffic to CloudFront.</p><p>Next go to your S3 logs bucket and select the object, wait for a few minutes for the logs to appear or select the refresh button. This may take some time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*1YhbOYhRvo3_9iHC_y1hBQ.png" /></figure><h4>7. Review the logs</h4><p>Select the log file and select open, this generates a zip file that will be downloaded to our local machine and we can view it in a format of our choice. I chose VScode.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*rFADrt24V2OnKjosZJGfoA.png" /></figure><p>There is a lot of detailed information in the logs about our CloudFront requests: the request date, the request time, our Edge location, our client’s (my machine) IP address, the request which is a GET request, the object that was requested, HTTP response codes, some info about my client machine and the browser I used to access it, our edge location response which was a cache Hit.</p><h4>8. Conclusion</h4><p>CloudFront can speed up the delivery of your static content to viewers around the world.</p><p>Origin Access Control allows you to restrict access to the contents of your bucket so that all users must use the CloudFront url instead of the direct S3 url. This allows us to remove public access from our bucket configuration.</p><p>You can configure CloudFront to create logs that contain very detailed information about every user request that CloudFront receives, the files are stored in S3 and are known as Standard logs or access logs. Very detailed information gets logged — things like Request time, edge location, client IP address, method (e.g, GET, object requested, HTTP status code, edge location response (e.g, HIT, MISS, REDIRECT or ERROR). CloudFront usually delivers the log files within an hour of the events happening.</p><p>Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=53a586004c66" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create a Docker image for a custom web application and deploy to Kubernetes]]></title>
            <link>https://medium.com/@aadefolaju/create-a-docker-image-for-a-custom-web-application-and-deploy-to-kubernetes-fec19ce2d5dd?source=rss-b80f9ff65340------2</link>
            <guid isPermaLink="false">https://medium.com/p/fec19ce2d5dd</guid>
            <dc:creator><![CDATA[Adeola Adefolaju]]></dc:creator>
            <pubDate>Tue, 24 Oct 2023 04:41:47 GMT</pubDate>
            <atom:updated>2023-12-04T05:20:22.004Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*mI8pK3cKCGEGvPyuy4XZmA.png" /></figure><p>In this blog, I am going to be talking about creating a Docker image for a custom web application using the COPY instruction in a Dockerfile. I will then create a running container from the image and describe the different ways to access the application. Later, I will push the image to Docker hub so it can be reused, deploy the application on a Kubernetes cluster by creaing a deployment with 2 replicas for high availability and also create a NodePort service to be able to access the application.</p><h3>Prerequisite</h3><ol><li>AWS account</li><li>Virtual Machine (I used an ec2 instance with an Ubuntu 20.04 image)</li><li>Docker Hub account</li><li>VS code</li><li>Kubernetes environment (I used a Kubernetes playground from <a href="https://killercoda.com/playgrounds/scenario/kubernetes">Killercoda</a></li></ol><p><strong>Steps</strong></p><ol><li>Set up your environment</li><li>Create a Docker image using a Dockerfile</li><li>Create a running container from the image</li><li>Access the application on Docker</li><li>Push the image to Docker hub</li><li>Deploy the application on Kubernetes</li><li>Access the application</li><li>Conclusion</li></ol><p><strong>Set up your Environment</strong><br>In my previous <a href="https://medium.com/@aadefolaju/install-docker-engine-on-ubuntu-20-04-8c828120b9fc">blog</a>, I talked about how to automate the installation of Docker engine on Ubuntu 20.04 using an ec2 instance as our virtual machine.<br>So, after connecting to the instance, I decided to change it’s hostname (instead of it’s private ip address) by running:</p><pre>$ hostnamectl set-hostname server</pre><p>Close the connection and reconnect for this to take effect.</p><p>2. <strong>Create a Docker image using a Dockerfile</strong><br>Create a directory, change to it and create an index.html file with the desired web contents. Create a Dockerfile in the same directory as the index.html file. The contents of both files can be found <a href="https://github.com/adeola2020-git/custom-web-app">here</a>.</p><p>In this Dockerfile, we used an nginx base image and a COPY instruction to copy the index.html file on our host to the specified path on the container when it is created. The COPY instruction copies new files or directories from source and adds them to the filesystem of the container at the specified path. We will use this Dockerfile to create an image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/869/0*Fz7nUwcFMnZFYiI8.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/764/0*9VlECB2khRPxH319.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/756/0*ljpwAnaBTF6aMrQ-.png" /></figure><p>Build the image with the tag v1 by running;</p><pre>$ docker image build -t adeola007/custom-web-app:v1 .</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*0duXCwN5-xsCJgIQ.png" /></figure><p>Confirm the image is built by running</p><pre>$ docker image ls</pre><p><strong>3. Create a running container from the image</strong><br>Run a container named custom-web-app to host the web application from the built image in the detached mode, publish port 8080 on the host to port 80 on the container and confirm its running.</p><pre>$ docker container run -d -p 8080:80 --name custom-web-app adeola007/custom-web-app:v1</pre><pre>$ docker container ls</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*IFVzUm1kou_tIdfz.png" /></figure><p><strong>4. Access the application on Docker</strong><br>There are several ways to confirm our custom web contents on the index.html file on the host was copied into /usr/share/nginx/html folder on the container. Run;</p><p>a.</p><pre>$ curl localhost:8080</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/0*SHt3AtOs9nFMS7yU.png" /></figure><p>b. Now, we will inspect the container and search for its allocated IP address. Curl the IP Address to see our populated custom web contents from the index.html file on the host. Run;</p><pre>$ docker container inspect custom-web-app | grep -i ipaddress<br>$ curl 172.17.0.2</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*g7No1Zb7Xrrb9kLg.png" /></figure><p>c. Paste the host IP Address on a browser and append port 8080 to it</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/0*3Ci0dtCUrh5E9HW4.png" /></figure><p>We can also run a curl command on the host IP Address and port 8080 on the terminal.</p><pre>$ curl 52.56.135.153:8080</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*xtOsvT33lb0CKWre.png" /></figure><p><strong>5. Push the Docker image to Docker hub</strong><br>Push the image to docker hub. You will need to login to your Docker hub account. Run;</p><pre>$ docker login<br>$ docker image push adeola007/custom-web-app:v1</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*JkzJvl4mSAoldxJ1.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*ZZoS7PLPPjbVq-Gq.png" /></figure><p>Log in to your Docker hub account to verify the image has been pushed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*eOSyr2r6qBiZl_tk.png" /></figure><p><strong>6. Deploy the application on Kubernetes</strong><br>We will now deploy the custom web app on a Kubernetes Cluster<br>I use will a Kubernetes playground from <a href="https://killercoda.com/playgrounds/scenario/kubernetes">Killerkoda</a> to deploy the custom web application.</p><p>Create a deployment definition file which will also contain the definition file to create a service that will enable us to access our custom web application. Here is the <a href="https://github.com/adeola2020-git/custom-web-app/blob/master/web-deployment.yaml">file</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*u71aFr04IwnXqukw.png" /></figure><p>From the definition file above, we are creating a deployment called <strong>custom-web-app</strong> using the image we pushed to Docker hub earlier, <strong>adeola007/custom-web-app:v1</strong> and for high availability, we will create 2 replicas. Also, to be able to access our application outside the cluster, we will create a NodePort service called <strong>custom-web-app-service</strong>. This will allow our application to be accessible externally using the port 31500 on the node, port 80 on the Pod and target port 80 on the container running in the Pod. Deploy the application by running;</p><pre>$ kubectl apply -f web-deployment.yaml</pre><p>Let us confirm the status of our deployment. Run;</p><pre>$ kubectl get deploy<br>$ kubectl get pods<br>$ kubectl get svc</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*ofGgr38XgR8U1UEC.png" /></figure><p>We can confirm that our deployment with 2 replicas has created 2 running pods and our application is highly available.</p><p><strong>7. Access the application</strong><br>Let’s now confirm on which node in the cluster the pods were scheduled by running;</p><pre>$ kubectl get pods -o wide</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*JOZdozii2SW71NZE.png" /></figure><p>We see that one has been scheduled on node01 and the other on the controlplane.</p><p>Next, confirm the IP addresses of the 2 nodes to be able to access the application on them. Run;</p><pre>$ kubectl get nodes -o wide</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*4Jh4y9sweN492_FA.png" /></figure><p>Access the application by running a curl command on each of the node IPs and append our node port, 31500, configured in our service.</p><pre>$ curl 172.30.1.2:31500<br>$ curl 172.30.2.2:31500</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/0*4h0huBfdLXGlY0yD.png" /></figure><p>We are able to access our application and it is highly available, running on 2 different nodes on our Kubernetes cluster.</p><p>Let’s now go into our container running inside one of the pods to confirm our custom web contents by running the <strong>exec</strong> command and specifying /bin/sh to open a shell. Then run the <strong>cat</strong> command on the <strong>/usr/share/nginx/html/index.html</strong> file in the container running on the pod.</p><pre>$ kubectl exec -it custom-web-app-7c4b95b567-77scm /bin/sh<br>$ ls<br>$ ls -l /usr/share/nginx/html<br>$ cat /usr/share/nginx/html/index.html</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*uAQhbHJYYmbVOuWH.png" /></figure><p><strong>8. Conclusion</strong><br>We have been able to deploy a custom web application on a container using the COPY instruction in a Dockerfile to copy our custom web contents from our host which was available on the container created from the image. We created the container from the image by publishing port 8080 on our Docker host to port 80 on the container.</p><p>We described several ways of accessing the application on the container as well as on our host.</p><p>Finally, for high availability and to be able to deploy a large scale of our application, it was deployed to Kubernetes. We created just 2 replicas of the deployment, created a NodePort service to allow external access to our application and we verified the application is accessible.</p><p>Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fec19ce2d5dd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Install Docker Engine on Ubuntu 20.04]]></title>
            <link>https://medium.com/@aadefolaju/install-docker-engine-on-ubuntu-20-04-8c828120b9fc?source=rss-b80f9ff65340------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c828120b9fc</guid>
            <dc:creator><![CDATA[Adeola Adefolaju]]></dc:creator>
            <pubDate>Sat, 21 Oct 2023 05:26:09 GMT</pubDate>
            <atom:updated>2023-10-21T05:33:14.583Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*gV_nihJo7PES5Im7fajeag.png" /></figure><p>In this blog, I am going to be demonstrating how to set up and install Docker Engine from Docker’s Apt repository on Ubuntu 20.04 in 2 ways:</p><p>a. By running the set up and installation commands on the terminal<br>b. Automate the set up and installation by using script</p><p><strong>a. Run the set up and installation commands on the terminal</strong><br>To install Docker Engine on Ubuntu 20.04 using the Apt repository for the first time on a new host machine (e.g., EC2 instance created with an Ubuntu 20.04 AMI), you need to first set up the Docker repository. Afterward, you can install and update Docker from the repository.</p><p>Installation commands can be found <a href="https://docs.docker.com/engine/install/ubuntu/">here</a></p><p><strong>Step 1: Set up Docker’s Apt repository.</strong></p><p><strong>a. Add Docker’s official GPG key</strong></p><pre>$ sudo apt-get update</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*dA-E3i62Nwl2ctM6-9ewlw.png" /></figure><pre>$ sudo apt-get install ca-certificates curl gnupg</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*PvS29moDbodZ57viqBHSyg.png" /></figure><pre>$ sudo install -m 0755 -d /etc/apt/keyrings</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*sTcFE6jQjzCZ_PbA98RiaQ.png" /></figure><pre>$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*IaeXOEJkfdCW-mgu3UHXzQ.png" /></figure><pre>$ sudo chmod a+r /etc/apt/keyrings/docker.gpg</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*XUaRelGUUw803uzLb-bmeg.png" /></figure><p><strong>b. Add the repository to Apt sources</strong></p><pre>$ echo \<br>  &quot;deb [arch=&quot;$(dpkg --print-architecture)&quot; signed-  by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \<br>  &quot;$(. /etc/os-release &amp;&amp; echo &quot;$VERSION_CODENAME&quot;)&quot; stable&quot; | \<br>  sudo tee /etc/apt/sources.list.d/docker.list &gt; /dev/null</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*gYhu7V2BeQTjeZwtwb2fmA.png" /></figure><pre>$ sudo apt-get update</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*58lDtAK1BfHk4U6S9WkFsQ.png" /></figure><p><strong>Step 2: Install the Docker Packages</strong></p><p>To install the latest version, run;</p><pre>$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*aLejL2g9xHseiqoyNYUxzA.png" /></figure><p><strong>Confirm Docker is installed</strong></p><pre>$ docker --version</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*zIPqInVcq07pF7V6w_21Yg.png" /></figure><p><strong>Step 3: Set Permissions for Docker</strong><br>To do this, run;</p><pre>$ sudo chmod 666 /var/run/docker.sock</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*paWkzPaCDoqqvlNfm-747A.png" /></figure><p>To run Docker commands without sudo, add the user to the docker group. Verify the <strong>docker</strong> group exists by searching in the folder <strong>/etc/group. </strong>Run;</p><pre>$ cat /etc/group | grep docker</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*p8t_F7BPb6n1Ntc7NF_Itw.png" /></figure><p>Then add your user (ubuntu) to the docker group by running;</p><pre>$ sudo usermod -aG docker $USER</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*mpF7bx_-sGrQbPXdbJh0MQ.png" /></figure><p><strong>Confirm if the user ubuntu has been added to the docker group</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*PzGVzy4MFOBFxG8txbF6Bw.png" /></figure><p><strong>Confirm if you can now run docker commands without sudo</strong></p><pre>$ docker container ls<br>$ docker image ls</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*53SM_j1rxdtNONQ7UfRB0A.png" /></figure><p><strong>b. Automate the Installation of Docker Engine on Ubuntu 20.4</strong></p><p>To automate the installation of Docker Engine on Ubuntu 20.04, log in to your AWS account and spin up an ec2 instance (or an instance from any other provider). Before you launch the instance, add the following script to the userdata section under Advanced details on the Launch an Instance page on your AWS EC2 Management Console.</p><p>The script can be found <a href="https://github.com/adeola2020-git/docker-labs/blob/master/docker.sh">here</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*BSNj2FZtDv5cW-NUAYZJvQ.png" /></figure><p>Once the instance is up and running, connect to it and run the following commands on the terminal to verify Docker has been installed and that you have permissions to run Docker commands without sudo.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*HHSgTj7k-ET6TFYVKCYckg.png" /></figure><p>I hope you find this useful. Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c828120b9fc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploy a Highly Available Web Server on AWS using Terraform]]></title>
            <link>https://medium.com/@aadefolaju/deploy-a-highly-available-web-server-on-aws-using-terraform-1554692309b?source=rss-b80f9ff65340------2</link>
            <guid isPermaLink="false">https://medium.com/p/1554692309b</guid>
            <dc:creator><![CDATA[Adeola Adefolaju]]></dc:creator>
            <pubDate>Sun, 17 Sep 2023 02:55:40 GMT</pubDate>
            <atom:updated>2023-09-17T02:55:40.449Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Scenario:</strong> We have been tasked to deploy a highly available web server on AWS using Terraform.</p><p><strong>Architecture Diagram</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*Vh0SShVRfbcZIG6vJAt-zg.png" /></figure><p><strong>Introduction</strong></p><p>What do we mean by high availability and how can it be achieved?<br>High availability, HA, is a quality of a system or component that assures a high level of operational performance, usually uptime, for a given period of time. HA functions as a failure response mechanism for infrastructure. One of the goals of HA is to eliminate single points of failure in your infrastructure. A single point of failure is a component of your technology stack that would cause a service interruption if it became unavailable. To eliminate single points of failure, each layer of your stack must be prepared for redundancy.<br>In essence, HA refers to systems that are durable and likely to operate continuously without failure for a long time.</p><p>Some key strategies for achieving HA are;</p><ul><li>Distribute your resources across multiple Availability Zones within a region to achieve fault tolerance and redundancy, to help minimize the impact of an AZ failure.</li><li>Leverage AWS services for high availability AWS offers a wide range of services and features designed to help you build highly available and fault-tolerant architectures. Some key services include Amazon EC2 Auto Scaling, Elastic Load Balancing (ELB), and Amazon RDS Multi-AZ deployments.</li><li>Use automated monitoring, failure detection, and failover mechanisms for both stateless and stateful components to ensure that your system can quickly recover from failures.</li></ul><p>So, to achieve HA in this project, we will create an Application Load Balance and an Auto Scaling Group. This is because auto scaling is vital to creating a highly available application.</p><p><strong>Why use Terraform?</strong><br>Terraform is an open-source Infrastructure as Code (IaC) tool created by HashiCorp. It’s platform-agnostic, meaning it can manage infrastructure on multiple cloud platforms. It can help automate most tasks allowing you to write infrastructure code that are reusable quickly.</p><p><strong>Prerequisites</strong><br>1. An AWS account<br>2. Basic knowledge of Linux Commands<br>3. An IDE (Visual Studio Code)<br>4. Terraform installed on your local machine<br>5. AWS CLI installed and configured with your programmatic access credentials</p><p><strong>Tasks</strong><br>1. Set up your development environment on VS Code<br>2. Create main.tf, variables.tf, output.tf, and userdata.sh files<br>3. Populate the files<br>4. Deploy the resources<br>5. View the created resources on AWS Management Console<br>6. Test for HA<br>7. Delete the resources<br>8. Conclusion</p><p><strong>Task 1: Set up your development environment on VS Code</strong><br>Create a directory for your project and change to it.</p><pre>$ mkdir terraform-project</pre><pre>$ cd terraform-project/</pre><p><strong>Task 2: Create main.tf, variables.tf, output.tf and userdata.sh files</strong><br>Create the files and verify they are created.</p><pre>$ touch main.tf variables.tf output.tf userdata.sh<br>$ ls</pre><p><strong>Task 3: Populate the files</strong><br><strong>a. variables.tf</strong><br>This file is where you can define variables for your Terraform configuration. It can contain the variable definitions as well as the optional default value for the variable. Variables make your Terraform configuration more flexible by defining values that your end users can assign to customize the configuration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/429/1*ZiXWG6yHJdwZDlEZRVNZ4Q.png" /><figcaption>variables.tf</figcaption></figure><p><strong>b. main.tf</strong><br>This is the primary entry point for your Terraform configuration. It’s where most of the resources are created. For a simple module, like ours, this may be where all the resources are created. For a complex module, resource creation may be split into multiple files but any nested module calls should be in the main file.<br>In this file, we will define the following resources: VPC, subnets, IGW, Route Table, Route Table Association, SG, ALB, Listeners, Target Group, Launch Template, and ASG.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*91jvw8p_6ByZgVQ6coLWXQ.png" /><figcaption>main.tf — Provider and VPC</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*zxkA0uZf9vTnUIHEYHhciA.png" /><figcaption>main.tf — IGW</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*tVT11maQtoJaZUXlk9w52w.png" /><figcaption>main.tf — Availability Zones</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/891/1*U_gI9rgJ9-U4OV6lUHJiuQ.png" /><figcaption>main.tf — Route Table</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*om_laGd2sEtUfeSSEM5y1w.png" /><figcaption>main.tf — route table association</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*JkuPRpoz0vgMoffgZyHOzA.png" /><figcaption>main.tf — Security Group</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*mXLY2COiH6X_qwqXYxeelw.png" /><figcaption>main.tf — ALB</figcaption></figure><p>An <strong>Application Load Balancer</strong> functions at the Application Layer — the seventh layer of the Open Systems Interconnection (OSI) model. After the load balancer receives a request, it evaluates the listener rules in priority order to determine which rule to apply, and then selects a target from the target group for the rule action.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*8wP2dri-YebShqK67tIs8A.png" /><figcaption>main.tf — ALB Listener</figcaption></figure><p>A <strong>Listener</strong> checks for connection requests using the protocol and port that you configure. The default action and any additional rules that you create determine how the Application Load Balancer routes requests to its registered targets.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/934/1*e68TpsGd1TN-s8zYi3jQUA.png" /><figcaption>main.tf — ALB Target Group</figcaption></figure><p>Each <strong>Target Group </strong>routes requests to one or more targets, such as EC2 instances, using the protocol (HTTP) and port number (80) you specify.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*Gc58_a3I0Co-XZil0O9qoA.png" /><figcaption>main.tf — Launch Template</figcaption></figure><p>As shown in the launch template resource above, we referenced a <strong>userdata.sh </strong>file in the user_data argument. The userdata is used to bootstrap the instances as they come online. The script will install an Apache webserver on the instances at launch and we will be able to view our webpage with the contents in the index.html file configured.<br>The script is also shown below;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*SMxCMX9ZGwniQtTqt1hV8w.png" /><figcaption>userdata.sh</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*XuPJQAabRlvKOAQrjzojHA.png" /><figcaption>main.tf — ASG</figcaption></figure><p>An <strong>Auto Scaling Group</strong> contains a collection of EC2 instances that are treated as a collective group for purposes of scaling management. We used auto scaling restrictions to set capacity limits;<br><strong>Min size</strong> — the lowest number of EC2 instances you will ever have online, and you will not go below this.<br><strong>Desired capacity </strong>— how many instances you want right now.<br><strong>Max size </strong>— the highest number of EC2 instances you will ever provision and you will not go above this.</p><p><strong>c. output.tf</strong><br>This file is used to define output values that will show up when deploying with terraform apply. Output values make information about your infrastructure available on the command line and can expose information for other Terraform configurations to use. They provide a way to expose some of that information to the user of your module. Here, we specify the Application Load Balancer DNS name, and we get a value in url. This will be used to test for availability when pasted on our browser.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/1*8g4G0bwmruvA2fz1Cm3Zvg.png" /><figcaption>output.tf</figcaption></figure><p><strong>Task 4: Deploy the resources</strong><br>Before we begin the deployment, we need to configure our AWS CLI credentials, which Terraform needs to authenticate with AWS services. For this we run the aws configure command on our terminal.<br>This command prompts us for our AWS access key ID, secret access key, default region name, and default output format. Once we provide these details, they are stored in a local file called ~/.aws/credentials. We can then use these credentials to authenticate with AWS services when running Terraform commands that interact with AWS resources.</p><pre>$ aws configure</pre><p>a. Next, we run <strong>terraform init</strong> to initialize our working directory containing the Terraform configuration files. This command downloads and installs any required modules and providers specified in the configuration files. It also initializes a backend for storing Terraform state files, which can be used to store state remotely or locally.</p><pre>$ terraform init</pre><p>b. Run <strong>terraform validate</strong> to check if the configuration is valid, then, <strong>terraform plan</strong> to show the number of resources to create and changes required by the current configuration.</p><pre>$ terraform validate</pre><pre>$ terraform plan</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/766/1*7oeVs96WKfddkdzczZNdSw.png" /><figcaption>terraform plan</figcaption></figure><p>c. Next, we run <strong>terraform apply</strong> and see the result of the deployment. terraform apply creates or updates resources in a Terraform configuration according to the configuration file. Also, our output.tf file will ensure we have the load balancer dns name displayed with a url.</p><pre>$ terraform apply</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*dviyOSN9wYNveFVTn4Wtbw.png" /><figcaption>terraform apply</figcaption></figure><p>.</p><p>.</p><p>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*y1XO-afTxpDS5Dv9IFlfIg.png" /><figcaption>terraform apply</figcaption></figure><p><strong>Task 5: View the created resources on AWS Management Console</strong><br>We will view the ALB, Listener, Target Group, ASG and instances.</p><p><strong>a. ALB</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*7diXuIpMdujqee8Lffa5mg.png" /><figcaption>Application Load Balancer</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*7nOgg15k8oRMb2GGSRHO-Q.png" /><figcaption>Application Load Balancer</figcaption></figure><p><strong>b. Listener</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*07_6HGsJC9d3j_-PiLzOmA.png" /><figcaption>Listener</figcaption></figure><p><strong>c. Target Group</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*m9T4l3iNoT_ToX01_LfwVA.png" /><figcaption>Target Group</figcaption></figure><p><strong>d. Auto Scaling Group</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*SzGtTrF_Owgxg5x6MnFpSA.png" /><figcaption>ASG</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*KfCIig-9fLaY_DaB69J-OQ.png" /><figcaption>ASG</figcaption></figure><p><strong>e. Instances</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*moC1NtIyeg-0XXgjBnBVSQ.png" /><figcaption>Instances</figcaption></figure><p><strong>Task 6: Test for High Availability</strong><br>Test for highly available by copying and pasting the load balancer dns name in a browser and refresh the page a couple of times. This way, you are essentially sending a request to the load balancer. The load balancer then routes your request to one of its registered targets, EC2 instances, in one or more Availability Zones created.</p><p>If one of the targets or Availability Zones fails or becomes unhealthy, the load balancer automatically routes the traffic to the healthy targets in another Availability Zone. This ensures high availability and fault tolerance.</p><p>Also, because EC2 Auto Scaling is configured, if there’s a sudden increase in traffic, more instances will be automatically added and the load balancer will start routing traffic to these new instances.</p><p>We will observe that 2 different hostnames are displayed for the instances launched by the Autoscaling group in two different AZs provisioned by Terraform.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/710/1*3Eztg-rImk4vxHSK9I8oMg.png" /><figcaption>web server in AZ1</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/711/1*rC2LxUNgBJYfRfe4HkMIjg.png" /><figcaption>web server in AZ2</figcaption></figure><p>This is because we configured the auto scaling group to launch instances with the following design;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/719/1*N8kQ8i5C5SfxmkQD1ePu1g.png" /><figcaption>max, desired and min instances</figcaption></figure><p>This means we will always have 2 instances created in each public subnet in the 2 AZs while our Load Balancer will also distribute traffic to the subnets in the AZs ensuring our infrastructure is highly available.<br>This way, even if one of the AZs goes down, there will be 1 other AZ available.</p><p><strong>Testing</strong><br>a. From the screenshot below, we can see 2 instances launched into 2 availability zones.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*7RyN1N753GyJcGp_U2B98Q.png" /><figcaption>2 running instances in 2 AZs</figcaption></figure><p>We will now terminate one of the instances in one of the AZs to test for HA.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*wQxJ8tMPXIGQb1s10qYuLg.png" /><figcaption>1 instance running, 1 instance terminated</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*2j7kj5YzhJoJ2pUH7hXMrw.png" /><figcaption>ASG — instance health state</figcaption></figure><p>Here, from the instance management tab of the ASG, we observe the instance we terminated has become unhealthy and the instance coming up to replace it has become healthy to avoid downtime and make the infrastructure highly available.</p><p>If we keep refreshing the page, we will notice that another instance has started initializing and then it is now running in the same AZ making the infrastructure highly available.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*YoVk7FzDS95dVg4Bnl7aMg.png" /><figcaption>2 instances running</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*UYiZipBWZlL4MZW0sbuxHw.png" /><figcaption>ASG</figcaption></figure><p><strong>Task 7: Delete the Resources</strong><br>Go back to the terminal to run terraform destroy to delete the created resources, type yes when prompted. All the created resources are deleted automatically.</p><pre>$ terraform destroy</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*9OTzUqOqsfLFRk9v6dVC-Q.png" /><figcaption>deleting resources</figcaption></figure><p><strong>.</strong></p><p><strong>.</strong></p><p><strong>.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*mOwzVllKAuJGDyB3A7Z9UQ.png" /><figcaption>Resources destroyed</figcaption></figure><p><strong>Task 8: Conclusion</strong><br>We have been able to use Terraform to automate the deployment of a highly availability web server on AWS. We distributed our resources across multiple Availability Zones within a region to achieve fault tolerance and redundancy, to help minimize the impact of an AZ failure. We also leveraged AWS services for high availability — an Elastic Load Balancer and an Autoscaling Group. Both are designed to help build a highly available and fault-tolerant architecture. The Elastic Load Balancer distributed incoming traffic to the instances created by the Autoscaling Group and the Autoscaling Group ensured the architecture had no single point of failure.</p><p>Thanks for reading.</p><p>Terraform scripts available <a href="https://github.com/adeola2020-git/HA-webserver-terraform">here</a></p><p>You can connect with me on <a href="http://www.linkedin.com/in/adeolaadefolaju">LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1554692309b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploy a Highly Available Infrastructure on AWS using Terraform]]></title>
            <link>https://medium.com/@aadefolaju/deploy-a-highly-available-infrastructure-on-aws-using-terraform-b7a294aa8d7d?source=rss-b80f9ff65340------2</link>
            <guid isPermaLink="false">https://medium.com/p/b7a294aa8d7d</guid>
            <dc:creator><![CDATA[Adeola Adefolaju]]></dc:creator>
            <pubDate>Mon, 29 May 2023 06:16:13 GMT</pubDate>
            <atom:updated>2023-06-08T02:31:04.630Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Architecture Diagram</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MTdEQWZ-Wpr5iRPWWvJX0g.jpeg" /></figure><p><strong>What is Terraform?</strong></p><p>Terraform is an infrastructure-as-code (IaC) tool through which you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse and share across various cloud platforms as well as on-premises.</p><p>Terraform allows you to quickly automate and deploy resources on-prem and in the cloud. By creating a Terraform a module with configuration files, we can create reproducible infrastructure. Even when the resources are deleted, they can be reproduced easily and quickly because the codes are reusable.</p><p>Terraform code is stored in the <strong>.tf </strong>or <strong>.tfjson </strong>file extension and the syntax is written in Hashicorp Configuration Language.</p><p>In this project, we will use Terraform to deploy a highly available and scalable architecture with no single point of failure on AWS.</p><p><strong>Prerequisites</strong></p><p>1. AWS account</p><p>2. AWS CLI</p><p>3. VS Code</p><p>4. Terraform Installed</p><p>5. Git account</p><p>To begin, launch VScode and create a terraform module with the following configuration files needed by Terraform to create our resources; <strong>main.tf, variable.tf and output.tf</strong></p><p><strong>Tasks</strong></p><p>The following tasks will be performed;</p><p>1. Create a directory for the project and change to it.</p><p>2. Create main.tf and variables.tf and output.tf files</p><p>3. Populate the files</p><p>4. Deploy the resources</p><p>5. View the created resources</p><p>6. Test HA of the provisioned infrastructure</p><p>7. Destroy the resources</p><p>8. Version Control.</p><p>9. Conclusion</p><p><strong>Task 1: Create a project directory and change to it</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/861/1*91YAj4mP-GpQlbTMYqh-qw.png" /><figcaption>Project Directory</figcaption></figure><p><strong>Task 2: Create main.tf, variables.tf and output.tf files</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*RuTxg6vAQglXpZvXIhAAQA.png" /><figcaption>Terraform files</figcaption></figure><p><strong>Task 3: Populate the .tf files with terraform scripts</strong></p><p>a. <strong>variables.tf </strong>— this defines the values for the variables as well as the optional default value for the variable in our main configuration so we do not hard code the values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/813/1*oaipgPmC1V_kDWFt6Dfpig.png" /><figcaption>variables.tf</figcaption></figure><p>b. <strong>main.tf</strong> — specifies our provider (AWS) and also defines the resources for Terraform to provision.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*kFPwhE4u9eAVJqjshzf1_A.png" /><figcaption>main.tf</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*dUqHhUEKzVY3V8bSkiMEIw.png" /><figcaption>main.tf</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/894/1*poojVpMpLDHrFYoVhkuAew.png" /><figcaption>main.tf</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*5x65glNGMN5q02NwcJ4Aug.png" /><figcaption>main.tf</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*me7SLl9hfOKHzH2lXXOfDQ.png" /><figcaption>main.tf</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*yKNPZoXWjPdAZptwLSghCQ.png" /><figcaption>main.tf</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*zp6yUOMYXw6x2viazIFbKw.png" /><figcaption>main.tf</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*exZA9vnCi3lgOSumhbzQyQ.png" /><figcaption>main.tf</figcaption></figure><p>As shown in the launch template resource above, we referenced a <strong>script.sh </strong>file in the <strong>user_data</strong> argument. This file contains the user data we will use to bootstrap the ec2 instances that will be created by Autoscaling Group. The script will install an nginx webserver on the instances at launch and we will be able to view our webpage with the contents in the index.html file configured.</p><p>The script is also shown below;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*UpGh2qAzfWy3nNz_jfHc3w.png" /><figcaption>script.sh</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*Mjq9KkTi7F0IXYMWvmtlvg.png" /><figcaption>main.tf</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*rGtYEwWintnHR47CH8eB1w.png" /><figcaption>output.tf</figcaption></figure><p><strong>Task 4: Deploy the Resources</strong></p><p>Before we begin the deployment, we need to configure our AWS CLI credentials, which Terraform needs to authenticate with AWS services. For this we run the <strong>aws configure </strong>command on our terminal.</p><p>This command prompts us for our AWS access key ID, secret access key, default region name, and default output format. Once we provide these details, they are stored in a local file called <strong>~/.aws/credentials</strong>. We can then use these credentials to authenticate with AWS services when running Terraform commands that interact with AWS resources.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*m9jRkrM8X93wuMWetC_CgQ.png" /><figcaption>Configure AWS CLI Credentials</figcaption></figure><p>a. Next, we run <strong>terraform init </strong>to initialize our working directory containing the Terraform configuration files. This command downloads and installs any required modules and providers specified in the configuration files. It also initializes a backend for storing Terraform state files, which can be used to store state remotely or locally.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*4RVCxyrn3WTHg08UKecJ1w.png" /><figcaption>intitialize the working directory</figcaption></figure><p>b. Run <strong>terraform validate </strong>to check if the configuration is valid, then, <strong>terraform plan </strong>to show changes required by the current configuration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*IGijvO_tw2GICbo2R-uDww.png" /><figcaption>check configuration validity</figcaption></figure><pre>$ terraform plan</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*ztaXebDbTE1tASE6f52tvw.png" /><figcaption>changes required by the current configuration</figcaption></figure><p>Also, our <strong>output.tf </strong>file will ensure we have the load balancer dns name displayed with the <strong>website url</strong> after deploying the infrastructure with <strong>terraform apply.</strong></p><p>c. Next, we run <strong>terraform apply </strong>and see the result of the deployment. <strong>terraform apply </strong>creates or updates resources in a Terraform configuration according to the configuration file.</p><pre>$ terraform apply</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*htdhFOmLFSXGzvnD0i2Rwg.png" /><figcaption>Infrastructure Provisioning</figcaption></figure><p>.</p><p>.</p><p>.</p><p>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*-XvBEDNAQd0da7kM8Yr4rw.png" /><figcaption>Infrastructure Provisioned</figcaption></figure><p><strong>Task 5: View the created Resources on AWS Management Console</strong></p><p>a. vpc</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*dFuzl-4O_HAyhON9bXmdCA.png" /><figcaption>web-vpc</figcaption></figure><p>b. subnets</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*4HO7sMRWjt5aHIX-cmVQMA.png" /><figcaption>3 public subnets</figcaption></figure><p>c. internet gateway</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*BeYTKut-CQIofc7aJp2CSg.png" /><figcaption>web-igw</figcaption></figure><p>d. route table</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*7iZ3Hm2pxtyHEMhYcML89g.png" /><figcaption>route table</figcaption></figure><p>e. security Group</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*xZZvNuURg-W758fGXUQp-A.png" /><figcaption>web-sg</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*YAhxwc4lqPx21zX-t6-mTg.png" /><figcaption>web-sg — inbound rules</figcaption></figure><p>e. Load Balancer</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*Z4A_rYkLvkj1odpFtzc_sg.png" /><figcaption>web-alb</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*MBev98GsvM7unyk-GonyBg.png" /><figcaption>web-alb details</figcaption></figure><p>f. autoscaling group</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*fFLb9zNG2Rz4EUjnmUobeQ.png" /><figcaption>web-asg</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*a0df-GghJsitnsgFjdecdA.png" /><figcaption>web-asg details</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*fKn39KfVeaKmuhUap0ULWQ.png" /><figcaption>healthy instances launched</figcaption></figure><p>h. 2 ec2 instances launched</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*3v7chFyCSzaBHa02PYGBJg.png" /><figcaption>2 web servers</figcaption></figure><p><strong>Task 6: Test High Availability of the provisioned resources</strong></p><p>To confirm we have used Terraform to deploy a highly available web server on AWS, we will copy and paste the displayed url above in a browser and refresh the page. We will observe that 2 different hostnames/ip addresses are displayed for the instances launched by the Autoscaling group provisioned by Terraform.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/776/1*FeS1G5JbON7f3pIyxkYXYg.png" /><figcaption>web server 1</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*c588Khk-5xvQK_lYX8kaIw.png" /><figcaption>web server 2</figcaption></figure><p>This is because we configured the auto scaling group to launch instances with the following design;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/822/1*BMuEn7Wb5juym_TXebH9vg.png" /><figcaption>auto scaling with ASG</figcaption></figure><p>This means we will always have 2 instances created in each public subnet in the 3 available zones while our Load Balancer will also distribute traffic to the subnets in the available zones ensuring our infrastructure is highly available.</p><p>This way, even if one of the AZs goes down, there will be 2 other AZs available.</p><p>b. From the screenshot below, we can see 2 instances launched into 2 availability zones.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*3v7chFyCSzaBHa02PYGBJg.png" /><figcaption>2 launched web servers</figcaption></figure><p>We will now terminate one of the instances in one of the AZs to test for HA.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*coQ2Gpl7_XRwYYoNT3342g.png" /><figcaption>1 web server terminated</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*ZBY_eE98A6PG2wzxgVWsgw.png" /><figcaption>terminated web server becomes unhealthy</figcaption></figure><p>Here, from the instance management tab of the autoscaling group, we observe the instance we terminated has become unhealthy and the instance coming up to replace it has <strong>become healthy automatically to avoid downtime and make the infrastructure highly available</strong>.</p><p>If we keep refreshing the page, we will notice that another instance has started initializing and then it is running making the infrastructure highly available.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*-eza3mTJo-2QIIc71kNFGQ.png" /><figcaption>new web server intializing</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*9RtMYseU1ppTL7a4KN6ulA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*J8w9IQcNXPzjzzI1HgP5dg.png" /><figcaption>healthy web bservers</figcaption></figure><p><strong>Task 7: Destroy the Resources</strong></p><p>Go back to the terminal to run <strong>terraform destroy </strong>to delete the created resources, type <strong>yes</strong> when prompted. All the created resources are deleted automatically.</p><pre>$ terraform destroy</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*754yWCa7q9EAN-wNfn4oYg.png" /><figcaption>destroy the resources</figcaption></figure><p>.</p><p>.</p><p>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*KXDkiOLB1ycYW47-I6DohA.png" /><figcaption>resources destroyed</figcaption></figure><p><strong>Task 7: Version Control — clone a GitHub repo to push the terraform files to</strong></p><p>a. Next, log in to your GitHub account to create a repository for the project and git clone the repo.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*jerSWvTWyYMrH37hdWPITw.png" /><figcaption>clone an empty remote repo for version control</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*cERu0o8yLxBXZsLOYnRdKw.png" /><figcaption>cloned repo</figcaption></figure><p>b. Move the <strong>main.tf, output.tf, variables.tf </strong>and <strong>script.sh </strong>to the initialized Git folder (<strong>terraform-aws-ha-project1</strong>) and change to it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*9pyxY-a3N-idP32SFGLDng.png" /><figcaption>move .tf files to the cloned remote repo</figcaption></figure><p>c. Add the .tf files to the staging area and commit</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*UzKOyjEnfK0qurrKuyZMAw.png" /><figcaption>stage and commit the files</figcaption></figure><p>d. Push to remote and confirm</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*yYMTMbM4eqzvIHssQgLZng.png" /><figcaption>push to the remote repo</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*ZqtHBfeEQMlSXV0GYyatwQ.png" /><figcaption>files pushed to the remote repository</figcaption></figure><p><strong>Task 8: Conclusion</strong></p><p>We have been able to use Terraform to automate the deployment of a high availability and scalability infrastructuren on AWS, creating 3 AZs in a custom VPC, an Elastic Load Balancer, Autoscaling Group. The Elastic Load Balancer distributes incoming traffic to the instances created by the Autoscaling Group and the Autoscaling Group ensures the architecture has no single point of failure.</p><p>Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b7a294aa8d7d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Launch a Web Application using Docker on Ubuntu 20.04]]></title>
            <link>https://medium.com/@aadefolaju/launch-a-web-application-using-docker-on-ubuntu-20-04-afea7750d4e7?source=rss-b80f9ff65340------2</link>
            <guid isPermaLink="false">https://medium.com/p/afea7750d4e7</guid>
            <dc:creator><![CDATA[Adeola Adefolaju]]></dc:creator>
            <pubDate>Fri, 21 Apr 2023 14:40:56 GMT</pubDate>
            <atom:updated>2023-04-24T01:07:49.346Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/511/1*fTOMe8ad4x1hvSY9fxiDIQ.jpeg" /></figure><p><strong>Docker and Containers</strong></p><p><strong>Docker</strong> is an open platform for developing, shipping, and running applications. It enables developers to build, deploy, run, and manage containers.</p><p><strong>Containers</strong> are standardized, executable components that combine application source code with operating system (OS) libraries and dependencies required to run that code in any environment.</p><p>Containers package all the dependencies and code your application needs to run into a single file which will run the same way on any machine.</p><p>In this project, we will install Docker on Ubuntu 20.04, launch a simple Python Flask web application, access the application on a web browser and connect to the running container on the terminal.</p><p><strong>Prerequisites</strong></p><p>1. AWS account</p><p>2. Launch an Amazon ec2 instance with an Ubuntu 20.04 AMI and connect to it</p><p><strong>Steps</strong></p><p>1. Install Docker Engine on Ubuntu 20.04 using the apt repository</p><p>2. Launch the web application — Python Flask application</p><p>3. Connect to the running container from the terminal</p><p>4. Access the Flask application on a browser and the terminal</p><p>5. Troubleshooting with logs</p><p><strong>Step 1: Install Docker Engine on Ubuntu 20.04 using the apt repository</strong></p><p><strong>A. Set up the repository</strong></p><p>After spinning up an Amazon ec2 instance with an Ubuntu 20.04 AMI, SSH into it and follow the steps here <a href="https://docs.docker.com/engine/install/ubuntu/">https://docs.docker.com/engine/install/ubuntu/</a> to install Docker Engine:</p><ol><li>Update the apt package index and install packages to allow apt to use a repository over HTTPS:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*2QQpz4MgADIdz_xjWb5zJA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*EPFWXB8AbaNurS8E1ZsaEw.png" /></figure><p>2. Add Docker’s official GPG key:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*KWsoTRYNnyezNy89wqHcIw.png" /></figure><p><strong>B. Install Docker Engine</strong></p><ol><li>Update the <strong>apt</strong> package index:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*IUznITtcKgMkaqok1UpOCg.png" /></figure><p>2. Install Docker Engine, containerd, and Docker Compose.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*U0kRaUHhgKzkSWjcnrCAzA.png" /></figure><p>3. Confirm Docker status and version</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*lzEqpoA8LJAiTDlURHhDcQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*iz82NQnY0tOGkmse28sDrA.png" /></figure><p><strong>Step 2: Launch the web application — Python Flask application</strong></p><p>1. To launch our web application, we will pull a Python Flask image from Docker Hub and launch a container from it. I already created this image and pushed to DockerHub. To do this, run;</p><p><strong>$ sudo docker container run -idt -p 8080:80 — name python-flask-app adeola007/dockerize-python-flask-demo:v1</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*F23PVcX0fMu7EKhFRA14pA.png" /></figure><p>2. <strong>$ sudo docker container run </strong>does 3 things all in one —</p><p><strong>$ sudo docker image pull — </strong>pulls an image from DockerHub</p><p><strong>$ sudo docker create — </strong>creates a container from it</p><p><strong>$ sudo docker start — </strong>starts the container</p><p>3. From the command above;</p><p><strong>-idt </strong>— 3 options put together:</p><p><strong>-it </strong>allows us to create the container with an interactive terminal so that we will be able to enter the container once its created.</p><p><strong>-d stands for — detached</strong>. This will run the container in the background. If we did not do this, we wouldn’t be able to use our terminal as we would be inside the container.</p><p><strong>-p</strong> allows you to define a port mapping. This will allow us to access the container from a browser.</p><p><strong>8080</strong> is the host-side port</p><p><strong>80</strong> is the container-side port, the one on which the application is listening.</p><p>So, <strong>-p 8080:80 </strong>means we are mapping the container port to a port on the host. When a browser visits the host, the container will serve the host and the host will serve the browser. It will look something like the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/687/1*cllrq9jtBgt0qq425doojQ.png" /></figure><p>4. To see the running container on the terminal run;</p><p><strong>$ sudo docker ps</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*hgsUnacvtNKmoE9vjeVsSw.png" /></figure><p><strong>Step 3: Connect to the running container from the terminal</strong></p><p>With the <strong>exec</strong> command, we can connect to the container and launch a shell inside it, similar to an ssh connection. It also allows running one-off commands:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*OuNrmlIYvRvuYQ9CJ-D2CA.png" /></figure><p>- <strong>exec — </strong>allows us to connect to the running container</p><p>- <strong>sh — </strong>launches a shell to run commands inside the container</p><p>- <strong>85e3 — </strong>container id</p><p>From inside the container, we can view its contents.</p><p>- <strong>Dockerfile — </strong>contains instructions to create the Docker image</p><p><strong>- app.py — </strong>the Python script used to build the flask application</p><ul><li><strong>requirements.txt — </strong>contains the dependencies for the Python flask app to run</li></ul><p><strong>Step 4: Access the Flask application on a browser and the terminal</strong></p><p>1. To access the application on a browser, visit</p><p><a href="http://ipaddress:8080/"><strong>http://IPADDRESS:8080</strong></a></p><p>where our IP address is the public IPV4 address of the host machine — ec2 instance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/1*RqtYZESEgAlgNyy-KfHQbQ.png" /></figure><p>2. To access the application from the terminal, run;</p><p><strong>$ curl </strong><a href="http://ipaddress:8080/"><strong>http://IPADDRESS:8080</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*TY5AWgQidyP_8ejmKjwbcw.png" /></figure><p><strong>Step 5: Troubleshooting with logs</strong></p><p>To troubleshoot the running container, run;</p><p><strong>$ sudo docker container logs -f &lt;container id&gt;</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*BS6PuseJW6sEyigtsHJ6pA.png" /></figure><p>The <strong>-f </strong>option, tells docker to follow the logs, so the filter also applies to new entries and it continues streaming the new output from the container.</p><p>Every time we refresh the browser page, the container will log what IP address the request came from, what time it happened, and what type of request the browser sent.</p><p>This can be very useful when a container isn’t working as expected to help pinpoint the issue.</p><p><strong>Conclusion</strong></p><p>We have been able to install Docker on Ubuntu 20.04, launched a simple Python Flask web application, accessed the app on a web browser and connected to the running container on the terminal.</p><p>Thank you for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=afea7750d4e7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spin up a simple Hello-world with Docker Compose]]></title>
            <link>https://medium.com/@aadefolaju/spin-up-a-simple-hello-world-with-docker-compose-9d31ff7f6af1?source=rss-b80f9ff65340------2</link>
            <guid isPermaLink="false">https://medium.com/p/9d31ff7f6af1</guid>
            <dc:creator><![CDATA[Adeola Adefolaju]]></dc:creator>
            <pubDate>Thu, 20 Apr 2023 21:08:01 GMT</pubDate>
            <atom:updated>2023-04-21T02:30:27.867Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*Vu-rLPSxL-8Dh17ALWKUQA.jpeg" /></figure><p>Docker Compose is a tool for defining and running multi-container Docker applications. It is a great tool for any application that requires multiple microservices allowing each service to be in a separately managed container.</p><p>With Docker Compose, we use a Compose file written in yaml to configure our application’s services. Then, using a single command, we create and start all the services from your configuration.</p><p>Basically, there is a 3-step process involved in using Docker Compose:</p><p>1. Use a <strong>Dockerfile</strong> to build the images needed for the application so it can be reused and reproduced anywhere or pull from a registry.</p><p>2. Use a <strong>docker-compose.yaml</strong> file to define the services that make up the application so that they can be run together in an isolated environment. These services are the individual containers needed for the application to run.</p><p>3. Then, run <strong>docker-compose up </strong>(for Compose V1) or <strong>docker compose up </strong>(for Compose V2) and Docker Compose will start and run the entire application.</p><p><strong>Building with Docker Compose</strong></p><p>Docker Compose can be used within a Dockerfile project and we can set it up to run an image we build locally or pulled from Docker Hub. To do this, we just need to add a <strong>build </strong>section on the service.</p><p>We will set up a quick Docker Compose <strong>hello-world application</strong> and run it. For this project, we need at minimum, a docker-compose.yaml file and an image (<strong>hello-world</strong> image pulled from Docker Hub) to run. We will also use a Dockerfile to feed into the docker-compose.yaml file.</p><p><strong>Prerequisites</strong></p><p>1. Spin up an ec2 instance for our working environment and connect to it</p><p>2. Install Docker Engine, containerd and Docker Compose from Docker’s apt repository <a href="https://docs.docker.com/engine/install/ubuntu/">https://docs.docker.com/engine/install/ubuntu/</a></p><p><strong>Step 1: Create a directory for the project</strong></p><p>We will create a directory on our terminal. Name the directory <strong>hello-world-repo </strong>and change into it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/898/1*Ul21dwOviG80eQEMJy3LWg.png" /></figure><p><strong>Step 2: Create a Dockerfile</strong></p><p>Create a Dockerfile specifying hello-world as base image. This file will be used to feed into the docker-compose.yaml file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*-Ii_0RuOlhtjjJ1m7ir3Vw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/405/1*nhYFUm81QX2Lrvsvf-25RQ.png" /></figure><p><strong>Step 3: Create a docker-compose.yaml file</strong></p><p>In the same directory, create a docker-compose.yaml file</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*V6x4nWNi6vShiOOlacuvUQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/605/1*UNbrUtxu3mYArV75zxAUuA.png" /></figure><p>Let’s look at the format of this file.</p><p><strong>- version — </strong>defines the syntax to use and it is tied to the version of the docker engine.</p><p><strong>- services — </strong>defines the container we want Docker Compose to run. In this case, our container is called <strong>hello </strong>and we a using the image called <strong>hello-world.</strong></p><p><strong>- build </strong>— defines configuration options to build our docker image from our Dockerfile present in the same directory as the docker-compose.yaml</p><p><strong>“context: .” — </strong>defines the directory that the <strong>docker-compose.yaml </strong>file is in (the present working directory).</p><p><strong>Step 4: Spin up the Hello-world application</strong></p><p>To spin up the hello-world application with docker compose, we simply run</p><p><strong>$ sudo docker compose up</strong></p><p>From the screenshot below, the output also gives us a quick summary of how a container is run.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*0bB5b43dMValNkU9rdHdOw.png" /></figure><p>From the output above, <strong>Container hello-world-repo-hello-1</strong> shows that we are running things from a directory we named <strong>hello-world-repo </strong>and the service (container) is the <strong>hello </strong>service and only <strong>1 </strong>is being run. A default network called <strong>hello-world-repo_default </strong>is also created.</p><p>Docker ran the container and then shut it down once its processes have completed. Containers based on the <strong>hello-world</strong> image are impermanent. They run, produce the needed output, and then exit.</p><p>So, since it is not currently running, to check it, we run</p><p><strong>$ sudo docker ps -a</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*oISGbgwSHBSIqigJ5_5OVQ.png" /></figure><p><strong>Conclusion</strong></p><p>We have used Docker Compose to spin up a simple hello-world application.</p><p>Thank you for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9d31ff7f6af1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DEPLOY AN EC2 INSTANCE INSIDE A CUSTOM VPC USING TERRAFORM]]></title>
            <link>https://medium.com/@aadefolaju/deploy-an-ec2-instance-inside-a-custom-vpc-using-terraform-31ce6a5ecc84?source=rss-b80f9ff65340------2</link>
            <guid isPermaLink="false">https://medium.com/p/31ce6a5ecc84</guid>
            <dc:creator><![CDATA[Adeola Adefolaju]]></dc:creator>
            <pubDate>Mon, 12 Dec 2022 12:33:56 GMT</pubDate>
            <atom:updated>2022-12-12T12:56:25.640Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/741/1*Csk9r7EvVCL81_m30NfhnA.png" /></figure><h3><strong>Project Description</strong></h3><p>You have a cloud management team, and they are in charge of deploying. You deploy an Amazon EC2 instance inside a custom VPC just for your team and you are to add this instance inside a public subnet making it accessible to the public using IaC (Terraform).</p><h3><strong>Architecture Diagram</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*cmRIIG3Pu3eo34ST_L-Bzw.png" /></figure><h3><strong>What is Terraform?</strong></h3><p>Terraform is an infrastructure-as-code (IaC) tool through which you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse and share across various cloud platforms as well as on-premises.</p><p>Terraform allows you to quickly automate and deploy resources on-prem and in the cloud. In my first project, I provisioned an Amazon EC2 instance in a custom VPC manually via the AWS Management console. This was time consuming and when I completed the project, I deleted all the resources deployed in the project. In case I need to quickly redeploy the instance, I would have to start all over again. But, this is not so with Terraform. By creating a Terraform module with configuration files, we can create reproducible infrastructure. Even when the resources are deleted, they can be recreated easily and quickly because the codes are reusable.</p><p>Terraform code is stored in the <strong>.tf </strong>or <strong>.tfjson </strong>file extension and the syntax is written in Hashicorp Configuration Language.</p><h3>Prerequisites</h3><p>1. AWS account</p><p>2. AWS CLI</p><p>3. Visual Studio Code</p><p>4. Terraform Installed</p><p>5. Git account</p><p>To begin, launch <strong>VScode</strong> and create a terraform module with the following configuration files needed by Terraform to create our resources; <strong>main.tf, variable.tf and output.tf</strong></p><p>The following tasks will be performed</p><p>1. Clone the GitHub repo to push the configuration files to</p><p>2. Create a custom VPC</p><p>3. Create a public subnet</p><p>4. Configure and launch an ec2 instance inside the public subnet in the custom VPC.</p><p>5. Add user data</p><p>6. Attach a security group to the ec2 instance created</p><p>7. Initialize Terraform and provider plugins</p><p>8. Check the plan — the changes required by the configuration</p><p>9. Launch the EC2 instance</p><p>10. View the website and other resources Terraform created</p><p>11. Delete the provisioned resources</p><p>12. Add configuration files to the remote repository</p><h3><strong>Task 1: Clone the GitHub repo</strong></h3><p>Clone the GitHub repo and change to the initialized directory. Then, create the <strong>main.tf, variable.tf </strong>and <strong>output.tf </strong>configuration files and start writing configuration codes to provision the resources needed to build our infrastructure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*ntQmJI_rk5TMN3D4gNC5cA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*0tpI43ydjMmI-ssp6tFioQ.png" /></figure><h3><strong>Task 2: Create a custom VPC</strong></h3><p>The <strong>variable.tf </strong>file has the configuration for our variables used in the <strong>main.tf </strong>file. And on the <strong>main.tf </strong>file, we have the resource block for the custom VPC as shown below;</p><h4><strong>- variable.tf</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/843/1*5pGe-pn3ZDBHygpJyi734g.png" /></figure><h4><strong>- main.tf</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/1*s8JurmRrwkF2Ofuz_IiQ0g.png" /></figure><h3><strong>Task 3: Create a Public subnet</strong></h3><p>A public subnet on the VPC is where our EC2 instance will be created to make it accessible the public.</p><h4><strong>- main.tf</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*8ArgG9Gth7sNG2_pb_7BHQ.png" /></figure><h3><strong>Tasks 4, 5 and 6: Configure the ec2 instance</strong></h3><p>Configure the ec2 instance inside the public subnet of the custom VPC, attach a Security Group to the instance and add userdata.</p><h4><strong>- main.tf</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*BwPDbuoCG8PsDfnTGv5m6w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*rIJ1b-ZfKhrNSimt218Ieg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*r0z9Z4Frc8wwXdqCybDbCQ.png" /></figure><p>From the instance resource block in the <strong>main.tf </strong>file, we added a <strong>user_data </strong>argument with a bashscript to install an Apache webserver on the instance at launch and we will be able to view our webpage with the contents in the index.html file configured.</p><h3><strong>Task 7: Initialize Terraform and provider plugins</strong></h3><p>Run <strong>terraform init </strong>to initialize our Terraform infrastructure and provider plugin</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*WInCybauxGcg77yshqDYWQ.png" /></figure><h3><strong>Task 8: Check the changes required by the current configuration</strong></h3><p>Run <strong>terraform validate </strong>to check if the configuration is valid, then, <strong>terraform plan </strong>to show changes required by the current configuration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*hv_mZSVYj6-dsvkZyrr-ZQ.png" /></figure><pre><strong>$ terraform plan</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*WTQTsh1Hz7Ik6iMI1hd3Jw.png" /></figure><h3><strong>Task 9: Launch the EC2 instance</strong></h3><p>To launch the EC2 instance, we run <strong>terraform apply </strong>and select <strong>yes </strong>when prompted. This creates and launches our EC2 instance and all the other resources in the configuration plan.</p><p>Also, we created an <strong>output.tf </strong>file which displays our <strong>website url</strong> after instance is launched with <strong>terraform apply.</strong></p><h4>- <strong>output.tf</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*OXQwYJ20OGPvth3zu02qNA.png" /></figure><p>Result after running <strong>terraform apply</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*8dbID5bkq3Uy2ObMIX-p4A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*QK3yrhQcr6ZI5_hh-Yng_A.png" /></figure><h3><strong>Task 10: View the website and other resources Terraform created</strong></h3><p>Verify the other resources created via the AWS Management console.</p><h4>a) The custom VPC</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*Tat_s60dsBJBMOH4HUF0Xg.png" /></figure><h4>b). Public and private subnets</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*iW19DM_MPvEXQqTupvVMUA.png" /></figure><h4>c). Security group</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4myqMqlkh7QOoUAuAepRXg.png" /></figure><p>Copy the displayed url from the terminal on <strong>VScode </strong>or navigate to the <strong>AWS console → Services → EC2 → Instances New. </strong>Select the instance, copy the displayed <strong>Public ipv4 address</strong> and place in your browser to view the configured webpage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*NO3Z0x_qKBD77aQOdub7DA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*BE89TVPci45yx0gc1JVzPg.png" /></figure><h3><strong>Task 11: Delete the resources</strong></h3><p>On the VScode terminal, run <strong>terraform destroy </strong>to delete the created resources, type yes when prompted. All the created resources are deleted automatically.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*8FPHhn-BLVQgD3nsdpkT-Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*XQhEKmdP3FVIhuK0utit1A.png" /></figure><h3><strong>Task 12: Add the configuration files and push to GitHub</strong></h3><p>Add the <strong>main.tf, output.tf </strong>and <strong>variable.tf</strong> configurations files to the staging area and commit to the local repository. Then push to the remote repository on GitHub.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*N2t7O_TPn3LoTQAN6RMDpg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*OD8wC75TAD7ciLXMqIOWKA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*DnpjiVzpfT0DnXZXrvkDXg.png" /></figure><p>Confirm that the configuration files have been added to the remote repository on GitHub.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*783BhWicGG5yN7L0PV8ewg.png" /></figure><h3><strong>Conclusion</strong></h3><p>We have been able to provision an Amazon EC2 instance in a custom VPC using Terraform. We also installed an Apache web server on our instance and made it accessible to the public. In the previous project where we provisioned the instance via the AWS console, the deployment process was slow — we spent time manually deploying each resource needed to complete the project. But with Terraform, we were able to automate the deployment of our infrastructure very quickly and easily simply by using codes in our configuration files. These files are version-controlled and reusable. So, we can at any time tear down the infrastructure and re-build it again whenever we make changes to the files.</p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=31ce6a5ecc84" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploy an Amazon EC2 instance in a custom VPC]]></title>
            <link>https://medium.com/@aadefolaju/deploy-an-amazon-ec2-instance-in-a-custom-vpc-8d5d5385b30?source=rss-b80f9ff65340------2</link>
            <guid isPermaLink="false">https://medium.com/p/8d5d5385b30</guid>
            <dc:creator><![CDATA[Adeola Adefolaju]]></dc:creator>
            <pubDate>Mon, 05 Dec 2022 21:43:48 GMT</pubDate>
            <atom:updated>2022-12-06T03:01:24.603Z</atom:updated>
            <content:encoded><![CDATA[<h4>Architecture Diagram</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pjfkt-VrQz9jw3RI.jpg" /></figure><p>As seen from the architecture diagram above, I need to deploy an EC2 instance in the public subnet of a custom VPC which would be used for deployments and would be accessible to the public.</p><p>A custom Virtual Private Cloud is a fully customized VPC that I set up manually and where my resources reside in. It contains 2 subnets; a public subnet and a private subnet, an internet gateway, a routing table, security group and an EC2 instance which is launched in the public subnet to make it accessible to the public.</p><h4><strong>Step 1: Create a Custom VPC</strong></h4><ol><li>Navigate to <strong>VPC Dashboard</strong> on AWS Console and select <strong>Create VPC. </strong>Select <strong>VPC Only. I</strong>t automatically creates a main route table, main NACLs and an SG.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/764/0*ONS_ejVbAnGQuvBJ.PNG" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/0*XZfoj5B-_-S2E1Db.PNG" /></figure><p>2. Give the VPC a name, select an IPV4 CIDR, leave everything as default and select <strong>Create VPC</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/0*0XnC2MXK3lGdhYKM.PNG" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/0*M2WSEvOVnlqpNaS9.PNG" /></figure><p>3. Create 2 subnets; public and private and assign a public IP to the public subnet.</p><p>Navigate to <strong>Subnets →</strong> <strong>Create Subnet. </strong>Select the VPC to create the subnets, give each subnet a name, select their Availability Zones and their IPv4 CIDR blocks.</p><p>To easily differentiate the subnets, I gave them names associated with what they need to do as well as their IPv4 CIDR blocks on the VPC as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*RtcIp-A0nVK1F5T1.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*-nxB3c1m2cXfvGJg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/0*qaHHOXuZIKQYAQ17.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*nYuFjIeWMg4oAq9X.png" /></figure><p>- We need to assign a public IP to our public subnet. Select the public subnet, go to <strong>Actions </strong>and select <strong>Edit subnet settings </strong>from the drop down menu, check <strong>Enable auto-assign public IPv4 address </strong>and select <strong>Save.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*pUIQDtebwn0Czx4j.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/0*yUZb0HEE1cS4ITwc.png" /></figure><p>4. Create an Internet Gateway with the name <strong>public-subnet-IGW</strong> and attach it to the custom VPC. Navigate to <strong>Internet gateways → create internet gateway</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*H-8kE8JOXoOxvkv7.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*tQWAoNCYxLD1rZd7.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*Sqrj3uDLyNzdqMP8.png" /></figure><p><strong>5. </strong>Create a new routing table and add a route out of it to the internet, then point it to the newly created internet gateway. The purpose of this is to associate the public subnet with it so it can be connected to the internet gateway for public access.</p><p>Navigate to <strong>Route tables → Create route table, </strong>give it a name; public-access-RT, select our custom VPC and select <strong>Create route table</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*AK1lXgCEm2nmiztN.png" /></figure><p><strong>Note that this newly created route table is different from the VPC’s main route table that was generated automatically when the VPC was created. The VPC’s main routing table is meant to protect it from public access.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*um5BoO8Xp8kIzrQU.png" /></figure><p><strong>6. </strong>Select the newly created routing table; public-access-RT, edit it and create a route out to the internet by adding the route <strong>0.0.0.0/0</strong> and point it at our internet gateway; public-subnet-IGW</p><p><strong>Select public-access-RT → Actions → Edit routes</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*nOzyB4bucRsgdiCf.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*3dqTPvRBZBPlzhA8.png" /></figure><p>Select <strong>Internet Gateway</strong>, pick the previously created internet gateway and select <strong>save changes.</strong></p><p>7. Next, we need to associate our public subnet to the route table; public-access-RT which is the only route table that now has a route to the internet not the main RT created for the VPC.</p><p>- So, we select public-access-RT again and go to <strong>Actions and</strong> select <strong>Edit subnet associations </strong>from the drop down.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*VVcdmObTJ-i2iCFe.png" /></figure><p>- Select the public subnet to associate with this routing table and select <strong>Save associations.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*rVpZZepJAP4_OkWi.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*lv_Ksd8k0auOTJF6.png" /></figure><h4><strong>Step 2: Create an EC2 instance in the public subnet of the created custom VPC</strong></h4><p>1. From services, in the AWS Console, search for EC2 and open in a new tab.</p><p>2. Select <strong>Instances → Launch Instance</strong></p><p>3.<strong> </strong>Give the instance a name, select an AMI, preferably Amazon Linux AMI, Select <strong>t2.micro</strong> instance type and <strong>proceed without a Key Pair (Not recommended)</strong> for Key Pair.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/516/0*cvShtBxcyWrdnUot.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*AI5u9STJP9kmAYo4.png" /></figure><p>4. Edit <strong>Network settings, </strong>select web-access-VPC, the public subnet and choose <strong>Enable </strong>under <strong>Auto-assign public IP</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*I3jMKXU2kdf_IGyO.png" /></figure><p><strong>5. </strong>Create a new security group and add rules for SSH and HTTP to allow public access to the instance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*bRl5bXdx2NMv-Xvs.png" /></figure><p>6. To view our apache webpage installed on the instance, we will add a bootstrap script to the userdata under <strong>Advanced details. </strong>This script will install an Apache webserver on the EC2 instance at launch and we will be able to view our webpage with the contents in the index.html file configured. Then, select <strong>Launch instance</strong></p><pre>#!/bin/bash<br>yum update -y<br>yum install httpd -y<br>echo &quot;&lt;html&gt;&lt;h1&gt;This is a deployment server&lt;/h1&gt;&lt;/html&gt;&quot; &gt; /var/www/html/index.html<br>systemctl start httpd<br>systemctl enable httpd</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*bBSzxKbElxItNnvr.png" /></figure><p>7. Our instance has been launched and its running</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*luRN21a4Dcec7Kla.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*LmDR77h3vo0Ajh0U.png" /></figure><h4><strong>Step 3: View the website</strong></h4><p>Copy and paste the public ipv4 address of the instance in another tab to see the webpage</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/0*CmMKj53H6TC7aKFo.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/0*xeGyC1uDht1__rCC.png" /></figure><h4><strong>Conclusion</strong></h4><p>This concludes deploying an EC2 instance in a custom VPC where we have deployed an apache web server and also shows that our EC2 instance can be accessed publicly over the internet.</p><p>Thank you for taking your time to read about this project.</p><p>Connect with me on LinkedIn:</p><p><a href="http://www.linkedin.com/in/adeolaadefolaju">www.linkedin.com/in/adeolaadefolaju</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8d5d5385b30" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>