<?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 Sujanravichandran on Medium]]></title>
        <description><![CDATA[Stories by Sujanravichandran on Medium]]></description>
        <link>https://medium.com/@sujanravichandran?source=rss-9acb3bcff743------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Ma8Dh1I62c3HKnE4</url>
            <title>Stories by Sujanravichandran on Medium</title>
            <link>https://medium.com/@sujanravichandran?source=rss-9acb3bcff743------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 03:21:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sujanravichandran/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/@sujanravichandran/s3-backed-for-terraform-state-file-bd6510a760d5?source=rss-9acb3bcff743------2</link>
            <guid isPermaLink="false">https://medium.com/p/bd6510a760d5</guid>
            <dc:creator><![CDATA[Sujanravichandran]]></dc:creator>
            <pubDate>Sat, 13 Jul 2024 10:18:18 GMT</pubDate>
            <atom:updated>2024-07-13T10:18:18.247Z</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><h3>Architecture:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*igNs594zlZ52nziELzVVTg.jpeg" /></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><h4>Terraform Configuration (main.tf)</h4><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># Create instance<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># Create bucket<br>resource &quot;aws_s3_bucket&quot; &quot;terraform_state4&quot; {<br>  bucket = &quot;mnbvcxzzxcvbnm12&quot; #bucket name should be different globally<br><br>  # 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.</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><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><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><br>  # Prevent accidental deletion of this S3 bucket<br>  lifecycle {<br>    prevent_destroy = true<br>  }<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><br>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>}<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><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><br>  attribute {<br>    name = &quot;LockID&quot;<br>    type = &quot;S&quot;<br>  }<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><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/1024/1*BROsW871nsmI7iNIO6UPEQ.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/919/1*vZQOssggxe6wIehswG5ELw.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=bd6510a760d5" 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/@sujanravichandran/host-your-static-website-on-aws-s3-with-terraform-7067a6f74f42?source=rss-9acb3bcff743------2</link>
            <guid isPermaLink="false">https://medium.com/p/7067a6f74f42</guid>
            <dc:creator><![CDATA[Sujanravichandran]]></dc:creator>
            <pubDate>Thu, 27 Jun 2024 18:26:56 GMT</pubDate>
            <atom:updated>2024-06-27T18:26:56.247Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r2xIMI6awZBycZCnM8Jz3g.png" /></figure><p>In this we are going to see about the step by step procedure about how to deploy a static website on Amazon S3 using Terraform.</p><h3>Step-by-Step Process:</h3><ol><li><strong>Install Terraform</strong>: First install the Terraform on your local machine.</li><li><strong>Set Up AWS CLI</strong>: Configure the AWS credentials using the AWS CLI.</li><li><strong>Create an S3 Bucket</strong>: Us Terraform to create an S3 bucket.</li><li><strong>Configure the S3 Bucket for Static Website Hosting</strong>: Set the necessary permissions and configurations.</li><li><strong>Upload The Content</strong>: Use Terraform to upload the static files to the S3 bucket.</li><li><strong>Deploy The S3 Bucket</strong>: Apply the Terraform configuration to deploy the S3 bucket.</li><li><strong>Destroy The S3 Bucket: </strong>Apply the Terraform destroy to destroy the S3 Bucket.</li></ol><h3>Steps to Create..</h3><p><strong>Step 1: Setup the Terraform</strong></p><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/710/1*FVjmP1B86MGtihY5p4C_Ig.png" /></figure><pre># Terraform<br>terraform {<br>  required_version = &quot;1.7.4&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>Step 2: Configuration for S3 bucket</strong></p><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/670/1*6wRic4F2Q5B6ejPfzGUrAw.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><br>  index_document {<br>    suffix = &quot;index.html&quot;<br>  }<br>}</pre><p><strong>Step 3: Configuration for bucket Policy</strong></p><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/676/1*h7phbGvgXQ7XrhwaYMVe3g.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><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>Step 4: Configuration for Output variable</strong></p><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><figure><img alt="" src="https://cdn-images-1.medium.com/max/651/1*9BhZNGxeQmIN_JCDwpjNjw.png" /></figure><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><p><strong>Step 5: Initialize Terraform</strong></p><p>Open the command prompt or terminal &amp; navigate to the folder where the terraform file is located and run the below command.</p><pre>terraform init</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mf772buVrNFjiRdH3DnYcw.png" /></figure><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><p><strong>Step 6: Terraform Validate</strong></p><pre>terraform validate</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*AwQa7B0LJc1EdBXD15jb_Q.png" /></figure><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><p><strong>Step 7: Terraform Plan</strong></p><pre>terraform plan</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/857/1*pHxGFrJn71GITtYQvDYKbg.png" /></figure><p>It is used for understanding and reviewing the intended changes to your infrastructure before actually applying them.</p><p><strong>Step 8: Terraform Apply</strong></p><pre>terraform apply</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/925/1*U7l9R2cpunP8zIIzuJQodA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1015/1*Nr9dElO2Upc1Y_cvo3ZhjQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3lRHhJLn9l-cPS5vZQcznw.png" /></figure><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><p><strong>Step 9: Output</strong></p><p>Just copy the above url link and paste in the browser and the result will be shown as.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*59d-hJH3W7vm34SotULphQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aLPpuOnmNyDa1ZdqCAOXGA.png" /></figure><p><strong>Step 10: Destroy</strong></p><pre>terraform destroy</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*m3YY9mqzQ6VHn3Z36Ur-0A.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><p><strong>Conclusion:</strong></p><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=7067a6f74f42" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>