<?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 Varneth Thangaraju on Medium]]></title>
        <description><![CDATA[Stories by Varneth Thangaraju on Medium]]></description>
        <link>https://medium.com/@varneth2004?source=rss-1a0f30591902------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*JC105fyzosDaefgO</url>
            <title>Stories by Varneth Thangaraju on Medium</title>
            <link>https://medium.com/@varneth2004?source=rss-1a0f30591902------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 03:06:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@varneth2004/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[S3 backed for terraform state file]]></title>
            <link>https://medium.com/@varneth2004/s3-backed-for-terraform-state-file-345a7ea8f73a?source=rss-1a0f30591902------2</link>
            <guid isPermaLink="false">https://medium.com/p/345a7ea8f73a</guid>
            <dc:creator><![CDATA[Varneth Thangaraju]]></dc:creator>
            <pubDate>Sat, 13 Jul 2024 14:00:55 GMT</pubDate>
            <atom:updated>2024-07-13T14:00:55.373Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Terraform is a powerful tool for managing infrastructure as code. When working with Terraform in a team or with multiple environments, storing the state file remotely is crucial for collaboration and state consistency. In this guide, I’ll walk you through creating an S3 bucket to store your Terraform state files.</p><h3>Prerequisites</h3><ul><li>AWS CLI installed and configured</li><li>Terraform installed</li><li>Basic knowledge of Terraform ,Amazon Ec2 and AWS S3</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*igNs594zlZ52nziELzVVTg.jpeg" /><figcaption>S3 backed for terraform state file Architecture</figcaption></figure><h3>Step 1: Create an Ec2 instance and S3 Bucket</h3><p>First, we need to create an Ec2 instance and S3 bucket. Here’s a Terraform configuration to do that:</p><h3>Terraform Configuration (main.tf)</h3><pre>terraform {<br>  required_providers {<br>    aws = {<br>      source  = &quot;hashicorp/aws&quot;<br>      version = &quot;5.57.0&quot;<br>    }<br>  }<br>}<br>provider &quot;aws&quot; {<br>  region = &quot;ap-south-1&quot;<br>}<br># To Create instance and ami your instance ami<br>resource &quot;aws_instance&quot; &quot;amazon-server&quot; {<br>  ami           = &quot;ami-01376101673c89611&quot;<br>  instance_type = &quot;t2.micro&quot;<br>  tags = {<br>    Name = &quot;demo-server&quot;<br>  }<br>}<br># To Create bucket terraform_state4 it should be unique <br>resource &quot;aws_s3_bucket&quot; &quot;terraform_state4&quot; {<br>  bucket = &quot;mnbvcxzzxcvbnm12&quot; #bucket name should be different globally<br><br>  # To Prevent accidental deletion of this S3 bucket<br>  lifecycle {<br>    prevent_destroy = true<br>  }<br>}</pre><p>To run the code go to terminal</p><ol><li>Initiating Terraform</li></ol><pre>terraform init</pre><p>2. To create an Ec2 instance and S3 bucket apply..</p><pre>terraform apply</pre><p>Then your Ec2 instance and S3 bucket will be created. Check your Aws console.</p><h3>Step 2: Backend Configuration</h3><p>Here we are adding the backend code for s3 bucket in the above code. You just have to add backend s3 this steps should be done step by step otherwise it will cause error .</p><pre>terraform {<br>  required_providers {<br>    aws = {<br>      source  = &quot;hashicorp/aws&quot;<br>      version = &quot;5.57.0&quot;<br>    }<br>  }<br>  backend &quot;s3&quot; {<br>    # Replace this with your bucket name!<br>    bucket         = &quot;mnbvcxzzxcvbnm12&quot;<br>    key            = &quot;global/s3/terraform.tfstate&quot;<br>    region         = &quot;ap-south-1&quot;<br>}<br>}<br>provider &quot;aws&quot; {<br>  region = &quot;ap-south-1&quot;<br>}<br><br>resource &quot;aws_instance&quot; &quot;amazon-server&quot; {<br>  ami           = &quot;ami-01376101673c89611&quot;<br>  instance_type = &quot;t2.micro&quot;<br>  tags = {<br>    Name = &quot;demo-server&quot;<br>  }<br>}<br>resource &quot;aws_s3_bucket&quot; &quot;terraform_state4&quot; {<br>  bucket = &quot;mnbvcxzzxcvbnm12&quot;<br>  # Prevent accidental deletion of this S3 bucket<br>  lifecycle {<br>    prevent_destroy = true<br>  }<br>}</pre><p>Here we are running the S3 bucket code and backend code separately because the s3 code and s3 backend code should no compile at the same time.</p><p>Here we have to run the initiating command again to initiate the backend.</p><pre>terraform init</pre><p>To apply..</p><pre>terraform apply</pre><p>Then the backend will be created check it in the console.</p><p>After applying s3 Backend Configuration to create DynamoDB add dynamodb terraform code after s3 Backed Configuration.</p><pre> # Replace this with your DynamoDB table name!<br>    dynamodb_table = &quot;terraform-up-and-running-locks&quot;<br>    encrypt        = true</pre><p>After adding the code it will be like</p><pre><br>    backend &quot;s3&quot; {<br>    # Replace this with your bucket name!<br>    bucket         = &quot;valaxy-terraform-state-file&quot;<br>    key            = &quot;global/s3/terraform.tfstate&quot;<br>    region         = &quot;us-east-1&quot;<br>    # Replace this with your DynamoDB table name!<br>    dynamodb_table = &quot;terraform-up-and-running-locks&quot;<br>    encrypt        = true<br>  }</pre><h3>Step 3: Versioning for S3 Bucket</h3><p>To enable versioning.</p><pre># Enable versioning<br>resource &quot;aws_s3_bucket_versioning&quot; &quot;enabled&quot; {<br>  bucket = aws_s3_bucket.terraform_state4.id<br>  versioning_configuration {<br>    status = &quot;Enabled&quot;<br>  }<br>}</pre><h3>Step 4: Encryption for S3</h3><p>To enable encryption.</p><pre>resource &quot;aws_s3_bucket_server_side_encryption_configuration&quot; &quot;default&quot; {<br>  bucket = aws_s3_bucket.terraform_state4.id<br><br>rule {<br>    apply_server_side_encryption_by_default {<br>      sse_algorithm = &quot;AES256&quot;<br>    }<br>  }<br>}</pre><h3>Step 5: Block public access to S3</h3><p>To block public access.</p><pre>resource &quot;aws_s3_bucket_public_access_block&quot; &quot;public_access&quot; {<br>  bucket                  = aws_s3_bucket.terraform_state4.id<br>  block_public_acls       = true<br>  block_public_policy     = true<br>  ignore_public_acls      = true<br>  restrict_public_buckets = true<br>}</pre><h3>Step 6: Locking Mechanism</h3><p>Creating DynamoDB for locking mechanism. In this case the apply command will be proceeded in the one system if someone apply the terraform apply command it will not allow to proceed.</p><pre>resource &quot;aws_dynamodb_table&quot; &quot;terraform_locks&quot; {<br>  name         = &quot;terraform-up-and-running-locks2&quot;<br>  billing_mode = &quot;PAY_PER_REQUEST&quot;<br>  hash_key     = &quot;LockID&quot;<br><br>attribute {<br>    name = &quot;LockID&quot;<br>    type = &quot;S&quot;<br>  }<br>}</pre><h3>Step 7: Output</h3><p>To display the output.</p><pre>output &quot;s3_bucket_arn&quot; {<br>  value       = aws_s3_bucket.terraform_state4.arn<br>  description = &quot;The ARN of the S3 bucket&quot;<br>}<br><br>output &quot;dynamodb_table_name&quot; {<br>  value       = aws_dynamodb_table.terraform_locks.name<br>  description = &quot;The name of the DynamoDB table&quot;<br>}</pre><p>By this we can get the arn of s3 bucket and name of the dynamoDB.</p><h3>Entire code</h3><pre>terraform {<br>  required_providers {<br>    aws = {<br>      source  = &quot;hashicorp/aws&quot;<br>      version = &quot;5.57.0&quot;<br>    }<br>  }<br>  backend &quot;s3&quot; {<br>    # Replace this with your bucket name!<br>    bucket         = &quot;mnbvcxzzxcvbnm12&quot;<br>    key            = &quot;global5/s32/terraform.tfstate3&quot;<br>    region         = &quot;ap-south-1&quot;<br>    dynamodb_table = &quot;terraform-up-and-running-locks2&quot;<br>    encrypt        = true<br>}<br>}<br>provider &quot;aws&quot; {<br>  region = &quot;ap-south-1&quot;<br>}<br><br>resource &quot;aws_instance&quot; &quot;amazon-server&quot; {<br>  ami           = &quot;ami-01376101673c89611&quot;<br>  instance_type = &quot;t2.micro&quot;<br>  tags = {<br>    Name = &quot;demo-server&quot;<br>  }<br>}<br>resource &quot;aws_s3_bucket&quot; &quot;terraform_state4&quot; {<br>  bucket = &quot;mnbvcxzzxcvbnm12&quot;<br>  # Prevent accidental deletion of this S3 bucket<br>  lifecycle {<br>    prevent_destroy = true<br>  }<br>}<br>resource &quot;aws_s3_bucket_versioning&quot; &quot;enabled&quot; {<br>  bucket = aws_s3_bucket.terraform_state4.id<br>  versioning_configuration {<br>    status = &quot;Enabled&quot;<br>  }<br>}<br>resource &quot;aws_s3_bucket_server_side_encryption_configuration&quot; &quot;default&quot; {<br>  bucket = aws_s3_bucket.terraform_state4.id<br>  rule {<br>    apply_server_side_encryption_by_default {<br>      sse_algorithm = &quot;AES256&quot;<br>    }<br>  }<br>}<br>resource &quot;aws_s3_bucket_public_access_block&quot; &quot;public_access&quot; {<br>  bucket                  = aws_s3_bucket.terraform_state4.id<br>  block_public_acls       = true<br>  block_public_policy     = true<br>  ignore_public_acls      = true<br>  restrict_public_buckets = true<br>}<br>resource &quot;aws_dynamodb_table&quot; &quot;terraform_locks&quot; {<br>  name         = &quot;terraform-up-and-running-locks2&quot;<br>  billing_mode = &quot;PAY_PER_REQUEST&quot;<br>  hash_key     = &quot;LockID&quot;<br>  attribute {<br>    name = &quot;LockID&quot;<br>    type = &quot;S&quot;<br>  }<br>}<br>output &quot;s3_bucket_arn&quot; {<br>  value       = aws_s3_bucket.terraform_state4.arn<br>  description = &quot;The ARN of the S3 bucket&quot;<br>}<br>output &quot;dynamodb_table_name&quot; {<br>  value       = aws_dynamodb_table.terraform_locks.name<br>  description = &quot;The name of the DynamoDB table&quot;<br>}</pre><ol><li>To initiate</li></ol><pre>terraform init</pre><p>2. To apply</p><pre>terraform apply</pre><h3>Output:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*sBhxYuo8jc6UfdXf.png" /></figure><p>If both the person are applying who applies first it will run and the output will be shown.</p><p>Who applies second simultaneously it will shown as..</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*aMx2HxZSBKjkoXWy.png" /></figure><p>By this we can stop corruption at the files.</p><p>By following these steps, you ensure that your Terraform state is safely stored and accessible for your team, enhancing collaboration and infrastructure management.</p><p>Thanks for reading….</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=345a7ea8f73a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cloud Resume Hosting on AWS]]></title>
            <link>https://medium.com/@varneth2004/cloud-resume-hosting-on-aws-ae2f860037b3?source=rss-1a0f30591902------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae2f860037b3</guid>
            <dc:creator><![CDATA[Varneth Thangaraju]]></dc:creator>
            <pubDate>Thu, 11 Jul 2024 16:43:30 GMT</pubDate>
            <atom:updated>2024-07-11T16:43:30.343Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello Everyone!</p><p>I hope you are all doing well and staying healthy. I’m thrilled to share that I recently completed some hands-on projects involving S3 and Cloudfront. Documenting these experiences in a blog not only serves as a helpful reference for myself in the future but also aims to assist others who are navigating similar paths. In this blog, I’ll take you through my journey of deploying a static website using cloudfront on AWS. This project involves a variety of technologies, including S3 for storing the files , for speed access cloudfront , DynamoDB for storing the count of views , AWS Lambda for triggering and adding the count for the views. Let’s dive in and explore the steps, best practices, and tips that made this project a success!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/971/1*y9c53MnXDMNQOOhztObF9w.png" /><figcaption>AWS Cloud Architecture diagram for Cloud Resume Challenge</figcaption></figure><h3>Getting started</h3><p>I started looking for a project to do, and then I got my first AWS project topic: hosting a resume using AWS. In this blog, I’ll share my experience and guide you through the process of hosting any application in an S3 bucket, enabling it with CloudFront for fast access, and recording view counts using DynamoDB and AWS Lambda. Whether you’re looking to host your resume or any other static site, this tutorial will help you set it up efficiently on AWS.</p><h3>Step 1: Setting up S3 bucket</h3><p>In this first step we have to create a S3 bucket with giving unique name . Next there is no need to give public access and static hosting . If you need you can give bucket versioning and there is no need any other changes . Other things can be default . Before uploading the file connect the file to Lambda using Lambda url (This step see in step 4)so that when other view the files it will trigger the lambda function . You are going to give permission to bucket using bucket policy which you can get after creating cloudfront .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hCW-3bEPgqABFfnVsU7dfA.png" /></figure><h3>Step 2: Setting up Cloudfront Distribution</h3><p>In this Origin domain connect the Cloudfront and S3 bucket . If your are going to host it in the website then you will be having it in https because it gives encryption choose it in Viewer protocol policy . If you need firewall it’s your choice . After creating the distribution go to origins create origin then after creating [copy the bucket policy after creating not before creating] .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P7rfrsaNDJnQrBUdtyeV8w.png" /></figure><h3>Step 3: Setting up DynamoDB</h3><p>Create the table with giving it a name then give the primary key something like id . If you need tag give it . Then create the table . After creating the table add attributes for views as numbers .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U3UWu3WEhe8XxFNRJagBiA.png" /></figure><h3>Step 4: Setting up Lambda</h3><p>When creating lambda function give it a name then next choose the language you know I chosed Python . Then <strong>Enable function URL</strong> because with this we can get url without using gateway , Auth type this should be none to give person to use for everyone , Configure cross-origin resource sharing (CORS) should be enable to get world access then create it . After creating it check if the url is working if not then recreate it . Write code to add the views in the DynamoDB .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0olF5tOM-b9V0-eJsWAj6g.png" /></figure><pre>import json<br>import boto3<br>dynamodb = boto3.resource(&#39;dynamodb&#39;)<br>table = dynamodb.Table(&#39;Project-trail&#39;)<br><br>def lambda_handler(event, context):<br>    response = table.get_item(Key={<br>        &#39;id&#39;: &#39;0&#39;<br>        <br>    })<br>    views = response[&#39;Item&#39;][&#39;views&#39;]<br>    views = views + 1<br>    print(views)<br>    response = table.put_item(Item={<br>        &#39;id&#39;: &#39;0&#39;,<br>        &#39;views&#39;: views<br>        <br>    })<br>    return views</pre><p>In this id is the table Primary key . Then the table name should be name of your table name then the values extra check it .</p><p>Then there is one configuration you should change so that lambda can access DynamoDB . In configuration go to permission and add DynamoDB access . Then check if the lambda adds the views count if lambda is triggered . Last work then copy the lambda url and connect to the files so that whenever the files are hosted it will trigger the lambda then adds the views count make it like .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7jHxrMph4Ui7L8oxOTLCUQ.png" /></figure><h4>This is my AWS project everyone try it.</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae2f860037b3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Host Your Static Website on AWS S3 with Terraform]]></title>
            <link>https://medium.com/@varneth2004/host-your-static-website-on-aws-s3-with-terraform-93d477b687b7?source=rss-1a0f30591902------2</link>
            <guid isPermaLink="false">https://medium.com/p/93d477b687b7</guid>
            <dc:creator><![CDATA[Varneth Thangaraju]]></dc:creator>
            <pubDate>Thu, 27 Jun 2024 18:19:48 GMT</pubDate>
            <atom:updated>2024-06-27T18:21:51.354Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_dz7K2z7AeOaoOIrrctR0A.jpeg" /></figure><h3>Step-by-Step Guide to Deploying a Static Website on Amazon S3 Using Terraform</h3><h4>Install Terraform</h4><blockquote>First, install Terraform on your local machine.</blockquote><h4>Set Up AWS CLI</h4><blockquote>Configure your AWS credentials using the AWS CLI.</blockquote><h4>Create an S3 Bucket</h4><blockquote>Use Terraform to create an S3 bucket.</blockquote><h4>Configure the S3 Bucket for Static Website Hosting</h4><blockquote>Set the necessary permissions and configurations for static website hosting.</blockquote><h4>Upload the Content</h4><blockquote>Use Terraform to upload your static files to the S3 bucket.</blockquote><h4>Deploy the Content</h4><blockquote>Apply the Terraform configuration to deploy your content.</blockquote><h4>Destroy the Content</h4><blockquote><strong><em>Apply the Terraform destroy command to remove the deployed content resources.</em></strong></blockquote><h3><strong>Steps to Deploying a Static Website on Amazon S3 Using Terraform</strong></h3><h4><strong>Step 1: Setup the Terraform</strong></h4><p>Create a ‘terraform.tf’ file to set up the terraform and provider.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*vem3Bv31jNlOIQCFXl-Lhw.png" /></figure><pre># Terraform<br>terraform {<br>  required_version = &quot;1.8.5&quot;<br>  required_providers {<br>    aws = {<br>        source = &quot;hashicorp/aws&quot;<br>        version = &quot;5.40.0&quot;<br>    }<br>  }<br>}<br><br>#Provider<br>provider &quot;aws&quot; {<br>  profile = &quot;default&quot;<br>  region = &quot;ap-south-1&quot;<br>}</pre><p><strong><em>Terraform Configuration Block:</em></strong></p><ul><li>terraform { ... }: This block defines the Terraform configuration itself.</li><li>required_version = &quot;1.8.5&quot;: This line specifies the minimum Terraform version required to run this configuration. Ensure your Terraform installation meets or exceeds this version.</li><li>required_providers { ... }: This section defines the external providers required for this infrastructure.</li></ul><p><strong><em>AWS Provider Block:</em></strong></p><ul><li>provider &quot;aws&quot; { ... }: This block further configures the AWS provider we defined earlier.</li><li>profile = &quot;default&quot;: This tells Terraform to use the default AWS profile configured in your local AWS credentials. Make sure you have a proper AWS profile set up with the necessary permissions to create S3 buckets and configure web hosting.</li><li>region = &quot;ap-south-1&quot;: This line specifies the AWS region where your infrastructure will be deployed.</li></ul><h4><strong>Step 2: Configuration for S3 bucket</strong></h4><p>Create a ‘bucket.tf’ file to store the terraform configuration related to the S3 bucket.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/566/1*dGhTTG6rW5IkFHk9w95qqQ.png" /></figure><pre># Create S3 Bucket<br>resource &quot;aws_s3_bucket&quot; &quot;terraform-demo-43234&quot; {<br>  bucket = &quot;terraform-demo-43234&quot;<br>}<br><br># Upload file to S3<br>resource &quot;aws_s3_object&quot; &quot;terraform_index&quot; {<br>  bucket = aws_s3_bucket.terraform-demo-43234.id<br>  key = &quot;index.html&quot;<br>  source = &quot;index.html&quot;<br>  content_type = &quot;text/html&quot;<br>  etag = filemd5(&quot;index.html&quot;)<br>}<br><br># S3 Web hosting<br>resource &quot;aws_s3_bucket_website_configuration&quot; &quot;terraform_hosting&quot; {<br>  bucket = aws_s3_bucket.terraform-demo-43234.id<br>  index_document {<br>    suffix = &quot;index.html&quot;<br>  }<br>}</pre><p><strong><em>Create S3 Bucket (resource “aws_s3_bucket” “terraform-demo-43234”)</em></strong></p><ul><li>This block creates a new S3 bucket named “terraform-demo-43234”. This bucket will store all the files for your website.</li></ul><p><strong><em>Upload File to S3 (resource “aws_s3_object” “terraform_index”)</em></strong></p><ul><li>This block uploads a specific file, “index.html”, to the S3 bucket created earlier.</li><li>It references the bucket using aws_s3_bucket.terraform-demo-43234.id. Terraform automatically retrieves the bucket&#39;s unique identifier after creating it.</li><li>It sets the key to &quot;index.html&quot;, which acts like a filename within the S3 bucket.</li><li>The source property tells Terraform where to find the &quot;index.html&quot; file on your local machine.</li><li>It sets the content_type as &quot;text/html&quot; to properly identify the file as HTML content.</li><li>The etag property calculates a hash of the local file, ensuring data integrity during upload.</li></ul><p><strong><em>S3 Web Hosting (resource “aws_s3_bucket_website_configuration” “terraform_hosting”)</em></strong></p><ul><li>This block configures the S3 bucket for website hosting.</li><li>It references the same bucket again using aws_s3_bucket.terraform-demo-43234.id.</li><li>Within the index_document block, it specifies that the &quot;index.html&quot; file (identified by its suffix) should be the default file served when someone visits the website.</li></ul><h4><strong>Step 3: Configuration for bucket Policy</strong></h4><p>Create a ‘policy.tf’ file to store the terraform configuration related to the bucket policy for public access.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*pmwKbr20lcs2qD1-J_goNA.png" /></figure><pre># S3 public access<br>resource &quot;aws_s3_bucket_public_access_block&quot; &quot;terraform-demo&quot; {<br>    bucket = aws_s3_bucket.terraform-demo-43234.id<br>  block_public_acls = false<br>  block_public_policy = false<br>}<br><br># S3 public Read policy<br>resource &quot;aws_s3_bucket_policy&quot; &quot;open_access&quot; {<br>  bucket = aws_s3_bucket.terraform-demo-43234.id<br>  policy = jsonencode({<br>    Version = &quot;2012-10-17&quot;<br>    Id      = &quot;Public_access&quot;<br>    Statement = [<br>      {<br>        Sid = &quot;IPAllow&quot;<br>        Effect = &quot;Allow&quot;<br>        Principal = &quot;*&quot;<br>        Action = [&quot;s3:GetObject&quot;]<br>        Resource = &quot;${aws_s3_bucket.terraform-demo-43234.arn}/*&quot;<br>      },<br>    ]<br>  })<br>  depends_on = [ aws_s3_bucket_public_access_block.terraform-demo ]<br>}</pre><p><strong><em>S3 Public Access Block (resource “aws_s3_bucket_public_access_block” “terraform-demo”)</em></strong></p><ul><li>This block temporarily disables S3’s default Block Public Access settings for this specific bucket. This is necessary because we’ll be applying a custom policy to grant public read access, and the default block could override those permissions.</li><li>bucket = aws_s3_bucket.terraform-demo-43234.id: References the S3 bucket we created earlier.</li><li>block_public_acls = false: Disables blocking of public access control lists (ACLs).</li><li>block_public_policy = false: Disables blocking of public bucket policies.</li></ul><p><strong><em>S3 Public Read Policy (resource “aws_s3_bucket_policy” “open_access”)</em></strong></p><ul><li>This block defines a custom bucket policy that grants public read access to the objects within the bucket.</li><li>bucket = aws_s3_bucket.terraform-demo-43234.id: References the S3 bucket again.</li><li>policy = jsonencode({ ... }): Specifies the actual policy document in JSON format.</li><li>depends_on = [ aws_s3_bucket_public_access_block.terraform-demo ]: Ensures the public access block is applied before the policy, as they interact with each other.</li></ul><h3><strong>Step 4: Configuration for Output variable</strong></h3><p>This is an optional step. We are doing this to get a static website URL which we can also get through AWS Console. Create an ‘output.tf’ to print out the URL to access the website.</p><pre># Website URL<br>output &quot;website_url&quot; {<br>  value = &quot;http://${aws_s3_bucket.terraform-demo-43234.bucket}.s3-website.${aws_s3_bucket.terraform-demo-43234.region}.amazonaws.com&quot;<br>}</pre><h4><strong>Step 5: Initialize Terraform</strong></h4><p>Open the command prompt or terminal &amp; navigate to the folder where the terraform file is located and run the below command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*RAmw_NWxPBvvLEMOjvmxlQ.png" /></figure><pre>terraform init</pre><p>It essentially prepares Terraform’s working directory for managing infrastructure. It downloads and installs any required provider plugins based on your configuration like hashicorp/aws provider.</p><h4><strong>Step 6: Terraform Validate</strong></h4><pre>terraform validate</pre><p>It performs a static analysis of your Terraform configuration files and validates the overall syntax of your Terraform code, ensuring it adheres to the Terraform language rules.</p><h4><strong>Step 7: Terraform Plan</strong></h4><pre>terraform plan</pre><p>It is used for understanding and reviewing the intended changes to your infrastructure before actually applying them.</p><h4><strong>Step 8: Terraform Apply</strong></h4><pre>terraform apply</pre><p>The Terraform apply command in Terraform is the one that actually executes the actions outlined in the plan generated by the Terraform plan. It’s the final step in making the desired infrastructure changes a reality.</p><p>If it runs successfully you can go ahead and open the AWS console to verify the open EC2 instance is created.</p><h4>Step 9: Output</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a4edEneS9_f0iFhfYfQx0w.png" /></figure><p>After the terraform apply the output can be seen but for that you should not have the same name file more than one then it may cause to error. You have to give the version of terraform in the initial stage correctly.</p><h4><strong>Step 10: Destroy</strong></h4><pre>terraform destroy</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DC2rKT1Ip5FG7xGb9MYPGw.png" /></figure><p>The terraform destroy command in Terraform serves the purpose of tearing down the infrastructure resources that your Terraform configuration currently manages.</p><h4><strong>Conclusion:</strong></h4><p>In this comprehensive guide, we have walked through the process of deploying a static website on AWS S3 using Terraform. By following the step-by-step instructions outlined above, you can automate the creation of S3 buckets, effortlessly upload your website files, configure public access policies, and enable web hosting with ease.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93d477b687b7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>