<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dhiraj Bhatt</title>
    <description>The latest articles on DEV Community by Dhiraj Bhatt (@dhiraj072).</description>
    <link>https://dev.to/dhiraj072</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F318142%2F7cb4f6e5-8862-4b5c-ab14-40442deb0a40.jpeg</url>
      <title>DEV Community: Dhiraj Bhatt</title>
      <link>https://dev.to/dhiraj072</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dhiraj072"/>
    <language>en</language>
    <item>
      <title>How I implemented a random word generator in java?</title>
      <dc:creator>Dhiraj Bhatt</dc:creator>
      <pubDate>Sat, 25 Jan 2020 13:44:54 +0000</pubDate>
      <link>https://dev.to/dhiraj072/why-i-wrote-my-own-random-word-generator-1nb9</link>
      <guid>https://dev.to/dhiraj072/why-i-wrote-my-own-random-word-generator-1nb9</guid>
      <description>&lt;p&gt;I was building an &lt;a href="https://github.com/Dhiraj072/yrandom"&gt;app&lt;/a&gt; to display random youtube videos. For the app, I needed an easy way to get a random english word. Suprisingly, finding an API which fit my requirements was harder than I expected. Finally, I ended up writing my own &lt;a href="https://github.com/Dhiraj072/random-word-generator"&gt;random word generator&lt;/a&gt; using &lt;a href="https://www.datamuse.com/api/"&gt;Datamuse API&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why I couldn't use existing options?
&lt;/h1&gt;

&lt;p&gt;I analysed a bunch of APIs, and none of them suited my requirements&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.wordnik.com/"&gt;Wordnik&lt;/a&gt; - Rich API with lots of documentation, but they were taking more than 7 days to send me an API key unless I donated money.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.wordsapi.com/"&gt;WordsAPI&lt;/a&gt; - Sleak website with straightforward random word API, but the free plan allowed only 2500 requests per day, which is a bit too low in my opinion.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://random-word-api.herokuapp.com"&gt;random-word-api&lt;/a&gt; - Pretty good except that it generates a "random" word by choosing from a static list of words, which may result in words getting repeated frequently.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.datamuse.com/api/"&gt;DataMuse API&lt;/a&gt; - This was most promising with 100,000 free requests per day, though they didn't have a straightforward random word api. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, I decided to write my own &lt;a href="https://github.com/Dhiraj072/random-word-generator"&gt;random word generator&lt;/a&gt; java library using the DataMuse word-search API.&lt;/p&gt;

&lt;h1&gt;
  
  
  Writing my own random word generator
&lt;/h1&gt;

&lt;p&gt;As explained before, &lt;a href="https://www.datamuse.com/api/"&gt;Datamuse API&lt;/a&gt; was my best option. Sadly, instead of a random-word API they only had a &lt;a href="http://www.datamuse.com/api/"&gt;word-search&lt;/a&gt; API. You make a REST API call to them with a &lt;code&gt;topics&lt;/code&gt; parameter, and they send you back a list of words on those &lt;code&gt;topics&lt;/code&gt;. The &lt;a href="https://github.com/Dhiraj072/random-word-generator"&gt;random word generator&lt;/a&gt; library which is just a wrapper around the Datamuse API. The library allows you to get a random word with a simple static method call as below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Import the class&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.github.dhiraj072.randomwordgenerator.RandomWordGenerator&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// A simple static method call to get the random word&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;randomWord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RandomWordGenerator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getRandomWord&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Behind the scenes, a few things are going on: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We have

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;RandomWordGenerator&lt;/code&gt; class which holds a list of words(&lt;code&gt;randomWords&lt;/code&gt;) in memory&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Topics&lt;/code&gt; class which holds a static list of various topics e.g. acting, chess, etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;During initialization, &lt;code&gt;RandomWordGenerator&lt;/code&gt; makes an HTTP request to DataMuse API with a random &lt;code&gt;Topic&lt;/code&gt; and updates &lt;code&gt;randomWords&lt;/code&gt; value with the list of words returned by the API&lt;/li&gt;
&lt;li&gt;When a user makes a call to &lt;code&gt;RandomWordGenerator.getRandomWord()&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt; an HTTP request to DataMuse API is initiated to get the next list of words &lt;em&gt;in a separate thread&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt; a randomly chosen word from the &lt;code&gt;randomWords&lt;/code&gt; list is returned &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;In case the HTTP request made above fails, system defaults to getting a random word from the &lt;code&gt;Topics&lt;/code&gt; class itself. Something is better than nothing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that &lt;em&gt;in a separate thread&lt;/em&gt; part is pretty important above. Instantiating the HTTP request to DataMuse API in a separate thread allows the &lt;code&gt;RandomWordGenerator.getRandomWord()&lt;/code&gt;  call to be instantaneous as it doesn't need to wait for the HTTP response to come back.&lt;/p&gt;

&lt;p&gt;The above design achieves a few things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Every call to &lt;code&gt;RandomWordGenerator.getRandomWord()&lt;/code&gt; is instantaneous&lt;/strong&gt; since we return the word from the &lt;code&gt;randomWords&lt;/code&gt; list already present in memory. I believe this is better than most/all REST APIs as there is no overhead of an HTTP request.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Randomness of the words is ensured over time&lt;/strong&gt; as every call to &lt;code&gt;RandomWordGenerator.getRandomWord()&lt;/code&gt; has the side-effect of updating the current &lt;code&gt;randomWords&lt;/code&gt; list with words for a new random &lt;code&gt;Topic&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Randomness of words is ensured for quick multiple calls&lt;/strong&gt; to &lt;code&gt;RandomWordGenerator.getRandomWord()&lt;/code&gt;, because for every call a random word is chosen from the &lt;code&gt;randomWords&lt;/code&gt; list&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It's robust.&lt;/strong&gt; If due to any reason the call to DataMuse API fails, then we fall back to the offline &lt;code&gt;Topics&lt;/code&gt; class to get a random word.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In case you want to have a look at the source code or use this library in your own project, the link is below. Please feel free to post your suggestions/questions in comments!&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vJ70wriM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo-ba8488d21cd8ee1fee097b8410db9deaa41d0ca30b004c0c63de0a479114156f.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/Dhiraj072"&gt;
        Dhiraj072
      &lt;/a&gt; / &lt;a href="https://github.com/Dhiraj072/random-word-generator"&gt;
        random-word-generator
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      A java library to generate random words
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h1&gt;
Random word generator&lt;/h1&gt;
&lt;p&gt;&lt;a href="https://travis-ci.com/Dhiraj072/random-word-generator" rel="nofollow"&gt;&lt;img src="https://camo.githubusercontent.com/0cce7c2fb1bdcb712ff2c33eb510e8e3ed96fdf5/68747470733a2f2f7472617669732d63692e636f6d2f44686972616a3037322f72616e646f6d2d776f72642d67656e657261746f722e7376673f6272616e63683d6d6173746572" alt="Build Status"&gt;&lt;/a&gt;
&lt;a href="https://coveralls.io/github/Dhiraj072/random-word-generator?branch=master" rel="nofollow"&gt;&lt;img src="https://camo.githubusercontent.com/4359d41ae026d8e96ff1566926341e5f15b003f5/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f44686972616a3037322f72616e646f6d2d776f72642d67656e657261746f722f62616467652e7376673f6272616e63683d6d6173746572" alt="Coverage Status"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A java library to generate random words. This utilizes the &lt;a href="https://www.datamuse.com/api/" rel="nofollow"&gt;DataMuse API&lt;/a&gt; as its word-finding engine.&lt;/p&gt;
&lt;h2&gt;
Usage&lt;/h2&gt;
&lt;p&gt;Import from Maven Central&lt;/p&gt;
&lt;p&gt;Using Maven&lt;/p&gt;
&lt;div class="highlight highlight-text-xml"&gt;&lt;pre&gt;&amp;lt;&lt;span class="pl-ent"&gt;dependency&lt;/span&gt;&amp;amp;gt
  &amp;lt;&lt;span class="pl-ent"&gt;groupId&lt;/span&gt;&amp;gt;com.github.dhiraj072&amp;lt;/&lt;span class="pl-ent"&gt;groupId&lt;/span&gt;&amp;amp;gt
  &amp;lt;&lt;span class="pl-ent"&gt;artifactId&lt;/span&gt;&amp;gt;random-word-generator&amp;lt;/&lt;span class="pl-ent"&gt;artifactId&lt;/span&gt;&amp;amp;gt
  &amp;lt;&lt;span class="pl-ent"&gt;version&lt;/span&gt;&amp;gt;1.1.0&amp;lt;/&lt;span class="pl-ent"&gt;version&lt;/span&gt;&amp;gt;
&amp;lt;/&lt;span class="pl-ent"&gt;dependency&lt;/span&gt;&amp;gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Using Gradle&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;compile 'com.github.dhiraj072:random-word-generator:1.1.0'
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Get a random word&lt;/p&gt;
&lt;div class="highlight highlight-source-java"&gt;&lt;pre&gt;&lt;span class="pl-c"&gt;&lt;span class="pl-c"&gt;//&lt;/span&gt; Import the class&lt;/span&gt;
&lt;span class="pl-k"&gt;import&lt;/span&gt; &lt;span class="pl-smi"&gt;com.github.dhiraj072.randomwordgenerator.RandomWordGenerator&lt;/span&gt;;

&lt;span class="pl-c"&gt;&lt;span class="pl-c"&gt;//&lt;/span&gt; Get a random word with a simple static method call&lt;/span&gt;
&lt;span class="pl-smi"&gt;String&lt;/span&gt; randomWord &lt;span class="pl-k"&gt;=&lt;/span&gt; &lt;span class="pl-smi"&gt;RandomWordGenerator&lt;/span&gt;&lt;span class="pl-k"&gt;.&lt;/span&gt;getRandomWord();&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Get a random word skewed towards various parameters supported by &lt;code&gt;DataMuseRequest&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight highlight-source-java"&gt;&lt;pre&gt;&lt;span class="pl-k"&gt;import&lt;/span&gt; &lt;span class="pl-smi"&gt;com.github.dhiraj072.randomwordgenerator.datamuse.WordsRequest&lt;/span&gt;;
&lt;span class="pl-k"&gt;import&lt;/span&gt; &lt;span class="pl-smi"&gt;com.github.dhiraj072.randomwordgenerator.datamuse.DataMuseRequest&lt;/span&gt;;

&lt;span class="pl-c"&gt;&lt;span class="pl-c"&gt;//&lt;/span&gt; Get a random word skewed towards topics "Car" and "Road"&lt;/span&gt;
&lt;span class="pl-smi"&gt;WordsRequest&lt;/span&gt; customRequest &lt;span class="pl-k"&gt;=&lt;/span&gt; &lt;span class="pl-k"&gt;new&lt;/span&gt; &lt;span class="pl-smi"&gt;DataMuseRequest&lt;/span&gt;()&lt;span class="pl-k"&gt;.&lt;/span&gt;topics(&lt;span class="pl-s"&gt;&lt;span class="pl-pds"&gt;"&lt;/span&gt;Car&lt;span class="pl-pds"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="pl-s"&gt;&lt;span class="pl-pds"&gt;"&lt;/span&gt;Road&lt;span class="pl-pds"&gt;"&lt;/span&gt;&lt;/span&gt;);
&lt;span class="pl-smi"&gt;String&lt;/span&gt; randomWord &lt;span class="pl-k"&gt;=&lt;/span&gt; &lt;span class="pl-smi"&gt;RandomWordGenerator&lt;/span&gt;&lt;span class="pl-k"&gt;.&lt;/span&gt;getRandomWord(customRequest);&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
Built With&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://gradle.org/" rel="nofollow"&gt;Gradle&lt;/a&gt; - Build tool&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.datamuse.com/api/" rel="nofollow"&gt;DataMuse API&lt;/a&gt; - For getting list of words&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://junit.org/" rel="nofollow"&gt;JUnit&lt;/a&gt; - Testing Framework&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
License&lt;/h2&gt;
&lt;p&gt;This project…&lt;/p&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/Dhiraj072/random-word-generator"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;



</description>
      <category>java</category>
      <category>design</category>
      <category>showdev</category>
    </item>
    <item>
      <title>How to deploy a static web app on AWS S3 for free?</title>
      <dc:creator>Dhiraj Bhatt</dc:creator>
      <pubDate>Sat, 18 Jan 2020 16:23:28 +0000</pubDate>
      <link>https://dev.to/dhiraj072/how-to-deploy-a-static-web-app-on-aws-s3-for-free-2a17</link>
      <guid>https://dev.to/dhiraj072/how-to-deploy-a-static-web-app-on-aws-s3-for-free-2a17</guid>
      <description>&lt;p&gt;So you built a &lt;a href="https://www.staticapps.org/articles/defining-static-web-apps/"&gt;static&lt;/a&gt; react web app, and you want the world to see it. This post provides a step-by-step guide on deploying your static web app on Amazon S3 for free. &lt;/p&gt;

&lt;p&gt;For this tutorial, you will need &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a basic understanding of react web apps, terraform and AWS S3&lt;/li&gt;
&lt;li&gt;an activated AWS account. If you don't have one yet, you can follow &lt;a href="https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/"&gt;instructions here&lt;/a&gt; to set it up&lt;/li&gt;
&lt;li&gt;an AWS IAM user with 

&lt;ul&gt;
&lt;li&gt;AWS API keys (Access Key ID and Secret Access Key) set up. You can follow instructions here to &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html"&gt;setup&lt;/a&gt; and &lt;a href="https://docs.aws.amazon.com/en_en/IAM/latest/UserGuide/id_credentials_access-keys.html"&gt;configure&lt;/a&gt; the IAM user.&lt;/li&gt;
&lt;li&gt;Permissions to create/delete S3 buckets, add IAM policies&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.terraform.io/downloads.html"&gt;Terraform CLI&lt;/a&gt; to set up our infrastructure on AWS&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/install-cliv1.html"&gt;AWS CLI&lt;/a&gt; which has been &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html"&gt;configured&lt;/a&gt; with the IAM user API keys&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AWS has a &lt;a href="https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&amp;amp;all-free-tier.sort-order=asc"&gt;free tier&lt;/a&gt; so running the steps below should not cost you anything. In any case, make sure you TERMINATE all your resources after completing this tutorial to avoid any unwanted charges in your AWS account.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preparing the package
&lt;/h2&gt;

&lt;p&gt;I presume you already have a static react web app which you want to deploy. If not, you can pull a sample web application from my repository. I will be writing this guide using the indecision-app below. It has been build using yarn and webpack, though the steps should be easily reproducible with other package managers (npm, gulp, etc.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git clone https://github.com/Dhiraj072/indecision-app.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once you have the app, build a production package.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn webpack -p
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This above command will create a &lt;code&gt;public/&lt;/code&gt; directory inside your project folder, containing everything required to host your web application, including an &lt;code&gt;index.html&lt;/code&gt;, &lt;code&gt;bundle.js&lt;/code&gt;, etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up AWS infrastructure
&lt;/h2&gt;

&lt;p&gt;We will write the terraform configuration file main.tf step-by-step in this section. This file tells terraform which AWS resources to instantiate, and their respective configuration. Full version of the file can be found &lt;a href="https://github.com/Dhiraj072/blog-posts/blob/master/deploy-app-to-s3-terraform/main.tf"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Create a blank main.tf file in your project dir&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add below to our main.tf to tell terraform we are using AWS for our infrastructure&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight terraform"&gt;&lt;code&gt;&lt;span class="k"&gt;provider&lt;/span&gt; &lt;span class="s2"&gt;"aws"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;# region you want your resource to be in&lt;/span&gt;
  &lt;span class="nx"&gt;region&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"ap-southeast-1"&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We store our terraform states locally for now, so add
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight terraform"&gt;&lt;code&gt;&lt;span class="k"&gt;terraform&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;backend&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"local"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To define local variables we will be using later on, add
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight terraform"&gt;&lt;code&gt;&lt;span class="nx"&gt;locals&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;s3_origin_id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"myappS3Origin"&lt;/span&gt;
  &lt;span class="nx"&gt;app_name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"my_app"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Finally, add the config to create the S3 bucket we want our app to be hosted in. This also sets the IAM policy to allow public read as we want everyone to be able to access our web app.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight terraform"&gt;&lt;code&gt;&lt;span class="k"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_s3_bucket"&lt;/span&gt; &lt;span class="s2"&gt;"my_app"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;bucket&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="kd"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app_name&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;# S3 bucket name&lt;/span&gt;
  &lt;span class="nx"&gt;acl&lt;/span&gt;    &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"public-read"&lt;/span&gt;
  &lt;span class="nx"&gt;website&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;index_document&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"index.html"&lt;/span&gt; &lt;span class="c1"&gt;# our web app's entry point&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;policy&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;POLICY&lt;/span&gt;&lt;span class="sh"&gt;
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowPublicReadAccess",
      "Effect": "Allow",
      "Principal": "*",
      "Action": [
        "s3:GetObject"
      ],
      "Resource": [
        "arn:aws:s3:::${local.app_name}/*"
      ]
    }
  ]
}
&lt;/span&gt;&lt;span class="no"&gt;POLICY
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Save your main.tf file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set the environment variable for terraform to be able to access AWS&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ export AWS_SECRET_ACCESS_KEY=&amp;lt;YOUR_AWS_SECRET_ACCESS_KEY&amp;gt;
$ export AWS_ACCESS_KEY_ID=&amp;lt;YOUR_AWS_ACCESS_KEY_ID&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Initialize terraform
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ terraform init
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Spin up the AWS infra, type &lt;code&gt;y&lt;/code&gt; when asked for confirmation
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ terraform apply
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Terraform may take 5-10 minutes to spin everything up. Once complete, you can login to you AWS account and you should see you AWS bucket created.&lt;/p&gt;

&lt;p&gt;Your AWS infrastructure is now ready to host your web application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploying the package
&lt;/h2&gt;

&lt;p&gt;Finally, you can deploy the package we built earlier to S3 by running the following AWS CLI command in the project dir&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ aws s3 sync public/ s3://my_app
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Accessing deployed web app
&lt;/h2&gt;

&lt;p&gt;You should be able to access the application at &lt;a href="https://s3-ap-southeast-1.amazonaws.com/my_app/index.html"&gt;https://s3-ap-southeast-1.amazonaws.com/my_app/index.html&lt;/a&gt;. This is basically the &lt;code&gt;Object URL&lt;/code&gt; of the &lt;code&gt;index.html&lt;/code&gt; file inside our S3 bucket &lt;code&gt;my_app&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Terminating created resources
&lt;/h2&gt;

&lt;p&gt;AWS has a &lt;a href="https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&amp;amp;all-free-tier.sort-order=asc"&gt;free tier&lt;/a&gt; so running the steps in this tutorial should not cost you anything. In any case, do TERMINATE all your resources to avoid any unwanted charges by running the following command in your project dir&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ terraform destroy
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>aws</category>
      <category>tutorial</category>
      <category>terraform</category>
      <category>react</category>
    </item>
  </channel>
</rss>
